How to use the mwparserfromhell.nodes.text 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 notconfusing / harvest_infobox_book / fix_databaseconstraints.py View on Github external
for section in sections:
    if section[:10] == '== "Format':
        linenum = 0    
        for line in section.split('\n',10000):
            linenum+=1
            print linenum
            if fixcases['prevtouched'] > linenum-1:
                continue
            linecode = mwp.parse(line)
            linebits = linecode.filter()
            qid = ''
            isbn = ''
            for linebit in linebits:
                if isinstance(linebit, mwp.nodes.wikilink.Wikilink):
                    qid = linebit[2:-2]
                if isinstance(linebit, mwp.nodes.text.Text) and linebit != '*':
                    isbn = linebit[1:]
                    print 'qid', qid, ' isbn', isbn
            if qid.startswith('Q'):
                wditem = pywikibot.ItemPage(wikidata, qid)
                cleanedisbn = cleanisbn(isbn)
                if cleanedisbn:
                    if boolvalidate(cleanedisbn):
                        isbn10claim = pywikibot.Claim(site=wikidata, pid='P957')
                        isbn10claim.setTarget(cleanedisbn)
                        wditem.addClaim(isbn10claim)
                    page_parts = wditem.get()
                    claims = page_parts['claims']
                    for claimnum, claimlist in claims.iteritems():
                        if claimnum == 'P212':
                            for claim in claimlist:
                                isbn = claim.target
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
github lahwaacz / wiki-scripts / ws / ArchWiki / header.py View on Github external
def build_header(wikicode, parent, magics, cats, langlinks):
    # first strip blank lines if there is some text
    if len(wikicode.nodes) > 0:
        node = parent.get(0)
        if isinstance(node, mwparserfromhell.nodes.text.Text):
            if node.value:
                firstline = node.value.splitlines(keepends=True)[0]
                while firstline.strip() == "":
                    node.value = node.value.replace(firstline, "", 1)
                    if node.value == "":
                        break
                    firstline = node.value.splitlines(keepends=True)[0]

    count = 0
    # If the parent is not the top-level wikicode object (i.e. nested wikicode inside
    # some node, such as ), starting with newline does not produce the
    # infamous gap in the HTML and the wikicode looks better
    # NOTE: not tested with different nodes than 
    if parent is not wikicode:
        parent.insert(count, "\n")
        count += 1
github amyxzhang / wikum / wikum / wikichatter / indentutils.py View on Github external
def _split_text_node_on_endline(text_node):
    text = text_node.value
    lines = _split_text_and_leave_delimiter(text, "\n")
    results = []
    for line in lines:
        if line != "":
            results.append(mwp.nodes.text.Text(line))
    return results
github lahwaacz / wiki-scripts / update-package-templates.py View on Github external
which does not `disappear` in the rendered wikitext.
        """
        try:
            param = template.get(1)
        except ValueError:
            raise TemplateParametersError(template)

        parent = get_parent_wikicode(wikicode, template)
        index = parent.index(template)

        if param.value.startswith(" "):
            try:
                prev = parent.get(index - 1)
            except IndexError:
                prev = None
            if isinstance(prev, mwparserfromhell.nodes.text.Text):
                if not prev.endswith("\n") and not prev.endswith(" "):
                    prev.value += " "

        if param.value.endswith(" "):
            try:
                next_ = parent.get(index + 1)
            except IndexError:
                next_ = None
            if isinstance(next_, mwparserfromhell.nodes.text.Text):
                if not next_.startswith("\n") and not next_.startswith(" "):
                    next_.value = " " + next_.value

        template.name = str(template.name).strip()
        param.value = param.value.strip()
github lahwaacz / wiki-scripts / update-package-templates.py View on Github external
if param.value.startswith(" "):
            try:
                prev = parent.get(index - 1)
            except IndexError:
                prev = None
            if isinstance(prev, mwparserfromhell.nodes.text.Text):
                if not prev.endswith("\n") and not prev.endswith(" "):
                    prev.value += " "

        if param.value.endswith(" "):
            try:
                next_ = parent.get(index + 1)
            except IndexError:
                next_ = None
            if isinstance(next_, mwparserfromhell.nodes.text.Text):
                if not next_.startswith("\n") and not next_.startswith(" "):
                    next_.value = " " + next_.value

        template.name = str(template.name).strip()
        param.value = param.value.strip()
github lahwaacz / wiki-scripts / link-checker.py View on Github external
def _get_text(index):
            try:
                node = parent.get(index)
                if not isinstance(node, mwparserfromhell.nodes.text.Text):
                    return None
                return node
            except IndexError:
                return None
github lahwaacz / wiki-scripts / ws / parser_helpers / wikicode.py View on Github external
def _get_text(index):
        # the first node has no previous node, especially not the last node
        if index < 0:
            return None, None
        try:
            node = parent.get(index)
            # don't EVER remove whitespace from non-Text nodes (it would
            # modify the objects by converting to str, making the operation
            # and replacing the object with str, but we keep references to
            # the old nodes)
            if not isinstance(node, mwparserfromhell.nodes.text.Text):
                return None, mwparserfromhell.nodes.text.Text
            return node, mwparserfromhell.nodes.text.Text
        except IndexError:
            return None, None
github amyxzhang / wikum / wikum / wikichatter / indentblock.py View on Github external
def _divide_wikicode_into_lines(wcode):
    lines = []
    line = []
    for node in wcode.nodes:
        line.append(node)
        if type(node) is mwp.nodes.text.Text and '\n' in node:
            lines.append(mwp.wikicode.Wikicode(line))
            line = []
    if len(line) > 0:
        lines.append(mwp.wikicode.Wikicode(line))
    return lines