Beispiel #1
0
def test_generate_tag():
    result = utils.generate_tag('span', 'inner text', {'class': 'test'})
    expect = '<span class="test">inner text</span>'
    assert result == expect

    text = 'Übermensch'
    attributes = {'href': 'http://de.wikipedia.org/wiki/%C3%C9bermensch'}
    expect = '<a href="http://de.wikipedia.org/wiki/%C3%C9bermensch">Übermensch</a>'
    result = utils.generate_tag('a', text, attributes)
    assert result == expect
Beispiel #2
0
def test_generate_tag():
    result = utils.generate_tag('span', 'inner text', {'class': 'test'})
    expect = '<span class="test">inner text</span>'
    assert result == expect

    text = 'Übermensch'
    attributes = {'href': 'http://de.wikipedia.org/wiki/%C3%C9bermensch'}
    expect = '<a href="http://de.wikipedia.org/wiki/%C3%C9bermensch">Übermensch</a>'
    result = utils.generate_tag('a', text, attributes)
    assert result == expect
Beispiel #3
0
 def process(self):
     output = []
     for c in self.cells:
         output.append(c)
     cell_data = '{0}\n\t\t'.format(''.join(output))
     tag = generate_tag('tr', cell_data, self.attributes)
     return '\n\t\t{0}'.format(tag)
Beispiel #4
0
 def process(self):
     output = []
     for c in self.cells:
         output.append(c)
     cell_data = "{0}\n\t\t".format("".join(output))
     tag = generate_tag("tr", cell_data, self.attributes)
     return "\n\t\t{0}".format(tag)
Beispiel #5
0
 def process(self):
     output = []
     for c in self.cells:
         output.append(c)
     cell_data = '{0}\n\t\t'.format(''.join(output))
     tag = generate_tag('tr', cell_data, self.attributes)
     return '\n\t\t{0}'.format(tag)
Beispiel #6
0
 def process(self):
     return generate_tag(self.tag, '{0}\n\t'.format(''.join(self.rows)),
                         self.attributes)
Beispiel #7
0
 def process(self):
     return generate_tag(self.tag, self.content, self.attributes)
Beispiel #8
0
 def process(self, cap):
     tag = generate_tag('caption', cap, self.attributes)
     return '\t{0}\n\t'.format(tag)
