Sublime Forum

First .pv script

#1

This script is supposed to update a datetime stamp in a comment tag. I am trapping for the type of file because ColdFusion pages have a comment string that starts with <!— instead of the normal <!-- from html files .

The script works if I am saving a .cfm file but not if the type of file is a .htm file.

import sublime
import sublime_plugin
import hashlib
import datetime

# generate a timestamp for the latest document and insert/update it

class AddTimeToColdfusionFile(sublime_plugin.EventListener):
    def on_pre_save(self, view):
        edit = view.begin_edit()
        fileType = view.file_name()-4:]
        if (not fileType == ".cfm") and (not fileType == ".cfc"):
            dashesCount = 2
            hashes = view.find_all('<!-- Version ([a-z0-9 \-\:]+) -->', 0)
            for hashRegion in reversed(hashes):
                    timestamp = datetime.datetime.now().strftime('%m-%d-%Y %H:%M:%S')
                    replacement = '<!-- Version %s -->' % timestamp
                    view.replace(edit, hashRegion,replacement)

            hashes = view.find_all('<!-- Version 09-24-2012 16:02:38 -->', 0)
            for hashRegion in reversed(hashes):
                    timestamp = datetime.datetime.now().strftime('%m-%d-%Y %H:%M:%S')
                    replacement = '<!-- Version %s -->' % timestamp
                    view.replace(edit, hashRegion,replacement)    
        else:
            dashesCount = 3
            hashes = view.find_all('<!--- Version ([a-z0-9 \-\:]+) --->', 0)
            for hashRegion in reversed(hashes):
                    timestamp = datetime.datetime.now().strftime('%m-%d-%Y %H:%M:%S')
                    replacement = '<!--- Version %s --->' % timestamp
                    view.replace(edit, hashRegion,replacement)

            hashes = view.find_all('<!--- Version --->', 0)
            for hashRegion in reversed(hashes):
                    timestamp = datetime.datetime.now().strftime('%m-%d-%Y %H:%M:%S')
                    replacement = '<!--- Version %s --->' % timestamp
                    view.replace(edit, hashRegion,replacement)
        return dashesCount

        view.end_edit(edit)        

0 Likes

#2

I figured out what the problem was. Through reviewing the code from the Insert Hash on Save package I was able to resolve my problem. The code now works on other pages as well as cfm pages.

if anyone is interested the following should be saved as a .py file

import sublime
import sublime_plugin
import hashlib
import datetime

# generate a timestamp for the latest document and insert/update it

class AddTimeToColdfusionFile(sublime_plugin.EventListener):
    def on_pre_save(self, view):
        edit = view.begin_edit()
        fileType = view.file_name()-4:]
        if (not fileType == ".cfm") and (not fileType == ".cfc"):
            hashes = view.find_all('<!-- Version ([a-z0-9 \-\:]+) -->', 0)
            for hashRegion in reversed(hashes):
                    timestamp = datetime.datetime.now().strftime('%m-%d-%Y %H:%M:%S')
                    replacement = '<!-- Version %s -->' % timestamp
                    view.replace(edit, hashRegion,replacement)

            hashes = view.find_all('<!-- Version -->', 0)
            for hashRegion in reversed(hashes):
                    timestamp = datetime.datetime.now().strftime('%m-%d-%Y %H:%M:%S')
                    replacement = '<!-- Version %s -->' % timestamp
                    view.replace(edit, hashRegion,replacement)
        else:
            hashes = view.find_all('<!--- Version ([a-z0-9 \-\:]+) --->', 0)
            for hashRegion in reversed(hashes):
                    timestamp = datetime.datetime.now().strftime('%m-%d-%Y %H:%M:%S')
                    replacement = '<!--- Version %s --->' % timestamp
                    view.replace(edit, hashRegion,replacement)

            hashes = view.find_all('<!--- Version --->', 0)
            for hashRegion in reversed(hashes):
                    timestamp = datetime.datetime.now().strftime('%m-%d-%Y %H:%M:%S')
                    replacement = '<!--- Version %s --->' % timestamp
                    view.replace(edit, hashRegion,replacement)

        view.end_edit(edit)        
0 Likes