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,100 +27,87 @@ 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],
column_key: str,
column_values: list[str],
) -> tuple[str, list[Icon]]:
"""
Generate Röntgen icon table for the OpenStreetMap wiki page.
:param tags: core tags def __init__(self, collection: Collection, page_name: str):
:param row_key: tag key to be used in rows self.collection: Collection = collection
:param row_values: list of tag values to be used in rows self.page_name: str = page_name
: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] = []
text: str = '{| class="wikitable"\n'
if column_key is not None: def generate_wiki_table(self) -> tuple[str, list[Icon]]:
text += f"! {{{{Key|{column_key}}}}}" """
else: Generate Röntgen icon table for the OpenStreetMap wiki page.
text += "! Tag || Icon" """
icons: list[Icon] = []
text: str = '{| class="wikitable"\n'
if not column_values: if self.collection.column_key is not None:
column_values = [""] text += f"! {{{{Key|{self.collection.column_key}}}}}"
else:
for column_value in column_values:
text += " ||"
if column_value:
text += (
" {{vert header|"
f"{{{{TagValue|{column_key}|{column_value}}}}}"
"}}"
)
text += "\n"
processed: set[str] = set()
for row_value in row_values:
text += "|-\n"
if row_value:
text += f"| {{{{Tag|{row_key}|{row_value}}}}}\n"
else: else:
text += "|\n" text += "! Tag || Icon"
for column_value in column_values:
current_tags: Tags = dict(tags) | {row_key: row_value}
if column_value:
current_tags |= {column_key: column_value}
icon, _ = SCHEME.get_icon(
EXTRACTOR, current_tags, processed, MapConfiguration()
)
if not icon:
print("Icon was not constructed.")
text += (
f"| [[Image:Röntgen {icon.main_icon.get_name()}.svg|32px]]\n"
)
icons.append(icon.main_icon)
text += "|}\n" if not self.collection.column_values:
self.collection.column_values = [""]
else:
for column_value in self.collection.column_values:
text += " ||"
if column_value:
text += (
f" {{{{vert header|{{{{TagValue|"
f"{self.collection.column_key}|{column_value}}}}}}}}}"
)
text += "\n"
return text, icons processed: set[str] = set()
for row_value in self.collection.row_values:
text += "|-\n"
if row_value:
text += f"| {{{{Tag|{self.collection.row_key}|{row_value}}}}}\n"
else:
text += "|\n"
for column_value in self.collection.column_values:
current_tags: Tags = dict(self.collection.tags) | {
self.collection.row_key: row_value
}
if column_value:
current_tags |= {self.collection.column_key: column_value}
icon, _ = SCHEME.get_icon(
EXTRACTOR, current_tags, processed, MapConfiguration()
)
if not icon:
print("Icon was not constructed.")
text += (
"| "
f"[[Image:Röntgen {icon.main_icon.get_name()}.svg|32px]]\n"
)
icons.append(icon.main_icon)
text += "|}\n"
return text, icons
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 = (