Beispiel #9
0
    def process(self):
        rgrp = None
        groups = []
        if self.input[-1] == '|': # pragma: no branch
            self.input = '{0}\n'.format(self.input)
        split = self.input.split('|\n')
        for i, row in enumerate([x for x in split if x]):
            row = row.lstrip()

            # Caption -- only occurs on row 1, otherwise treat '|=. foo |...'
            # as a normal center-aligned cell.
            if i == 0 and row[:2] == '|=':
                captionpattern = (r"^\|\=(?P<capts>{s}{a}{c})\. "
                                  r"(?P<cap>[^\n]*)(?P<row>.*)".format(**{
                                      's': table_span_re_s, 'a': align_re_s,
                                      'c': cls_re_s}))
                caption_re = re.compile(captionpattern, re.S)
                cmtch = caption_re.match(row)
                if cmtch:
                    caption = Caption(**cmtch.groupdict())
                    self.caption = '\n{0}'.format(caption.caption)
                    row = cmtch.group('row').lstrip()
                    if row == '':
                        continue

            # Colgroup -- A colgroup row will not necessarily end with a |.
            # Hence it may include the next row of actual table data.
            if row[:2] == '|:':
                if '\n' in row:
                    colgroup_data, row = row[2:].split('\n')
                else:
                    colgroup_data, row = row[2:], ''
                colgroup_atts, cols = colgroup_data, None
                if '|' in colgroup_data:
                    colgroup_atts, cols = colgroup_data.split('|', 1)
                colgrp = Colgroup(cols, colgroup_atts)
                self.colgroup = colgrp.process()
                if row == '':
                    continue

            # search the row for a table group - thead, tfoot, or tbody
            grpmatchpattern = (r"(:?^\|(?P<part>{v})(?P<rgrpatts>{s}{a}{c})"
                    r"\.\s*$\n)?^(?P<row>.*)").format(**{'v': valign_re_s, 's':
                        table_span_re_s, 'a': align_re_s, 'c': cls_re_s})
            grpmatch_re = re.compile(grpmatchpattern, re.S | re.M)
            grpmatch = grpmatch_re.match(row.lstrip())

            grptypes = {'^': Thead, '~': Tfoot, '-': Tbody}
            if grpmatch.group('part'):
                # we're about to start a new group, so process the current one
                # and add it to the output
                if rgrp:
                    groups.append('\n\t{0}'.format(rgrp.process()))
                rgrp = grptypes[grpmatch.group('part')](grpmatch.group(
                    'rgrpatts'))
            row = grpmatch.group('row')

            rmtch = re.search(r'^(?P<ratts>{0}{1}\. )(?P<row>.*)'.format(
                align_re_s, cls_re_s), row.lstrip())
            if rmtch:
                row_atts = parse_attributes(rmtch.group('ratts'), 'tr')
                row = rmtch.group('row')
            else:
                row_atts = {}

            # create a row to hold the cells.
            r = Row(row_atts, row)
            for cellctr, cell in enumerate(row.split('|')[1:]):
                ctag = 'td'
                if cell.startswith('_'):
                    ctag = 'th'

                cmtch = re.search(r'^(?P<catts>_?{0}{1}{2}\. )'
                        '(?P<cell>.*)'.format(table_span_re_s, align_re_s,
                            cls_re_s), cell, flags=re.S)
                if cmtch:
                    catts = cmtch.group('catts')
                    cell_atts = parse_attributes(catts, 'td')
                    cell = cmtch.group('cell')
                else:
                    cell_atts = {}

                if not self.textile.lite:
                    a_pattern = r'(?P<space>{0}*)(?P<cell>.*)'.format(
                            regex_snippets['space'])
                    a = re.search(a_pattern, cell, flags=re.S)
                    cell = self.textile.redcloth_list(a.group('cell'))
                    cell = self.textile.textileLists(cell)
                    cell = '{0}{1}'.format(a.group('space'), cell)

                # create a cell
                c = Cell(ctag, cell, cell_atts)
                cline_tag = '\n\t\t\t{0}'.format(c.process())
                # add the cell to the row
                r.cells.append(self.textile.doTagBr(ctag, cline_tag))

            # if we're in a group, add it to the group's rows, else add it
            # directly to the content
            if rgrp:
                rgrp.rows.append(r.process())
            else:
                self.content.append(r.process())

        # if there's still an rgrp, process it and add it to the output
        if rgrp:
            groups.append('\n\t{0}'.format(rgrp.process()))

        content = '{0}{1}{2}{3}\n\t'.format(self.caption, self.colgroup,
                ''.join(groups), ''.join(self.content))
        tbl = generate_tag('table', content, self.attributes)
        return '\t{0}\n\n'.format(tbl)
Beispiel #10
0
 def process(self):
     return generate_tag(self.tag, '{0}\n\t'.format(''.join(self.rows)), self.attributes)
Beispiel #11
0
 def process(self):
     return generate_tag(self.tag, self.content, self.attributes)
Beispiel #12
0
 def process(self, cap):
     tag = generate_tag('caption', cap, self.attributes)
     return '\t{0}\n\t'.format(tag)
