Esempio n. 1
0
    def check_state_header(self, line):
        """ Keep the state__location current """
        heading_match = re.search(
            r'^(?P<tag>#+).*name="(?P<name>[^"]+)".*',
            line)
        if not heading_match:
            return

        data = heading_match.groupdict()
        depth = len(data['tag'])
        # Should almost always exist because of repl_header.
        name = data.get('name') or ''
        data.update(depth=depth, name=name)
        if not self.state__location:
            self.state__location = [data]
            return

        shallower, same_or_deeper = pyaux.split_list(
            self.state__location, lambda info: info['depth'] < depth)
        self.state__location = shallower + [data]
Esempio n. 2
0
    def process_list(self, line):
        # any line should match
        match = re.search(
            r'^(?P<spaces> *)(?:(?P<num>[0-9a-z.]+)\. )?(?P<text>.*)$',
            line)
        data = match.groupdict()
        spaces = data['spaces']
        indent = len(spaces)
        num = data.get('num')
        text = data['text']
        # Synopsis: markdown considers everything within html tags to
        # be written as-is. Therefore, process all htat stuff
        # explicitly.
        # TODO: this all should've probably been done as markdown
        # extender subclass.
        text = _markdown_process(text)
        text = re.sub(r'^ *<p>(.*)</p> *$', r'\1', text)

        if not num:
            # put as-is
            # e.g.: empty lines
            self.result.append(line)
            return

        item_header = '<li value="%s">' % (num,)

        anchor_name = ''.join(
            "%s__" % (info['name'],) for info in self.state__location)
        anchor_name = anchor_name + num.rstrip('.').replace('.', '_')
        item_header = item_header + '<a name="%s"></a>' % (anchor_name,)
        item_footer = self.item_footer
        item_info = dict(data, indent=indent)

        # else:  if num:
        if not self.state__list:
            # starting a list
            self.result.extend((
                self.list_header,  # <ol>
                spaces + item_header,  # <li>
                spaces + text))
            self.state__list = [item_info]
            return

        # else: if within a list already:
        last_info = self.state__list[-1]
        if last_info['indent'] == indent:
            # same indent, i.e. continuing the list
            self.result.extend((
                spaces + item_footer,  # </li>
                spaces + item_header,  # <li>
                spaces + text))
            last_info.update(item_info)  # replace the num for possible recursion
        elif 0 < indent - last_info['indent'] <= 2:
            # going deeper
            # ol-li-ol-li chain
            self.result.extend((
                spaces + self.list_header,  # <ol>
                spaces + item_header,  # <li>
                spaces + text))
            self.state__list.append(item_info)
        elif indent - last_info['indent'] < 0:
            # returning
            state_closing, state_remain = pyaux.split_list(
                self.state__list, lambda info: info['indent'] > indent)
            self.unwind_list(state_closing)  # </li></ol>
            self.state__list = state_remain
            self.result.extend((
                spaces + item_footer,  # </li>
                spaces + item_header,  # <li>
                spaces + text))
        else:  # more than 2 spaces deeper in; assume it's just more text
            self.result.append(line)