def test_parse_html2(self): parser = QqParser( allowed_tags={ 'chapter', 'section', 'subsection', 'subsubsection', 'eq', 'eqref', 'ref', 'equation', 'label', 'idx' }) doc = r"""\chapter \label h1:label Hello This is a \ref{h1:label}. """ tree = parser.parse(doc) html = QqHTMLFormatter(tree) s = html.do_format() soup = BeautifulSoup(s, 'html.parser') self.assertEqual(soup.h1['id'], 'label_h1_label') self.assertEqual(soup.span['class'], ['section__number']) self.assertEqual(soup.span.string, "1") self.assertEqual( soup("a")[1].attrs, { 'class': ['a-ref'], 'title': '', 'href': '#label_h1_label' }) self.assertEqual(soup("a")[1].string, "1")
def test_parse_html_math_align(self): html = QqHTMLFormatter() parser = QqParser(allowed_tags=html.uses_tags()) doc = r"""\align \item c^2 &= a^2 + b^2 \label eq:one \item c &= \sqrt{a^2 + b^2} \label eq:two See \ref{eq:one} and \ref{eq:two} """ tree = parser.parse(doc) html.root = tree html.counters['equation'].showparents = False s = html.do_format() soup = BeautifulSoup(s, 'html.parser') print(repr(soup.text)) self.assertEqual( soup.text, "\\[\n\\begin{align}\n\nc^2 &= a^2 + b^2 \n\\tag{1}\n\\\\\n" "c &= \\sqrt{a^2 + b^2} \n\\tag{2}\n\\\\\n\\end{align}\n\\]\n\n\n\nSee (1) and (2)" ) self.assertEqual(soup.a['href'], "#mjx-eqn-1") self.assertEqual(soup.a.string, "(1)") self.assertEqual(soup("a")[1]['href'], "#mjx-eqn-2") self.assertEqual(soup("a")[1].string, "(2)")
def test_parse_html1(self): parser = QqParser( allowed_tags={ 'chapter', 'section', 'subsection', 'subsubsection', 'eq', 'eqref', 'ref', 'equation', 'label', 'idx' }) doc = r"""\chapter Hello \label h1:label \section World """ tree = parser.parse(doc) html = QqHTMLFormatter(tree) s = html.do_format() print(s) soup = BeautifulSoup(s, 'html.parser') #self.assertEqual(s, """<h1 id="label_h1_label"><span class="section__number"><a href="#label_h1_label" class="section__number">1</a></span>Hello #</h1><h2 id="label_h2_number_1_1"><span class="section__number"><a href="#label_h2_number_1_1" class="section__number">1.1</a></span>World #</h2>""") self.assertEqual('label_h1_label', soup.h1['id']) self.assertEqual(['section__number'], soup.span['class']) self.assertEqual('#label_h1_label', soup.a['href']) self.assertEqual(['section__number'], soup.a['class']) self.assertEqual('1Hello', soup.h1.text.strip()) self.assertEqual('1.1World', soup.h2.text.strip())
def test_ref_with_separator(self): doc = r"""\chapter Hello \label sec:first See \ref[section][sec:first] for details. """ parser = QqParser() formatter = QqHTMLFormatter() parser.allowed_tags.update(formatter.uses_tags()) tree = parser.parse(doc) formatter.root = tree html = formatter.do_format() soup = BeautifulSoup(html, "html.parser") self.assertEqual(soup("a")[1]['href'], "#label_sec_first") self.assertEqual(soup("a")[1].string, "section 1")
def test_ref_with_separator(self): doc = r"""\h1 Hello \label sec:first See \ref[section|sec:first] for details. """ parser = QqParser() formatter = QqHTMLFormatter() parser.allowed_tags.update(formatter.uses_tags()) tree = parser.parse(doc) formatter.root = tree html = formatter.do_format() soup = BeautifulSoup(html, "html.parser") self.assertEqual(soup("a")[1]['href'], "#label_sec_first") self.assertEqual(soup("a")[1].string, "section 1")
def test_refs_with_separator(self): doc = r"""\chapter Hello \label sec:first \chapter World \label sec:other See \ref[section][sec:first] and \ref[section][sec:other] for details. """ parser = QqParser() formatter = QqHTMLFormatter() parser.allowed_tags.update(formatter.uses_tags()) tree = parser.parse(doc) formatter.root = tree print(tree.as_list()) html = formatter.do_format() soup = BeautifulSoup(html, "html.parser") self.assertEqual(soup("a")[2].contents[0], "section 1")
def test_parse_html3(self): parser = QqParser(allowed_tags={'h1', 'h2', 'h3', 'h4', 'eq', 'eqref', 'ref', 'equation', 'label', 'idx'}) doc = r"""\equation \label eq:x2y2 x^2 + y^2 = z^2 See \ref{eq:x2y2}. """ tree = parser.parse(doc) html = QqHTMLFormatter(tree) html.counters['equation'].showparents = False s = html.do_format() soup = BeautifulSoup(s, 'html.parser') self.assertEqual(soup.div.attrs, {'id':"label_eq_x2y2",'class':["latex_equation"]}) self.assertEqual(soup.span['class'], ['ref']) self.assertEqual(soup.a['class'], ['a-ref']) self.assertEqual(soup.a['href'], '#mjx-eqn-1') self.assertEqual(soup.a.string, "(1)")
def test_parse_html2(self): parser = QqParser(allowed_tags={'h1', 'h2', 'h3', 'h4', 'eq', 'eqref', 'ref', 'equation', 'label', 'idx'}) doc = r"""\h1 \label h1:label Hello This is a \ref{h1:label}. """ tree = parser.parse(doc) html = QqHTMLFormatter(tree) s = html.do_format() soup = BeautifulSoup(s, 'html.parser') self.assertEqual(soup.h1['id'], 'label_h1_label') self.assertEqual(soup.span['class'], ['section__number']) self.assertEqual(soup.span.string, "1") self.assertEqual(soup("a")[1].attrs,{'class': ['a-ref'], 'title': '', 'href': '#label_h1_label'}) self.assertEqual(soup("a")[1].string, "1")
def test_refs_with_separator(self): doc = r"""\h1 Hello \label sec:first \h1 World \label sec:other See \ref[section|sec:first] and \ref[section|sec:other] for details. """ parser = QqParser() formatter = QqHTMLFormatter() parser.allowed_tags.update(formatter.uses_tags()) tree = parser.parse(doc) formatter.root = tree print(tree.as_list()) html = formatter.do_format() soup = BeautifulSoup(html, "html.parser") self.assertEqual(soup("a")[2].contents[0], "section 1")
def render(): text = request.values.get('text', '').replace('\r\n', '\n') formatter = QqHTMLFormatter() formatter.localnames = {} parser = QqParser(allowed_tags=formatter.safe_tags) try: tree = parser.parse(text) except Exception as e: return jsonify(error='parse error: ' + str(e)) formatter.root = tree try: output = formatter.do_format() except Exception as e: return jsonify(error='format error: ' + str(e)) return jsonify(output=output)
def convert(**args): path = os.path.join(curdir, app.config["FILE"]) with open(path) as f: lines = f.readlines() formatter = QqHTMLFormatter() parser = QqParser(allowed_tags=formatter.uses_tags()) try: tree = parser.parse(lines) except Exception as e: print("Parse error:", str(e)) raise formatter.root = tree try: output = formatter.do_format() except Exception as e: print("Formatting error:", str(e)) raise print(output)
def test_parse_html3(self): parser = QqParser( allowed_tags={ 'h1', 'h2', 'h3', 'h4', 'eq', 'eqref', 'ref', 'equation', 'label', 'idx' }) doc = r"""\equation \label eq:x2y2 x^2 + y^2 = z^2 See \ref{eq:x2y2}. """ tree = parser.parse(doc) html = QqHTMLFormatter(tree) html.counters['equation'].showparents = False s = html.do_format() soup = BeautifulSoup(s, 'html.parser') self.assertEqual(soup.div.attrs, { 'id': "label_eq_x2y2", 'class': ["latex_equation"] }) self.assertEqual(soup.span['class'], ['ref']) self.assertEqual(soup.a['class'], ['a-ref']) self.assertEqual(soup.a['href'], '#mjx-eqn-1') self.assertEqual(soup.a.string, "(1)")
def render(path): print(path_to_source_url(path)) r = requests.get(path_to_source_url(path)) r.encoding = 'UTF-8' if not r: abort(500) formatter = QqHTMLFormatter(with_chapters=False) formatter.localnames = {} parser = QqParser(allowed_tags=formatter.safe_tags) try: tree = parser.parse(r.text) except Exception as e: if app.debug: raise e return render_template("error.html", error='parse error: ' + str(e)), 400 formatter.root = tree try: output = formatter.do_format() except Exception as e: if app.debug: raise e return render_template("error.html", error='format error: ' + str(e)), 400 meta = tree.find_or_empty("meta") print(meta.as_list()) return render_template("render_url.html", output=output, source_url=path_to_url(path), meta=meta)
def test_parse_html_math_align(self): html = QqHTMLFormatter() parser = QqParser(allowed_tags=html.uses_tags()) doc = r"""\align \item c^2 &= a^2 + b^2 \label eq:one \item c &= \sqrt{a^2 + b^2} \label eq:two See \ref{eq:one} and \ref{eq:two} """ tree = parser.parse(doc) html.root = tree html.counters['equation'].showparents = False with working_directory("../qqmbr"): s = html.do_format() soup = BeautifulSoup(s, 'html.parser') self.assertEqual(soup.text, "\\[\n\\begin{align}\n\nc^2 &= a^2 + b^2 \n\\tag{1}\n\\\\\n" "c &= \\sqrt{a^2 + b^2} \n\\tag{2}\n\\\\\n\\end{align}\n\\]\nSee (1) and (2)\n") self.assertEqual(soup.a['href'], "#mjx-eqn-1") self.assertEqual(soup.a.string, "(1)") self.assertEqual(soup("a")[1]['href'], "#mjx-eqn-2") self.assertEqual(soup("a")[1].string, "(2)")
def test_parse_html1(self): parser = QqParser(allowed_tags={'h1', 'h2', 'h3', 'h4', 'eq', 'eqref', 'ref', 'equation', 'label', 'idx'}) doc = r"""\h1 Hello \label h1:label \h2 World """ tree = parser.parse(doc) html = QqHTMLFormatter(tree) s = html.do_format() soup = BeautifulSoup(s, 'html.parser') #self.assertEqual(s, """<h1 id="label_h1_label"><span class="section__number"><a href="#label_h1_label" class="section__number">1</a></span>Hello #</h1><h2 id="label_h2_number_1_1"><span class="section__number"><a href="#label_h2_number_1_1" class="section__number">1.1</a></span>World #</h2>""") self.assertEqual('label_h1_label', soup.h1['id']) self.assertEqual(['section__number'], soup.span['class']) self.assertEqual('#label_h1_label', soup.a['href']) self.assertEqual(['section__number'], soup.a['class']) self.assertEqual('1Hello', soup.h1.text.strip()) self.assertEqual('1.1World', soup.h2.text.strip())