Beispiel #13
0
    def process(self):
        rgrp = None
        groups = []
        if self.input[-1] == "|":  # pragma: no branch
            self.input = "{0}\n".format(self.input)
        split = self.input.split("|\n")
        for i, row in enumerate([x for x in split if x]):
            row = row.lstrip()

            # Caption -- only occurs on row 1, otherwise treat '|=. foo |...'
            # as a normal center-aligned cell.
            if i == 0 and row[:2] == "|=":
                captionpattern = r"^\|\=(?P<capts>{s}{a}{c})\. " r"(?P<cap>[^\n]*)(?P<row>.*)".format(
                    **{"s": table_span_re_s, "a": align_re_s, "c": cls_re_s}
                )
                caption_re = re.compile(captionpattern, re.S)
                cmtch = caption_re.match(row)
                caption = Caption(**cmtch.groupdict())
                self.caption = "\n{0}".format(caption.caption)
                row = cmtch.group("row").lstrip()
                if row == "":
                    continue

            # Colgroup -- A colgroup row will not necessarily end with a |.
            # Hence it may include the next row of actual table data.
            if row[:2] == "|:":
                if "\n" in row:
                    colgroup_data, row = row[2:].split("\n")
                else:
                    colgroup_data, row = row[2:], ""
                colgroup_atts, cols = colgroup_data, None
                if "|" in colgroup_data:
                    colgroup_atts, cols = colgroup_data.split("|", 1)
                colgrp = Colgroup(cols, colgroup_atts)
                self.colgroup = colgrp.process()
                if row == "":
                    continue

            # search the row for a table group - thead, tfoot, or tbody
            grpmatchpattern = (r"(:?^\|(?P<part>{v})(?P<rgrpatts>{s}{a}{c})" r"\.\s*$\n)?^(?P<row>.*)").format(
                **{"v": valign_re_s, "s": table_span_re_s, "a": align_re_s, "c": cls_re_s}
            )
            grpmatch_re = re.compile(grpmatchpattern, re.S | re.M)
            grpmatch = grpmatch_re.match(row.lstrip())

            grptypes = {"^": Thead, "~": Tfoot, "-": Tbody}
            if grpmatch.group("part"):
                # we're about to start a new group, so process the current one
                # and add it to the output
                if rgrp:
                    groups.append("\n\t{0}".format(rgrp.process()))
                rgrp = grptypes[grpmatch.group("part")](grpmatch.group("rgrpatts"))
            row = grpmatch.group("row")

            rmtch = re.search(r"^(?P<ratts>{0}{1}\. )(?P<row>.*)".format(align_re_s, cls_re_s), row.lstrip())
            if rmtch:
                row_atts = parse_attributes(rmtch.group("ratts"), "tr")
                row = rmtch.group("row")
            else:
                row_atts = {}

            # create a row to hold the cells.
            r = Row(row_atts, row)
            for cellctr, cell in enumerate(row.split("|")[1:]):
                ctag = "td"
                if cell.startswith("_"):
                    ctag = "th"

                cmtch = re.search(
                    r"^(?P<catts>_?{0}{1}{2}\. )" "(?P<cell>.*)".format(table_span_re_s, align_re_s, cls_re_s),
                    cell,
                    flags=re.S,
                )
                if cmtch:
                    catts = cmtch.group("catts")
                    cell_atts = parse_attributes(catts, "td")
                    cell = cmtch.group("cell")
                else:
                    cell_atts = {}

                if not self.textile.lite:
                    a_pattern = r"(?P<space>{0}*)(?P<cell>.*)".format(regex_snippets["space"])
                    a = re.search(a_pattern, cell, flags=re.S)
                    cell = self.textile.redcloth_list(a.group("cell"))
                    cell = self.textile.textileLists(cell)
                    cell = "{0}{1}".format(a.group("space"), cell)

                # create a cell
                c = Cell(ctag, cell, cell_atts)
                cline_tag = "\n\t\t\t{0}".format(c.process())
                # add the cell to the row
                r.cells.append(self.textile.doTagBr(ctag, cline_tag))

            # if we're in a group, add it to the group's rows, else add it
            # directly to the content
            if rgrp:
                rgrp.rows.append(r.process())
            else:
                self.content.append(r.process())

        # if there's still an rgrp, process it and add it to the output
        if rgrp:
            groups.append("\n\t{0}".format(rgrp.process()))

        content = "{0}{1}{2}{3}\n\t".format(self.caption, self.colgroup, "".join(groups), "".join(self.content))
        tbl = generate_tag("table", content, self.attributes)
        return "\t{0}\n\n".format(tbl)
