Skip to content

Commit

Permalink
Add tests; fix minor bugs with stop_if()
Browse files Browse the repository at this point in the history
Also use stop_if() to tweak stringify() of definition list
  • Loading branch information
sergiocorreia committed Jul 14, 2022
1 parent e68d2d0 commit c8b4365
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
9 changes: 5 additions & 4 deletions panflute/autofilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def get_filter_dirs(hardcoded=True):
res = []
# str below to convert from Path to str is necessary
# for test_panfl.test_get_filter_dirs
# and also for consistencies of the return types in the
# pandoc 2.12 dropped this historical convention
# and also for consistencies of the return types
# Pandoc 2.12 dropped this historical convention
if version[:2] < (2, 12):
res.append(str(base / '.pandoc' / 'filters'))
res.append(str(base / '.local' / 'share' / 'pandoc' / 'filters'))
Expand Down Expand Up @@ -89,7 +89,8 @@ def stdio(filters=None, search_dirs=None, data_dir=True, sys_path=True,

if verbose:
debug('panflute: data_dir={} sys_path={}'.format(data_dir, sys_path))
search_dirs = [p.normpath(p.expanduser(p.expandvars(dir_))) for dir_ in search_dirs]
search_dirs = [p.normpath(p.expanduser(p.expandvars(d)))
for d in search_dirs]

if not panfl_:
# default panflute behaviour:
Expand Down Expand Up @@ -165,7 +166,7 @@ def main():
@click.command(help=help_str)
@click.argument('filters', nargs=-1)
@click.option('-w', '-t', '--write', '--to', 'to', type=str, default=None,
help='Derivative of Pandoc writer option that Pandoc passes to filters.')
help='Output format passed to Pandoc (and filters).')
@click.option('--dir', '-d', 'search_dirs', multiple=True,
help="Search filters in provided directories: `-d dir1 -d dir2`.")
@click.option('--data-dir', is_flag=True, default=False,
Expand Down
12 changes: 11 additions & 1 deletion panflute/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,23 @@ def stringify(element, newlines=True):
:rtype: :class:`str`
"""

def stop_if(e):
return isinstance(e, DefinitionList)

def attach_str(e, doc, answer):
if hasattr(e, 'text'):
ans = e.text
elif isinstance(e, HorizontalSpaces):
ans = ' '
elif isinstance(e, VerticalSpaces) and newlines:
ans = '\n\n'
elif type(e) == DefinitionList:
ans = []
for item in e.content:
term = ''.join(stringify(part) for part in item.term)
definitions = '; '.join(stringify(defn) for defn in item.definitions)
ans.append(f'- {term}: {definitions}')
ans = '\n'.join(ans)
elif type(e) == Citation:
ans = ''
else:
Expand All @@ -270,7 +280,7 @@ def attach_str(e, doc, answer):

answer = []
f = partial(attach_str, answer=answer)
element.walk(f)
element.walk(f, stop_if=stop_if)
return ''.join(answer)


Expand Down
31 changes: 31 additions & 0 deletions tests/test_stringify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import panflute as pf


def validate(markdown_text, expected_text):
doc = pf.convert_text(markdown_text, input_format='markdown', output_format='panflute', standalone=True)
output_text = pf.stringify(doc)
assert expected_text == output_text


def test_simple():
markdown_text = '''Hello **world**! *How* are ~you~ doing?'''
expected_text = '''Hello world! How are you doing?\n\n'''
validate(markdown_text, expected_text)


def test_definition_list():
markdown_text = '''Term 1\n: Definition 1\n\nTerm 2 with *inline markup*\n\n: Definition 2'''
expected_text = '''- Term 1: Definition 1\n- Term 2 with inline markup: Definition 2\n\n'''
validate(markdown_text, expected_text)


def test_definition_list_complex():
markdown_text = '''Term 1\n~ Definition 1\n\nTerm 2\n~ Definition 2a\n~ Definition 2b'''
expected_text = '''- Term 1: Definition 1\n- Term 2: Definition 2a; Definition 2b'''
validate(markdown_text, expected_text)


if __name__ == "__main__":
test_simple()
test_definition_list()
test_definition_list_complex()

0 comments on commit c8b4365

Please sign in to comment.