Skip to content

Commit

Permalink
Without frontmatter, the top line of the file gets eaten. Stop that.
Browse files Browse the repository at this point in the history
  • Loading branch information
mblayman committed Dec 26, 2016
1 parent 3872a9d commit fc817b5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
3 changes: 3 additions & 0 deletions handroll/composers/j2.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Jinja2Composer(FrontmatterComposerMixin, Composer):
The output file uses the same name as the source file with
the ``.j2`` extension removed.
"""
guess_title = False

def compose(self, catalog, source_file, out_dir):
filename = os.path.basename(source_file.rstrip('.j2'))
Expand All @@ -32,6 +33,8 @@ def compose(self, catalog, source_file, out_dir):
template = jinja2.Template(source)
with open(output_file, 'wb') as out:
out.write(template.render(data).encode('utf-8'))
# Frontmatter loading seems to munch the final line separator.
out.write(os.linesep)
else:
logger.debug(_('Skipping {filename} ... It is up to date.').format(
filename=filename))
Expand Down
6 changes: 5 additions & 1 deletion handroll/composers/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
class FrontmatterComposerMixin(object):
"""Mixin the ability to extract frontmatter from a source file."""
document_marker = '---' + os.linesep
guess_title = True

def get_data(self, source_file):
"""Get data and source from the source file."""
Expand All @@ -31,9 +32,12 @@ def get_data(self, source_file):
data, source = self._split_content_with_frontmatter(
first, source, source_file)
signals.frontmatter_loaded.send(source_file, frontmatter=data)
else:
elif self.guess_title:
# This is a plain file so pull title from the first line.
data['title'] = escape(first)
else:
f.seek(0)
source = f.read()

return data, source

Expand Down
16 changes: 15 additions & 1 deletion handroll/tests/test_composers.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,23 @@ def test_composes(self):
composer.compose(None, f.name, composer._config.outdir)
content = open(output_file, 'r').read()
self.assertEqual(
'title: A Fake Title\ndomain: http://www.example.com',
'title: A Fake Title\ndomain: http://www.example.com\n',
content)

def test_composes_no_frontmatter(self):
source = inspect.cleandoc("""First row
domain: {{ config.domain }}
""")
with tempfile.NamedTemporaryFile(delete=False, suffix='.txt.j2') as f:
f.write(source.encode('utf-8'))
composer = self._make_one()
output_file = os.path.join(
composer._config.outdir, os.path.basename(f.name.rstrip('.j2')))
composer.compose(None, f.name, composer._config.outdir)
content = open(output_file, 'r').read()
self.assertEqual(
'First row\ndomain: http://www.example.com\n', content)

def test_needs_update(self):
site = tempfile.mkdtemp()
output_file = os.path.join(site, 'output.md')
Expand Down

0 comments on commit fc817b5

Please sign in to comment.