Example #1
0
    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())
Example #2
0
    def test_special_inline_mode(self):
        doc = r"""\blocktag
    Some \inlinetag[started
    and here \otherinlinetag{continued}
    here \otherblocktag started
    and here two lines | separated from each other
    and that's all for inlinetag] we continue
"""
        parser = QqParser(
            allowed_tags={
                'blocktag', 'inlinetag', 'otherinlinetag', 'otherblocktag',
                'separator'
            })
        tree = parser.parse(doc)
        self.assertEqual(tree.as_list(), [
            '_root',
            [
                'blocktag', 'Some ',
                [
                    'inlinetag', 'started\nand here ',
                    ['otherinlinetag', 'continued'], '\nhere ',
                    [
                        'otherblocktag',
                        'started\nand here two lines ',
                    ], ['separator'],
                    'separated from each other\nand that\'s all for inlinetag'
                ], ' we continue\n'
            ]
        ])
Example #3
0
    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)")
Example #4
0
    def test_block_and_inline_tags(self):
        doc = r"""Hello,
\tag
    I'm your \othertag{tag}
    \tag
        {
        \tag
            {
            this \tag{is a {a test}
            okay}
        }
    }
"""
        parser = QqParser(allowed_tags={'tag', 'othertag'})
        tree = parser.parse(doc)
        self.assertEqual(tree.as_list(), [
            '_root', 'Hello,\n',
            [
                'tag', "I'm your ", ['othertag', 'tag'], '\n',
                [
                    'tag', '{\n',
                    [
                        'tag', '{\nthis ', [
                            'tag',
                            'is a {a test}\nokay',
                        ], '\n'
                    ], '}\n'
                ], '}\n'
            ]
        ])
Example #5
0
def show_html(filename):
    path = os.path.join("samplefiles", filename)
    if not os.path.isfile(path):
        abort(404)
    with open(path) as f:
        lines = f.readlines()
    parser = QqParser()
    formatter = QqFlaskHTMLFormatter()
    parser.allowed_tags.update(formatter.uses_tags())
    parser.allowed_tags.add('idx')  # for indexes
    tree = parser.parse(lines)
    formatter.root = tree
    formatter.pythonfigure_globals.update({'ob': qqmbr.odebook, 'np': numpy})
    formatter.code_prefixes['pythonfigure'] += (
        "import numpy as np\n"
        "import odebook as ob\n"
        "# see https://github.com/ischurov/qqmbr/blob/master/qqmbr/odebook.py"
        "\n\n")

    formatter.plotly_globals.update({'np': numpy})
    formatter.code_prefixes['plotly'] = formatter.code_prefixes.get(
        'plotly', "") + "import numpy as np\n\n"

    formatter.counters['h1'].value = 2
    html = formatter.do_format()
    return render_template("preview.html",
                           html=html,
                           title=tree._h1.text_content,
                           toc=formatter.mk_toc())
Example #6
0
    def test_inline_tag2(self):
        doc = r"""Hello, \othertag{\tag{inline} tag}!
"""
        parser = QqParser(allowed_tags={'tag', 'othertag'})
        tree = parser.parse(doc)
       # self.assertEqual(tree._othertag._tag.value, 'inline')
        self.assertEqual(tree.as_list(), ['_root', 'Hello, ', ['othertag', ['tag', 'inline'], ' tag'], '!\n'])
Example #7
0
    def test_special_inline_mode(self):
        doc = r"""\blocktag
    Some \inlinetag[started
    and here \otherinlinetag{continued}
    here \otherblocktag started
    and here two lines | separated from each other
    and that's all for inlinetag] we continue
"""
        parser = QqParser(allowed_tags={'blocktag', 'inlinetag', 'otherinlinetag', 'otherblocktag', 'separator'})
        tree = parser.parse(doc)
        self.assertEqual(tree.as_list(),
                         ['_root',
                          ['blocktag',
                           'Some ',
                           ['inlinetag',
                            'started\nand here ',
                            ['otherinlinetag', 'continued'],
                            '\nhere ',
                            ['otherblocktag',
                             'started\nand here two lines ',
                             ],
                             ['separator'],
                             'separated from each other\nand that\'s all for inlinetag'
                            ],
                           ' we continue\n'
                           ]
                         ]
                         )
