示例#1
0
def link(instance: WikiPage, wikilink: wikitextparser.WikiLink):
    target = wikilink.target[len(":folder:"):].strip()

    # As we are browsing folders, [[Folder:en]] and [[Folder:en/]] are
    # identical in meaning.
    if not target.endswith("/"):
        target += "/"
    target += "Main Page"

    wikilink.target = "Folder/" + target
    return False
示例#2
0
def link_file(instance: WikiPage, wikilink: wikitextparser.WikiLink):
    target = wikilink.target[len(":file:"):].strip()

    # Generate a link to the language root, which will list all templates
    # of that language.
    # Example: [[:Template:en]]
    if len(target) == 2:
        target += "/Main Page"

    wikilink.target = "File/" + target
    return False
示例#3
0
def link_media(instance: WikiPage, wikilink: wikitextparser.WikiLink):
    target = wikilink.target
    target = target[len("media:"):].strip()

    url = f"File/{target}"

    text = ""
    # Always run clean_title() on the URL, as it detects URLs that are invalid
    # because of invalid namespaces, etc.
    if url:
        try:
            text = instance.clean_title(url)
        except InvalidWikiLink as e:
            # Errors always end with a dot, hence the [:-1].
            instance.add_error(
                f'{e.args[0][:-1]} (wikilink "{wikilink.string}").')
            return False

    if wikilink.text:
        text = wikilink.text.strip()

    title = html.escape(wikilink.target)
    text = html.escape(text)

    url = instance.clean_url(url)

    if url and not instance.page_exists(url):
        instance.add_error(
            f'Linked page "{wikilink.title}" does not exist (wikilink "{wikilink.string}").'
        )

        url = f"/{urllib.parse.quote(url)}"
        wikilink.string = f'<a href="{url}" class="new" title="{title}">{title}</a>'
        return True

    if url:
        url = "uploads/" + url[len("File/"):]
        url = f"/{urllib.parse.quote(url)}"

    wikilink.string = f'<a href="{url}" title="{title}">{text}</a>'
    return True
示例#4
0
def link(instance: WikiPage, wikilink: wikitextparser.WikiLink):
    target = wikilink.target[len(":category:"):].strip()

    # Generate a link to the language root, which will list all categories
    # of that language.
    # Example: [[:Category:en]]
    if len(target) == 2:
        target += "/Main Page"

    # Tell the target what the URL is to find this category, and let the
    # normal WikiLink handler take care of making that into a link.
    wikilink.target = "Category/" + target
    return False
示例#5
0
def replace(instance: WikiPage, wikilink: wikitextparser.WikiLink):
    # This indicates that the page belongs to this category. We update
    # the WikiPage to indicate this, and otherwise remove the WikiLink.
    category = wikilink.target[len("category:"):].strip()

    error = instance.page_is_valid(f"Category/{category}")
    if error:
        instance.add_error(f'{error[:-1]} (wikilink "{wikilink.string}")')
        return True

    instance.categories.append(category)
    wikilink.string = ""
    return True
示例#6
0
def test_set_target():
    wl = WikiLink('[[A | B]]')
    wl.target = ' C '
    assert '[[ C | B]]' == wl.string
    del wl.target
    assert '[[ B]]' == wl.string
    del wl.target
    assert '[[]]' == wl.string
    wl = WikiLink('[[A]]')
    wl.target = ' C '
    assert '[[ C ]]' == wl.string
示例#7
0
def test_title_and_fragment_deleters():
    # no pipe, no frag
    wl = WikiLink('[[a]]')
    del wl.fragment
    assert wl.string == '[[a]]'
    del wl.title
    assert wl.string == '[[]]'

    # no pipe, frag
    wl = WikiLink('[[a#]]')
    del wl.fragment
    assert wl.string == '[[a]]'
    wl.fragment = 'f'
    del wl.title
    assert wl.string == '[[f]]'

    # pipe, no frag
    wl = WikiLink('[[a|]]')
    del wl.fragment
    assert wl.string == '[[a|]]'
    del wl.title
    assert wl.string == '[[|]]'

    # pipe, frag
    wl = WikiLink('[[a#|]]')
    del wl.fragment
    assert wl.string == '[[a|]]'
    wl.fragment = 'f'
    del wl.title
    assert wl.string == '[[f|]]'

    # pipe, no frag, special case
    wl = WikiLink('[[a|#]]')
    del wl.fragment
    del wl.title
    assert wl.string == '[[|#]]'