How to use the mwparserfromhell.wikicode.Wikicode function in mwparserfromhell

To help you get started, we’ve selected a few mwparserfromhell examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github earwig / mwparserfromhell / tests / test_wikicode.py View on Github external
func = partial(meth, code3)
        obj = code3.get(0).params[0].value.get(0)
        self.assertRaises(ValueError, func, obj, "x", recursive=False)
        func(obj, "x", recursive=True)
        self.assertRaises(ValueError, func, "{{f}}", "y", recursive=False)
        func("{{f}}", "y", recursive=True)
        self.assertEqual(expected[2], code3)

        code4 = parse("{{a}}{{b}}{{c}}{{d}}{{e}}{{f}}{{g}}{{h}}{{i}}{{j}}")
        func = partial(meth, code4)
        fake = parse("{{b}}{{c}}")
        self.assertRaises(ValueError, func, fake, "q", recursive=False)
        self.assertRaises(ValueError, func, fake, "q", recursive=True)
        func("{{b}}{{c}}", "w", recursive=False)
        func("{{d}}{{e}}", "x", recursive=True)
        func(Wikicode(code4.nodes[-2:]), "y", recursive=False)
        func(Wikicode(code4.nodes[-2:]), "z", recursive=True)
        self.assertEqual(expected[3], code4)
        self.assertRaises(ValueError, func, "{{c}}{{d}}", "q", recursive=False)
        self.assertRaises(ValueError, func, "{{c}}{{d}}", "q", recursive=True)

        code5 = parse("{{a|{{b}}{{c}}|{{f|{{g}}={{h}}{{i}}}}}}")
        func = partial(meth, code5)
        self.assertRaises(ValueError, func, "{{b}}{{c}}", "x", recursive=False)
        func("{{b}}{{c}}", "x", recursive=True)
        obj = code5.get(0).params[1].value.get(0).params[0].value
        self.assertRaises(ValueError, func, obj, "y", recursive=False)
        func(obj, "y", recursive=True)
        self.assertEqual(expected[4], code5)

        code6 = parse("here is {{some text and a {{template}}}}")
        func = partial(meth, code6)
github earwig / mwparserfromhell / tests / _test_tree_equality.py View on Github external
def assertWikicodeEqual(self, expected, actual):
        """Assert that two Wikicode objects have the same data."""
        self.assertIsInstance(actual, Wikicode)
        length = len(expected.nodes)
        self.assertEqual(length, len(actual.nodes))
        for i in range(length):
            self.assertNodeEqual(expected.get(i), actual.get(i))
github lahwaacz / wiki-scripts / ws / parser_helpers / title.py View on Github external
def parse(self, full_title):
        """
        Splits the title into ``(iwprefix, namespace, pagename, sectionname)``
        parts and canonicalizes them. Can be used to set these attributes from
        a string of full title instead of creating new instance.

        :param full_title:
            The full title to be parsed, either a :py:obj:`str` or
            :py:class:`mwparserfromhell.wikicode.Wikicode` object.
        :raises:
            :py:exc:`InvalidTitleCharError` when the page title is not valid
        """
        # Wikicode has to be converted to str, but we don't want to convert
        # numbers or any arbitrary objects.
        if not isinstance(full_title, str) and not isinstance(full_title, mwparserfromhell.wikicode.Wikicode):
            raise TypeError("full_title must be either 'str' or 'Wikicode'")
        full_title = str(full_title)

        if full_title.startswith(":"):
            self._leading_colon = ":"

        def lstrip_one(text, char):
            if text.startswith(char):
                return text.replace(char, "", 1)
            return text

        # parse interwiki prefix
        try:
            iw, _rest = lstrip_one(full_title, ":").split(":", maxsplit=1)
            self._set_iwprefix(iw)
        except ValueError:
github earwig / mwparserfromhell / mwparserfromhell / parser / builder.py View on Github external
def _pop(self):
        """Pop the current node list off of the stack.

        The raw node list is wrapped in a :class:`.SmartList` and then in a
        :class:`.Wikicode` object.
        """
        return Wikicode(SmartList(self._stacks.pop()))
github amyxzhang / wikum / wikum / wikichatter / signatureutils.py View on Github external
def extract_signatures(wcode):
    """
    Returns all signatures found in the text as a list of dictionaries
    [
        {'user':,
         'timestamp':,
         'wikicode':}
    ]
    """
    nodes = wcode.nodes
    signature_list = []
    signature_loc = _find_signatures_in_nodes(nodes)
    for (start, end) in signature_loc:
        sig_code = mwp.wikicode.Wikicode(nodes[start:end + 1])
        signature = _extract_signature_dict_from_sig_code(sig_code)
        signature_list.append(signature)
    return signature_list
github amyxzhang / wikum / wikum / wikichatter / signatureutils.py View on Github external
def _extract_rightmost_timestamp(wcode):
    nodes = wcode.nodes
    ts_loc = _find_timestamp_locations(nodes)
    if len(ts_loc) == 0:
        raise NoTimestampError(text)
    return mwp.wikicode.Wikicode(nodes[ts_loc[-1]])
github amyxzhang / wikum / wikum / wikichatter / signatureutils.py View on Github external
def _divide_wikicode_on_timestamps(wcode):
    nodes = wcode.nodes
    divided_nodes = _divide_nodes_on_timestamp(nodes)
    return [mwp.wikicode.Wikicode(ns) for ns in divided_nodes]
github amyxzhang / wikum / wikum / wikichatter / indentutils.py View on Github external
def _join_wikicode(wikicode_list):
    nodes = []
    for wc in wikicode_list:
        nodes.extend(wc.nodes)
    return mwp.wikicode.Wikicode(nodes)
github earwig / mwparserfromhell / mwparserfromhell / wikicode.py View on Github external
def _do_strong_search(self, obj, recursive=True):
        """Search for the specific element *obj* within the node list.

        *obj* can be either a :class:`.Node` or a :class:`.Wikicode` object. If
        found, we return a tuple (*context*, *index*) where *context* is the
        :class:`.Wikicode` that contains *obj* and *index* is its index there,
        as a :class:`slice`. Note that if *recursive* is ``False``, *context*
        will always be ``self`` (since we only look for *obj* among immediate
        descendants), but if *recursive* is ``True``, then it could be any
        :class:`.Wikicode` contained by a node within ``self``. If *obj* is not
        found, :exc:`ValueError` is raised.
        """
        if isinstance(obj, Wikicode):
            if not self._is_child_wikicode(obj, recursive):
                raise ValueError(obj)
            return obj, slice(0, len(obj.nodes))

        if isinstance(obj, Node):
            mkslice = lambda i: slice(i, i + 1)
            if not recursive:
                return self, mkslice(self.index(obj))
            for node in self.nodes:
                for context, child in self._get_children(node, contexts=True):
                    if obj is child:
                        if not context:
                            context = self
                        return context, mkslice(context.index(child))
            raise ValueError(obj)
github amyxzhang / wikum / wikum / wikichatter / indentutils.py View on Github external
def _split_wikicode_on_endlines(wikicode):
    divided = []
    cur = []
    for node in wikicode.nodes:
        if type(node) is mwp.nodes.text.Text:
            split_nodes = _split_text_node_on_endline(node)
            for sn in split_nodes:
                cur.append(sn)
                if "\n" in sn.value:
                    divided.append(mwp.wikicode.Wikicode(cur))
                    cur = []
        else:
            cur.append(node)
    if len(cur) > 0:
        divided.append(mwp.wikicode.Wikicode(cur))
    return divided