Beispiel #14
0
 def process(self):
     return generate_tag(self.tag, "{0}\n\t".format("".join(self.rows)), self.attributes)
Beispiel #15
0
    def process(self):
        rgrp = None
        groups = []
        if self.input[-1] == '|':  # pragma: no branch
            self.input = '{0}\n'.format(self.input)
        split = self.input.split('|\n')
        for i, row in enumerate([x for x in split if x]):
            row = row.lstrip()

            # Caption -- only occurs on row 1, otherwise treat '|=. foo |...'
            # as a normal center-aligned cell.
            if i == 0 and row[:2] == '|=':
                captionpattern = (r"^\|\=(?P<capts>{s}{a}{c})\. "
                                  r"(?P<cap>[^\n]*)(?P<row>.*)".format(
                                      **{
                                          's': table_span_re_s,
                                          'a': align_re_s,
                                          'c': cls_re_s
                                      }))
                caption_re = re.compile(captionpattern, re.S)
                cmtch = caption_re.match(row)
                if cmtch:
                    caption = Caption(**cmtch.groupdict())
                    self.caption = '\n{0}'.format(caption.caption)
                    row = cmtch.group('row').lstrip()
                    if row == '':
                        continue

            # Colgroup -- A colgroup row will not necessarily end with a |.
            # Hence it may include the next row of actual table data.
            if row[:2] == '|:':
                if '\n' in row:
                    colgroup_data, row = row[2:].split('\n')
                else:
                    colgroup_data, row = row[2:], ''
                colgroup_atts, cols = colgroup_data, None
                if '|' in colgroup_data:
                    colgroup_atts, cols = colgroup_data.split('|', 1)
                colgrp = Colgroup(cols, colgroup_atts)
                self.colgroup = colgrp.process()
                if row == '':
                    continue

            # search the row for a table group - thead, tfoot, or tbody
            grpmatchpattern = (r"(:?^\|(?P<part>{v})(?P<rgrpatts>{s}{a}{c})"
                               r"\.\s*$\n)?^(?P<row>.*)").format(
                                   **{
                                       'v': valign_re_s,
                                       's': table_span_re_s,
                                       'a': align_re_s,
                                       'c': cls_re_s
                                   })
            grpmatch_re = re.compile(grpmatchpattern, re.S | re.M)
            grpmatch = grpmatch_re.match(row.lstrip())

            grptypes = {'^': Thead, '~': Tfoot, '-': Tbody}
            if grpmatch.group('part'):
                # we're about to start a new group, so process the current one
                # and add it to the output
                if rgrp:
                    groups.append('\n\t{0}'.format(rgrp.process()))
                rgrp = grptypes[grpmatch.group('part')](
                    grpmatch.group('rgrpatts'))
            row = grpmatch.group('row')

            rmtch = re.search(
                r'^(?P<ratts>{0}{1}\. )(?P<row>.*)'.format(
                    align_re_s, cls_re_s), row.lstrip())
            if rmtch:
                row_atts = parse_attributes(rmtch.group('ratts'), 'tr')
                row = rmtch.group('row')
            else:
                row_atts = {}

            # create a row to hold the cells.
            r = Row(row_atts, row)
            for cellctr, cell in enumerate(row.split('|')[1:]):
                ctag = 'td'
                if cell.startswith('_'):
                    ctag = 'th'

                cmtch = re.search(r'^(?P<catts>_?{0}{1}{2}\. )'
                                  '(?P<cell>.*)'.format(
                                      table_span_re_s, align_re_s, cls_re_s),
                                  cell,
                                  flags=re.S)
                if cmtch:
                    catts = cmtch.group('catts')
                    cell_atts = parse_attributes(catts, 'td')
                    cell = cmtch.group('cell')
                else:
                    cell_atts = {}

                if not self.textile.lite:
                    a_pattern = r'(?P<space>{0}*)(?P<cell>.*)'.format(
                        regex_snippets['space'])
                    a = re.search(a_pattern, cell, flags=re.S)
                    cell = self.textile.redcloth_list(a.group('cell'))
                    cell = self.textile.textileLists(cell)
                    cell = '{0}{1}'.format(a.group('space'), cell)

                # create a cell
                c = Cell(ctag, cell, cell_atts)
                cline_tag = '\n\t\t\t{0}'.format(c.process())
                # add the cell to the row
                r.cells.append(self.textile.doTagBr(ctag, cline_tag))

            # if we're in a group, add it to the group's rows, else add it
            # directly to the content
            if rgrp:
                rgrp.rows.append(r.process())
            else:
                self.content.append(r.process())

        # if there's still an rgrp, process it and add it to the output
        if rgrp:
            groups.append('\n\t{0}'.format(rgrp.process()))

        content = '{0}{1}{2}{3}\n\t'.format(self.caption, self.colgroup,
                                            ''.join(groups),
                                            ''.join(self.content))
        tbl = generate_tag('table', content, self.attributes)
        return '\t{0}\n\n'.format(tbl)