Example #8
0
    def test_split_by_sep(self):
        doc = r"""\splittedtag[one|two\three|four]"""
        parser = QqParser(allowed_tags={'splittedtag', 'three'})
        tree = parser.parse(doc)
        splitted = tree._splittedtag.split_by_sep()

        self.assertEqual(splitted, [['one'], ['two', QqTag('three')], ['four']])
Example #9
0
    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")
Example #10
0
 def test_inline_tag4(self):
     doc = r"Hello, \tag{I'm [your{taggy}] tag} okay"
     parser = QqParser(allowed_tags={'tag', 'othertag'})
     tree = parser.parse(doc)
     self.assertEqual(
         tree.as_list(),
         ['_root', 'Hello, ', ['tag', "I'm [your{taggy}] tag"], " okay"])
Example #11
0
 def test_non_allowed_tag_with_bracket(self):
     doc = r"""Hello \inlinetag{some \forbiddentag{here} okay} this"""
     parser = QqParser(allowed_tags={'inlinetag'})
     tree = parser.parse(doc)
     self.assertEqual(tree.as_list(), [
         "_root", "Hello ", ["inlinetag", "some \\forbiddentag{here} okay"],
         " this"
     ])
Example #12
0
    def test_split_by_sep(self):
        doc = r"""\splittedtag[one|two\three|four]"""
        parser = QqParser(allowed_tags={'splittedtag', 'three'})
        tree = parser.parse(doc)
        splitted = tree._splittedtag.split_by_sep()

        self.assertEqual(splitted,
                         [['one'], ['two', QqTag('three')], ['four']])
Example #13
0
    def test_inline_tag1(self):
        doc = r"""Hello, \tag{inline} tag!
"""
        parser = QqParser(allowed_tags={'tag'})
        tree = parser.parse(doc)
        self.assertEqual(tree[0], 'Hello, ')
        self.assertEqual(tree._tag.value, 'inline')
        self.assertEqual(tree[2], ' tag!\n')
Example #14
0
    def test_alias2tag(self):
        doc = r"""\# Heading 1
\## Heading 2
Hello
"""
        parser = QqParser(allowed_tags={'h1', 'h2'}, alias2tag={"#": 'h1', "##": 'h2'})
        tree = parser.parse(doc)
        self.assertEqual(tree.as_list(), ["_root", ["h1", "Heading 1\n"], ["h2", "Heading 2\n"], "Hello\n"])
Example #15
0
    def test_split_line_by_tags(self):
        doc = r"""something \subtag other | this \is \a \test"""

        parser = QqParser(allowed_tags={'subtag', 'this', 'is', 'a', 'test'})

        self.assertEqual(parser.split_line_by_tags(doc), ['something ', r'\subtag other ', r'\separator', r'this ',
                                                            r'\is ', r'\a ', r'\test']
                         )
Example #16
0
    def test_inline_tag1(self):
        doc = r"""Hello, \tag{inline} tag!
"""
        parser = QqParser(allowed_tags={'tag'})
        tree = parser.parse(doc)
        self.assertEqual(tree[0], 'Hello, ')
        self.assertEqual(tree._tag.value, 'inline')
        self.assertEqual(tree[2], ' tag!\n')
Example #17
0
    def test_inline_tag2(self):
        doc = r"""Hello, \othertag{\tag{inline} tag}!
"""
        parser = QqParser(allowed_tags={'tag', 'othertag'})
        tree = parser.parse(doc)
        # self.assertEqual(tree._othertag._tag.value, 'inline')
        self.assertEqual(tree.as_list(), [
            '_root', 'Hello, ', ['othertag', ['tag', 'inline'], ' tag'], '!\n'
        ])
Example #18
0
 def test_inline_tag_at_the_beginning_of_the_line(self):
     doc = r"""\tag
 some content here here and here and we have some inline
 \tag{here and \othertag{there}}
 """
     parser = QqParser(allowed_tags={'tag', 'othertag'})
     tree = parser.parse(doc)
     self.assertEqual(tree.as_list(), ['_root', ['tag','some content here here and here and we have some inline\n',
                                       ['tag', 'here and ',['othertag', 'there']],'\n']])
Example #19
0
    def test_split_line_by_tags(self):
        doc = r"""something \subtag other | this \is \a \test"""

        parser = QqParser(allowed_tags={'subtag', 'this', 'is', 'a', 'test'})

        self.assertEqual(parser.split_line_by_tags(doc), [
            'something ', r'\subtag other ', r'\separator', r'this ', r'\is ',
            r'\a ', r'\test'
        ])
