Last active
July 16, 2017 23:35
-
-
Save bryanchow/2b0fa0cc99c2582e5bb65b9f1088473f to your computer and use it in GitHub Desktop.
ulize.py: Convert plaintext lists to HTML unordered lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def ulize(text): | |
r""" | |
Convert lists embedded in plain text to HTML unordered lists. Yes, kinda | |
like Markdown or Textile but without the other junk. Expects each list | |
item to be prefixed by zero or more spaces, a dash, and a single space. | |
https://gist.github.com/bryanchow/2b0fa0cc99c2582e5bb65b9f1088473f | |
>>> ulize( | |
... "Here is a list of items:\n" | |
... "- Item one\n" | |
... "- Item two\n" | |
... ) | |
'Here is a list of items:<ul><li>Item one</li><li>Item two</li></ul>' | |
>>> ulize( | |
... "Another\n" | |
... "list:\n" | |
... " - One\n" | |
... " - Two\n" | |
... " Not-an-item\n" | |
... ) | |
'Another\nlist:<ul><li>One</li><li>Two</li></ul> Not-an-item\n' | |
""" | |
return re.sub( | |
r"\s*- (.*)\n*", | |
r"<li>\1</li>", | |
re.sub(r"((\s*- [^\n]*(\n|$))+)", r"<ul>\n\1\n</ul>", text) | |
) | |
if __name__ == "__main__": | |
import doctest | |
doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment