Support wiki table generation for row tags.

This commit is contained in:
Sergey Vartanov 2021-12-10 23:51:16 +03:00
parent 6fead57e2f
commit 774766e117

View file

@ -20,11 +20,12 @@ EXTRACTOR: ShapeExtractor = ShapeExtractor(
) )
HEADER_PATTERN: re.Pattern = re.compile("==?=?.*==?=?") HEADER_PATTERN: re.Pattern = re.compile("==?=?.*==?=?")
SEE_ALSO_HEADER_PATTERN: re.Pattern = re.compile("==\\s*See also\\s*==") HEADER_PATTERNS: list[re.Pattern] = [
EXAMPLE_HEADER_PATTERN: re.Pattern = re.compile("==\\s*Example.*==") re.compile("==\\s*See also\\s*=="),
RENDERING_HEADER_PATTERN: re.Pattern = re.compile( re.compile("==\\s*Example.*=="),
"===\\s*\\[\\[Röntgen]] icons\\s*===" ]
) RENDERING_HEADER_PATTERN: re.Pattern = re.compile("==\\s*Rendering\\s*==")
ROENTGEN_HEADER_PATTERN: re.Pattern = re.compile("===.*Röntgen.*===")
class WikiTable: class WikiTable:
@ -46,6 +47,33 @@ class WikiTable:
else: else:
text += "! Tag || Icon" text += "! Tag || Icon"
if self.collection.row_tags:
text += "\n"
for current_tags in self.collection.row_tags:
text += "|-\n"
text += "| "
if current_tags:
for key, value in current_tags.items():
if value == "*":
text += f"{{{{Key|{key}}}}}<br />"
else:
text += f"{{{{Tag|{key}|{value}}}}}<br />"
text = text[:-6]
text += "\n"
icon, _ = SCHEME.get_icon(
EXTRACTOR,
current_tags | self.collection.tags,
set(),
MapConfiguration(ignore_level_matching=True),
)
icons.append(icon.main_icon)
text += (
"| "
f"[[Image:Röntgen {icon.main_icon.get_name()}.svg|32px]]\n"
)
text += "|}\n"
return text, icons
if not self.collection.column_values: if not self.collection.column_values:
self.collection.column_values = [""] self.collection.column_values = [""]
else: else:
@ -58,8 +86,6 @@ class WikiTable:
) )
text += "\n" text += "\n"
processed: set[str] = set()
for row_value in self.collection.row_values: for row_value in self.collection.row_values:
text += "|-\n" text += "|-\n"
if row_value: if row_value:
@ -72,9 +98,7 @@ class WikiTable:
} }
if column_value: if column_value:
current_tags |= {self.collection.column_key: column_value} current_tags |= {self.collection.column_key: column_value}
icon, _ = SCHEME.get_icon( icon, _ = SCHEME.get_icon(EXTRACTOR, current_tags, set())
EXTRACTOR, current_tags, processed, MapConfiguration()
)
if not icon: if not icon:
print("Icon was not constructed.") print("Icon was not constructed.")
text += ( text += (
@ -102,7 +126,7 @@ def generate_new_text(
wiki_text: str wiki_text: str
icons = [] icons = []
if table.collection.row_key: if table.collection.row_key or table.collection.row_tags:
wiki_text, icons = table.generate_wiki_table() wiki_text, icons = table.generate_wiki_table()
else: else:
processed = set() processed = set()
@ -129,12 +153,14 @@ def generate_new_text(
start: Optional[int] = None start: Optional[int] = None
end: int = -1 end: int = -1
# If Röntgen rendering section already exists.
for index, line in enumerate(lines): for index, line in enumerate(lines):
if HEADER_PATTERN.match(line): if HEADER_PATTERN.match(line):
if start is not None: if start is not None:
end = index end = index
break break
if RENDERING_HEADER_PATTERN.match(line): if ROENTGEN_HEADER_PATTERN.match(line):
start = index start = index
if start is not None: if start is not None:
@ -146,23 +172,23 @@ def generate_new_text(
+ "\n".join(lines[end:]) + "\n".join(lines[end:])
), icons ), icons
example_header: Optional[int] = None # Otherwise.
headers = [None, None]
for index, line in enumerate(lines): for index, line in enumerate(lines):
if EXAMPLE_HEADER_PATTERN.match(line) or SEE_ALSO_HEADER_PATTERN.match( for i, pattern in enumerate(HEADER_PATTERNS):
line if pattern.match(line):
): headers[i] = index
example_header = index
break
if example_header is not None: filtered = filter(lambda x: x is not None, headers)
if filtered:
header: int = filtered.__next__()
return ( return (
"\n".join(lines[:example_header]) "\n".join(lines[:header])
+ "\n" + "\n== Rendering ==\n\n=== [[Röntgen]] icons in [[Map Machine]] "
+ "== Rendering ==\n\n=== [[Röntgen]] icons ===\n\n" "===\n\n" + wiki_text + "\n" + "\n".join(lines[header:])
+ wiki_text
+ "\n"
+ "\n".join(lines[example_header:])
), icons ), icons
return None, [] return None, []