Example #20
0
    def test_empty_special_tag(self):
        doc = r"""\blocktag
    Some \empty[

    ] tag
"""
        parser = QqParser(allowed_tags={'blocktag', 'empty'})
        tree = parser.parse(doc)
        self.assertEqual(tree.as_list(),["_root", ['blocktag', 'Some ', ['empty'], ' tag\n']])
Example #21
0
    def test_block_additional_indent(self):
        doc = r"""Hello
\tag
    First
        Second
    Third
End"""
        parser = QqParser(allowed_tags={'tag'})
        tree = parser.parse(doc)
        self.assertEqual(tree._tag._children, ['First\n    Second\nThird\n'])
Example #22
0
    def test_block_additional_indent(self):
        doc = r"""Hello
\tag
    First
        Second
    Third
End"""
        parser = QqParser(allowed_tags={'tag'})
        tree = parser.parse(doc)
        self.assertEqual(tree._tag._children, ['First\n    Second\nThird\n'])
Example #23
0
    def test_empty_special_tag(self):
        doc = r"""\blocktag
    Some \empty[

    ] tag
"""
        parser = QqParser(allowed_tags={'blocktag', 'empty'})
        tree = parser.parse(doc)
        self.assertEqual(tree.as_list(),
                         ["_root", ['blocktag', 'Some ', ['empty'], ' tag\n']])
Example #24
0
    def test_block_tags1(self):
        doc = r"""Hello
\tag
    World
"""
        parser = QqParser(allowed_tags={'tag'})
        tree = parser.parse(doc)
        self.assertEqual(tree[0], "Hello\n")

        self.assertEqual(tree._tag.name, 'tag')
        self.assertEqual(tree._tag.value, 'World')
Example #25
0
    def test_block_tags1(self):
        doc = r"""Hello
\tag
    World
"""
        parser = QqParser(allowed_tags={'tag'})
        tree = parser.parse(doc)
        self.assertEqual(tree[0], "Hello\n")

        self.assertEqual(tree._tag.name, 'tag')
        self.assertEqual(tree._tag.value, 'World')
Example #26
0
 def test_inline_tag4(self):
     doc = r"Hello, \tag{I'm [your{taggy}] tag} okay"
     parser = QqParser(allowed_tags={'tag', 'othertag'})
     tree = parser.parse(doc)
     self.assertEqual(tree.as_list(), [
         '_root', 'Hello, ',
         [
             'tag',
             "I'm [your{taggy}] tag"
         ],
         " okay"
     ])
Example #27
0
    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")
Example #28
0
    def test_alias2tag(self):
        doc = r"""\# Heading 1
\## Heading 2
Hello
"""
        parser = QqParser(allowed_tags={'h1', 'h2'},
                          alias2tag={
                              "#": 'h1',
                              "##": 'h2'
                          })
        tree = parser.parse(doc)
        self.assertEqual(
            tree.as_list(),
            ["_root", ["h1", "Heading 1\n"], ["h2", "Heading 2\n"], "Hello\n"])
Example #29
0
    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")
Example #30
0
def simple_show_html(filename):
    path = os.path.join("samplefiles",filename)
    if not os.path.isfile(path):
        abort(404)
    with open(path) as f:
        lines = f.readlines()
    parser = QqParser()
    formatter = QqFlaskHTMLFormatter()
    parser.allowed_tags.update(formatter.uses_tags())
    parser.allowed_tags.add('idx') # for indexes
    tree = parser.parse(lines)
    formatter.root = tree
    formatter.counters['h1'].value = 2
    html = formatter.do_format()
    return render_template("preview.html", html=html, title=tree._h1.text_content)
Example #31
0
 def test_inline_tag_at_the_beginning_of_the_line(self):
     doc = r"""\tag
 some content here here and here and we have some inline
 \tag{here and \othertag{there}}
 """
     parser = QqParser(allowed_tags={'tag', 'othertag'})
     tree = parser.parse(doc)
     self.assertEqual(tree.as_list(), [
         '_root',
         [
             'tag',
             'some content here here and here and we have some inline\n',
             ['tag', 'here and ', ['othertag', 'there']], '\n'
         ]
     ])
