コード例 #1
0
def if_(instance: WikiTextHtml,
        parser_function: wikitextparser.ParserFunction):
    if len(parser_function.arguments) < 1:
        raise ParserFunctionWrongArgumentCount

    condition = get_argument(parser_function, 0)

    if condition.strip():
        parser_function.string = get_argument(parser_function, 1)
    else:
        parser_function.string = get_argument(parser_function, 2)
コード例 #2
0
ファイル: ifexist.py プロジェクト: Xaroth/wikitexthtml
def ifexist(instance: WikiTextHtml,
            parser_function: wikitextparser.ParserFunction):
    if len(parser_function.arguments) < 1:
        raise ParserFunctionWrongArgumentCount

    page_title = get_argument(parser_function, 0)
    page_exists = instance.page_exists(page_title)

    if page_exists:
        parser_function.string = get_argument(parser_function, 1)
    else:
        parser_function.string = get_argument(parser_function, 2)
コード例 #3
0
def ifeq(
    instance: WikiTextHtml,
    parser_function: wikitextparser.ParserFunction,
):
    if len(parser_function.arguments) < 2:
        raise ParserFunctionWrongArgumentCount

    left = get_argument(parser_function, 0)
    right = get_argument(parser_function, 1)

    if left.strip() == right.strip():
        parser_function.string = get_argument(parser_function, 2)
    else:
        parser_function.string = get_argument(parser_function, 3)
コード例 #4
0
ファイル: ifexpr.py プロジェクト: Xaroth/wikitexthtml
def ifexpr(instance: WikiTextHtml,
           parser_function: wikitextparser.ParserFunction):
    if len(parser_function.arguments) < 1:
        raise ParserFunctionWrongArgumentCount

    expr = get_argument(parser_function, 0)

    try:
        result = evaluate(expr)
    except EvaluationError as e:
        parser_function.string = f"Expression error: {e.args[0]}"
        return

    if result:
        parser_function.string = get_argument(parser_function, 1)
    else:
        parser_function.string = get_argument(parser_function, 2)
コード例 #5
0
ファイル: url.py プロジェクト: Xaroth/wikitexthtml
def encode(instance: WikiTextHtml, parser_function: wikitextparser.ParserFunction):
    if len(parser_function.arguments) != 1:
        raise ParserFunctionWrongArgumentCount

    url = get_argument(parser_function, 0).strip()

    # All variables should already be HTML escaped, so unescape first,
    # as otherwise we are double encoding them.
    url = urllib.parse.quote(html.unescape(url))

    parser_function.string = url
コード例 #6
0
ファイル: expr.py プロジェクト: Xaroth/wikitexthtml
def expr(instance: WikiTextHtml,
         parser_function: wikitextparser.ParserFunction):
    if len(parser_function.arguments) < 1:
        raise ParserFunctionWrongArgumentCount

    expr = get_argument(parser_function, 0)

    try:
        result = evaluate(expr, as_str=True)
    except EvaluationError as e:
        result = f"Expression error: {e.args[0]}"

    parser_function.string = result
コード例 #7
0
def variable(instance: WikiTextHtml,
             parser_function: wikitextparser.ParserFunction):
    if len(parser_function.arguments) != 0:
        raise ParserFunctionWrongArgumentCount

    name = parser_function.name.lower()
    if name == "currentyear":
        parser_function.string = str(datetime.datetime.now().year)
    elif name in ("pagename", "fullpagename", "basepagename"):
        parser_function.string = html.escape(instance.page)
    elif name == "subpagename":
        try:
            parser_function.string = html.escape(
                instance.clean_title(instance.page))
        except InvalidWikiLink as e:
            # Errors always end with a dot, hence the [:-1].
            instance.add_error(
                f'{e.args[0][:-1]} (parserfunction "{parser_function.string}").'
            )
            parser_function.string = ""
    elif name == "namespace":
        parser_function.string = ""
コード例 #8
0
ファイル: switch.py プロジェクト: Xaroth/wikitexthtml
def switch(instance: WikiTextHtml, parser_function: wikitextparser.ParserFunction):
    if len(parser_function.arguments) < 1:
        raise ParserFunctionWrongArgumentCount

    branch = get_argument(parser_function, 0).strip()

    explicit_default = None
    positional_default = None
    fallthrough = False

    for argument in parser_function.arguments[1:]:
        # Fallthough cases are positional from our perspective
        if argument.positional:
            name = argument.value.strip()
        else:
            name = argument.name.strip()

        if name == "#default":
            explicit_default = argument.value
        # The last positional argument is the positional default, which is
        # used if no explicit default is set.
        if argument.positional:
            positional_default = argument.value.strip()

        # Find the fallthrough value
        if fallthrough and not argument.positional:
            value = argument.value
            break

        if name == branch:
            if argument.positional:
                fallthrough = True
            else:
                value = argument.value
                break
    else:
        if explicit_default is not None:
            value = explicit_default
        elif positional_default is not None:
            value = positional_default
        else:
            value = ""

    # Only strip if this was not a multiline value
    if value.count("\n") <= 1:
        value = value.strip()
    parser_function.string = value