Ejemplo n.º 1
0
class TestRenderer(TestCase):
    def setUp(self):
        self.renderer = HTMLRenderer()
        self.renderer.render_inner = mock.Mock(return_value='inner')
        self.renderer.__enter__()
        self.addCleanup(self.renderer.__exit__, None, None, None)

    def _test_token(self, token_path, output, children=True, **kwargs):
        def mock_render(token_path):
            def inner(token):
                _, token_name = token_path.split('.')
                return self.renderer.render_map[token_name](token)

            return inner

        self.renderer.render = mock_render(token_path)

        path = 'mistletoe.{}'.format(token_path)
        MockToken = mock.Mock(path, name=token_path)
        if children:
            MockToken.return_value = mock.Mock(children=mock.MagicMock(),
                                               **kwargs)
        else:
            MockToken.return_value = mock.Mock(**kwargs)

        self.assertEqual(self.renderer.render(MockToken()), output)
Ejemplo n.º 2
0
class TestRenderer(TestCase):
    def setUp(self):
        self.renderer = HTMLRenderer()
        self.renderer.render_inner = mock.Mock(return_value='inner')
        self.renderer.__enter__()
        self.addCleanup(self.renderer.__exit__, None, None, None)

    def _test_token(self, token_name, output, children=True, **kwargs):
        render_func = self.renderer.render_map[token_name]
        children = mock.MagicMock() if children else None
        mock_token = mock.Mock(children=children, **kwargs)
        self.assertEqual(render_func(mock_token), output)
Ejemplo n.º 3
0
class TestHTMLToken(TestCase):
    def setUp(self):
        self.renderer = HTMLRenderer()
        self.renderer.__enter__()
        self.addCleanup(self.renderer.__exit__, None, None, None)

    def _test_html_token(self, token, token_cls, content):
        self.assertIsInstance(token, token_cls)
        self.assertEqual(token.content, content)

    @mock.patch('mistletoe.span_token.RawText')
    def test_span(self, MockRawText):
        raw = 'some <span>more</span> text'
        tokens = tokenize_inner(raw)
        next(tokens)
        MockRawText.assert_called_with('some ')
        content = '<span>more</span>'
        self._test_html_token(next(tokens), html_token.HTMLSpan, content)
        next(tokens)
        MockRawText.assert_called_with(' text')

    def test_block(self):
        lines = ['<p>a paragraph\n', 'within an html block\n', '</p>\n']
        token = next(tokenize(lines))
        content = '<p>a paragraph\nwithin an html block\n</p>\n'
        self._test_html_token(token, html_token.HTMLBlock, content)

    def test_span_attrs(self):
        raw = '<span class="foo">more</span>'
        token = next(tokenize_inner(raw))
        content = '<span class="foo">more</span>'
        self._test_html_token(token, html_token.HTMLSpan, content)

    def test_block_attrs(self):
        lines = [
            '<p class="bar">a paragraph\n', 'within an html block\n', '</p>\n'
        ]
        token = next(tokenize(lines))
        content = '<p class="bar">a paragraph\nwithin an html block\n</p>\n'
        self._test_html_token(token, html_token.HTMLBlock, content)

    def test_empty_span(self):
        raw = '<span></span>'
        token = next(tokenize_inner(raw))
        content = '<span></span>'
        self._test_html_token(token, html_token.HTMLSpan, content)

    def test_self_closing_span(self):
        raw = '<span />'
        token = next(tokenize_inner(raw))
        content = '<span />'
        self._test_html_token(token, html_token.HTMLSpan, content)
Ejemplo n.º 4
0
class TestHTMLRendererFootnotes(TestCase):
    def setUp(self):
        self.renderer = HTMLRenderer()
        self.renderer.__enter__()
        self.addCleanup(self.renderer.__exit__, None, None, None)

    def test_footnote_image(self):
        from mistletoe import Document
        token = Document(['![alt] [foo]\n', '\n', '[foo]: bar "title"\n'])
        output = '<p><img src="bar" title="title" alt="alt">\n</p>\n'
        self.assertEqual(self.renderer.render(token), output)

    def test_footnote_link(self):
        from mistletoe import Document
        token = Document(['[name] [foo]\n', '\n', '[foo]: target\n'])
        output = '<p><a href="target">name</a>\n</p>\n'
        self.assertEqual(self.renderer.render(token), output)
