Better half-markdown (#202)

Better half-markdown.

This fixes some issues with the current semi-markdown-parser to make life easier until a full markdown parser is implemented.

Changes:
- A * that would normally start italics but isn't matched by a closing *, is now left alone.
- A ` that would normally start a code block but isn't matched by a closing `, is now left alone.
- Backslashes should work as expected.
- Support for **bold** and the alternative _italic_ style.
This commit is contained in:
Tom Smeding 2020-05-31 14:09:29 +02:00 committed by GitHub
parent 0ce5b65835
commit 170c5811a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 132 additions and 79 deletions

View file

@ -5,7 +5,7 @@ from __future__ import unicode_literals
import webcolors
from collections import OrderedDict
from hypothesis import given
from hypothesis.strategies import sampled_from, text
from hypothesis.strategies import sampled_from, text, characters
from matrix.colors import (G, Formatted, FormattedString,
color_html_to_weechat, color_weechat_to_html)
@ -58,15 +58,16 @@ def test_normalize_spaces_in_inline_code():
assert formatted.to_weechat() == valid_result
# FIXME: this case doesn't and can't work yet (until a proper Markdown parser
# is integrated)
# @given(text().map(lambda s: '*' + s)
# def test_unpaired_prefix_asterisk_without_space_is_literal(text):
# """An unpaired asterisk at the beginning of the line, without a space
# after it, is considered literal.
# """
# formatted = Formatted.from_input_line(text)
# assert text == formatted.to_weechat()
@given(
text(alphabet=characters(min_codepoint=32,
blacklist_characters="*_"))
.map(lambda s: '*' + s))
def test_unpaired_prefix_asterisk_without_space_is_literal(text):
"""An unpaired asterisk at the beginning of the line, without a space
after it, is considered literal.
"""
formatted = Formatted.from_input_line(text)
assert text.strip() == formatted.to_weechat()
def test_input_line_color():
@ -79,7 +80,7 @@ def test_input_line_bold():
assert "\x1b[01mHello\x1b[021m" == formatted.to_weechat()
assert "<strong>Hello</strong>" == formatted.to_html()
def test_input_line_bold():
def test_input_line_underline():
formatted = Formatted.from_input_line("\x1FHello")
assert "\x1b[04mHello\x1b[024m" == formatted.to_weechat()
assert "<u>Hello</u>" == formatted.to_html()
@ -89,6 +90,25 @@ def test_input_line_markdown_emph():
assert "\x1b[03mHello\x1b[023m" == formatted.to_weechat()
assert "<em>Hello</em>" == formatted.to_html()
def test_input_line_markdown_bold():
formatted = Formatted.from_input_line("**Hello**")
assert "\x1b[01mHello\x1b[021m" == formatted.to_weechat()
assert "<strong>Hello</strong>" == formatted.to_html()
def test_input_line_markdown_various():
inp = "**bold* bold *bital etc* bold **bold** * *italic*"
formatted = Formatted.from_input_line(inp)
assert "<strong>bold* bold </strong>" \
"<em><strong>bital etc</strong></em><strong> bold **bold</strong>" \
" * <em>italic</em>" \
== formatted.to_html()
def test_input_line_markdown_various2():
inp = "norm** `code **code *code` norm `norm"
formatted = Formatted.from_input_line(inp)
assert "norm** <code>code **code *code</code> norm `norm" \
== formatted.to_html()
def test_conversion():
formatted = Formatted.from_input_line("*Hello*")
formatted2 = Formatted.from_html(formatted.to_html())