Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def inner_test_stringify(input_fn, output_fn):
output_txt_benchmark = './tests/temp_benchmark.txt'
output_txt_panflute = './tests/temp_panflute.txt'
print('Testing stringify()')
with open(input_fn, encoding='utf-8') as f:
doc = pf.load(f)
ans = pf.stringify(doc)
#print(repr(ans).encode('utf-8'))
with open(output_txt_panflute, encoding='utf-8', mode='w') as f:
f.write(ans)
with open(input_fn, encoding='utf-8') as f:
doc = json.load(f)
ans = pandocfilters.stringify(doc)
with open(output_txt_benchmark, encoding='utf-8', mode='w') as f:
f.write(ans)
def get_md_opts(self, meta):
'pickup user preferences from meta block'
dct = {}
try:
sep = "."
for k,v in meta.items():
if not k.lower().startswith("imagine."): continue
if k.count(sep) == 1:
_, opt = k.split(sep) # imagine.option: value
dct[opt] = pf.stringify(v)
elif k.count(sep) == 2:
_, klass, opt = k.split(sep) # imagine.klass.option: val
# klass = klass.lower()
if not dct.get(klass): dct[klass] = {}
dct[klass][opt] = pf.stringify(v)
except AttributeError:
pass
self.msg(4, "meta-data:", dct)
self.md_opts = dct
return dct
def getListMap(meta, name):
# Return a MetaMap as defined in the meta
#if not hasattr(getListMap, 'value'):
getListMap.value = {}
if name in meta and meta[name]['t'] == 'MetaList':
getListMap.value = []
for valuemap in meta[name]['c']:
tmpmap = {}
if valuemap['t'] == 'MetaMap':
for key, value in valuemap['c'].items():
if value['t'] == 'MetaInlines':
string = stringify(value['c'])
if re.match('^[a-zA-Z][\w.:-]*$', string):
tmpmap[key] = string
if tmpmap :
getListMap.value.append(tmpmap)
getListMap.value = list(getListMap.value)
return getListMap.value
def get_list_or_inline(metadata, field):
""" return content of MetaList or MetaInlines item coerced as list """
field_type = get_type(metadata, field)
if field_type == 'MetaInlines':
content_raw = get_content(metadata, field, 'MetaInlines')
content = [pandocfilters.stringify(content_raw)]
return content
elif field_type == 'MetaString':
content_raw = get_content(metadata, field, 'MetaString')
content = [content_raw]
return content
elif field_type == 'MetaList':
content = list()
for content_raw in get_content(metadata, field, 'MetaList'):
content.append(pandocfilters.stringify(content_raw))
return content
else:
raise error.WrongType('"%s" value must be of type "MetaInlines", '
'"MetaList", or "MetaString"' % field)
def getDefined(meta):
# Return the latex-environment defined in the meta
if not hasattr(getDefined, 'value'):
getDefined.value = {}
if 'pandoc-latex-environment' in meta and meta['pandoc-latex-environment']['t'] == 'MetaMap':
for environment, classes in meta['pandoc-latex-environment']['c'].items():
if classes['t'] == 'MetaList':
getDefined.value[environment] = []
for klass in classes['c']:
string = stringify(klass)
if re.match('^[a-zA-Z][\w.:-]*$', string):
getDefined.value[environment].append(string)
getDefined.value[environment] = set(getDefined.value[environment])
return getDefined.value
def create_figures(key, value, format, metadata):
"""Convert Images with attributes to Figures.
Images are [caption, (filename, title)].
Figures are [caption, (filename, title), attrs].
This isn't a supported pandoc type, we just use it internally.
"""
if isattrfigure(key, value):
image = value[0]
attr = PandocAttributes(pf.stringify(value[1:]), 'markdown')
caption, target = image['c']
return Figure(caption, target, attr.to_pandoc())
elif isdivfigure(key, value):
# use the first image inside
attr, blocks = value
images = [b['c'][0] for b in blocks if b['c'][0]['t'] == 'Image']
image = images[0]
caption, target = image['c']
return Figure(caption, target, attr)
else:
return None
This function can be passed directly to toJSONFilter
from pandocfilters.
"""
if key == 'BlockQuote':
blockquote = value
header = find_header(blockquote)
if not header:
return
else:
level, attr, inlines = header
id, classes, kvs = attr
ltitle = pf.stringify(inlines).lower()
if ltitle in SPECIAL_TITLES:
classes.append(SPECIAL_TITLES[ltitle])
return pf.Div(attr, blockquote)
elif len(classes) == 1 and classes[0] in SPECIAL_CLASSES:
remove_attributes(blockquote)
# a blockquote is just a list of blocks, so it can be
# passed directly to Div, which expects Div(attr, blocks)
return pf.Div(attr, blockquote)
def figure_replacement(self, key, value, format, metadata):
"""Replace figures with appropriate representation.
This works with Figure, which is our special type for images
with attributes. This allows us to set an id in the attributes.
The other way of doing it would be to pull out a '\label{(.*)}'
from the caption of an Image and use that to update the references.
"""
_caption, (filename, target), attrs = value
caption = pf.stringify(_caption)
attr = PandocAttributes(attrs)
if 'unnumbered' in attr.classes:
star = '*'
fcaption = caption
else:
self.fig_replacement_count += 1
if not attr.id:
attr.id = self.auto_fig_id(self.fig_replacement_count)
ref = self.references[attr.id]
star = ''
if caption:
fcaption = u'Figure {n}: {caption}'.format(n=ref['id'],
caption=caption)
def get_list_or_inline(metadata, field):
""" return content of MetaList or MetaInlines item coerced as list """
field_type = get_type(metadata, field)
if field_type == 'MetaInlines':
content_raw = get_content(metadata, field, 'MetaInlines')
content = [pandocfilters.stringify(content_raw)]
return content
elif field_type == 'MetaString':
content_raw = get_content(metadata, field, 'MetaString')
content = [content_raw]
return content
elif field_type == 'MetaList':
content = list()
for content_raw in get_content(metadata, field, 'MetaList'):
content.append(pandocfilters.stringify(content_raw))
return content
else:
raise error.WrongType('"%s" value must be of type "MetaInlines", '
'"MetaList", or "MetaString"' % field)