|
1 | 1 | # Changed by tibia.sh in 2026. See "About this copy" in README.md. |
| 2 | +import re |
2 | 3 | from typing import Any, ClassVar |
3 | 4 |
|
4 | 5 | import tibiawikisql.schema |
|
8 | 9 | from tibiawikisql.parsers import BaseParser |
9 | 10 | from tibiawikisql.utils import clean_links, client_color_to_rgb, parse_boolean, parse_client_id, parse_integer |
10 | 11 |
|
| 12 | +TIBIA_COINS = "Tibia Coins" |
| 13 | +TOURNAMENT_COINS = "Tournament Coins" |
| 14 | + |
| 15 | +CURRENCY_TEMPLATES = {"TC": TIBIA_COINS, "TC3": TOURNAMENT_COINS} |
| 16 | +"""The currency templates that `Template:Infobox Mount` prints, by name.""" |
| 17 | + |
| 18 | +CURRENCY_TEMPLATE_PATTERN = re.compile(r"\{\{([Tt]C3?)\}\}") |
| 19 | +"""A value that is only `{{TC}}` or `{{TC3}}`. As in MediaWiki, only the first letter of the name ignores case.""" |
| 20 | + |
11 | 21 |
|
12 | 22 | def remove_mount(name: str) -> str: |
13 | 23 | """Remove "(Mount)" from the name, if found. |
@@ -47,11 +57,19 @@ def parse_attributes(cls, article: Article) -> dict[str, Any]: |
47 | 57 | raw_attributes = row["_raw_attributes"] |
48 | 58 | row["price_currency"] = None |
49 | 59 | if row["price"] is not None: |
50 | | - currency = clean_links(raw_attributes.get("pricecurrency", "")) |
| 60 | + currency = cls._parse_currency(raw_attributes.get("pricecurrency", "")) |
51 | 61 | if currency: |
52 | 62 | row["price_currency"] = currency |
53 | 63 | elif parse_boolean(raw_attributes.get("tournament", "")): |
54 | | - row["price_currency"] = "Tournament Coins" |
| 64 | + row["price_currency"] = TOURNAMENT_COINS |
55 | 65 | else: |
56 | | - row["price_currency"] = "Tibia Coins" |
| 66 | + row["price_currency"] = TIBIA_COINS |
57 | 67 | return row |
| 68 | + |
| 69 | + @staticmethod |
| 70 | + def _parse_currency(value: str) -> str: |
| 71 | + """Read `pricecurrency`: the currency that `{{TC}}` or `{{TC3}}` prints, otherwise the text without links.""" |
| 72 | + template = CURRENCY_TEMPLATE_PATTERN.fullmatch(value.strip()) |
| 73 | + if template: |
| 74 | + return CURRENCY_TEMPLATES[template.group(1).upper()] |
| 75 | + return clean_links(value) |
0 commit comments