Support wiki section for single icon.

This commit is contained in:
Sergey Vartanov 2021-11-18 06:51:46 +03:00
parent 29a8f001ac
commit 4d2a990d55

View file

@ -19,6 +19,7 @@ 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*==")
EXAMPLE_HEADER_PATTERN: re.Pattern = re.compile("==\\s*Example\\s*==") EXAMPLE_HEADER_PATTERN: re.Pattern = re.compile("==\\s*Example\\s*==")
RENDERING_HEADER_PATTERN: re.Pattern = re.compile( RENDERING_HEADER_PATTERN: re.Pattern = re.compile(
"===\\s*\\[\\[Röntgen]] icons\\s*===" "===\\s*\\[\\[Röntgen]] icons\\s*==="
@ -73,7 +74,7 @@ def generate_table(
current_tags: Tags = dict(tags) | {row_key: row_value} current_tags: Tags = dict(tags) | {row_key: row_value}
if column_value: if column_value:
current_tags |= {column_key: column_value} current_tags |= {column_key: column_value}
icon, priority = SCHEME.get_icon( icon, _ = SCHEME.get_icon(
EXTRACTOR, current_tags, processed, MapConfiguration() EXTRACTOR, current_tags, processed, MapConfiguration()
) )
if not icon: if not icon:
@ -106,35 +107,56 @@ def generate_new_text(
:param column_values: list of tag values 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
""" """
table_wiki_text: str = generate_table( wiki_text: str
tags, row_key, row_values, column_key, column_values
) if row_key:
wiki_text = generate_table(
tags, row_key, row_values, column_key, column_values
)
else:
processed = set()
icon, _ = SCHEME.get_icon(
EXTRACTOR, tags, processed, MapConfiguration()
)
if icon.main_icon.is_default():
wiki_text = (
f"Röntgen icon set has additional icon for the tag: "
f"[[Image:Röntgen {icon.extra_icons[0].get_name()}.svg|32px]]."
f"\n"
)
else:
wiki_text = (
f"[[Image:Röntgen {icon.main_icon.get_name()}.svg|32px]]\n"
)
lines: list[str] = old_text.split("\n") lines: list[str] = old_text.split("\n")
table_start: Optional[int] = None start: Optional[int] = None
table_end: int = -1 end: int = -1
for index, line in enumerate(lines): for index, line in enumerate(lines):
if HEADER_PATTERN.match(line): if HEADER_PATTERN.match(line):
if table_start is not None: if start is not None:
table_end = index end = index
break break
if RENDERING_HEADER_PATTERN.match(line): if RENDERING_HEADER_PATTERN.match(line):
table_start = index start = index
if table_start is not None: if start is not None:
return ( return (
"\n".join(lines[: table_start + 2]) "\n".join(lines[: start + 2])
+ "\n" + "\n"
+ table_wiki_text + wiki_text
+ "\n" + "\n"
+ "\n".join(lines[table_end:]) + "\n".join(lines[end:])
) )
example_header: Optional[int] = None example_header: Optional[int] = None
for index, line in enumerate(lines): for index, line in enumerate(lines):
if EXAMPLE_HEADER_PATTERN.match(line): if EXAMPLE_HEADER_PATTERN.match(line) or SEE_ALSO_HEADER_PATTERN.match(
line
):
example_header = index example_header = index
break break
@ -143,7 +165,7 @@ def generate_new_text(
"\n".join(lines[:example_header]) "\n".join(lines[:example_header])
+ "\n" + "\n"
+ "== Rendering ==\n\n=== [[Röntgen]] icons ===\n\n" + "== Rendering ==\n\n=== [[Röntgen]] icons ===\n\n"
+ table_wiki_text + wiki_text
+ "\n" + "\n"
+ "\n".join(lines[example_header:]) + "\n".join(lines[example_header:])
) )