Ejemplo n.º 1
0
    def preprocess(self, root: QqTag):
        """
        Uses tags: number, label, nonumber, flabel

        :return:
        """
        for tag in root:
            if isinstance(tag, QqTag):
                name = tag.name
                if (name in self.counters or name in self.enumerateable_envs
                    ) and not (tag.find('number') or tag.exists('nonumber')):
                    counter = self.get_counter_for_tag(tag)
                    if counter is not None:
                        counter.increase()
                        tag.append_child(QqTag({'number': str(counter)}))
                        if tag.find('label'):
                            label = tag._label.value
                            self.label2number[label] = str(counter)
                            # self.label2title[label] = tag.text_content
                if tag.find('label') and tag.find('number'):
                    self.label2number[tag._label.value] = tag._number.value
                if tag.find('label'):
                    self.label2tag[tag._label.value] = tag
                if tag.find('flabel'):
                    self.flabel2tag[tag._flabel.value.lower()] = tag
                self.preprocess(tag)
Ejemplo n.º 2
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']])
Ejemplo n.º 3
0
 def mk_chapters(self):
     curchapter = Chapter(QqTag("_zero_chapter"), [])
     self.chapters = []
     for tag in self.root:
         if isinstance(tag, QqTag) and tag.name == 'h1':
             self.add_chapter(curchapter)
             curchapter = Chapter(tag, [])
         curchapter.content.append(tag)
     self.add_chapter(curchapter)
Ejemplo n.º 4
0
    def test_qqtag_prev_next(self):
        q = QqTag('a', [
            QqTag('b', 'hello'),
            QqTag('c', 'world'),
            QqTag('b', 'this'),
            QqTag('--+-', [QqTag('b', 'way'), "this"])
        ])

        self.assertEqual(q._c.prev().value, 'hello')
        self.assertEqual(q._b.next().value, 'world')
        self.assertEqual(q._c.next().value, 'this')
Ejemplo n.º 5
0
    def test_qqtag_backlinks(self):
        q = QqTag('a', [
            QqTag('b', 'hello'),
            QqTag('c', 'world'),
            QqTag('b', 'this'),
            QqTag('--+-', [QqTag('b', 'way'), "this"])
        ])
        self.assertTrue(q._is_consistent())
        new_tag = QqTag({'qqq': 'bbb'})
        q.append_child(new_tag)
        self.assertEqual(new_tag.my_index, 4)
        del q[0]
        self.assertEqual(new_tag.my_index, 3)
        self.assertTrue(q._is_consistent())

        other_tag = QqTag({'other': ['some', 'values']})
        q.insert(2, other_tag)
        self.assertEqual(other_tag.my_index, 2)
        self.assertEqual(new_tag.my_index, 4)

        third_tag = QqTag({'this': 'hi'})
        q[3] = third_tag
        self.assertEqual(third_tag.my_index, 3)
        self.assertTrue(q._is_consistent())
Ejemplo n.º 6
0
    def test_qqtag_accessors(self):
        q = QqTag('a', [
            QqTag('b', 'hello'),
            QqTag('c', 'world'),
            QqTag('b', 'this'),
            QqTag('--+-', [QqTag('b', 'way'), "this"])
        ])

        self.assertEqual(q._b.value, 'hello')
        self.assertEqual(q._c.value, 'world')
        self.assertEqual([b.as_list() for b in q('b')],
                         [['b', 'hello'], ['b', 'this']])
        self.assertEqual(q.find('--+-')._b.value, 'way')
        self.assertEqual(q[0].value, 'hello')
        self.assertEqual(q[1].value, 'world')
        self.assertEqual(q[3][0].value, 'way')
Ejemplo n.º 7
0
    def test_create_qqtag(self):
        q = QqTag({'a': 'b'})
        self.assertEqual(q.name, 'a')
        self.assertEqual(q.value, 'b')

        q = QqTag('a', [
            QqTag('b', 'hello'),
            QqTag('c', 'world'),
            QqTag('b', 'this'),
            QqTag('--+-', [QqTag('b', 'way'), "this"])
        ])

        self.assertEqual(q.name, 'a')
        #self.assertEqual(q._children, IndexedList([QqTag('b', ['hello']), QqTag('c', ['world']), QqTag('b', ['this']),
        #                                           QqTag('--+-', [QqTag('b', ['way']), 'this'])]))
        #self.assertEqual(eval(repr(q)), q)
        self.assertEqual(q.as_list(), [
            'a', ['b', 'hello'], ['c', 'world'], ['b', 'this'],
            ['--+-', ['b', 'way'], 'this']
        ])
Ejemplo n.º 8
0
    def handle_quiz(self, tag: QqTag):
        """
        Uses tags: choice, correct, comment

        Example:

        \question
        Do you like qqmbr?
        \quiz
            \choice
                No.
                \comment You didn't even try!
            \choice \correct
                Yes, i like it very much!
                \comment And so do I!

        :param tag:
        :return:
        """
        if not tag.exists('md5id'):
            tag.append_child(QqTag('md5id', [self.tag_hash_id(tag)]))
        template = Template(filename="templates/quiz.html")
        return template.render(formatter=self, tag=tag)