From 943e3dc057eca53e68ddec51529bd6a1279ebd8e Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 29 Apr 2024 06:23:02 +0200 Subject: Adding upstream version 0.18.1. Signed-off-by: Daniel Baumann --- tests/test_html/html_ast.md | 124 +++++++++++++++++++++ tests/test_html/html_round_trip.md | 87 +++++++++++++++ tests/test_html/html_to_nodes.md | 199 ++++++++++++++++++++++++++++++++++ tests/test_html/test_html_to_nodes.py | 35 ++++++ tests/test_html/test_parse_html.py | 41 +++++++ 5 files changed, 486 insertions(+) create mode 100644 tests/test_html/html_ast.md create mode 100644 tests/test_html/html_round_trip.md create mode 100644 tests/test_html/html_to_nodes.md create mode 100644 tests/test_html/test_html_to_nodes.py create mode 100644 tests/test_html/test_parse_html.py (limited to 'tests/test_html') diff --git a/tests/test_html/html_ast.md b/tests/test_html/html_ast.md new file mode 100644 index 0000000..0abbdee --- /dev/null +++ b/tests/test_html/html_ast.md @@ -0,0 +1,124 @@ +tags +. + + +Title of the document + + +The content of the document...... + + +. +Root('') +Tag('html') +Data('\n') +Tag('head') +Data('\n') +Tag('title', {'class': 'a b', 'other': 'x'}) +Data('Title of the docu...') +Data('\n') +Data('\n') +Tag('body') +Data('\nThe content of t...') +Data('\n') +Data('\n') +. + +un-closed tags +. +
+
+. +Root('') +Tag('div', {'class': 'a'}) +Data('\n') +Tag('div', {'class': 'b'}) +Data('\n') +. + +xtag +. +Girl in a jacket +. +Root('') +XTag('img', {'src': 'img_girl.jpg', 'alt': 'Girl in a jacket', 'width': '500', 'height': '600'}) +Data('\n') +. + +data +. +a +. +Root('') +Data('a\n') +. + +declaration +. + +. +Root('') +Declaration('DOCTYPE html') +Data('\n') +. + +process information +. + +. +Root('') +Pi('xml-stylesheet ?') +Data('\n') +. + +entities +. +& + +{ +. +Root('') +Entity('amp') +Data('\n\n') +Char('123') +Data('\n') +. + +comments +. + +. +Root('') +Comment('This is a comment...') +Data('\n') +. + +admonition +. +
+
Tip
+parameter allows to get a deterministic results even if we +use some random process (i.e. data shuffling). +
+. +Root('') +Tag('div', {'class': 'admonition tip alert alert-warning'}) +Data('\n') +Tag('div', {'class': 'admonition-title', 'style': 'font-weight: bold;'}) +Data('Tip') +Data('\nparameter allows...') +Data('\n') +. + +image +. +fishy +fishy +. +Root('') +VoidTag('img', {'src': 'img/fun-fish.png', 'alt': 'fishy', 'class': 'bg-primary mb-1', 'width': '200px'}) +Data('\n') +VoidTag('img', {'src': 'img/fun-fish.png', 'alt': 'fishy', 'class': 'bg-primary mb-1', 'width': '300px'}) +Data('\n') +. diff --git a/tests/test_html/html_round_trip.md b/tests/test_html/html_round_trip.md new file mode 100644 index 0000000..c5ceb5c --- /dev/null +++ b/tests/test_html/html_round_trip.md @@ -0,0 +1,87 @@ +tags +. + + +Title of the document + + +The content of the document...... + + +. + + +Title of the document + + +The content of the document...... + + +. + +un-closed tags +. +
+
+. +
+
+
+. + +xtag +. + Girl in a jacket +. + Girl in a jacket +. + +data +. +a +. +a +. + +declaration +. + +. + +. + +process information +. + +. + +. + +entities +. +& + +{ +. +& + +{ +. + +comments +. + +. + +. + +image +. +fishy +fishy +. +fishy +fishy +. diff --git a/tests/test_html/html_to_nodes.md b/tests/test_html/html_to_nodes.md new file mode 100644 index 0000000..e118eb9 --- /dev/null +++ b/tests/test_html/html_to_nodes.md @@ -0,0 +1,199 @@ +empty +. + +. + + +. + +text +. +abc +. + + + abc +. + +normal HTML +. +
+. + + +
+. + +image no src +. + +. + + + + error +. + +image +. + +. + + +. + +image unknown attribute +. + +. + + +. + +image known attributes +. + +. + + + :align: left + :class: a b + :height: 200px + :name: b +. + +multiple images +. + + +. + + + +. + +admonition no close +. +
+. + + + :class: admonition +. + +admonition +. +
+
+. + + + :class: admonition +. + +admonition attributes +. +
+
+. + + + :class: admonition tip + :name: aname +. + +admonition div-title +. +
+
*Hallo*
+. + + + :class: admonition tip +. + +admonition p-title +. +
+

*Hallo*

+. + + + :class: admonition tip +. + +admonition title+content +. +
+
*Hallo*
+content +
+. + + + :class: admonition + + content +. + +admonition multiple +. +
+
first
+content 1 +
+
+
second
+content 2 +
+. + + + :class: admonition + + content 1 + + :class: admonition + + content 2 +. + +admonition with paragraphs +. +
+

paragraph 1

+

paragraph 2

+
+. + + + :class: admonition + + paragraph 1 + + paragraph 2 +. + +nested +. +
+

Some **content**

+
+
A *title*
+

Paragraph 1

+

Paragraph 2

+
+
+. + + + :class: admonition + + Some **content** + +
+
A *title*
+

Paragraph 1

+

Paragraph 2

+
+. diff --git a/tests/test_html/test_html_to_nodes.py b/tests/test_html/test_html_to_nodes.py new file mode 100644 index 0000000..207a627 --- /dev/null +++ b/tests/test_html/test_html_to_nodes.py @@ -0,0 +1,35 @@ +from pathlib import Path +from unittest.mock import Mock + +import pytest +from docutils import nodes + +from myst_parser.config.main import MdParserConfig +from myst_parser.mdit_to_docutils.html_to_nodes import html_to_nodes + +FIXTURE_PATH = Path(__file__).parent + + +@pytest.fixture() +def mock_renderer(): + def _run_directive(name: str, first_line: str, content: str, position: int): + node = nodes.Element(name=name, first=first_line, position=position) + node += nodes.Text(content) + return [node] + + return Mock( + md_config=MdParserConfig(enable_extensions=["html_image", "html_admonition"]), + document={"source": "source"}, + reporter=Mock( + warning=Mock(return_value=nodes.system_message("warning")), + error=Mock(return_value=nodes.system_message("error")), + ), + run_directive=_run_directive, + ) + + +@pytest.mark.param_file(FIXTURE_PATH / "html_to_nodes.md") +def test_html_to_nodes(file_params, mock_renderer): + output = nodes.container() + output += html_to_nodes(file_params.content, line_number=0, renderer=mock_renderer) + file_params.assert_expected(output.pformat(), rstrip=True) diff --git a/tests/test_html/test_parse_html.py b/tests/test_html/test_parse_html.py new file mode 100644 index 0000000..3b4cdc1 --- /dev/null +++ b/tests/test_html/test_parse_html.py @@ -0,0 +1,41 @@ +from pathlib import Path + +import pytest + +from myst_parser.parsers.parse_html import tokenize_html + +FIXTURE_PATH = Path(__file__).parent + + +@pytest.mark.param_file(FIXTURE_PATH / "html_ast.md") +def test_html_ast(file_params): + tokens = "\n".join( + repr(t) for t in tokenize_html(file_params.content).walk(include_self=True) + ) + file_params.assert_expected(tokens, rstrip=True) + + +@pytest.mark.param_file(FIXTURE_PATH / "html_round_trip.md") +def test_html_round_trip(file_params): + ast = tokenize_html(file_params.content) + file_params.assert_expected(str(ast), rstrip=True) + + +def test_render_overrides(): + text = "
" + ast = tokenize_html(text) + + def _render_abc(element, *args, **kwargs): + return "hallo" + + output = ast.render(tag_overrides={"abc": _render_abc}) + assert output == "
hallo
" + + +def test_ast_find(): + text = ( + '
z
' + ) + ast = tokenize_html(text) + found = list(ast.find("div", classes=["a"])) + assert [e.attrs.classes for e in found] == [["a"], ["a", "b"]] -- cgit v1.2.3