summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSunaina Pai <sunainapai.in@gmail.com>2018-08-11 14:40:18 +0530
committerSunaina Pai <sunainapai.in@gmail.com>2018-08-11 14:40:18 +0530
commit8195677cae913e2f964bf631d88f00a33e16bc24 (patch)
tree3dddc9320f46c48e7b5e43bbbe4fb765b75b4e9b
parent020ab349067939a121f8b75f003501a7e00198c6 (diff)
Populate placeholders in content when specified
Placeholders in content files are not populated by default. If placeholders are to be populated in content files, parameter named `render` with a string value of `yes` must be specified. This `render` parameter may be specified as a header in content files or it may be specified as a keyword argument in `make_pages` call.
-rwxr-xr-xmakesite.py13
-rw-r--r--test/test_content.py14
-rw-r--r--test/test_pages.py48
-rw-r--r--test/test_truncate.py9
4 files changed, 69 insertions, 15 deletions
diff --git a/makesite.py b/makesite.py
index cf23374..f4dcf0f 100755
--- a/makesite.py
+++ b/makesite.py
@@ -107,10 +107,9 @@ def read_content(filename):
except ImportError as e:
log('WARNING: Cannot render Markdown in {}: {}', filename, str(e))
- # Update the dictionary with content, summary, and RFC 2822 date.
+ # Update the dictionary with content and RFC 2822 date.
content.update({
'content': text,
- 'summary': truncate(text),
'rfc_2822_date': rfc_2822_format(content['date'])
})
@@ -130,10 +129,17 @@ def make_pages(src, dst, layout, **params):
for src_path in glob.glob(src):
content = read_content(src_path)
- items.append(content)
page_params = dict(params, **content)
+ # Populate placeholders in content if content-rendering is enabled.
+ if page_params.get('render') == 'yes':
+ rendered_content = render(page_params['content'], **page_params)
+ page_params['content'] = rendered_content
+ content['content'] = rendered_content
+
+ items.append(content)
+
dst_path = render(dst, **page_params)
output = render(layout, **page_params)
@@ -148,6 +154,7 @@ def make_list(posts, dst, list_layout, item_layout, **params):
items = []
for post in posts:
item_params = dict(params, **post)
+ item_params['summary'] = truncate(post['content'])
item = render(item_layout, **item_params)
items.append(item)
diff --git a/test/test_content.py b/test/test_content.py
index 06ec97d..66342d9 100644
--- a/test/test_content.py
+++ b/test/test_content.py
@@ -11,7 +11,6 @@ class ContentTest(unittest.TestCase):
self.blog_path = path.temppath('blog')
self.undated_path = os.path.join(self.blog_path, 'foo.txt')
self.dated_path = os.path.join(self.blog_path, '2018-01-01-foo.txt')
- self.long_post_path = os.path.join(self.blog_path, 'bar.txt')
self.normal_post_path = os.path.join(self.blog_path, 'baz.txt')
self.md_post_path = os.path.join(self.blog_path, 'qux.md')
self.no_md_post_path = os.path.join(self.blog_path, 'qux.txt')
@@ -24,10 +23,6 @@ class ContentTest(unittest.TestCase):
with open(self.dated_path, 'w') as f:
f.write('hello world')
- with open(self.long_post_path, 'w') as f:
- self.long_text = ' \n'.join('word' + str(i) for i in range(50))
- f.write(self.long_text)
-
with open(self.normal_post_path, 'w') as f:
f.write('<!-- a: 1 -->\n<!-- b: 2 -->\nFoo')
@@ -45,13 +40,8 @@ class ContentTest(unittest.TestCase):
self.mock_args = args
def test_content_content(self):
- content = makesite.read_content(self.long_post_path)
- self.assertEqual(content['content'], self.long_text)
-
- def test_content_summary(self):
- content = makesite.read_content(self.long_post_path)
- expected_text = ' '.join('word' + str(i) for i in range(25))
- self.assertEqual(content['summary'], expected_text)
+ content = makesite.read_content(self.undated_path)
+ self.assertEqual(content['content'], 'hello world')
def test_content_date(self):
content = makesite.read_content(self.dated_path)
diff --git a/test/test_pages.py b/test/test_pages.py
index 9f52fb7..0fefdaa 100644
--- a/test/test_pages.py
+++ b/test/test_pages.py
@@ -22,6 +22,10 @@ class PagesTest(unittest.TestCase):
f.write('<!-- tag: foo -->Foo')
with open(os.path.join(self.blog_path, 'header-bar.txt'), 'w') as f:
f.write('<!-- title: bar -->Bar')
+ with open(os.path.join(self.blog_path, 'placeholder-foo.txt'), 'w') as f:
+ f.write('<!-- title: foo -->{{ title }}:{{ author }}:Foo')
+ with open(os.path.join(self.blog_path, 'placeholder-bar.txt'), 'w') as f:
+ f.write('<!-- title: bar --><!-- render: yes -->{{ title }}:{{ author }}:Bar')
def tearDown(self):
shutil.rmtree(self.blog_path)
@@ -77,3 +81,47 @@ class PagesTest(unittest.TestCase):
self.assertEqual(f.read(), '{{ title }}:foo:Foo')
with open(os.path.join(self.site_path, 'header-bar.txt')) as f:
self.assertEqual(f.read(), 'bar:{{ tag }}:Bar')
+
+ def test_content_no_rendering(self):
+ # Test that placeholders are not populated in content rendering
+ # by default.
+ src = os.path.join(self.blog_path, 'placeholder-foo.txt')
+ dst = os.path.join(self.site_path, '{{ slug }}.txt')
+ tpl = '<div>{{ content }}</div>'
+ makesite.make_pages(src, dst, tpl, author='Admin')
+ with open(os.path.join(self.site_path, 'placeholder-foo.txt')) as f:
+ self.assertEqual(f.read(), '<div>{{ title }}:{{ author }}:Foo</div>')
+
+ def test_content_rendering_via_kwargs(self):
+ # Test that placeholders are populated in content rendering when
+ # requested in make_pages.
+ src = os.path.join(self.blog_path, 'placeholder-foo.txt')
+ dst = os.path.join(self.site_path, '{{ slug }}.txt')
+ tpl = '<div>{{ content }}</div>'
+ makesite.make_pages(src, dst, tpl, author='Admin', render='yes')
+ with open(os.path.join(self.site_path, 'placeholder-foo.txt')) as f:
+ self.assertEqual(f.read(), '<div>foo:Admin:Foo</div>')
+
+ def test_content_rendering_via_header(self):
+ # Test that placeholders are populated in content rendering when
+ # requested in content header.
+ src = os.path.join(self.blog_path, 'placeholder-bar.txt')
+ dst = os.path.join(self.site_path, '{{ slug }}.txt')
+ tpl = '<div>{{ content }}</div>'
+ makesite.make_pages(src, dst, tpl, author='Admin')
+ with open(os.path.join(self.site_path, 'placeholder-bar.txt')) as f:
+ self.assertEqual(f.read(), '<div>bar:Admin:Bar</div>')
+
+ def test_rendered_content_in_summary(self):
+ # Test that placeholders are populated in summary if and only if
+ # content rendering is enabled.
+ src = os.path.join(self.blog_path, 'placeholder*.txt')
+ post_dst = os.path.join(self.site_path, '{{ slug }}.txt')
+ list_dst = os.path.join(self.site_path, 'list.txt')
+ post_layout = ''
+ list_layout = '<div>{{ content }}</div>'
+ item_layout = '<p>{{ summary }}</p>'
+ posts = makesite.make_pages(src, post_dst, post_layout, author='Admin')
+ makesite.make_list(posts, list_dst, list_layout, item_layout)
+ with open(os.path.join(self.site_path, 'list.txt')) as f:
+ self.assertEqual(f.read(), '<div><p>{{ title }}:{{ author }}:Foo</p><p>bar:Admin:Bar</p></div>')
diff --git a/test/test_truncate.py b/test/test_truncate.py
new file mode 100644
index 0000000..3486d71
--- /dev/null
+++ b/test/test_truncate.py
@@ -0,0 +1,9 @@
+import unittest
+import makesite
+
+
+class TruncateTest(unittest.TestCase):
+ def test_truncate(self):
+ long_text = ' \n'.join('word' + str(i) for i in range(50))
+ expected_text = ' '.join('word' + str(i) for i in range(25))
+ self.assertEqual(makesite.truncate(long_text), expected_text)