Esempio n. 1
0
    def macro(self):
        return ET.XML("""
<form xmlns="%s" method="get" action="%s/%s">
    <input type="hidden" name="do" value="goto" />
    <p>
        <input type="text" name="target" size="30" />
        <input type="submit" value="%s" />
    </p>
</form>
""" % (html, self.request.getScriptname(), self.page_name,
        _("Go To Item")))  #HHH ?
Esempio n. 2
0
    def visit_moinpage_note(self, elem):
        # TODO: Check note-class
        top = self._special_stack[-1]
        if len(elem) == 0:
            # explicit footnote placement:  show prior footnotes, empty stack, reset counter
            if len(top._footnotes) == 0:
                return

            footnotes_div = self.create_footnotes(top)
            top.remove_footnotes()
            self._id.zero_id('note')
            # bump note-placement counter to insure unique footnote ids
            self._id.gen_id('note-placement')
            return footnotes_div

        body = None
        for child in elem:
            if child.tag.uri == moin_page:
                if child.tag.name == 'note-body':
                    body = self.do_children(child)

        id = self._id.gen_id('note')
        prefixed_id = '%s-%s' % (self._id.get_id('note-placement'), id)

        elem_ref = ET.XML("""
<html:sup xmlns:html="{0}" html:id="note-{1}-ref" html:class="moin-footnote">
<html:a html:href="#note-{2}">{3}</html:a>
</html:sup>
""".format(html, prefixed_id, prefixed_id, id))

        elem_note = ET.XML("""
<html:p xmlns:html="{0}" html:id="note-{1}">
<html:sup><html:a html:href="#note-{2}-ref">{3}</html:a></html:sup>
</html:p>
""".format(html, prefixed_id, prefixed_id, id))

        elem_note.extend(body)
        top.add_footnote(elem_note)

        return elem_ref
Esempio n. 3
0
    def visit_moinpage_note(self, elem):
        # TODO: Check note-class

        body = None
        for child in elem:
            if child.tag.uri == moin_page:
                if child.tag.name == 'note-body':
                    body = self.do_children(child)

        id = self._id.gen_id('note')

        elem_ref = ET.XML("""
<html:sup xmlns:html="%s" html:id="note-%d-ref" html:class="moin-footnote"><html:a html:href="#note-%d">%d</html:a></html:sup>
""" % (html, id, id, id))

        elem_note = ET.XML("""
<html:p xmlns:html="%s" html:id="note-%d"><html:sup><html:a html:href="#note-%d-ref">%d</html:a></html:sup></html:p>
""" % (html, id, id, id))

        elem_note.extend(body)
        self._special_stack[-1].add_footnote(elem_note)

        return elem_ref
Esempio n. 4
0
def test_converter_refs(tree_xml, links_expected, transclusions_expected, external_expected):
    converter = ConverterItemRefs()
    tree = ET.XML(tree_xml)

    converter(tree)
    links_result = converter.get_links()
    transclusions_result = converter.get_transclusions()
    external_result = converter.get_external_links()

    # sorting instead of sets
    # so that we avoid deduplicating duplicated items in the result
    assert sorted(links_result) == sorted(links_expected)
    assert sorted(transclusions_result) == sorted(transclusions_expected)
    assert sorted(external_result) == sorted(external_expected)
Esempio n. 5
0
    def __call__(self, content, aruments=None):
        """
        Function called by the converter to process
        the conversion.
        """
        # Initalize our attributes
        self.section_depth = 0
        self.heading_level = 0
        self.is_section = False

        # We store the standard attributes of an element.
        # Once we have been able to put it into an output element,
        # we clear this attribute.
        self.standard_attribute = {}

        # We will create an element tree from the DocBook content
        # The content is given to the converter as a list of string,
        # line per line.
        # So we will concatenate all in one string.
        docbook_str = u'\n'.join(content)
        logging.debug(docbook_str)
        try:
            # XXX: The XML parser need bytestring.
            tree = ET.XML(docbook_str.encode('utf-8'))
        except ET.ParseError as detail:
            return self.error(str(detail))

        try:
            if tree.tag.name in self.block_tags:
                return self.start_dom_tree(tree, 0)
            else:
                # XXX: Internationalization
                return self.error("The root element of the docbook document is not supported by the converter")
        # XXX: Error handling could probably be better.
        except NameSpaceError as detail:
            return self.error(str(detail))
        return result
Esempio n. 6
0
 def handle_input(self, input):
     i = self.input_re.sub(r'\1 ' + self.input_namespaces, input)
     return ET.XML(i.encode("utf-8"))
Esempio n. 7
0
 def test_unknown(self):
     page = ET.XML("<page:unknown {0}/>".format(self.input_namespaces))
     pytest.raises(ElementException, self.conv.__call__, page)
Esempio n. 8
0
def run_test(tree_xml, output):
    tree = ET.XML(tree_xml)
    converter = ConverterPage()
    tree = converter(tree)
    assert serialize(tree, method="html") == output
