Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# - lift first H1 element (if it starts the document) to the title
# - lift all other headers by one level
# - upcase all L2 headers
#
# This script requires the 'pandocfilters' package (pip install pandocfilters)
import json
import sys
import re
from collections import OrderedDict
import pandocfilters
from pandocfilters import walk, stringify, Header, Str, Strong, Span, attributes
MetaString = pandocfilters.elt('MetaString', 1)
def liftTitle(doc):
"Lift the title from the document."
meta = doc[0]
content = doc[1]
heading = None
if content[0]['t'] == 'Header':
if content[0]['c'][0] == 1:
heading = content[0]
if heading is None:
print >> sys.stderr, 'warning: first block not a heading'
sys.exit(1)
metadata = document['meta']
elif document[0]: # old API
metadata = document[0]['unMeta']
if 'draft' in metadata:
DRAFT = metadata['draft']['c']
else:
DRAFT = False
newDocument = document
newDocument = walk(newDocument, handle_comments, format, metadata)
# Need to ensure the LaTeX/beamer template knows if `mdframed` package is
# required (when `` has been used).
if (format == 'latex' or format == 'beamer') and USED_BOX:
MetaList = elt('MetaList', 1)
MetaInlines = elt('MetaInlines', 1)
rawinlines = [MetaInlines([RawInline('tex',
'\\RequirePackage{mdframed}')])]
if 'header-includes' in metadata:
headerIncludes = metadata['header-includes']
if headerIncludes['t'] == 'MetaList':
rawinlines += headerIncludes['c']
else: # headerIncludes['t'] == 'MetaInlines'
rawinlines += [headerIncludes]
metadata['header-includes'] = MetaList(rawinlines)
newDocument['meta'] = metadata
json.dump(newDocument, sys.stdout)
def isheader(key, value):
return (key == 'Header')
math_label = r'\\label{(.*?)}'
def islabeledmath(key, value):
return (key == 'Math' and re.search(math_label, value[1]))
def isattr(string):
return string.startswith('{') and string.endswith('}')
# define a new Figure type - an image with attributes
Figure = pf.elt('Figure', 3) # caption, target, attrs
def isfigure(key, value):
return (key == 'Para' and len(value) == 2 and value[0]['t'] == 'Image')
def isattrfigure(key, value):
return (key == 'Para'
and value[0]['t'] == 'Image'
and isattr(pf.stringify(value[1:])))
def isdivfigure(key, value):
"""Matches images contained in a Div with 'figure' as a class."""
return (key == 'Div' and 'figure' in value[0][1])
def RawBlock(format, string):
"""Overwrite pandocfilters RawBlock so that html5
and html raw output both use the html writer.
"""
if format == 'html5':
format = 'html'
return pf.RawBlock(format, string)
# define a new type for internal references [pre, label, post]
InternalRef = pf.elt('InternalRef', 3)
# and multiple references [pre, [label], post]
# https://tex.stackexchange.com/questions/15728/multiple-references-with-autoref
# https://github.com/mathjax/MathJax/issues/71
# http://docs.mathjax.org/en/latest/tex.html#automatic-equation-numbering
MultiInternalRef = pf.elt('MultiInternalRef', 3)
# define a new Figure type - an image with attributes
Figure = pf.elt('Figure', 3) # caption, target, attrs
def create_pandoc_multilink(strings, refs):
inlines = [[pf.Str(str(s))] for s in strings]
targets = [(r, "") for r in refs]
links = [pf.Link(inline, target)
for inline, target in zip(inlines, targets)]
return join_items(links)
def join_items(items, method='append'):
"""Join the list of items together in the format
if format == 'html5':
format = 'html'
return pf.RawInline(format, string)
def RawBlock(format, string):
"""Overwrite pandocfilters RawBlock so that html5
and html raw output both use the html writer.
"""
if format == 'html5':
format = 'html'
return pf.RawBlock(format, string)
# define a new type for internal references [pre, label, post]
InternalRef = pf.elt('InternalRef', 3)
# and multiple references [pre, [label], post]
# https://tex.stackexchange.com/questions/15728/multiple-references-with-autoref
# https://github.com/mathjax/MathJax/issues/71
# http://docs.mathjax.org/en/latest/tex.html#automatic-equation-numbering
MultiInternalRef = pf.elt('MultiInternalRef', 3)
# define a new Figure type - an image with attributes
Figure = pf.elt('Figure', 3) # caption, target, attrs
def create_pandoc_multilink(strings, refs):
inlines = [[pf.Str(str(s))] for s in strings]
targets = [(r, "") for r in refs]
links = [pf.Link(inline, target)
for inline, target in zip(inlines, targets)]
return join_items(links)
and html raw output both use the html writer.
"""
if format == 'html5':
format = 'html'
return pf.RawBlock(format, string)
# define a new type for internal references [pre, label, post]
InternalRef = pf.elt('InternalRef', 3)
# and multiple references [pre, [label], post]
# https://tex.stackexchange.com/questions/15728/multiple-references-with-autoref
# https://github.com/mathjax/MathJax/issues/71
# http://docs.mathjax.org/en/latest/tex.html#automatic-equation-numbering
MultiInternalRef = pf.elt('MultiInternalRef', 3)
# define a new Figure type - an image with attributes
Figure = pf.elt('Figure', 3) # caption, target, attrs
def create_pandoc_multilink(strings, refs):
inlines = [[pf.Str(str(s))] for s in strings]
targets = [(r, "") for r in refs]
links = [pf.Link(inline, target)
for inline, target in zip(inlines, targets)]
return join_items(links)
def join_items(items, method='append'):
"""Join the list of items together in the format
'item[0]' if len(items) == 1
'item[0] and item[1]' if len(items) == 2
elif document[0]: # old API
metadata = document[0]['unMeta']
if 'draft' in metadata:
DRAFT = metadata['draft']['c']
else:
DRAFT = False
newDocument = document
newDocument = walk(newDocument, handle_comments, format, metadata)
# Need to ensure the LaTeX/beamer template knows if `mdframed` package is
# required (when `` has been used).
if (format == 'latex' or format == 'beamer') and USED_BOX:
MetaList = elt('MetaList', 1)
MetaInlines = elt('MetaInlines', 1)
rawinlines = [MetaInlines([RawInline('tex',
'\\RequirePackage{mdframed}')])]
if 'header-includes' in metadata:
headerIncludes = metadata['header-includes']
if headerIncludes['t'] == 'MetaList':
rawinlines += headerIncludes['c']
else: # headerIncludes['t'] == 'MetaInlines'
rawinlines += [headerIncludes]
metadata['header-includes'] = MetaList(rawinlines)
newDocument['meta'] = metadata
json.dump(newDocument, sys.stdout)