From 9f05f7f9367c89d5fd2073adb3969f2b010bdf1f Mon Sep 17 00:00:00 2001 From: Raniere Silva Date: Fri, 5 Dec 2014 19:05:20 -0200 Subject: [PATCH] Add id to glossary entry This is needed to enable users jump to glossary entries. --- tools/id4glossary.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100755 tools/id4glossary.py diff --git a/tools/id4glossary.py b/tools/id4glossary.py new file mode 100755 index 0000000..d77f52b --- /dev/null +++ b/tools/id4glossary.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python +"""Pandoc filter to convert add ids to glossary entries. + +Usage: + + pandoc source.md --filter=id4glossary.py --output=output.html +""" +import pandocfilters as pf + +def normalize_keyword(keyword): + """Normalize keyword for became id + + - Replace white space with '-' + - Convert to lowercase""" + return keyword.lower().replace(' ', '-') + +def keyword2html(keyword_node): + """Return HTML version of keyword with id.""" + keyword = ' '.join([word['c'] for word in keyword_node if word['t'] == 'Str']) + id = normalize_keyword(keyword) + return [{"t": "Span", + "c": [[id, [],[]], + keyword_node]}] + +def id4glossary(key, value, format, meta): + """Add id to keywords at glossary.""" + if "subtitle" in meta and \ + ''.join([string['c'] for string in meta["subtitle"]['c']]) == 'Reference': + if key == "DefinitionList": + for definition in value: + definition[0] = keyword2html(definition[0]) + return {"t": key, + "c": value} + +if __name__ == '__main__': + pf.toJSONFilter(id4glossary) -- GitLab