diff --git a/Pelican.py b/Pelican.py index f8eb220..56f88fb 100644 --- a/Pelican.py +++ b/Pelican.py @@ -1,4 +1,5 @@ from __future__ import unicode_literals + import codecs import datetime import os @@ -27,10 +28,10 @@ pelican_article_views = [] -class PelicanUpdateDateCommand(sublime_plugin.TextCommand): +class PelicanUpdateModifiedDateCommand(sublime_plugin.TextCommand): def run(self, edit): - date_region = self.view.find(':?date:\s*', 0, sublime.IGNORECASE) + date_region = self.view.find(':?modified:\s*', 0, sublime.IGNORECASE) if not date_region: return @@ -78,7 +79,8 @@ def run(self, edit): pelican_slug_template_result = normalize_line_endings( self.view, pelican_slug_template[meta_type]) - slug_region = self.view.find(':?slug:.+\s*', 0, sublime.IGNORECASE) + slug_region = self.view.find( + ':?slug:.+\s*', 0, sublime.IGNORECASE) if slug_region: self.view.replace( edit, slug_region, pelican_slug_template_result % slug) @@ -142,7 +144,8 @@ def run(self, edit, meta_type=None): if len(metadata_regions) > 0: for region in metadata_regions: metadata_str = self.view.substr(region) - metadata_str = normalize_article_metadata_case(metadata_str)[0] + metadata_str = normalize_article_metadata_case(metadata_str)[ + 0] regex = re.compile(":?(\w+):(.*)") find_all = regex.findall(metadata_str) if len(find_all) > 0: @@ -177,6 +180,22 @@ def run(self, edit, meta_type=None): else: self.view.insert(edit, 0, article_metadata_str) + # initialize modified field if it's empty + metadata_key_date = "Modified" + for key in metadata.keys(): + if key.lower() == "modified": + metadata_key_date = key + if metadata[metadata_key_date] is "": + metadata[metadata_key_date] = strDateNow() + + article_metadata_template = normalize_line_endings( + self.view, "\n".join(article_metadata_template_lines)) + article_metadata_str = article_metadata_template % metadata + if len(metadata_regions) > 0: + self.view.replace(edit, old_metadata_region, article_metadata_str) + else: + self.view.insert(edit, 0, article_metadata_str) + # initialize slug field if it's empty metadata_key_slug = "Slug" for key in metadata.keys(): @@ -382,7 +401,8 @@ def isPelicanArticle(view): return True if view.file_name(): - filepath_filter = load_setting(view, "filepath_filter", default_filter) + filepath_filter = load_setting( + view, "filepath_filter", default_filter) use_input_folder_in_makefile = load_setting( view, "use_input_folder_in_makefile", True) @@ -411,7 +431,8 @@ def load_setting(view, setting_name, default_value): global_settings = sublime.load_settings("Pelican.sublime-settings") - return view.settings().get(setting_name, global_settings.get(setting_name, default_value)) + return view.settings().get( + setting_name, global_settings.get(setting_name, default_value)) def normalize_line_endings(view, string): @@ -570,7 +591,9 @@ def get_metadata_regions(view, mode): if len(regions) > 0: region_begin = regions[0].begin() region_end = regions[len(regions) - 1].end() - result_region_list.append(sublime.Region(region_begin, region_end)) + result_region_list.append( + sublime.Region( + region_begin, region_end)) elif mode == "multiple": for region in regions: result_region_list.append(region) @@ -582,7 +605,8 @@ def get_metadata_regions(view, mode): return result_region_list -def normalize_article_metadata_case(template_str, normalize_template_var=True): +def normalize_article_metadata_case( + template_str, normalize_template_var=True): ''' Markdown diff --git a/Pelican.sublime-commands b/Pelican.sublime-commands index d57f450..a3a5d18 100644 --- a/Pelican.sublime-commands +++ b/Pelican.sublime-commands @@ -3,7 +3,7 @@ { "caption": "Pelican: New Article (Markdown)", "command": "pelican_new_markdown" }, { "caption": "Pelican: New Article (reStructuredText)", "command": "pelican_new_restructuredtext" }, { "caption": "Pelican: Select Article Metadata", "command": "pelican_select_metadata" }, - { "caption": "Pelican: Update Article Date", "command": "pelican_update_date" }, + { "caption": "Pelican: Update Modified Date", "command": "pelican_update_modified_date" }, { "caption": "Pelican: Update Slug using Title", "command": "pelican_generate_slug" }, { "caption": "Pelican: Insert Category", "command": "pelican_insert_category" }, { "caption": "Pelican: Insert Tag", "command": "pelican_insert_tag" } diff --git a/Pelican.sublime-settings b/Pelican.sublime-settings index abe026e..8911d13 100644 --- a/Pelican.sublime-settings +++ b/Pelican.sublime-settings @@ -34,6 +34,7 @@ "Title: %(title)s", "Slug: %(slug)s", "Date: %(date)s", + "Modified: %(modified)s", "Tags: %(tags)s", "Category: %(category)s", "Author: %(author)s", diff --git a/README.md b/README.md index 4073afb..ec7fee2 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ Right click on a file being edit, and access the commands under the **SublimePel If you think it's hard to remember what tags you've used when writing articles, then this command is made for you. This command lists tags you've used in your Pelican site in the quick panel, allowing you to fuzzily select and insert a previously used tag quickly. -* **Pelican: Update Article Date** +* **Pelican: Update Modified Date** This command updates the date metadata field to current date and time. @@ -124,6 +124,26 @@ Instead, customize your settings in **Preferences** > **Package Settings** > **S Default value: `"save"` +#### Modified date generation + +You can automatically update last modified date: + +![Update modified](http://i.imgur.com/L0po0FS.gif) + +Please, install [**Hooks**](https://packagecontrol.io/packages/Hooks) package → open any page/article in syntax, that you use for Pelican pages/articles → in Menu bar: `Preferences` → `Settings` - `Syntax Specific` → add in right-side file this lines: + +```json +"on_pre_save_language": [{ + "command": "pelican_update_modified_date" +}], +``` + +Save file. + +Now if you run `save` command for saving your article or page, your modified date will update automatically. + +**Note**: command `pelican_update_modified_date` will run in all files for syntax, in which you write your pages and articles, if file contains `:?Modified:\s*` string. + ### Customizable metadata template * **article_metadata_template**