Skip to content

Instantly share code, notes, and snippets.

@bryanchow
Last active July 16, 2017 23:35
Show Gist options
  • Select an option

  • Save bryanchow/2b0fa0cc99c2582e5bb65b9f1088473f to your computer and use it in GitHub Desktop.

Select an option

Save bryanchow/2b0fa0cc99c2582e5bb65b9f1088473f to your computer and use it in GitHub Desktop.

Revisions

  1. bryanchow revised this gist Jul 16, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion ulize.py
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,8 @@ def ulize(text):
    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"
    @@ -31,4 +33,4 @@ def ulize(text):

    if __name__ == "__main__":
    import doctest
    doctest.testmod()
    doctest.testmod()
  2. bryanchow revised this gist Jul 16, 2017. No changes.
  3. bryanchow created this gist Jul 16, 2017.
    34 changes: 34 additions & 0 deletions ulize.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    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.
    >>> 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()