diff options
author | Jon Dufresne <jon.dufresne@gmail.com> | 2019-10-31 21:10:28 -0700 |
---|---|---|
committer | Paul McGuire <ptmcg@users.noreply.github.com> | 2019-10-31 23:10:28 -0500 |
commit | 53d1b4a6f48a53c4c4ec4ac7031362b691c0366d (patch) | |
tree | 088ad3cf3561b78a00af4fb2fd474f4a2b8ca70c /examples/nested_markup.py | |
parent | 41752aa52cc97c710474bb2972cceab057b52ad4 (diff) | |
download | pyparsing-git-53d1b4a6f48a53c4c4ec4ac7031362b691c0366d.tar.gz |
Blacken the project (#141)
Diffstat (limited to 'examples/nested_markup.py')
-rw-r--r-- | examples/nested_markup.py | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/examples/nested_markup.py b/examples/nested_markup.py index 6d83636..3e3aafa 100644 --- a/examples/nested_markup.py +++ b/examples/nested_markup.py @@ -12,27 +12,34 @@ wiki_markup = pp.Forward() # a method that will construct and return a parse action that will # do the proper wrapping in opening and closing HTML, and recursively call # wiki_markup.transformString on the markup body text -def convert_markup_to_html(opening,closing): +def convert_markup_to_html(opening, closing): def conversionParseAction(s, l, t): return opening + wiki_markup.transformString(t[1][1:-1]) + closing + return conversionParseAction + # use a nestedExpr with originalTextFor to parse nested braces, but return the # parsed text as a single string containing the outermost nested braces instead # of a nested list of parsed tokens -markup_body = pp.originalTextFor(pp.nestedExpr('{', '}')) -italicized = ('ital' + markup_body).setParseAction(convert_markup_to_html("<I>", "</I>")) -bolded = ('bold' + markup_body).setParseAction(convert_markup_to_html("<B>", "</B>")) +markup_body = pp.originalTextFor(pp.nestedExpr("{", "}")) +italicized = ("ital" + markup_body).setParseAction( + convert_markup_to_html("<I>", "</I>") +) +bolded = ("bold" + markup_body).setParseAction(convert_markup_to_html("<B>", "</B>")) # another markup and parse action to parse links - again using transform string # to recursively parse any markup in the link text def convert_link_to_html(s, l, t): link_text, url = t._skipped - t['link_text'] = wiki_markup.transformString(link_text) - t['url'] = url + t["link_text"] = wiki_markup.transformString(link_text) + t["url"] = url return '<A href="{url}">{link_text}</A>'.format_map(t) -urlRef = (pp.Keyword('link') + '{' + ... + '->' + ... + '}').setParseAction(convert_link_to_html) + +urlRef = (pp.Keyword("link") + "{" + ... + "->" + ... + "}").setParseAction( + convert_link_to_html +) # now inject all the markup bits as possible markup expressions wiki_markup <<= urlRef | italicized | bolded |