Example #32
0
    def test_inline_tag3(self):
        doc = r"""Hello, \tag{
this is a continuation of inline tag on the next line

the next one\othertag{okay}}
"""
        parser = QqParser(allowed_tags={'tag', 'othertag'})
        tree = parser.parse(doc)
        self.assertEqual(tree.as_list(), [
            '_root', 'Hello, ',
            [
                'tag',
                '\nthis is a continuation of inline tag on the next line\n\nthe next one',
                ['othertag', 'okay']
            ], '\n'
        ])
Example #33
0
    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")
Example #34
0
    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")
Example #35
0
def simple_show_html(filename):
    path = os.path.join("samplefiles", filename)
    if not os.path.isfile(path):
        abort(404)
    with open(path) as f:
        lines = f.readlines()
    parser = QqParser()
    formatter = QqFlaskHTMLFormatter()
    parser.allowed_tags.update(formatter.uses_tags())
    parser.allowed_tags.add('idx')  # for indexes
    tree = parser.parse(lines)
    formatter.root = tree
    formatter.counters['h1'].value = 2
    html = formatter.do_format()
    return render_template("preview.html",
                           html=html,
                           title=tree._h1.text_content)
Example #36
0
    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")
Example #37
0
    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)")
Example #38
0
 def test_escape_unescape(self):
     doc = r"""Hello
 \sometag test
 \\sometag test
 \sometag
     \ here we are
     we are here
 some \inline{tag with \{ curve bracket inside} okay
 some \inline[square bracket \[ inside] okay
 """
     parser = QqParser(allowed_tags={'sometag', 'inline'})
     tree = parser.parse(doc)
     self.assertEqual(tree.as_list(), [
         "_root", "Hello\n", ["sometag", "test\n"], "\\sometag test\n",
         ["sometag", " here we are\nwe are here\n"], "some ",
         ["inline", "tag with { curve bracket inside"], " okay\nsome ",
         ["inline", "square bracket [ inside"], " okay\n"
     ])
Example #39
0
def show_snippet(label):
    tree, formatter = prepare_book()
    tag = formatter.label2tag.get(label)

    if tag is None or tag.name != 'snippet':
        abort(404)
    if tag.exists("backref"):
        backref = tag._backref.value
    else:
        backref = label

    parser = QqParser()
    parser.allowed_tags.update(formatter.uses_tags())
    backref_tag = parser.parse(r"\ref[Подробнее\nonumber|{}]".format(backref))
    tag.append_child(backref_tag._ref)

    html = formatter.format(tag, blanks_to_pars=True)

    return mathjax_if_needed(html)[1]
Example #40
0
def show_snippet(label):
    tree, formatter = prepare_book()
    tag = formatter.label2tag.get(label)

    if tag is None or tag.name != 'snippet':
        abort(404)
    if tag.exists("backref"):
        backref = tag._backref.value
    else:
        backref = label

    parser = QqParser()
    parser.allowed_tags.update(formatter.uses_tags())
    backref_tag = parser.parse(r"\ref[Подробнее\nonumber|{}]".format(backref))
    tag.append_child(backref_tag._ref)

    html = formatter.format(tag, blanks_to_pars=True)

    return mathjax_if_needed(html)[1]
Example #41
0
    def test_inline_tag3(self):
        doc = r"""Hello, \tag{
this is a continuation of inline tag on the next line

the next one\othertag{okay}}
"""
        parser = QqParser(allowed_tags={'tag', 'othertag'})
        tree = parser.parse(doc)
        self.assertEqual(tree.as_list(), [
            '_root', 'Hello, ',
            [
                'tag',
                '\nthis is a continuation of inline tag on the next line\n\nthe next one',
                [
                    'othertag',
                    'okay'
                ]
            ],
            '\n'
        ])