Beispiel #16
0
    def process(self):
        if self.tag == 'p':
            # is this an anonymous block with a note definition?
            notedef_re = re.compile(r"""
            ^note\#                               # start of note def marker
            (?P<label>[^%<*!@\#^([{{ {space}.]+)  # label
            (?P<link>[*!^]?)                      # link
            (?P<att>{cls})                        # att
            \.?                                   # optional period.
            [{space}]+                            # whitespace ends def marker
            (?P<content>.*)$                      # content""".format(
                space=regex_snippets['space'], cls=cls_re_s),
            flags=re.X | re.U)
            notedef = notedef_re.sub(self.textile.fParseNoteDefs, self.content)

            # It will be empty if the regex matched and ate it.
            if '' == notedef:
                self.content = notedef

        fns = re.search(r'fn(?P<fnid>{0}+)'.format(regex_snippets['digit']),
                self.tag, flags=re.U)
        if fns:
            self.tag = 'p'
            fnid = self.textile.fn.get(fns.group('fnid'), None)
            if fnid is None:
                fnid = '{0}{1}'.format(self.textile.linkPrefix,
                        self.textile._increment_link_index())

            # If there is an author-specified ID goes on the wrapper & the
            # auto-id gets pushed to the <sup>
            supp_id = OrderedDict()

            # if class has not been previously specified, set it to "footnote"
            if 'class' not in self.attributes:
                self.attributes.update({'class': 'footnote'})

            # if there's no specified id, use the generated one.
            if 'id' not in self.attributes:
                self.attributes.update({'id': 'fn{0}'.format(fnid)})
            else:
                supp_id = parse_attributes('(#fn{0})'.format(fnid))


            if '^' not in self.atts:
                sup = generate_tag('sup', fns.group('fnid'), supp_id)
            else:
                fnrev = generate_tag('a', fns.group('fnid'), {'href':
                    '#fnrev{0}'.format(fnid)})
                sup = generate_tag('sup', fnrev, supp_id)

            self.content = '{0} {1}'.format(sup, self.content)

        if self.tag == 'bq':
            if self.cite:
                self.cite = self.textile.shelveURL(self.cite)
                cite_att = OrderedDict(cite=self.cite)
                self.cite = ' cite="{0}"'.format(self.cite)
            else:
                self.cite = ''
                cite_att = OrderedDict()
            cite_att.update(self.attributes)
            self.outer_tag = 'blockquote'
            self.outer_atts = cite_att
            self.inner_tag = 'p'
            self.inner_atts = self.attributes
            self.eat = False

        elif self.tag == 'bc' or self.tag == 'pre':
            i_tag = ''
            if self.tag == 'bc':
                i_tag = 'code'
            content = encode_html(self.content)
            if not self.ext:
                content = '{0}\n'.format(content)
            self.content = self.textile.shelve(content)
            self.outer_tag = 'pre'
            self.outer_atts = self.attributes
            self.inner_tag = i_tag
            self.inner_atts = self.attributes
            self.eat = False

        elif self.tag == 'notextile':
            self.content = self.textile.shelve(self.content)

        elif self.tag == '###':
            self.eat = True

        else:
            self.outer_tag = self.tag
            self.outer_atts = self.attributes

        if not self.eat:
            self.content = self.textile.graf(self.content)
        else:
            self.content = ''
    def process(self):
        if self.tag == 'p':
            # is this an anonymous block with a note definition?
            notedef_re = re.compile(r"""
            ^note\#                               # start of note def marker
            (?P<label>[^%<*!@\#^([{{ {space}.]+)  # label
            (?P<link>[*!^]?)                      # link
            (?P<att>{cls})                        # att
            \.?                                   # optional period.
            [{space}]+                            # whitespace ends def marker
            (?P<content>.*)$                      # content""".format(
                space=regex_snippets['space'], cls=cls_re_s),
                                    flags=re.X | re.U)
            notedef = notedef_re.sub(self.textile.fParseNoteDefs, self.content)

            # It will be empty if the regex matched and ate it.
            if '' == notedef:
                self.content = notedef

        fns = re.search(r'fn(?P<fnid>{0}+)'.format(regex_snippets['digit']),
                        self.tag,
                        flags=re.U)
        if fns:
            self.tag = 'p'
            fnid = self.textile.fn.get(fns.group('fnid'), None)
            if fnid is None:
                fnid = '{0}{1}'.format(self.textile.linkPrefix,
                                       self.textile._increment_link_index())

            # If there is an author-specified ID goes on the wrapper & the
            # auto-id gets pushed to the <sup>
            supp_id = OrderedDict()

            # if class has not been previously specified, set it to "footnote"
            if 'class' not in self.attributes:
                self.attributes.update({'class': 'footnote'})

            # if there's no specified id, use the generated one.
            if 'id' not in self.attributes:
                self.attributes.update({'id': 'fn{0}'.format(fnid)})
            else:
                supp_id = parse_attributes('(#fn{0})'.format(fnid))

            if '^' not in self.atts:
                sup = generate_tag('sup', fns.group('fnid'), supp_id)
            else:
                fnrev = generate_tag('a', fns.group('fnid'),
                                     {'href': '#fnrev{0}'.format(fnid)})
                sup = generate_tag('sup', fnrev, supp_id)

            self.content = '{0} {1}'.format(sup, self.content)

        if self.tag == 'bq':
            if self.cite:
                self.cite = self.textile.shelveURL(self.cite)
                cite_att = OrderedDict(cite=self.cite)
                self.cite = ' cite="{0}"'.format(self.cite)
            else:
                self.cite = ''
                cite_att = OrderedDict()
            cite_att.update(self.attributes)
            self.outer_tag = 'blockquote'
            self.outer_atts = cite_att
            self.inner_tag = 'p'
            self.inner_atts = self.attributes
            self.eat = False

        elif self.tag == 'bc' or self.tag == 'pre':
            i_tag = ''
            if self.tag == 'bc':
                i_tag = 'code'
            self.content = self.textile.shelve(
                encode_html('{0}\n'.format(self.content.rstrip("\n"))))
            self.outer_tag = 'pre'
            self.outer_atts = self.attributes
            self.inner_tag = i_tag
            self.inner_atts = self.attributes
            self.eat = False

        elif self.tag == 'notextile':
            self.content = self.textile.shelve(self.content)

        elif self.tag == '###':
            self.eat = True

        else:
            self.outer_tag = self.tag
            self.outer_atts = self.attributes

        if not self.eat:
            self.content = self.textile.graf(self.content)
        else:
            self.content = ''
Beispiel #18
0
 def process(self, cap):
     tag = generate_tag("caption", cap, self.attributes)
     return "\t{0}\n\t".format(tag)