def main(): doc = pf.json.loads(pf.sys.stdin.read()) if len(pf.sys.argv) > 1: format = pf.sys.argv[1] else: format = "" metadata = doc[0]['unMeta'] args = {k: v['c'] for k, v in metadata.items()} autoref = args.get('autoref', True) numbersections = args.get('numbersections', True) refmanager = ReferenceManager(autoref=autoref, numbersections=numbersections) altered = doc for action in refmanager.reference_filter: altered = pf.walk(altered, action, format, metadata) # Need to ensure the LaTeX template knows about figures and tables # by adding to metadata (only if it's not already specified). if format == 'latex' or format == 'beamer': if refmanager.table_exists and 'tables' not in metadata: metadata['tables'] = pf.elt('MetaBool', 1)(True) if refmanager.figure_exists and 'graphics' not in metadata: metadata['graphics'] = pf.elt('MetaBool', 1)(True) altered[0]['unMeta'] = metadata pf.json.dump(altered, pf.sys.stdout)
def main(): doc = pf.json.loads(pf.sys.stdin.read()) if len(pf.sys.argv) > 1: format = pf.sys.argv[1] else: format = "" if 'meta' in doc: metadata = doc['meta'] elif doc[0]: # old API metadata = doc[0]['unMeta'] args = {k: v['c'] for k, v in metadata.items()} autoref = args.get('autoref', True) numbersections = args.get('numbersections', True) refmanager = ReferenceManager(autoref=autoref, numbersections=numbersections) altered = doc for action in refmanager.reference_filter: altered = pf.walk(altered, action, format, metadata) # Need to ensure the LaTeX template knows about figures and tables # by adding to metadata (only if it's not already specified). if format == 'latex' or format == 'beamer': if refmanager.table_exists and 'tables' not in metadata: metadata['tables'] = pf.elt('MetaBool', 1)(True) if refmanager.figure_exists and 'graphics' not in metadata: metadata['graphics'] = pf.elt('MetaBool', 1)(True) altered['meta'] = metadata pf.json.dump(altered, pf.sys.stdout)
def process_tables(key, value, fmt, meta): """Processes the attributed tables.""" if key == 'Table' and len(value) == 6: # Parse the table attrs, caption = value[0:2] # attrs, caption, align, x, head, body # Bail out if the label does not conform if not attrs[0] or not LABEL_PATTERN.match(attrs[0]): return if attrs[0] == 'tbl:': # Make up a unique description attrs[0] = 'tbl:' + str(uuid.uuid4()) # Save the reference is_tagged = _store_ref(attrs) # Adjust caption depending on the output format if fmt == 'latex': value[1] += [RawInline('tex', r'\label{%s}'%attrs[0])] elif type(references[attrs[0]]) is int: value[1] = [Str(captionname), Space(), Str('%d:'%references[attrs[0]]), Space()] + \ list(caption) else: # It is a string assert type(references[attrs[0]]) in STRTYPES # Handle both math and text text = references[attrs[0]] if text.startswith('$') and text.endswith('$'): math = text.replace(' ', r'\ ')[1:-1] els = [Math({"t":"InlineMath", "c":[]}, math), Str(':')] else: els = [Str(text + ':')] value[1] = [Str('Table'), Space()] + els + [Space()] + list(caption) # Context-dependent output if fmt == 'latex' and is_tagged: # Code in the tags tex = '\n'.join([r'\let\oldthetable=\thetable', r'\renewcommand\thetable{%s}'%\ references[attrs[0]]]) pre = RawBlock('tex', tex) table = elt('Table', 6)(*value) # pylint: disable=star-args table['c'] = list(table['c']) # Needed for attr filtering tex = '\n'.join([r'\let\thetable=\oldthetable', r'\addtocounter{table}{-1}']) post = RawBlock('tex', tex) return [pre, table, post] elif fmt in ('html', 'html5'): # Insert anchor table = elt('Table', 6)(*value) # pylint: disable=star-args table['c'] = list(table['c']) # Needed for attr filtering anchor = RawBlock('html', '<a name="%s"></a>'%attrs[0]) return [anchor, table]
def main(): # This grabs the output of `pandoc` as json file, retrieves `metadata` to # check for draft status, and runs the document through `handle_comments`. # Then adds any needed entries to `metadata` and passes the output back out # to `pandoc`. This code is modeled after # <https://github.com/aaren/pandoc-reference-filter>. global DRAFT document = json.loads(sys.stdin.read()) if len(sys.argv) > 1: format = sys.argv[1] else: format = '' if 'meta' in document: # new API 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 `<!box>` 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 main(): # This grabs the output of `pandoc` as json file, retrieves `metadata` to # check for draft status, and runs the document through `handle_comments`. # Then adds any needed entries to `metadata` and passes the output back out # to `pandoc`. This code is modeled after # <https://github.com/aaren/pandoc-reference-filter>. global DRAFT document = json.loads(sys.stdin.read()) if len(sys.argv) > 1: format = sys.argv[1] else: format = '' if 'meta' in document: # new API 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 `<!box>` 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)
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 and Table types -- with attributes Figure = pf.elt('Figure', 3) # caption, target, attrs TableAttrs = pf.elt('TableAttrs', 6) # caption, alignment, size, headers, rows, attrs def isParaFigure(key, value): try: return (key == 'Para' and value[0]['t'] == 'Image') except IndexError: return False def isdivfigure(key, value): """Matches images contained in a Div with 'figure' as a class.""" try: return (key == 'Div' and 'figure' in value[0][1])
def isheader(key, value): return (key == 'Header') def islabeledmath(key, value): return (key == 'Math' and re.search(r'\\label{\S*}', 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])
'tcolorbox' package to the 'header-includes' header automatically. Currently containers can be embedded only with a single closing delimiter, e. g. :::info :::spoiler Foo bar ::: Note that the container delimiters are required to be surrounded by blanklines in order to be parsed correctly by pandoc. See also https://www.npmjs.com/package/markdown-it-container. """ MetaInlines = elt('MetaInlines', 1) MetaList = elt('MetaList', 1) DELIMITER = '%3A%3A%3A' COLORS = { 'info': '[colback=blue!5!white,colframe=blue!75!black]', 'success': '[colback=green!5!white,colframe=green!75!black]', 'warning': '[colback=yellow!5!white,colframe=yellow!75!black]', 'danger': '[colback=red!5!white,colframe=red!75!black]', } configured = False depth = 0 def configure(meta):
# but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Foobar. If not, see <http://www.gnu.org/licenses/>. from pandocfilters import walk, Str, elt import codecs import json import sys import tempfile import urllib import urllib2 MetaInlines = elt('MetaInlines', 1) def toJSONFilter(filters=[], metafilters=[]): reader = codecs.getreader('utf8') doc = json.loads(reader(sys.stdin).read()) if len(sys.argv) > 1: format = sys.argv[1] else: format = "" altered = doc for action in filters: altered = walk(altered, action, format, doc['meta']) for action in metafilters: action(altered['meta']) json.dump(altered, sys.stdout)
import re import functools import itertools import io import sys # pylint: disable=import-error import pandocfilters from pandocfilters import stringify, walk from pandocfilters import RawInline, Str, Space, Para, Plain, Cite, elt from pandocattributes import PandocAttributes # Create our own pandoc image primitives to accommodate different pandoc # versions. # pylint: disable=invalid-name Image = elt('Image', 2) # Pandoc < 1.16 AttrImage = elt('Image', 3) # Pandoc >= 1.16 # Patterns for matching labels and references LABEL_PATTERN = re.compile(r'(fig:[\w/-]*)(.*)') REF_PATTERN = re.compile(r'@(fig:[\w/-]+)') # Detect python 3 PY3 = sys.version_info > (3, ) # Pandoc uses UTF-8 for both input and output; so must we if PY3: # Force utf-8 decoding (decoding of input streams is automatic in py3) STDIN = io.TextIOWrapper(sys.stdin.buffer, 'utf-8', 'strict') STDOUT = io.TextIOWrapper(sys.stdout.buffer, 'utf-8', 'strict') else: # No decoding; utf-8-encoded strings in means the same out STDIN = sys.stdin
# It does 3 things: # - 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, Link MetaString = pandocfilters.elt('MetaString', 1) _man_link_re = re.compile(r'^man:(.*)\((\d)\)') def interpretManLinks(key, value, fmt, meta): if key == 'Link': text, link = value url, title = link match = _man_link_re.match(url) if match is not None: html_url = "%s.%s.html" % (match.group(1), match.group(2)) return Link(text, (html_url, title)) else: return None
# A Pandoc filter script to make a manpage standards-conformant but look sane in source. # It does 3 things: # - 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 pandocfilters from pandocfilters import walk, stringify, Header, Str import json import sys import re from collections import OrderedDict 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)
import argparse import csv import json import sys from io import StringIO import pypandoc import requests from pandocfilters import Table, elt, toJSONFilter, Plain __VERSION__ = "0.1" __AUTHOR__ = "Julien Hadley Jack <*****@*****.**>" # Missing constructors for Pandoc elements responsible for the column alignment in tables ALIGNMENT = { "l": elt("AlignLeft", 1)([]), "c": elt("AlignCenter", 1)([]), "r": elt("AlignRight", 1)([]), "d": elt("AlignDefault", 1)([]), } def csv_table(key, value, fmt, meta): """ The filter that creates a table from a csv file. :param key: The type of pandoc object :type key: str :param value: The contents of the object :type value: str | list :param fmt: The target output format
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])
#! /usr/bin/env python3 from pandocfilters import toJSONFilter, Image, elt, Str from os import path import re """ Replaces :emoji: strings by actual images. Requires CodiMD's emoji images that can be obtained using the get-emojis.sh script in 'scripts/'. """ MetaBool = elt('MetaBool', 1) EMOJI_IMAGES = path.realpath( path.join(path.dirname(__file__), 'resources', 'emojis')) EMOJI_REGEX = re.compile('^:(.+):$') configured = False def configure(meta): global configured if not configured: configured = True meta.update({'graphics': MetaBool(True)}) def emojis(key, value, fmt, meta):
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 and Table types -- with attributes Figure = pf.elt('Figure', 3) # caption, target, attrs TableAttrs = pf.elt('TableAttrs', 6) # caption, alignment, size, headers, rows, attrs def isParaFigure(key, value): try: return (key == 'Para' and value[0]['t'] == 'Image') except IndexError: return False def isdivfigure(key, value): """Matches images contained in a Div with 'figure' as a class.""" try: return (key == 'Div' and 'figure' in value[0][1]) except IndexError: return False
from pandocfilters import toJSONFilter, Link, Image, Str, walk, stringify, elt import re import configparser from . import global_vars meta_tags_written = [] config = None MetaBool = elt("MetaBool", 1) MetaMap = elt("MetaMap", 1) MetaInlines = elt("MetaInlines", 1) def get_meta_list(meta, name, default=None): result = meta.get(name, {}) if not result or result["t"] != "MetaList": result = default return result def get_meta_string(meta, name, default=""): result = meta.get(name, {}) if result and result["t"] == "MetaInlines": result = stringify(result) elif result and result["t"] == "MetaString": result = result["c"] else: result = default
# pattern that matches #reflink # only allow characters that we can have in latex labels # currently have to be after whitespace # terminated by whitespace, period or backslash imp_reflink_pattern = re.compile(r'([\s]?)(#[\w:&^]+)([\. \t\\]?)') def isinternalref(key, value): # This can fall over if we don't create_figures from our # special attr images first - it can match #id in the attrs return key == 'Str' and imp_reflink_pattern.match(value) # define a new type for internal references [pre, label, post] InternalRef = pf.elt('InternalRef', 3) def isattr(string): return string.startswith('{') and string.endswith('}') 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:])))
import re import functools import itertools import io import sys # pylint: disable=import-error import pandocfilters from pandocfilters import stringify, walk from pandocfilters import RawInline, Str, Space, Para, Plain, Cite, elt from pandocattributes import PandocAttributes # Create our own pandoc image primitives to accommodate different pandoc # versions. # pylint: disable=invalid-name Image = elt('Image', 2) # Pandoc < 1.16 AttrImage = elt('Image', 3) # Pandoc >= 1.16 # Patterns for matching labels and references LABEL_PATTERN = re.compile(r'(fig:[\w/-]*)(.*)') REF_PATTERN = re.compile(r'@(fig:[\w/-]+)') # Detect python 3 PY3 = sys.version_info > (3,) # Pandoc uses UTF-8 for both input and output; so must we if PY3: # Force utf-8 decoding (decoding of input streams is automatic in py3) STDIN = io.TextIOWrapper(sys.stdin.buffer, 'utf-8', 'strict') STDOUT = io.TextIOWrapper(sys.stdout.buffer, 'utf-8', 'strict') else: # No decoding; utf-8-encoded strings in means the same out STDIN = sys.stdin