Example #42
0
def prepare_book():
    samplefiles = "samplefiles"
    path = os.path.join(samplefiles, 'thebook.qq')
    if not os.path.isfile(path):
        abort(404)
    with open(path) as f:
        lines = f.readlines()
    parser = QqParser()
    formatter = QqFlaskHTMLFormatter()
    parser.allowed_tags.update(formatter.uses_tags())
    parser.allowed_tags.add('idx')  # for indexes
    parser.include_dir = samplefiles
    tree = parser.parse(lines)
    formatter.root = tree
    formatter.pythonfigure_globals.update({'ob': qqmbr.odebook, 'np': numpy})
    formatter.code_prefixes['pythonfigure'] += (
        "import numpy as np\n"
        "import odebook as ob\n"
        "# see https://github.com/ischurov/qqmbr/blob/master/qqmbr/odebook.py"
        "\n\n")

    formatter.plotly_globals.update({'np': numpy})
    formatter.code_prefixes['plotly'] = formatter.code_prefixes.get(
        'plotly', "") + "import numpy as np\n\n"

    formatter.mode = 'bychapters'
    formatter.preprocess(tree)
    formatter.mk_chapters()

    # dirty hack to get equation snippet work
    global allthebook
    if allthebook is None:
        if app.config.get("MATHJAX_ALLTHEBOOK"):
            style, allthebook = mathjax(
                app.config.get('preamble', '') + formatter.format(tree))
        else:
            allthebook = formatter.format(tree)
            style = ""
        app.config['css_correction'] = style + app.config.get('css_correction')

    return tree, formatter
Example #43
0
    def test_block_tags_nested(self):
        doc = r"""Hello
\tag
    World
    \othertag
        This
        Is
    A test
The end

Blank line before the end
"""
        parser = QqParser(allowed_tags={'tag', 'othertag'})
        tree = parser.parse(doc)
        self.assertEqual(tree[0], "Hello\n")
        self.assertEqual(tree._tag[0], "World\n")
        self.assertEqual(tree._tag._othertag._children, ["This\nIs\n"])
        self.assertEqual(tree._tag[2], 'A test\n')
        self.assertEqual(tree[2], 'The end\n\nBlank line before the end\n')
        self.assertEqual(tree._tag.parent, tree)
        self.assertEqual(tree._tag._othertag.parent, tree._tag)
Example #44
0
    def test_block_tags_nested(self):
        doc = r"""Hello
\tag
    World
    \othertag
        This
        Is
    A test
The end

Blank line before the end
"""
        parser = QqParser(allowed_tags={'tag', 'othertag'})
        tree = parser.parse(doc)
        self.assertEqual(tree[0], "Hello\n")
        self.assertEqual(tree._tag[0], "World\n")
        self.assertEqual(tree._tag._othertag._children, ["This\nIs\n"])
        self.assertEqual(tree._tag[2], 'A test\n')
        self.assertEqual(tree[2], 'The end\n\nBlank line before the end\n')
        self.assertEqual(tree._tag.parent, tree)
        self.assertEqual(tree._tag._othertag.parent, tree._tag)
Example #45
0
    def test_block_and_inline_tags(self):
        doc = r"""Hello,
\tag
    I'm your \othertag{tag}
    \tag
        {
        \tag
            {
            this \tag{is a {a test}
            okay}
        }
    }
"""
        parser = QqParser(allowed_tags={'tag', 'othertag'})
        tree = parser.parse(doc)
        self.assertEqual(tree.as_list(), [
            '_root', 'Hello,\n',
            [
                'tag',
                "I'm your ",
                ['othertag', 'tag'],
                '\n',
                [
                    'tag',
                    '{\n',
                    [
                        'tag',
                        '{\nthis ',
                        [
                            'tag',
                            'is a {a test}\nokay',
                        ],
                        '\n'
                    ],
                    '}\n'
                ],
                '}\n'
            ]
        ])
Example #46
0
def prepare_book():
    samplefiles = "samplefiles"
    path = os.path.join(samplefiles, 'thebook.qq')
    if not os.path.isfile(path):
        abort(404)
    with open(path) as f:
        lines = f.readlines()
    parser = QqParser()
    formatter = QqFlaskHTMLFormatter()
    parser.allowed_tags.update(formatter.uses_tags())
    parser.allowed_tags.add('idx') # for indexes
    parser.include_dir = samplefiles
    tree = parser.parse(lines)
    formatter.root = tree
    formatter.pythonfigure_globals.update({'ob': qqmbr.odebook, 'np': numpy})
    formatter.code_prefixes['pythonfigure'] += ("import numpy as np\n"
                                            "import odebook as ob\n"
                                            "# see https://github.com/ischurov/qqmbr/blob/master/qqmbr/odebook.py"
                                            "\n\n")

    formatter.plotly_globals.update({'np': numpy})
    formatter.code_prefixes['plotly'] = formatter.code_prefixes.get('plotly',"") + "import numpy as np\n\n"

    formatter.mode = 'bychapters'
    formatter.preprocess(tree)
    formatter.mk_chapters()

    # dirty hack to get equation snippet work
    global allthebook
    if allthebook is None:
        if app.config.get("MATHJAX_ALLTHEBOOK"):
            style, allthebook = mathjax(app.config.get('preamble', '') + formatter.format(tree))
        else:
            allthebook = formatter.format(tree)
            style = ""
        app.config['css_correction'] = style + app.config.get('css_correction')

    return tree, formatter
