Skip to content

Commit

Permalink
Only finalize metadata if the current file content is different than …
Browse files Browse the repository at this point in the history
…the new metadata.

This will prevent an annoying behavior where a post that hasn't changed at all
gets rewritten because the finalization plugin *always* writes the file if it's turned
on.
  • Loading branch information
tylerbutler committed Nov 29, 2012
1 parent e35ad8a commit 7abda42
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 2 additions & 0 deletions engineer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Post(object):
# Make _content_raw only settable once. This is just to help prevent data loss that might be caused by
# inadvertantly messing with this property.
_content_raw = setonce()
_file_contents_raw = setonce()

@staticmethod
def convert_to_html(content):
Expand Down Expand Up @@ -214,6 +215,7 @@ def _parse_source(self):
with open(self.source, mode='r', encoding='UTF-8') as file:
item = file.read()

self._file_contents_raw = item
parsed_content = re.match(self._regex, item)

if parsed_content is None or parsed_content.group('metadata') is None:
Expand Down
23 changes: 20 additions & 3 deletions engineer/plugins/bundled.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,29 @@ def preprocess(cls, post, metadata):
def postprocess(cls, post):
from engineer.conf import settings

logger = cls.get_logger()
if settings.FINALIZE_METADATA:
output = cls.render_markdown(post)
with open(post.source, mode='wb', encoding='UTF-8') as file:
file.write(output)
if cls.need_update(post, output):
logger.debug("Finalizing metadata for post '%s'" % post)
with open(post.source, mode='wb', encoding='UTF-8') as file:
file.write(output)
else:
logger.debug("No metadata finalization needed for post '%s'" % post)
return post

@classmethod
def need_update(cls, post, new_post_content):
from engineer.models import Post

old_content = Post._regex.match(post._file_contents_raw).group('metadata').strip()
new_content = Post._regex.match(new_post_content).group('metadata').strip()

if new_content == old_content:
return False
else:
return True

@staticmethod
def render_markdown(post):
"""
Expand All @@ -103,7 +120,7 @@ def render_markdown(post):
# set of metadata that was in the file originally.
metadata_to_finalize = set([m for m, s in settings.FINALIZE_METADATA.iteritems() if post.status in s])
metadata_to_finalize.update(post.metadata_original)

if 'title' in metadata_to_finalize:
# insert at the top of the list
d.insert(0, ('title', post.title))
Expand Down

0 comments on commit 7abda42

Please sign in to comment.