Refactor wiki table generation.

This commit is contained in:
Sergey Vartanov 2021-11-28 12:21:49 +03:00
parent 4ef311f9ef
commit 1d0e9aea0d

View file

@ -5,6 +5,7 @@ import re
from pathlib import Path from pathlib import Path
from typing import Optional from typing import Optional
from map_machine.doc.collections import Collection
from map_machine.map_configuration import MapConfiguration from map_machine.map_configuration import MapConfiguration
from map_machine.osm.osm_reader import Tags from map_machine.osm.osm_reader import Tags
from map_machine.pictogram.icon import Icon, ShapeExtractor from map_machine.pictogram.icon import Icon, ShapeExtractor
@ -26,62 +27,59 @@ RENDERING_HEADER_PATTERN: re.Pattern = re.compile(
) )
def generate_table( class WikiTable:
tags: Tags, """SVG table with icon combinations."""
row_key: str,
row_values: list[str], def __init__(self, collection: Collection, page_name: str):
column_key: str, self.collection: Collection = collection
column_values: list[str], self.page_name: str = page_name
) -> tuple[str, list[Icon]]:
def generate_wiki_table(self) -> tuple[str, list[Icon]]:
""" """
Generate Röntgen icon table for the OpenStreetMap wiki page. Generate Röntgen icon table for the OpenStreetMap wiki page.
:param tags: core tags
:param row_key: tag key to be used in rows
:param row_values: list of tag values to be used in rows
:param column_key: tag key to be used in columns
:param column_values: list of tag values to be used in columns
""" """
icons: list[Icon] = [] icons: list[Icon] = []
text: str = '{| class="wikitable"\n' text: str = '{| class="wikitable"\n'
if column_key is not None: if self.collection.column_key is not None:
text += f"! {{{{Key|{column_key}}}}}" text += f"! {{{{Key|{self.collection.column_key}}}}}"
else: else:
text += "! Tag || Icon" text += "! Tag || Icon"
if not column_values: if not self.collection.column_values:
column_values = [""] self.collection.column_values = [""]
else: else:
for column_value in column_values: for column_value in self.collection.column_values:
text += " ||" text += " ||"
if column_value: if column_value:
text += ( text += (
" {{vert header|" f" {{{{vert header|{{{{TagValue|"
f"{{{{TagValue|{column_key}|{column_value}}}}}" f"{self.collection.column_key}|{column_value}}}}}}}}}"
"}}"
) )
text += "\n" text += "\n"
processed: set[str] = set() processed: set[str] = set()
for row_value in row_values: for row_value in self.collection.row_values:
text += "|-\n" text += "|-\n"
if row_value: if row_value:
text += f"| {{{{Tag|{row_key}|{row_value}}}}}\n" text += f"| {{{{Tag|{self.collection.row_key}|{row_value}}}}}\n"
else: else:
text += "|\n" text += "|\n"
for column_value in column_values: for column_value in self.collection.column_values:
current_tags: Tags = dict(tags) | {row_key: row_value} current_tags: Tags = dict(self.collection.tags) | {
self.collection.row_key: row_value
}
if column_value: if column_value:
current_tags |= {column_key: column_value} current_tags |= {self.collection.column_key: column_value}
icon, _ = SCHEME.get_icon( icon, _ = SCHEME.get_icon(
EXTRACTOR, current_tags, processed, MapConfiguration() EXTRACTOR, current_tags, processed, MapConfiguration()
) )
if not icon: if not icon:
print("Icon was not constructed.") print("Icon was not constructed.")
text += ( text += (
f"| [[Image:Röntgen {icon.main_icon.get_name()}.svg|32px]]\n" "| "
f"[[Image:Röntgen {icon.main_icon.get_name()}.svg|32px]]\n"
) )
icons.append(icon.main_icon) icons.append(icon.main_icon)
@ -92,34 +90,24 @@ def generate_table(
def generate_new_text( def generate_new_text(
old_text: str, old_text: str,
tags: Tags, table: WikiTable,
row_key: str,
row_values: list[str],
column_key: str,
column_values: list[str],
) -> tuple[Optional[str], list[Icon]]: ) -> tuple[Optional[str], list[Icon]]:
""" """
Generate Röntgen icon table for the OpenStreetMap wiki page. Generate Röntgen icon table for the OpenStreetMap wiki page.
:param old_text: previous wiki page text :param old_text: previous wiki page text
:param tags: core tags :param table: wiki table generator
:param row_key: tag key to be used in rows
:param row_values: list of tag values to be used in rows
:param column_key: tag key to be used in columns
:param column_values: list of tag values to be used in columns
:return: new wiki page text :return: new wiki page text
""" """
wiki_text: str wiki_text: str
icons = [] icons = []
if row_key: if table.collection.row_key:
wiki_text, icons = generate_table( wiki_text, icons = table.generate_wiki_table()
tags, row_key, row_values, column_key, column_values
)
else: else:
processed = set() processed = set()
icon, _ = SCHEME.get_icon( icon, _ = SCHEME.get_icon(
EXTRACTOR, tags, processed, MapConfiguration() EXTRACTOR, table.collection.tags, processed, MapConfiguration()
) )
if icon.main_icon.is_default(): if icon.main_icon.is_default():
wiki_text = ( wiki_text = (