Example #47
0
 def test_escape_unescape(self):
     doc = r"""Hello
 \sometag test
 \\sometag test
 \sometag
     \ here we are
     we are here
 some \inline{tag with \{ curve bracket inside} okay
 some \inline[square bracket \[ inside] okay
 """
     parser = QqParser(allowed_tags={'sometag', 'inline'})
     tree = parser.parse(doc)
     self.assertEqual(tree.as_list(), [
         "_root", "Hello\n",
         ["sometag", "test\n"],
         "\\sometag test\n",
         ["sometag", " here we are\nwe are here\n"],
         "some ",
         ["inline", "tag with { curve bracket inside"],
         " okay\nsome ",
         ["inline", "square bracket [ inside"],
         " okay\n"
     ])
Example #48
0
    def test_tag2chapter(self):
        html = QqHTMLFormatter()
        parser = QqParser(allowed_tags=html.uses_tags())
        parser.allowed_tags.add('author')
        doc = r"""\author Ilya V. Schurov
\h1 Chapter 1
This is the first chapter
\equation \label eq1
    x^2 + y^2

\h1 Chapter 2
This is the second chapter
\h2 Section 1
Hello
\remark \label rem
    This is the end. \ref{eq1}
"""
        tree = parser.parse(doc)
        html.root = tree
        self.assertEqual(html.tag2chapter(tree._author), 0)
        self.assertEqual(html.tag2chapter(tree._equation), 1)
        self.assertEqual(html.tag2chapter(tree._equation._label), 1)
        self.assertEqual(html.tag2chapter(tree._remark), 2)
        self.assertEqual(html.tag2chapter(tree._remark._ref), 2)
Example #49
0
    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())
Example #50
0
    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)")
Example #51
0
    def test_tag2chapter(self):
        html = QqHTMLFormatter()
        parser = QqParser(allowed_tags=html.uses_tags())
        parser.allowed_tags.add('author')
        doc = r"""\author Ilya V. Schurov
\h1 Chapter 1
This is the first chapter
\equation \label eq1
    x^2 + y^2

\h1 Chapter 2
This is the second chapter
\h2 Section 1
Hello
\remark \label rem
    This is the end. \ref{eq1}
"""
        tree = parser.parse(doc)
        html.root = tree
        self.assertEqual(html.tag2chapter(tree._author), 0)
        self.assertEqual(html.tag2chapter(tree._equation), 1)
        self.assertEqual(html.tag2chapter(tree._equation._label), 1)
        self.assertEqual(html.tag2chapter(tree._remark), 2)
        self.assertEqual(html.tag2chapter(tree._remark._ref), 2)
Example #52
0
    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)")
Example #53
0
def show_html(filename):
    path = os.path.join("samplefiles",filename)
    if not os.path.isfile(path):
        abort(404)
    with open(path) as f:
        lines = f.readlines()
    parser = QqParser()
    formatter = QqFlaskHTMLFormatter()
    parser.allowed_tags.update(formatter.uses_tags())
    parser.allowed_tags.add('idx') # for indexes
    tree = parser.parse(lines)
    formatter.root = tree
    formatter.pythonfigure_globals.update({'ob': qqmbr.odebook, 'np': numpy})
    formatter.code_prefixes['pythonfigure'] += ("import numpy as np\n"
                                                "import odebook as ob\n"
                                                "# see https://github.com/ischurov/qqmbr/blob/master/qqmbr/odebook.py"
                                                "\n\n")

    formatter.plotly_globals.update({'np': numpy})
    formatter.code_prefixes['plotly'] = formatter.code_prefixes.get('plotly',"") + "import numpy as np\n\n"

    formatter.counters['h1'].value = 2
    html = formatter.do_format()
    return render_template("preview.html", html=html, title=tree._h1.text_content, toc=formatter.mk_toc())
