Skip to content

Commit

Permalink
add navigation metadata to content files on rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
bboc committed Feb 20, 2022
1 parent 86a4950 commit 7f683ff
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
40 changes: 18 additions & 22 deletions mdbuild/build_jekyll.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,6 @@
UP = '▲'
NEXT = '▶'
NAVIGATION = "<a href=\"%(path)s.html\" title=\"%(link_title)s\">%(link_text)s</a>"
MOUSETRAP = """
<script type="text/javascript">
Mousetrap.bind('g n', function() {
window.location.href = '%s.html';
return false;
});
</script>
"""


def nav_el(link_text, path, link_title):
Expand Down Expand Up @@ -69,7 +59,7 @@ def configure(self):
partial(filters.convert_section_links, 'html'),
macros.MacroFilter.filter,
glossary.get_glossary_link_processor('tooltip'),
filters.jekyll_front_matter,
# filters.jekyll_front_matter is added below with some metadata added
]

def build(self):
Expand All @@ -96,11 +86,22 @@ def _make_content_page(self, node):
with codecs.open(target_path, 'w+', 'utf-8') as target:
renderer = Renderer(source, filters=self.filters)

renderer.add_filter(partial(filters.jekyll_front_matter, self._page_metadata(node)))
renderer.add_filter(partial(filters.write, target))
renderer.render()
if config.cfg.read_next_navigation:
self._add_bottom_navigation(node, target)

def _page_metadata(self, node):
metadata = {}
if node.predecessor:
metadata['prev_page_url'] = "%s.html" % node.predecessor.slug
metadata['prev_page_title'] = "%s.html" % node.predecessor.title
if node.successor:
metadata['next_page_url'] = "%s.html" % node.successor.slug
metadata['next_page_title'] = "%s.html" % node.successor.title
return metadata

def _add_bottom_navigation(self, node, target):
"""Insert navigation for prev/up/next at the bottom of the page.
Expand All @@ -110,33 +111,28 @@ def _add_bottom_navigation(self, node, target):

nav = []

previous_item = node.predecessor
if not node.parent.is_root():
parent_item = node.parent
else:
parent_item = None

# Skip previous if it is the parent item
if previous_item and previous_item is not parent_item:
if node.predecessor and node.predecessor is not parent_item:
nav.append(nav_el(PREV,
previous_item.slug,
' '.join((_('Back to:'), previous_item.title))))
node.predecessor.slug,
' '.join((_('Back to:'), node.predecessor.title))))

# up: parent
if not node.parent.is_root():
nav.append(nav_el(UP,
node.parent.slug,
' '.join((_('Up:'), node.parent.title))))

next_item = node.successor
if next_item:
title = ' '.join((_('Read next:'), next_item.title))
if node.successor:
title = ' '.join((_('Read next:'), node.successor.title))
nav.append(nav_el(' '.join((NEXT, title)),
next_item.slug,
node.successor.slug,
title))

target.write(' '.join(nav))
target.write("\n</div>\n")

if next_item and config.cfg.read_next_shortcuts:
target.write(MOUSETRAP % next_item.slug)
14 changes: 10 additions & 4 deletions mdbuild/renderer/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ def clean_images_old(lines):
FRONT_MATTER_SEPARATOR = "---\n"


def jekyll_front_matter(lines, params=None):
def jekyll_front_matter(metadata, lines):
"""
Inject Jekyll front matter.
TODO: this should use the library for yaml, insert handcrafted lines of text!
"""

line = next(lines)
yield FRONT_MATTER_SEPARATOR
match = HEADLINE_PATTERN.search(line)
Expand All @@ -116,11 +122,11 @@ def jekyll_front_matter(lines, params=None):
# escape quotes in title
yield FRONT_MATTER_TITLE % title.replace("\"", "\\\"")
line = None
if params:
if metadata:
# insert parameters into front matter if present
# preserve order of parameters to avoid random changes in files
for key in sorted(params.keys()):
yield ':'.join((key, params[key]))
for key in sorted(metadata.keys()):
yield '%s: "%s"\n' % (key, metadata[key])
yield FRONT_MATTER_SEPARATOR
yield "\n"

Expand Down
2 changes: 0 additions & 2 deletions mdbuild/renderer/metadata.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
from __future__ import absolute_import

import re
Expand Down Expand Up @@ -179,7 +178,6 @@ def filter(cls, lines, target_format=None):

cls.filter_function = cls._header_filter
for line in lines:
print(line)
res = cls.filter_function(line)
if res is not None:
yield res
2 changes: 1 addition & 1 deletion mdbuild/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def _processed_template(mode, src, dest):
glossary.get_glossary_link_processor('tooltip'),
])
if mode == 'markdown':
renderer.add_filter(filters.jekyll_front_matter)
renderer.add_filter(partial(filters.jekyll_front_matter, None))
renderer.add_filter(partial(filters.write, target))
renderer.render()

Expand Down

0 comments on commit 7f683ff

Please sign in to comment.