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)