Example #54
0
    def test_sameline_tags(self):
        self.maxDiff = None
        doc = r"""    Hello!
    \h1 Intro to qqmbr

    \h2 Fresh documentation system

    **qqmbr** is a documentation system intended to be extremely simple and extremely extensible.
    It was written to allow writing rich content that can be compiled into different formats.
    One source, multiple media: HTML, XML, LaTeX, PDF, eBooks, any other. Look below to see it in action.

    \h3 This is nice level-3 header

    Some paragraph text. See also \ref{sec:another} (reference to different header).

    There are LaTeX formulas here:

    \eq
        x^2 + y^2 = z^2

    `\eq` is a qqtag. It is better than tag, because it is auto-closing (look at the indent, like Python).

    Here is formula with the label:

    \equation \label eq:Fermat
        x^n + y^n = z^n, \quad n>2

    Several formulas with labels:

    \gather
        \item \label eq:2x2
            2\times 2 = 4
        \item \label eq:3x3
            3\times 3 = 9

    We can reference formula \eqref{eq:Fermat} and \eqref{eq:2x2} just like we referenced header before.

    \h3 Another level-3 header \label sec:another

    Here is the header we referenced.

    \h3 More interesting content

    \figure
        \source http://example.com/somefig.png
        \caption Some figure
        \width 500px

    \question
        Do you like qqmbr?
        \quiz
            \choice \correct false
                No.
                \comment You didn't even try!
            \choice \correct true
                Yes, i like it very much!
                \comment And so do I!
"""
        parser = QqParser(allowed_tags={'h1', 'h2', 'h3', 'eq', 'equation', 'label',
                                        'gather', 'inlne', 'item', 'ref', 'eqref',
                                        'source', 'caption', 'width', 'question', 'quiz', 'choice',
                                        'comment', 'correct', 'figure'})
        tree = parser.parse(doc)
        self.assertEqual(tree.as_list(), ['_root',
 'Hello!\n',
 ['h1', 'Intro to qqmbr\n\n'],
 ['h2', 'Fresh documentation system\n\n'],
 '**qqmbr** is a documentation system intended to be extremely simple and extremely extensible.\nIt was written to allow writing rich content that can be compiled into different formats.\nOne source, multiple media: HTML, XML, LaTeX, PDF, eBooks, any other. Look below to see it in action.\n\n',
 ['h3', 'This is nice level-3 header\n\n'],
 'Some paragraph text. See also ',
 ['ref', 'sec:another'],
 ' (reference to different header).\n\nThere are LaTeX formulas here:\n\n',
 ['eq', 'x^2 + y^2 = z^2\n\n'],
 '`\\eq` is a qqtag. It is better than tag, because it is auto-closing (look at the indent, like Python).\n\nHere is formula with the label:\n\n',
 ['equation', ['label', 'eq:Fermat\n'], 'x^n + y^n = z^n, \\quad n>2\n\n'],
 'Several formulas with labels:\n\n',
 ['gather',
  ['item', ['label', 'eq:2x2\n'], '2\\times 2 = 4\n'],
  ['item', ['label', 'eq:3x3\n'], '3\\times 3 = 9\n\n']],
 'We can reference formula ',
 ['eqref', 'eq:Fermat'],
 ' and ',
 ['eqref', 'eq:2x2'],
 ' just like we referenced header before.\n\n',
 ['h3', 'Another level-3 header ', ['label', 'sec:another\n\n']],
 'Here is the header we referenced.\n\n',
 ['h3', 'More interesting content\n\n'],
 ['figure',
  ['source', 'http://example.com/somefig.png\n'],
  ['caption', 'Some figure\n'],
  ['width', '500px\n\n']],
 ['question',
  'Do you like qqmbr?\n',
  ['quiz',
   ['choice',
    ['correct', 'false\n'],
    'No.\n',
    ['comment', "You didn't even try!\n"]],
   ['choice',
    ['correct', 'true\n'],
    'Yes, i like it very much!\n',
    ['comment', 'And so do I!\n']]]]])
