"""Module for providing the parts in the template.html file"""
import csv
from pathlib import Path
import markdown
from bs4 import BeautifulSoup as soup
from .helpers import (
CHARACTERISTICS,
FORMS,
TECHNIQUES,
enumerate_helper,
repeat_template,
)
from .translations import translation_attrs, translation_attrs_setup
# Xp helper
def xp(
name: str, *, suffix="_exp", adv_suffix="_advancementExp", tot_suffix="_totalExp"
) -> str:
"""
Generate the HTML for the Xp parts of arts & abilities
"""
return f"""[//]"""
def alert(title: str, text: str, *, level: str = "warning", ID: str = None):
"""
Generate the HTML to display a banner that can be permanently hidden
This is used to inform player of important changes in updates.
Arguments:
text: Main text of the banner
title: Title of the banner
type: On of "warning", "info". The aspect of the banner
ID: optional string ID of this banner, if you need to check if it is
open/closed somewhere. Do NOT use numbers
"""
if not level in ("info", "warning"):
raise ValueError("Level must be among 'info', 'warning'")
if ID is None:
alert_id = alert.numid
alert.numid += 1
else:
alert_id = str(ID)
alert.strid.append(alert_id)
return f"""
{level.title()} - {title}
{text}
"""
# python supports attributes on function
# we use that to store the internal global variable used by the function
alert.numid = 0
alert.strid = []
def disable_old_alerts(marker: str):
lines = ",\n ".join(
f'"alert-{i}": 1' for i in list(range(alert.numid)) + alert.strid
)
return f"""setAttrs({{
"{marker}": 1,
{lines}
}}); """
# Add new parts to this dictionary
# parts can be defined in other modules and imported if the generating
# code is long
GLOBALS = {
# makes the module available
"markdown": markdown,
# makes those function available in the HTML
"xp": xp,
"alert": alert,
"disable_old_alerts": disable_old_alerts,
# Makes those values available in the HTML
"translation_attrs": translation_attrs,
"translation_attrs_setup": translation_attrs_setup,
"html_header": "",
"css_header": "/* DO NOT MODIFY !\nThis file is automatically generated from a tempalte. Any change will be overwritten\n*/",
}
# Personality traits
GLOBALS["personality_trait_rows"] = repeat_template(
"""
""",
[(str(i), str(i / 1000)) for i in range(1, add_fatigue_lvl_num + 1)],
tuple_keys=("num", "value"),
)
GLOBALS["fatigue_level_css"] = "\n".join(
(
# IF the fatigue selector is not on a value for which the level is visible
"".join(
':not(.sheet-fatigue-proxy[value="%s"])' % val
for val in range(lvl, add_fatigue_lvl_num + 1)
)
# THEN hide the level
+ (" + table tr.sheet-addfatigue-%s" % lvl)
+ " {\n display: none;\n}"
)
for lvl in range(1, add_fatigue_lvl_num + 1)
)
# Documentation
with open(Path(__file__).parents[1] / "documentation.md") as f:
html = markdown.markdown("".join(f))
html = soup(html, "html.parser")
for i in range(1, 10):
for tag in html.find_all(f"h{i}"):
tag.attrs["class"] = tag.get("class", "") + " heading_label"
GLOBALS["documentation"] = html.prettify()
# Rolltemplate
## Custom rolltemplate colors
with open(Path(__file__).parent / "css_colors.csv", newline="") as f:
reader = csv.DictReader(f)
css_rules = []
for color_def in reader:
# Base CSS rules
lines_header = [
f".sheet-rolltemplate-custom .sheet-crt-container.sheet-crt-color-{color_def['color']} {{",
f" --header-bg-color: {color_def['hex']};",
]
lines_rolls = [
f".sheet-rolltemplate-custom .sheet-crt-container.sheet-crt-rlcolor-{color_def['color']} .inlinerollresult {{",
f" --roll-bg-color: {color_def['hex']};",
]
lines_buttons = [
f".sheet-rolltemplate-custom .sheet-crt-container.sheet-crt-btcolor-{color_def['color']} a {{",
f" --button-bg-color: {color_def['hex']};",
]
# Adapt text color to background color
hex = color_def["hex"].lstrip("#")
r, g, b = tuple(int(hex[2 * i : 2 * i + 2], 16) / 255 for i in range(3))
# Assuming sRGB -> Luma
# may need fixing, color spaces are confusing
luma = 0.2126 * r + 0.7152 * g + 0.0722 * b
if luma > 0.5: # arbitrary threshold
# switch to black text if luma is high enough
lines_header.append(" --header-text-color: #000;")
lines_buttons.append(" --button-text-color: #000;")
if luma < 0.5:
lines_rolls.append(" --roll-text-color: #FFF;")
# Build the rules
for lines in (lines_header, lines_rolls, lines_buttons):
lines.append("}")
css_rules.append("\n".join(lines))
GLOBALS["custom_rt_color_css"] = "\n".join(css_rules)