import html.parser
import os
import sys
import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from olt import md, records # noqa: E402
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
DOCS = ["README.md", "SOURCES.md", "NOTICE.md",
"CONTRIBUTING.md", "CHANGELOG.md", "docs/schema.md"]
def read_doc(name):
with open(os.path.join(ROOT, name), encoding="utf-8") as f:
return f.read()
class TagBalance(html.parser.HTMLParser):
VOID = {"hr", "br", "img", "meta", "link", "input", "col"}
def __init__(self):
super().__init__(convert_charrefs=True)
self.stack = []
self.errors = []
self.hrefs = []
def handle_starttag(self, tag, attrs):
if tag in self.VOID:
return
self.stack.append(tag)
if tag == "a":
self.hrefs.append(dict(attrs).get("href", ""))
def handle_startendtag(self, tag, attrs):
pass
def handle_endtag(self, tag):
if tag in self.VOID:
return
if self.stack and self.stack[-1] == tag:
self.stack.pop()
else:
self.errors.append("unmatched %s>, open=%r" % (tag, self.stack))
def check_well_formed(html):
checker = TagBalance()
checker.feed(html)
checker.close()
return checker
class TableCounter(html.parser.HTMLParser):
def __init__(self):
super().__init__(convert_charrefs=True)
self.counts = []
self.in_tbody = False
def handle_starttag(self, tag, attrs):
if tag == "tbody":
self.in_tbody = True
self.counts.append(0)
elif tag == "tr" and self.in_tbody:
self.counts[-1] += 1
def handle_endtag(self, tag):
if tag == "tbody":
self.in_tbody = False
class HeadingsTest(unittest.TestCase):
def test_levels(self):
self.assertEqual(md.render("# One"), '
One
')
self.assertEqual(md.render("### Three"), 'Three
')
self.assertEqual(md.render("###### Six"), 'Six
')
def test_offset(self):
self.assertEqual(md.render("# One", heading_offset=1),
'One
')
self.assertEqual(md.render("### Three", heading_offset=2),
'Three
')
def test_offset_capped_at_h6(self):
self.assertTrue(md.render("### Three", heading_offset=9)
.startswith("#nospace")
def test_headings_values(self):
self.assertEqual(md.headings("# Hello, World!\n\n## B *c*"),
[(1, "Hello, World!", "hello-world"),
(2, "B c", "b-c")])
def test_duplicate_slugs_numbered(self):
self.assertEqual(
[slug for _, _, slug in md.headings("# A\n# A\n# A")],
["a", "a-2", "a-3"])
def test_render_uses_same_slugs_as_headings(self):
text = "# Repeat\n\n# Repeat\n"
html = md.render(text)
for _, _, slug in md.headings(text):
self.assertIn('id="%s"' % slug, html)
def test_heading_inline_markup_excluded_from_slug(self):
self.assertEqual(md.headings("## Use `code` here"),
[(2, "Use code here", "use-code-here")])
class SectionsTest(unittest.TestCase):
def test_basic_split(self):
self.assertEqual(
md.split_sections("# Title\n\nintro\n\n## One\nbody one\n## Two\nbody two"),
[("One", "body one"), ("Two", "body two")])
def test_preamble_excluded(self):
sections = md.split_sections("# Title\n\nintro\n\n## One\nbody\n")
self.assertEqual(sections, [("One", "body")])
def test_subheadings_stay_in_body(self):
sections = md.split_sections("## One\ntext\n### Sub\nmore\n## Two\nend\n")
self.assertEqual(sections, [("One", "text\n### Sub\nmore"),
("Two", "end")])
def test_default_level_is_two(self):
self.assertEqual(len(md.split_sections("# T\n\n# U")), 0)
self.assertEqual(len(md.split_sections("# T\n\n## U")), 1)
def test_bad_level_rejected(self):
with self.assertRaises(ValueError):
md.split_sections("text", level=0)
with self.assertRaises(ValueError):
md.split_sections("text", level=7)
def test_readme_inclusion_rule(self):
sections = dict(md.split_sections(read_doc("README.md")))
self.assertIn("Inclusion rule", sections)
self.assertIn("A long trail here is a named, continuous walking route",
sections["Inclusion rule"])
self.assertEqual(len(sections), 11)
class ParagraphsTest(unittest.TestCase):
def test_simple_paragraph(self):
self.assertEqual(md.render("Hello world."), "
Hello world.
")
def test_blocks_separated(self):
self.assertEqual(md.render("One.\n\nTwo."),
"One.
\nTwo.
")
def test_raw_html_is_escaped(self):
html = md.render("Use /trail.toml and trails.")
self.assertIn("<id>/trail.toml", html)
self.assertIn("<n>", html)
self.assertNotIn("", html)
def test_ampersand_escaped(self):
self.assertIn("a & b", md.render("a & b"))
class ListsTest(unittest.TestCase):
def test_unordered(self):
self.assertEqual(md.render("- a\n- b"),
"")
def test_ordered(self):
self.assertEqual(md.render("1. one\n2. two"),
"\n- one
\n- two
\n
")
def test_ordered_paren_marker(self):
self.assertIn("", md.render("1) one\n2) two"))
def test_nested(self):
html = md.render("- a\n - b\n- c")
self.assertIn("- a\n
", html)
def test_multiline_item_joined(self):
html = md.render("1. first line\n second line\n2. next")
self.assertIn("- first line\nsecond line
", html)
def test_loose_list_keeps_paragraphs(self):
html = md.render("- a\n\n- b")
self.assertIn("a
", html)
self.assertIn("b
", html)
def test_markup_inside_items(self):
html = md.render("- `code` and **bold**")
self.assertIn("code and bold ",
html)
class TablesTest(unittest.TestCase):
def test_header_and_body(self):
html = md.render("| a | b |\n| --- | --- |\n| 1 | 2 |")
self.assertIn("", html)
self.assertIn("", html)
self.assertIn('a | ', html)
self.assertIn("1 | ", html)
def test_right_align_gets_num_class(self):
html = md.render("| a | b |\n| --- | ---: |\n| 1 | 2 |")
self.assertIn('b | ', html)
self.assertIn('2 | ', html)
self.assertIn('a | ', html)
def test_no_inline_styles(self):
html = md.render("| a |\n| ---: |\n| 1 |")
self.assertNotIn("style=", html)
def test_needs_alignment_row(self):
self.assertNotIn("