Example #55
0
 def test_non_allowed_tag_with_bracket(self):
     doc = r"""Hello \inlinetag{some \forbiddentag{here} okay} this"""
     parser = QqParser(allowed_tags={'inlinetag'})
     tree = parser.parse(doc)
     self.assertEqual(tree.as_list(), ["_root", "Hello ", ["inlinetag", "some \\forbiddentag{here} okay"], " this"])
Example #56
0
    def test_sameline_tags(self):
        self.maxDiff = None
        doc = r"""    Hello!
    \h1 Intro to qqmbr

    \h2 Fresh documentation system

    **qqmbr** is a documentation system intended to be extremely simple and extremely extensible.
    It was written to allow writing rich content that can be compiled into different formats.
    One source, multiple media: HTML, XML, LaTeX, PDF, eBooks, any other. Look below to see it in action.

    \h3 This is nice level-3 header

    Some paragraph text. See also \ref{sec:another} (reference to different header).

    There are LaTeX formulas here:

    \eq
        x^2 + y^2 = z^2

    `\eq` is a qqtag. It is better than tag, because it is auto-closing (look at the indent, like Python).

    Here is formula with the label:

    \equation \label eq:Fermat
        x^n + y^n = z^n, \quad n>2

    Several formulas with labels:

    \gather
        \item \label eq:2x2
            2\times 2 = 4
        \item \label eq:3x3
            3\times 3 = 9

    We can reference formula \eqref{eq:Fermat} and \eqref{eq:2x2} just like we referenced header before.

    \h3 Another level-3 header \label sec:another

    Here is the header we referenced.

    \h3 More interesting content

    \figure
        \source http://example.com/somefig.png
        \caption Some figure
        \width 500px

    \question
        Do you like qqmbr?
        \quiz
            \choice \correct false
                No.
                \comment You didn't even try!
            \choice \correct true
                Yes, i like it very much!
                \comment And so do I!
"""
        parser = QqParser(
            allowed_tags={
                'h1', 'h2', 'h3', 'eq', 'equation', 'label', 'gather', 'inlne',
                'item', 'ref', 'eqref', 'source', 'caption', 'width',
                'question', 'quiz', 'choice', 'comment', 'correct', 'figure'
            })
        tree = parser.parse(doc)
        self.assertEqual(tree.as_list(), [
            '_root', 'Hello!\n', ['h1', 'Intro to qqmbr\n\n'],
            ['h2', 'Fresh documentation system\n\n'],
            '**qqmbr** is a documentation system intended to be extremely simple and extremely extensible.\nIt was written to allow writing rich content that can be compiled into different formats.\nOne source, multiple media: HTML, XML, LaTeX, PDF, eBooks, any other. Look below to see it in action.\n\n',
            ['h3', 'This is nice level-3 header\n\n'
             ], 'Some paragraph text. See also ', ['ref', 'sec:another'],
            ' (reference to different header).\n\nThere are LaTeX formulas here:\n\n',
            ['eq', 'x^2 + y^2 = z^2\n\n'],
            '`\\eq` is a qqtag. It is better than tag, because it is auto-closing (look at the indent, like Python).\n\nHere is formula with the label:\n\n',
            [
                'equation', ['label', 'eq:Fermat\n'],
                'x^n + y^n = z^n, \\quad n>2\n\n'
            ], 'Several formulas with labels:\n\n',
            [
                'gather', ['item', ['label', 'eq:2x2\n'], '2\\times 2 = 4\n'],
                ['item', ['label', 'eq:3x3\n'], '3\\times 3 = 9\n\n']
            ], 'We can reference formula ', ['eqref', 'eq:Fermat'], ' and ',
            ['eqref', 'eq:2x2'], ' just like we referenced header before.\n\n',
            ['h3', 'Another level-3 header ', ['label', 'sec:another\n\n']
             ], 'Here is the header we referenced.\n\n',
            ['h3', 'More interesting content\n\n'],
            [
                'figure', ['source', 'http://example.com/somefig.png\n'],
                ['caption', 'Some figure\n'], ['width', '500px\n\n']
            ],
            [
                'question', 'Do you like qqmbr?\n',
                [
                    'quiz',
                    [
                        'choice', ['correct', 'false\n'], 'No.\n',
                        ['comment', "You didn't even try!\n"]
                    ],
                    [
                        'choice', ['correct', 'true\n'],
                        'Yes, i like it very much!\n',
                        ['comment', 'And so do I!\n']
                    ]
                ]
            ]
        ])