Ejemplo n.º 5
0
def test_commonmark(entry):
    test_case = entry["markdown"].splitlines(keepends=True)
    with HTMLRenderer() as renderer:
        output = renderer.render(Document(test_case))
    assert entry["html"] == output
Ejemplo n.º 6
0
 def setUp(self):
     self.renderer = HTMLRenderer()
     self.renderer.render_inner = mock.Mock(return_value='inner')
     self.renderer.__enter__()
     self.addCleanup(self.renderer.__exit__, None, None, None)
Ejemplo n.º 7
0
 def setUp(self):
     self.renderer = HTMLRenderer()
     self.renderer.__enter__()
     self.addCleanup(self.renderer.__exit__, None, None, None)
Ejemplo n.º 8
0
from datetime import datetime
from pathlib import Path
from typing import Iterator, NamedTuple, Optional

from ..common import get_logger, Extraction, Url, PathIsh, Res, Visit, echain, Loc

import mistletoe  # type: ignore
from mistletoe import Document  # type: ignore
from mistletoe.span_token import AutoLink, Link  # type: ignore
import mistletoe.block_token as BT  # type: ignore
from mistletoe.html_renderer import HTMLRenderer  # type: ignore

renderer = HTMLRenderer()

block_tokens = tuple(getattr(BT, name) for name in BT.__all__)


class Parsed(NamedTuple):
    url: Url
    context: Optional[str]


Result = Res[Parsed]

# the f**k...
#
# from mistletoe import Document
# d = Document('''
# # heading
# ## sub
# ## sub2
Ejemplo n.º 9
0
class TestHTMLToken(TestCase):
    def setUp(self):
        self.renderer = HTMLRenderer()
        self.renderer.__enter__()
        self.addCleanup(self.renderer.__exit__, None, None, None)

    def _test_html_token(self, token, token_cls, content):
        self.assertIsInstance(token, token_cls)
        self.assertEqual(token.content, content)

    def test_span(self):
        raw = 'some <span>more</span> text'
        tokens = tokenize_inner(raw)
        next(tokens)
        content = '<span>more</span>'
        self._test_html_token(next(tokens), html_token.HTMLSpan, content)
        next(tokens)

    def test_block(self):
        lines = ['<p>a paragraph\n', 'within an html block\n', '</p>\n']
        token = next(tokenize(lines))
        content = '<p>a paragraph\nwithin an html block\n</p>\n'
        self._test_html_token(token, html_token.HTMLBlock, content)

    def test_span_attrs(self):
        raw = '<span class="foo">more</span>'
        token = next(tokenize_inner(raw))
        content = '<span class="foo">more</span>'
        self._test_html_token(token, html_token.HTMLSpan, content)

    def test_block_attrs(self):
        lines = [
            '<p class="bar">a paragraph\n', 'within an html block\n', '</p>\n'
        ]
        token = next(tokenize(lines))
        content = '<p class="bar">a paragraph\nwithin an html block\n</p>\n'
        self._test_html_token(token, html_token.HTMLBlock, content)

    def test_comment(self):
        from mistletoe.block_token import Heading
        lines = ['<!-- hello -->\n', '\n', '# heading 1\n']
        token1, token2 = tokenize(lines)
        content = '<!-- hello -->\n'
        self._test_html_token(token1, html_token.HTMLBlock, content)
        self.assertIsInstance(token2, Heading)

    def test_empty_span(self):
        raw = '<span></span>'
        token = next(tokenize_inner(raw))
        content = '<span></span>'
        self._test_html_token(token, html_token.HTMLSpan, content)

    def test_self_closing_span(self):
        raw = '<span />'
        token = next(tokenize_inner(raw))
        content = '<span />'
        self._test_html_token(token, html_token.HTMLSpan, content)

    def test_autolink(self):
        from mistletoe.span_token import AutoLink
        self.assertIsInstance(next(tokenize_inner('<autolink>')), AutoLink)