1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
# This file is part of python-markups test suite
# License: 3-clause BSD, see LICENSE file
# Copyright: (C) Dmitry Shachnev, 2012-2025
import unittest
from markups import ReStructuredTextMarkup
basic_text = """\
Hello, world!
=============
Some subtitle
~~~~~~~~~~~~~
This is an example **reStructuredText** document.
"""
basic_html = """\
<main id="hello-world">
<h1 class="title" data-posmap="2">Hello, world!</h1>
<p class="subtitle" id="some-subtitle">Some subtitle</p>
<p data-posmap="7">This is an example <strong>reStructuredText</strong> document.</p>
</main>
"""
toc_backrefs_source = """\
.. contents::
:backlinks: entry
Section title
-------------
"""
@unittest.skipUnless(ReStructuredTextMarkup.available(), "Docutils not available")
class ReStructuredTextTest(unittest.TestCase):
def test_basic(self) -> None:
import docutils
markup = ReStructuredTextMarkup()
converted = markup.convert(basic_text)
text = converted.get_document_body()
title = converted.get_document_title()
stylesheet = converted.get_stylesheet()
text_expected = basic_html
if docutils.__version_info__[:2] < (0, 17):
# docutils uses <main> tag since revision 8474
text_expected = text_expected.replace("<main", '<div class="document"')
text_expected = text_expected.replace("</main>", "</div>")
title_expected = "Hello, world!"
self.assertEqual(text_expected, text)
self.assertEqual(title_expected, title)
self.assertIn(".code", stylesheet)
def test_mathjax_loading(self) -> None:
markup = ReStructuredTextMarkup()
self.assertEqual("", markup.convert("Hello, world!").get_javascript())
js = markup.convert("Hello, :math:`2+2`!").get_javascript()
self.assertIn("<script", js)
body = markup.convert("Hello, :math:`2+2`!").get_document_body()
self.assertIn('<span class="math">', body)
self.assertIn(r"\(2+2\)</span>", body)
def test_errors(self) -> None:
markup = ReStructuredTextMarkup(
"/dev/null",
settings_overrides={"warning_stream": False},
)
body = markup.convert("`").get_document_body() # unclosed role
self.assertIn('<p class="system-message-title">System Message: WARNING/2', body)
self.assertIn("/dev/null", body)
def test_errors_overridden(self) -> None:
markup = ReStructuredTextMarkup(
"/dev/null",
settings_overrides={"report_level": 4},
)
body = markup.convert("`").get_document_body() # unclosed role
self.assertNotIn("System Message", body)
def test_errors_severe(self) -> None:
markup = ReStructuredTextMarkup(settings_overrides={"warning_stream": False})
text = "***************\nfaulty headline"
# The following line should not raise SystemMessage exception
body = markup.convert(text).get_document_body()
self.assertIn("system-message", body)
self.assertIn("Incomplete section title.", body)
def test_whole_html(self) -> None:
markup = ReStructuredTextMarkup()
text = basic_text + "\n\n.. math::\n \\sin \\varphi"
html = markup.convert(text).get_whole_html()
self.assertIn("<title>Hello, world!</title>", html)
self.assertIn('<style type="text/css">', html)
self.assertIn('<script type="text/javascript"', html)
self.assertIn("This is an example", html)
def test_toc_backrefs(self) -> None:
markup = ReStructuredTextMarkup()
body = markup.convert(toc_backrefs_source).get_document_body()
self.assertIn('<a class="toc-backref"', body)
|