Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_code(self):
doc = Doc(
Para(
Code("", classes=["tip", "listing"]),
Code("", classes=["tip"]),
Code("", classes=["warning"]),
Code(
"",
attributes={
"latex-tip-icon": "warning",
"latex-tip-position": "right",
"latex-tip-size": 24,
},
),
),
metadata=self.metadata(),
format="latex",
api_version=(1, 17, 2),
)
def test_all():
x=pf.Para(pf.Str("a"))
y=pf.Para(pf.Str("b"))
c1=pf.TableCell(x)
c2=pf.TableCell(y)
row=pf.TableRow(c1,c2)
t1 = pf.Table(row)
t2 = pf.Table(row, header=row)
print(t1.header)
print(t2.header)
with io.StringIO() as f:
pf.dump(pf.Doc(t1), f)
print(f.getvalue())
with io.StringIO() as f:
pf.dump(pf.Doc(t2), f)
pf.dump(doc, f)
c_md_dump = f.getvalue()
with io.StringIO() as f:
doc = pf.Doc(*b_md)
pf.dump(doc, f)
b_md_dump = f.getvalue()
assert c_md_dump == b_md_dump
# ----------------------
print()
tex = r'Some $x^y$ or $x_n = \sqrt{a + b}$ \textit{a}'
c_tex = pf.convert_text(tex)
b_tex = [pf.Para(pf.Str("Some"), pf.Space,
pf.Math("x^y", format='InlineMath'), pf.Space,
pf.Str("or"), pf.Space,
pf.Math(r"x_n = \sqrt{a + b}", format='InlineMath'),
pf.Space, pf.RawInline(r"\textit{a}", format='tex'))]
print("Benchmark TEX:")
print(b_tex)
print("Converted TEX:")
print(c_tex)
assert repr(c_tex) == repr(b_tex)
with io.StringIO() as f:
doc = pf.Doc(*c_tex)
pf.dump(doc, f)
c_tex_dump = f.getvalue()
def table_to_csv(elem, doc):
"""convert Table element and to csv table in code-block with class "table" in panflute AST"""
if isinstance(elem, panflute.Table):
# get options as a dictionary
options = {}
# options: caption: panflute ast to markdown
if elem.caption:
options['caption'] = ast_to_markdown(panflute.Para(*elem.caption))
# options: alignment
parsed_alignment = [ALIGN_TO_LETTER[i] for i in elem.alignment]
options['alignment'] = "".join(parsed_alignment)
# options: width
options['width'] = elem.width
# options: table-width from width
options['table-width'] = sum(options['width'])
# options: header: False if empty header row, else True
options['header'] = bool(panflute.stringify(elem.header)) if elem.header else False
# options: markdown
options['markdown'] = True
# option in YAML
yaml_metadata = yaml.safe_dump(options)
# table in panflute AST
def tobullet(item):
ans = [Para(Strong(*item.term))]
for definition in item.definitions:
for block in definition.content:
ans.append(block)
return ListItem(*ans)
def tikz(elem, doc):
if type(elem) == RawBlock and elem.format == "latex":
code = elem.text
if re.match("\\\\begin{tikzpicture}", code):
outfile = imagedir + '/' + sha1(code)
filetype = {'html': 'png', 'latex': 'pdf'}.get(doc.format, 'png')
src = outfile + '.' + filetype
if not os.path.isfile(src):
try:
os.mkdir(imagedir)
sys.stderr.write('Created directory ' + imagedir + '\n')
except OSError:
pass
tikz2image(code, filetype, outfile)
sys.stderr.write('Created image ' + src + '\n')
return Para(Image(url=src))
def action(elem, doc):
global entryEnter
global options
if isinstance(elem, pf.Para):
includeType = is_include_line(elem)
if includeType == 0:
return
# Try to read inherited options from temp file
if options is None:
try:
with open(temp_filename, 'r') as f:
options = json.load(f)
except:
options = {}
pass
# pandoc options
pandoc_options = doc.get_metadata('pandoc-options')
if not pandoc_options:
latexsnippet('\\gregorioscore', elem.attributes)
))
elif type(elem) == CodeBlock and "gabc" in elem.classes:
if doc.format == "latex":
if elem.identifier == "":
label = ""
else:
label = '\\label{' + elem.identifier + '}'
return latexblock(
"\n\\smallskip\n{%\n" +
latexsnippet('\\gabcsnippet{' + elem.text + '}', elem.attributes) +
"%\n}" +
label
)
else:
return Para(Image(url=png(elem.text, latexsnippet('\\gabcsnippet', elem.attributes))))
def action(elem, doc):
if isinstance(elem, pf.Doc):
version = pkg_resources.get_distribution("panflute").version
json_serializer = lambda elem: elem.to_json()
raw = json.dumps(elem, default=json_serializer)
raw = json.loads(raw)
raw = json.dumps(raw, check_circular=False,
indent=4, separators=(',', ': '))
disclaimer = pf.Para(pf.Emph(pf.Str('Note: sort order not preserved')))
elem.content = [
pf.Header(pf.Str('Python version:'), level=2),
pf.Para(pf.Str(sys.version)),
pf.Header(pf.Str('Panflute version:'), level=2),
pf.Para(pf.Str(version)),
pf.Header(pf.Str('sys.argv:'), level=2),
pf.Plain(pf.Str(str(sys.argv))),
pf.Header(pf.Str('JSON Input:'), level=2),
disclaimer,
pf.CodeBlock(raw)
]
if not isinstance(element, pf.Div):
return None
if not "divtable" in element.classes:
return None
rows = []
headers = []
header_found = False
widths = []
aligns = []
caption = None
for tbl_el in element.content:
if isinstance(tbl_el, pf.Div) and "thead" in tbl_el.classes:
headers = []
assert isinstance(tbl_el.content[0], pf.Para), "the table header div must contain a paragraph"
for head_el in tbl_el.content[0].content:
if isinstance(head_el, pf.Span):
if list(head_el.content):
header_found = True
headers.append(pf.TableCell(pf.Plain(*head_el.content)))
if "width" in head_el.attributes:
widths.append(float(head_el.attributes["width"]))
else:
widths.append(0)
if "align" in head_el.attributes:
align = str(head_el.attributes["align"]).capitalize()
assert align in ["Left", "Right", "Center", "Default"], "table alignment must be one of left, right, center or default"
aligns.append("Align"+align)
else:
aligns.append("AlignDefault")