Пример #1
0
 def __init__(self, textile, tatts, rows, summary):
     self.textile = textile
     self.attributes = parse_attributes(tatts, 'table')
     if summary:
         self.attributes.update(summary=summary.strip())
     self.input = rows
     self.caption = ''
     self.colgroup = ''
     self.content = []
Пример #2
0
    def process(self):
        enc = 'unicode'
        if six.PY2: # pragma: no branch
            enc = 'UTF-8'

        group_atts = parse_attributes(self.attributes, 'col')
        colgroup = ElementTree.Element('colgroup', attrib=group_atts)
        colgroup.text = '\n\t'
        if self.cols is not None:
            has_newline = "\n" in self.cols
            match_cols = self.cols.replace('.', '').split('|')
            # colgroup is the first item in match_cols, the remaining items are
            # cols.
            for idx, col in enumerate(match_cols):
                col_atts = parse_attributes(col.strip(), 'col')
                ElementTree.SubElement(colgroup, 'col', col_atts)
        colgrp = ElementTree.tostring(colgroup, encoding=enc)
        # cleanup the extra xml declaration if it exists, (python versions
        # differ) and then format the resulting string accordingly: newline and
        # tab between cols and a newline at the end
        xml_declaration = "<?xml version='1.0' encoding='UTF-8'?>\n"
        colgrp = colgrp.replace(xml_declaration, '')
        return colgrp.replace('><', '>\n\t<')
Пример #3
0
    def __init__(self, textile, tag, atts, ext, cite, content):
        self.textile = textile
        self.tag = tag
        self.atts = atts
        self.ext = ext
        self.cite = cite
        self.content = content

        self.attributes = parse_attributes(atts)
        self.outer_tag = ''
        self.inner_tag = ''
        self.outer_atts = OrderedDict()
        self.inner_atts = OrderedDict()
        self.eat = False
        self.process()
Пример #4
0
 def __init__(self, capts, cap, row):
     self.attributes = parse_attributes(capts)
     self.caption = self.process(cap)
Пример #5
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 = ''
Пример #6
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)
Пример #7
0
 def __init__(self, tag, attributes):
     self.tag = tag
     self.attributes = parse_attributes(attributes)
     self.rows = []
Пример #8
0
 def __init__(self, tag, attributes, restricted):
     self.tag = tag
     self.attributes = parse_attributes(attributes, restricted=restricted)
     self.rows = []
Пример #9
0
 def __init__(self, capts, cap, row, restricted):
     self.attributes = parse_attributes(capts, restricted=restricted)
     self.caption = self.process(cap)
Пример #10
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)
Пример #11
0
 def __init__(self, tag, attributes):
     self.tag = tag
     self.attributes = parse_attributes(attributes)
     self.rows = []
Пример #12
0
 def __init__(self, capts, cap, row):
     self.attributes = parse_attributes(capts)
     self.caption = self.process(cap)
Пример #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)
Пример #14
0
def test_parse_attributes():
    assert parse_attributes('\\1', element='td') == {'colspan': '1'}
    assert parse_attributes('/1', element='td') == {'rowspan': '1'}
    assert parse_attributes('^', element='td') == {
        'style': 'vertical-align:top;'
    }
    assert parse_attributes('{color: blue}') == {'style': 'color: blue;'}
    assert parse_attributes('[en]') == {'lang': 'en'}
    assert parse_attributes('(cssclass)') == {'class': 'cssclass'}
    assert parse_attributes('(') == {'style': 'padding-left:1em;'}
    assert parse_attributes(')') == {'style': 'padding-right:1em;'}
    assert parse_attributes('<') == {'style': 'text-align:left;'}
    assert parse_attributes('(c#i)') == {'class': 'c', 'id': 'i'}
    assert parse_attributes('\\2 100', element='col') == {
        'span': '2',
        'width': '100'
    }
Пример #15
0
def test_parse_attributes():
    assert parse_attributes('\\1', element='td') == {'colspan': '1'}
    assert parse_attributes('/1', element='td') == {'rowspan': '1'}
    assert parse_attributes('^', element='td') == {'style': 'vertical-align:top;'}
    assert parse_attributes('{color: blue}') == {'style': 'color: blue;'}
    assert parse_attributes('[en]') == {'lang': 'en'}
    assert parse_attributes('(cssclass)') == {'class': 'cssclass'}
    assert parse_attributes('(') == {'style': 'padding-left:1em;'}
    assert parse_attributes(')') == {'style': 'padding-right:1em;'}
    assert parse_attributes('<') == {'style': 'text-align:left;'}
    assert parse_attributes('(c#i)') == {'class': 'c', 'id': 'i'}
    assert parse_attributes('\\2 100', element='col') == {'span': '2', 'width': '100'}