Esempio n. 9
0
    def recurse(self, elem, page_href):
        # Check if we reached a new page
        page_href_new = elem.get(self.tag_page_href)
        if page_href_new:
            page_href_new = Iri(page_href_new)
            if page_href_new != page_href:
                page_href = page_href_new
                self.stack.append(page_href)
            else:
                self.stack.append(None)
        else:
            self.stack.append(None)

        try:
            if elem.tag == self.tag_xi_include:
                href = elem.get(self.tag_xi_href)
                xpointer = elem.get(self.tag_xi_xpointer)

                xp_include_pages = None
                xp_include_sort = None
                xp_include_items = None
                xp_include_skipitems = None
                xp_include_heading = None
                xp_include_level = None

                if xpointer:
                    xp = XPointer(xpointer)
                    xp_include = None
                    xp_namespaces = {}
                    for entry in xp:
                        uri = None
                        name = entry.name.split(':', 1)
                        if len(name) > 1:
                            prefix, name = name
                            uri = xp_namespaces.get(prefix, False)
                        else:
                            name = name[0]

                        if uri is None and name == 'xmlns':
                            d_prefix, d_uri = entry.data.split('=', 1)
                            xp_namespaces[d_prefix] = d_uri
                        elif uri == moin_page.namespace and name == 'include':
                            xp_include = XPointer(entry.data)

                    if xp_include:
                        for entry in xp_include:
                            name, data = entry.name, entry.data
                            if name == 'pages':
                                xp_include_pages = data
                            elif name == 'sort':
                                xp_include_sort = data
                            elif name == 'items':
                                xp_include_items = int(data)
                            elif name == 'skipitems':
                                xp_include_skipitems = int(data)
                            elif name == 'heading':
                                xp_include_heading = data
                            elif name == 'level':
                                xp_include_level = data

                if href:
                    # We have a single page to include
                    href = Iri(href)
                    link = Iri(scheme='wiki', authority='')
                    if href.scheme == 'wiki':
                        if href.authority:
                            raise ValueError(
                                "can't handle xinclude for non-local authority"
                            )
                        else:
                            path = href.path[1:]
                    elif href.scheme == 'wiki.local':
                        page = page_href
                        path = href.path
                        if path[0] == '':
                            # /subitem
                            tmp = page.path[1:]
                            tmp.extend(path[1:])
                            path = tmp
                        elif path[0] == '..':
                            # ../sisteritem
                            path = page.path[1:] + path[1:]
                    else:
                        raise ValueError(
                            "can't handle xinclude for schemes other than wiki or wiki.local"
                        )

                    link.path = path

                    page = Item.create(unicode(path))
                    pages = ((page, link), )

                elif xp_include_pages:
                    # We have a regex of pages to include
                    from MoinMoin.search.term import NameFn
                    inc_match = re.compile(xp_include_pages)
                    root_item = Item(name=u'')
                    pagelist = [
                        item.name
                        for item in root_item.list_items(NameFn(inc_match))
                    ]
                    pagelist.sort()
                    if xp_include_sort == 'descending':
                        pagelist.reverse()
                    if xp_include_skipitems is not None:
                        pagelist = pagelist[xp_include_skipitems:]
                    if xp_include_items is not None:
                        pagelist = pagelist[xp_include_items + 1:]

                    pages = ((Item.create(p),
                              Iri(scheme='wiki', authority='', path='/' + p))
                             for p in pagelist)

                included_elements = []
                for page, page_href in pages:
                    if page_href in self.stack:
                        w = (
                            '<p xmlns="%s"><strong class="error">Recursive include of "%s" forbidden</strong></p>'
                            % (html.namespace, page.name))
                        div.append(ET.XML(w))
                        continue
                    # TODO: Is this correct?
                    if not flaskg.user.may.read(page.name):
                        continue

                    if xp_include_heading is not None:
                        attrib = {self.tag_href: page_href}
                        children = (xp_include_heading or page.name, )
                        elem_a = ET.Element(self.tag_a,
                                            attrib,
                                            children=children)
                        attrib = {
                            self.tag_outline_level: xp_include_level or '1'
                        }
                        elem_h = ET.Element(self.tag_h,
                                            attrib,
                                            children=(elem_a, ))
                        div.append(elem_h)

                    page_doc = page.internal_representation()
                    # page_doc.tag = self.tag_div # XXX why did we have this?
                    self.recurse(page_doc, page_href)
                    # Wrap the page with the overlay, but only if it's a "page", or "a".
                    # The href needs to be an absolute URI, without the prefix "wiki://"
                    if page_doc.tag.endswith("page") or page_doc.tag.endswith(
                            "a"):
                        page_doc = wrap_object_with_overlay(
                            page_doc, href=unicode(page_href.path))

                    included_elements.append(page_doc)

                if len(included_elements) > 1:
                    # use a div as container
                    result = ET.Element(self.tag_div)
                    result.extend(included_elements)
                elif included_elements:
                    result = included_elements[0]
                else:
                    result = None

                return result

            for i in xrange(len(elem)):
                child = elem[i]
                if isinstance(child, ET.Node):
                    ret = self.recurse(child, page_href)
                    if ret:
                        elem[i] = ret
        finally:
            self.stack.pop()