From 5f8597481459adecf257089d16f5423c79e09e3b Mon Sep 17 00:00:00 2001 From: Matthew Scroggs Date: Tue, 24 Sep 2024 10:50:16 +0100 Subject: [PATCH] hide implementation detail behind a "show more" --- build.py | 45 ++++++++++++++++++++++++++++++++++++----- defelement/citations.py | 6 ++---- defelement/markup.py | 9 ++++----- defelement/tools.py | 12 ++++++++++- 4 files changed, 57 insertions(+), 15 deletions(-) diff --git a/build.py b/build.py index f0c8710ff2..4f49fd77aa 100644 --- a/build.py +++ b/build.py @@ -19,7 +19,7 @@ from defelement.markup import (cap_first, heading_with_self_ref, insert_links, markup, python_highlight) from defelement.rss import make_rss -from defelement.tools import html_local, insert_author_info, parse_metadata +from defelement.tools import html_local, insert_author_info, parse_metadata, comma_and_join start_all = datetime.now() @@ -146,6 +146,7 @@ def write_html_page(path: str, title: str, content: str): v_date = v_json["metadata"]["date"] icon_style = "font-size:150%;vertical-align:middle" +icon_style_small = "font-size:80%;vertical-align:middle" text_style = "font-size:80%;vertical-align:middle" green_check = ("") @@ -155,6 +156,10 @@ def write_html_page(path: str, title: str, content: str): f"#FF0000;{icon_style}'>") blue_minus = ("") +green_check_small = green_check.replace(icon_style, icon_style_small) +orange_check_small = orange_check.replace(icon_style, icon_style_small) +red_check_small = red_check.replace(icon_style, icon_style_small) +blue_minus_small = blue_minus.replace(icon_style, icon_style_small) # Generate element pages all_examples = [] @@ -253,6 +258,8 @@ def write_html_page(path: str, title: str, content: str): for codename, libname, url, pip in libraries: if e.implemented(codename): info = e.list_of_implementation_strings(codename) + assert libname is not None + short_info = libname if e.has_implementation_examples(codename): jscodename = codename.replace('.', '_').replace('-', '_') @@ -265,7 +272,7 @@ def write_html_page(path: str, title: str, content: str): f"href='javascript:hide_{jscodename}_eg()' style='display:none'>" f"↑ Hide {libname} examples ↑" f"" if codename == "symfem": + short_info += f" {green_check_small}" info += ( f"{green_check} " "This implementation is used to compute the examples below and " @@ -284,11 +292,13 @@ def write_html_page(path: str, title: str, content: str): v = verification[e.filename][codename] if len(v["fail"]) == 0: if len(v["not implemented"]) == 0: + short_info += f" {green_check_small}" info += ( f"{green_check} " "This implementation is correct for all the examples below." "") else: + short_info += f" {green_check_small}" info += ( f"{green_check} " "This implementation is correct for all the examples below " @@ -324,6 +334,7 @@ def write_html_page(path: str, title: str, content: str): f"document.getElementById('{jscodename}-hiddenverification')" ".style.display = 'none'\n}\n") elif len(v["pass"]) > 0: + short_info += f" {orange_check_small}" info += ( f"{orange_check} " "This implementation is correct for some of " @@ -343,6 +354,7 @@ def write_html_page(path: str, title: str, content: str): f"{red_check} " f"Incorrect: {'; '.join(v['fail'])}") if len(v["not implemented"]) > 0: + short_info += f" {blue_minus_small}" info += ( f"
" f"{blue_minus} " @@ -364,6 +376,7 @@ def write_html_page(path: str, title: str, content: str): f"document.getElementById('{jscodename}-hiddenverification')" ".style.display = 'none'\n}\n") else: + short_info += f" {red_check_small}" info += ( f"{red_check} " "This implementation is incorrect for this element.") @@ -382,7 +395,8 @@ def write_html_page(path: str, title: str, content: str): "") impl.append( - (f"{libname}", info)) + (f"{libname}", info, short_info) + ) # Categories cats = e.categories() @@ -399,10 +413,31 @@ def write_html_page(path: str, title: str, content: str): # Write implementations if len(impl) > 0: content += heading_with_self_ref("h2", "Implementations") + content += "This element is implemented in " + content += comma_and_join([f"{i[2]}" for i in impl]) + content += "." + content += ("" + "↓ Show implementation detail ↓") + content += ( + "") + content += "" # Write examples using symfem if e.has_examples: diff --git a/defelement/citations.py b/defelement/citations.py index 42caefd5cb..86165497b0 100644 --- a/defelement/citations.py +++ b/defelement/citations.py @@ -2,6 +2,7 @@ import re import typing +from defelement.tools import comma_and_join def markup_authors(a: typing.Union[str, typing.List[str]]) -> str: @@ -16,10 +17,7 @@ def markup_authors(a: typing.Union[str, typing.List[str]]) -> str: if isinstance(a, str): return a else: - if len(a) == 2: - return a[0] + " and " + a[1] - else: - return ", ".join(a[:-1]) + ", and " + a[-1] + return comma_and_join(a) def markup_citation(r: typing.Dict[str, typing.Any]) -> str: diff --git a/defelement/markup.py b/defelement/markup.py index 17b64cbda6..42b7e8b73a 100644 --- a/defelement/markup.py +++ b/defelement/markup.py @@ -13,7 +13,7 @@ from github import Github from defelement import plotting, settings, symbols -from defelement.citations import markup_citation +from defelement.tools import comma_and_join page_references: typing.List[str] = [] @@ -79,10 +79,7 @@ def format_names(names: typing.List[str], format: str) -> str: else: return ", ".join([", ".join(formatted_names[:-1]), formatted_names[-1]]) else: - if len(formatted_names) <= 2: - return " and ".join(formatted_names) - else: - return ", and ".join([", ".join(formatted_names[:-1]), formatted_names[-1]]) + return comma_and_join(formatted_names) def person_sort_key(p: typing.Dict): @@ -535,6 +532,8 @@ def add_citation(matches: typing.Match[str]) -> str: HTML """ global page_references + from defelement.citations import markup_citation + ref = {} for i in shlex.split(matches[1]): a, b = i.split("=") diff --git a/defelement/tools.py b/defelement/tools.py index 44c833550f..63260371da 100644 --- a/defelement/tools.py +++ b/defelement/tools.py @@ -5,7 +5,6 @@ import yaml from defelement import settings -from defelement.markup import preprocess if typing.TYPE_CHECKING: from numpy import float64 @@ -24,6 +23,8 @@ def parse_metadata(content: str) -> typing.Tuple[typing.Dict[str, typing.Any], s Returns: Parsed metadata and content without metadata """ + from defelement.markup import preprocess + metadata: typing.Dict[str, typing.Any] = {"title": None} if content.startswith("--\n"): metadata_in, content = content[3:].split("\n--\n", 1) @@ -72,3 +73,12 @@ def to_array( if isinstance(data, (list, tuple)): return np.array([to_array(i) for i in data]) return float(data) + + +def comma_and_join(ls: typing.List[str], oxford_comma: bool = True) -> str: + """Join a list with commas and an and between the last two items.""" + if len(ls) == 1: + return ls[0] + if len(ls) == 2: + return f"{ls[0]} and {ls[1]}" + return ", ".join(ls[:-1]) + ("," if oxford_comma else "") + " and " + ls[-1]