def test_detach_attrs_factory(self): """Tests filter_attrs_factory().""" detach_attrs_math = detach_attrs_factory(Math) ## Use 'expected' from test_attach_attrs_factory ## src = eval( r'''{"meta":{},"blocks":[{"t":"Para","c":[{"t":"Math","c":[["eq:1",[],[["tag","B.1"]]],{"t":"DisplayMath"}," y = f(x) "]}]}],"pandoc-api-version":[1,17,3]}''' ) # test.md: $$ y = f(x) $$ # Command: pandoc test.md -t json expected = eval( r'''{"blocks":[{"t":"Para","c":[{"t":"Math","c":[{"t":"DisplayMath"}," y = f(x) "]}]}],"pandoc-api-version":[1,17,3],"meta":{}}''' ) # Check expected against current pandoc md = subprocess.Popen(('echo', '$$ y = f(x) $$'), stdout=subprocess.PIPE) output = eval( subprocess.check_output('pandoc -t json'.split(), stdin=md.stdout).strip()) self.assertEqual(expected, output) # Make the comparison self.assertEqual(walk(src, detach_attrs_math, '', {}), expected)
def main(): """Filters the document AST.""" # pylint: disable=global-statement global PANDOCVERSION global AttrMath # Get the output format and document fmt = args.fmt doc = json.loads(STDIN.read()) # Initialize pandocxnos # pylint: disable=too-many-function-args PANDOCVERSION = pandocxnos.init(args.pandocversion, doc) # Element primitives AttrMath = elt('Math', 3) # Chop up the doc meta = doc['meta'] if PANDOCVERSION >= '1.18' else doc[0]['unMeta'] blocks = doc['blocks'] if PANDOCVERSION >= '1.18' else doc[1:] # Process the metadata variables process(meta) # First pass attach_attrs_math = attach_attrs_factory(Math, allow_space=True) detach_attrs_math = detach_attrs_factory(Math) insert_secnos = insert_secnos_factory(Math) delete_secnos = delete_secnos_factory(Math) altered = functools.reduce(lambda x, action: walk(x, action, fmt, meta), [attach_attrs_math, insert_secnos, process_equations, delete_secnos, detach_attrs_math], blocks) # Second pass process_refs = process_refs_factory(references.keys()) replace_refs = replace_refs_factory(references, use_cleveref_default, use_eqref, plusname if not capitalize else [name.title() for name in plusname], starname, 'equation') altered = functools.reduce(lambda x, action: walk(x, action, fmt, meta), [repair_refs, process_refs, replace_refs], altered) # Update the doc if PANDOCVERSION >= '1.18': doc['blocks'] = altered else: doc = doc[:1] + altered # Dump the results json.dump(doc, STDOUT) # Flush stdout STDOUT.flush()
def main(): """Filters the document AST.""" # pylint: disable=global-statement global PANDOCVERSION global AttrMath # Get the output format and document fmt = args.fmt doc = json.loads(STDIN.read()) # Initialize pandocxnos # pylint: disable=too-many-function-args PANDOCVERSION = pandocxnos.init(args.pandocversion, doc) # Element primitives AttrMath = elt('Math', 2) # Chop up the doc meta = doc['meta'] if PANDOCVERSION >= '1.18' else doc[0]['unMeta'] blocks = doc['blocks'] if PANDOCVERSION >= '1.18' else doc[1:] # First pass attach_attrs_math = attach_attrs_factory(Math, allow_space=True) detach_attrs_math = detach_attrs_factory(Math) insert_secnos = insert_secnos_factory(Math) delete_secnos = delete_secnos_factory(Math) altered = functools.reduce(lambda x, action: walk(x, action, fmt, meta), [attach_attrs_math, insert_secnos, process_tables, delete_secnos, detach_attrs_math], blocks) # Second pass altered = functools.reduce(lambda x, action: walk(x, action, fmt, meta), [replace_table_references], altered) # Update the doc if PANDOCVERSION >= '1.18': doc['blocks'] = altered else: doc = doc[:1] + altered # Dump the results json.dump(doc, STDOUT) # Flush stdout STDOUT.flush()
def test_detach_attrs_factory(self): """Tests filter_attrs_factory().""" detach_attrs_math = detach_attrs_factory(Math) ## Use 'expected' from test_attach_attrs_factory ## src = eval(r'''{"meta":{},"blocks":[{"t":"Para","c":[{"t":"Math","c":[["eq:1",[],[["tag","B.1"]]],{"t":"DisplayMath"}," y = f(x) "]}]}],"pandoc-api-version":[1,17,5,1]}''') # test.md: $$ y = f(x) $$ # Command: pandoc test.md -t json expected = eval(r'''{"blocks":[{"t":"Para","c":[{"t":"Math","c":[{"t":"DisplayMath"}," y = f(x) "]}]}],"pandoc-api-version":[1,17,5,1],"meta":{}}''') # Check expected against current pandoc md = subprocess.Popen(('echo', '$$ y = f(x) $$'), stdout=subprocess.PIPE) output = eval(subprocess.check_output( 'pandoc -t json'.split(), stdin=md.stdout).strip()) self.assertEqual(expected, output) # Make the comparison self.assertEqual(walk(src, detach_attrs_math, '', {}), expected)
def main(): """Filters the document AST.""" # pylint: disable=global-statement global PANDOCVERSION global AttrTable # Get the output format and document fmt = args.fmt doc = json.loads(STDIN.read()) # Initialize pandocxnos # pylint: disable=too-many-function-args PANDOCVERSION = pandocxnos.init(args.pandocversion, doc) # Element primitives AttrTable = elt('Table', 6) # Chop up the doc meta = doc['meta'] if PANDOCVERSION >= '1.18' else doc[0]['unMeta'] blocks = doc['blocks'] if PANDOCVERSION >= '1.18' else doc[1:] # Process the metadata variables process(meta) # First pass detach_attrs_table = detach_attrs_factory(Table) insert_secnos = insert_secnos_factory(Table) delete_secnos = delete_secnos_factory(Table) altered = functools.reduce(lambda x, action: walk(x, action, fmt, meta), [attach_attrs_table, insert_secnos, process_tables, delete_secnos, detach_attrs_table], blocks) # Second pass process_refs = process_refs_factory(references.keys()) replace_refs = replace_refs_factory(references, use_cleveref_default, False, plusname if not capitalize else [name.title() for name in plusname], starname, 'table') altered = functools.reduce(lambda x, action: walk(x, action, fmt, meta), [repair_refs, process_refs, replace_refs], altered) # Insert supporting TeX if fmt in ['latex']: rawblocks = [] if has_unnumbered_tables: rawblocks += [RawBlock('tex', TEX0), RawBlock('tex', TEX1), RawBlock('tex', TEX2)] if captionname != 'Table': rawblocks += [RawBlock('tex', TEX3 % captionname)] insert_rawblocks = insert_rawblocks_factory(rawblocks) altered = functools.reduce(lambda x, action: walk(x, action, fmt, meta), [insert_rawblocks], altered) # Update the doc if PANDOCVERSION >= '1.18': doc['blocks'] = altered else: doc = doc[:1] + altered # Dump the results json.dump(doc, STDOUT) # Flush stdout STDOUT.flush()
def main(stdin=STDIN, stdout=STDOUT, stderr=STDERR): """Filters the document AST.""" # pylint: disable=global-statement global PANDOCVERSION global Image # Read the command-line arguments parser = argparse.ArgumentParser(\ description='Pandoc figure numbers filter.') parser.add_argument(\ '--version', action='version', version='%(prog)s {version}'.format(version=__version__)) parser.add_argument('fmt') parser.add_argument('--pandocversion', help='The pandoc version.') args = parser.parse_args() # Get the output format and document fmt = args.fmt doc = json.loads(stdin.read()) # Initialize pandocxnos PANDOCVERSION = pandocxnos.init(args.pandocversion, doc) # Element primitives if PANDOCVERSION < '1.16': Image = elt('Image', 2) # Chop up the doc meta = doc['meta'] if PANDOCVERSION >= '1.18' else doc[0]['unMeta'] blocks = doc['blocks'] if PANDOCVERSION >= '1.18' else doc[1:] # Process the metadata variables process(meta) # First pass replace = PANDOCVERSION >= '1.16' attach_attrs_image = attach_attrs_factory(Image, extract_attrs=_extract_attrs, replace=replace) detach_attrs_image = detach_attrs_factory(Image) insert_secnos_img = insert_secnos_factory(Image) delete_secnos_img = delete_secnos_factory(Image) insert_secnos_div = insert_secnos_factory(Div) delete_secnos_div = delete_secnos_factory(Div) altered = functools.reduce(lambda x, action: walk(x, action, fmt, meta), [ attach_attrs_image, insert_secnos_img, insert_secnos_div, process_figures, delete_secnos_img, delete_secnos_div, detach_attrs_image ], blocks) # Second pass process_refs = process_refs_factory(LABEL_PATTERN, targets.keys()) replace_refs = replace_refs_factory(targets, cleveref, False, plusname if not capitalise \ or plusname_changed else [name.title() for name in plusname], starname) attach_attrs_span = attach_attrs_factory(Span, replace=True) altered = functools.reduce( lambda x, action: walk(x, action, fmt, meta), [repair_refs, process_refs, replace_refs, attach_attrs_span], altered) if fmt in ['latex', 'beamer']: add_tex(meta) # Update the doc if PANDOCVERSION >= '1.18': doc['blocks'] = altered else: doc = doc[:1] + altered # Dump the results json.dump(doc, stdout) # Flush stdout stdout.flush()
def main(): """Filters the document AST.""" # pylint: disable=global-statement global PANDOCVERSION global Image # Get the output format and document fmt = args.fmt doc = json.loads(STDIN.read()) # Initialize pandocxnos # pylint: disable=too-many-function-args PANDOCVERSION = pandocxnos.init(args.pandocversion, doc) # Element primitives if PANDOCVERSION < '1.16': Image = elt('Image', 2) # Chop up the doc meta = doc['meta'] if PANDOCVERSION >= '1.18' else doc[0]['unMeta'] blocks = doc['blocks'] if PANDOCVERSION >= '1.18' else doc[1:] # Process the metadata variables process(meta) # First pass attach_attrs_image = attach_attrs_factory(Image, extract_attrs=_extract_attrs) detach_attrs_image = detach_attrs_factory(Image) insert_secnos = insert_secnos_factory(Image) delete_secnos = delete_secnos_factory(Image) filters = [insert_secnos, process_figures, delete_secnos] \ if PANDOCVERSION >= '1.16' else \ [attach_attrs_image, insert_secnos, process_figures, delete_secnos, detach_attrs_image] altered = functools.reduce(lambda x, action: walk(x, action, fmt, meta), filters, blocks) # Second pass process_refs = process_refs_factory(references.keys()) replace_refs = replace_refs_factory(references, cleveref_default, plusname, starname, 'figure') altered = functools.reduce(lambda x, action: walk(x, action, fmt, meta), [repair_refs, process_refs, replace_refs], altered) # Insert supporting TeX if fmt == 'latex': rawblocks = [] if has_unnumbered_figures: rawblocks += [ RawBlock('tex', TEX0), RawBlock('tex', TEX1), RawBlock('tex', TEX2) ] if captionname != 'Figure': rawblocks += [RawBlock('tex', TEX3 % captionname)] insert_rawblocks = insert_rawblocks_factory(rawblocks) altered = functools.reduce( lambda x, action: walk(x, action, fmt, meta), [insert_rawblocks], altered) # Update the doc if PANDOCVERSION >= '1.18': doc['blocks'] = altered else: doc = doc[:1] + altered # Dump the results json.dump(doc, STDOUT) # Flush stdout STDOUT.flush()
def main(stdin=STDIN, stdout=STDOUT, stderr=STDERR): """Filters the document AST.""" # pylint: disable=global-statement global PANDOCVERSION global AttrMath # Read the command-line arguments parser = argparse.ArgumentParser( \ description='Pandoc equations numbers filter.') parser.add_argument( \ '--version', action='version', version='%(prog)s {version}'.format(version=__version__)) parser.add_argument('fmt') parser.add_argument('--pandocversion', help='The pandoc version.') args = parser.parse_args() # Get the output format and document fmt = args.fmt doc = json.loads(stdin.read()) # Initialize pandocxnos PANDOCVERSION = pandocxnos.init(args.pandocversion, doc) # Element primitives AttrMath = elt('Math', 3) # Chop up the doc meta = doc['meta'] if version(PANDOCVERSION) >= version('1.18') \ else doc[0]['unMeta'] blocks = doc['blocks'] if version(PANDOCVERSION) >= version('1.18') \ else doc[1:] # Process the metadata variables process(meta) # First pass attach_attrs_math = attach_attrs_factory(Math, allow_space=True) detach_attrs_math = detach_attrs_factory(Math) insert_secnos = insert_secnos_factory(Math) delete_secnos = delete_secnos_factory(Math) altered = functools.reduce(lambda x, action: walk(x, action, fmt, meta), [ attach_attrs_math, insert_secnos, process_equations, delete_secnos, detach_attrs_math ], blocks) # Second pass process_refs = process_refs_factory(LABEL_PATTERN, targets.keys()) replace_refs = replace_refs_factory(targets, cleveref, eqref, plusname if not capitalise or \ plusname_changed else [name.title() for name in plusname], starname) attach_attrs_span = attach_attrs_factory(Span, replace=True) altered = functools.reduce( lambda x, action: walk(x, action, fmt, meta), [repair_refs, process_refs, replace_refs, attach_attrs_span], altered) if fmt in ['latex', 'beamer']: add_tex(meta) elif fmt in ['html', 'html4', 'html5', 'epub', 'epub2', 'epub3']: add_html(meta, fmt) # Update the doc if version(PANDOCVERSION) >= version('1.18'): doc['blocks'] = altered else: doc = doc[:1] + altered # Dump the results json.dump(doc, stdout) # Flush stdout stdout.flush()
Nreferences = 0 # The numbered references count (i.e., excluding tags) references = {} # Global references tracker unreferenceable = [] # List of labels that are unreferenceable # Meta variables; may be reset elsewhere plusname = ['eq.', 'eqs.'] # Used with \cref starname = ['Equation', 'Equations'] # Used with \Cref cleveref_default = False # Default setting for clever referencing # Element primitives AttrMath = elt('Math', 3) # Actions -------------------------------------------------------------------- attach_attrs_math = attach_attrs_factory(Math, allow_space=True) detach_attrs_math = detach_attrs_factory(Math) def _process_equation(value, fmt): """Processes the equation. Returns a dict containing eq properties.""" global Nreferences # pylint: disable=global-statement # Parse the equation attrs = value[0] # Initialize the return value eq = { 'is_unnumbered': False, 'is_unreferenceable': False, 'is_tagged': False,
def main(): """Filters the document AST.""" # pylint: disable=global-statement global PANDOCVERSION global AttrTable # Get the output format and document fmt = args.fmt doc = json.loads(STDIN.read()) # Initialize pandocxnos # pylint: disable=too-many-function-args PANDOCVERSION = pandocxnos.init(args.pandocversion, doc) # Element primitives AttrTable = elt('Table', 6) # Chop up the doc meta = doc['meta'] if PANDOCVERSION >= '1.18' else doc[0]['unMeta'] blocks = doc['blocks'] if PANDOCVERSION >= '1.18' else doc[1:] # Process the metadata variables process(meta) # First pass detach_attrs_table = detach_attrs_factory(Table) insert_secnos = insert_secnos_factory(Table) delete_secnos = delete_secnos_factory(Table) altered = functools.reduce(lambda x, action: walk(x, action, fmt, meta), [ attach_attrs_table, insert_secnos, process_tables, delete_secnos, detach_attrs_table ], blocks) # Second pass process_refs = process_refs_factory(references.keys()) replace_refs = replace_refs_factory( references, use_cleveref_default, False, plusname if not capitalize else [name.title() for name in plusname], starname, 'table') altered = functools.reduce(lambda x, action: walk(x, action, fmt, meta), [repair_refs, process_refs, replace_refs], altered) # Insert supporting TeX if fmt in ['latex']: rawblocks = [] if has_unnumbered_tables: rawblocks += [ RawBlock('tex', TEX0), RawBlock('tex', TEX1), RawBlock('tex', TEX2) ] if captionname != 'Table': rawblocks += [RawBlock('tex', TEX3 % captionname)] insert_rawblocks = insert_rawblocks_factory(rawblocks) altered = functools.reduce( lambda x, action: walk(x, action, fmt, meta), [insert_rawblocks], altered) # Update the doc if PANDOCVERSION >= '1.18': doc['blocks'] = altered else: doc = doc[:1] + altered # Dump the results json.dump(doc, STDOUT) # Flush stdout STDOUT.flush()
# image references for pandoc < 1.16 (pandoc-fignos Issue #14). # See http://pandoc.org/MANUAL.html#images for the syntax. # Note: This code does not handle the "optional title" for # image references (search for link_attributes in pandoc's docs). assert x[n-1]['t'] == 'Image' image = x[n-1] s = image['c'][-1][0] if '%20%7B' in s: path = s[:s.index('%20%7B')] attrs = unquote(s[s.index('%7B'):]) image['c'][-1][0] = path # Remove attr string from the path return PandocAttributes(attrs.strip(), 'markdown').to_pandoc() raise attach_attrs_image = attach_attrs_factory(Image, extract_attrs=_extract_attrs) detach_attrs_image = detach_attrs_factory(Image) def _process_figure(value, fmt): """Processes the figure. Returns a dict containing figure properties.""" # pylint: disable=global-statement global Nreferences global has_unnumbered_figures # Parse the image attrs, caption = value[0]['c'][:2] # Initialize the return value fig = {'is_unnumbered': False, 'is_unreferenceable': False,
assert len(value) == 5 caption = value[0] # caption, align, x, head, body # Set n to the index where the attributes start n = 0 while n < len(caption) and not \ (caption[n]['t'] == 'Str' and caption[n]['c'].startswith('{')): n += 1 try: attrs = extract_attrs(caption, n) value.insert(0, attrs) except (ValueError, IndexError): pass detach_attrs_table = detach_attrs_factory(Table) def _store_ref(attrs): """Stores the reference in the global references tracker. Returns True if this is a tagged table; False otherwise.""" # pylint: disable=global-statement global Nreferences attrs = PandocAttributes(attrs, 'pandoc') if 'tag' in attrs.kvs: # Remove any surrounding quotes if attrs['tag'][0] == '"' and attrs['tag'][-1] == '"': attrs['tag'] = attrs['tag'].strip('"') elif attrs['tag'][0] == "'" and attrs['tag'][-1] == "'": attrs['tag'] = attrs['tag'].strip("'")