Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] #6 #34

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions Pelican.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import unicode_literals

import codecs
import datetime
import os
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Pelican.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
1 change: 1 addition & 0 deletions Pelican.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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**
Expand Down