Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion layouts/glossary/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,17 @@

{{ with .Params.related_terms }}
{{/* Related terms link to their pages, found by title: this language's terms first, then English;
a translated term is also found by the English name in its brackets. A name is matched in full first, so
a translated term is also found by its English name (`en_title`), which its title shows in square brackets
only when the translated title does not already contain the English words, e.g. "Ödeme Duvarı (Paywall)".
A name is matched in full first, so
"Model (philosophy)" and "Model (statistical)" stay apart, then without its bracketed qualifier. */}}
{{ $index := dict }}{{ $exact := dict }}
{{ range slice "english" $lang }}
{{ range where (where site.RegularPages "Type" "glossary") "File.Dir" (printf "glossary/%s/" .) }}
{{ $page := . }}
{{ $names := slice .Title }}
{{ with findRESubmatch `\[(.*)\]\s*$` .Title 1 }}{{ $names = $names | append (index (index . 0) 1) }}{{ end }}
{{ with .Params.en_title }}{{ $names = $names | append . }}{{ end }}
{{ range $names }}
{{ $key := . | replaceRE `[*\\]` "" | replaceRE `\s*\(.*?\)` "" | replaceRE `\s*\[.*\]\s*$` "" | lower | replaceRE `[^\p{L}\p{N}]+` "" }}
{{ if $key }}{{ $index = merge $index (dict $key $page) }}{{ end }}
Expand Down
54 changes: 46 additions & 8 deletions scripts/check_seo_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
or hidden, so its links are tab stops only once opened. Every in-page link in the clusters map
reaches an id on the page.
22. The FLoRA page (replication-hub/flora/) contains the FLoRA citation DOI.
23. On a translated glossary page, no related term links to an English term page
that has a page in the same language (per the English page's hreflang links).
24. Every other name an English glossary term page shows ("Also known as") is in
that term's data-term on the glossary hub, so the hub's filter finds it.

Expand Down Expand Up @@ -105,6 +107,9 @@ def __init__(self):
self.citations = []
self.crumb_links = []
self._in_crumbs = False
self.alternates = []
self.related_links = []
self._related_tag = None # the element holding a term page's related terms (section.term-related)

def handle_starttag(self, tag, attrs):
attrs = dict(attrs)
Expand All @@ -114,6 +119,12 @@ def handle_starttag(self, tag, attrs):
self.ids.add(attrs["id"])
if tag == "nav" and "crumbs" in (attrs.get("class") or "").split():
self._in_crumbs = True
if tag == "link" and attrs.get("rel") == "alternate" and attrs.get("hreflang") and attrs.get("href"):
self.alternates.append(attrs["href"])
if self._related_tag is None and "term-related" in (attrs.get("class") or "").split():
self._related_tag = tag
if tag == "a" and self._related_tag:
self.related_links.append(attrs.get("href") or "")
if tag == "a" and self._in_crumbs:
self.crumb_links.append(attrs.get("href") or "")
if tag == "a" and "cite" in attrs.get("class", "").split():
Expand Down Expand Up @@ -151,6 +162,8 @@ def handle_starttag(self, tag, attrs):
def handle_endtag(self, tag):
if tag == "nav":
self._in_crumbs = False
if tag == self._related_tag:
self._related_tag = None
if tag == "script" and self._inline_script is not None:
self.largest_inline_script = max(self.largest_inline_script, self._inline_script)
self._inline_script = None
Expand Down Expand Up @@ -218,6 +231,8 @@ def __init__(self, path, parser):
self.citations = parser.citations
self.gloss_terms = parser.gloss_terms
self.crumb_links = parser.crumb_links
self.alternates = parser.alternates
self.related_links = parser.related_links

def loads(self, needle):
return any(needle in url for url in self.urls)
Expand Down Expand Up @@ -275,6 +290,35 @@ def check_glossary_links(pages):
return failures


# A preview build serves the site under a prefix (e.g. /rebrand-doors/); glossary URLs are compared from /glossary/ on.
GLOSSARY_URL_PATH = re.compile(r"/glossary/.*$")


def glossary_url_key(href):
match = GLOSSARY_URL_PATH.search(unquote(urlparse(href).path))
return match.group(0) if match else None


def check_glossary_related_language(pages):
"""A translated term page links its related terms to pages in its own language where they exist."""
english = {}
for page in pages:
path = page.path.replace(os.sep, "/")
if path.startswith("glossary/english/"):
english["/" + path.removesuffix("index.html")] = [glossary_url_key(h) or "" for h in page.alternates]
failures = []
for page in pages:
parts = page.path.replace(os.sep, "/").split("/")
if len(parts) < 4 or parts[0] != "glossary" or parts[1] == "english":
continue
own = "/glossary/%s/" % parts[1]
for href in page.related_links:
target = glossary_url_key(href)
if any(alt.startswith(own) for alt in english.get(target, [])):
failures.append("%s (%s)" % (page.path, href))
return failures


# Paired asterisks around text: Markdown emphasis shown as typed. Asterisks inside a word
# ("G*Power", "P*Hack") are not delimiters.
LITERAL_EMPHASIS = re.compile(r"(?<!\w)\*[^*\s](?:[^*]*[^*\s])?\*(?!\w)")
Expand Down Expand Up @@ -593,18 +637,11 @@ def check_flora_citation(public_dir):
GLOSSARY_HUB = os.path.join("glossary", "english", "index.html")
AKA_LINE = re.compile(r'<p class="term-aka">(.*?)</p>', re.S)
HUB_TERM = re.compile(r'<li data-term="([^"]*)">\s*<a href="([^"]+)"')
# A preview build serves the site under a prefix (e.g. /rebrand-doors/), so links are keyed from /glossary/ on.
GLOSSARY_PATH = re.compile(r"/glossary/.*$")


def glossary_key(href):
match = GLOSSARY_PATH.search(urlparse(unescape(href)).path)
return match.group(0) if match else None


def glossary_aka_failures(hub_html, term_pages):
"""term_pages: {URL path of the term page: its HTML}. Returns the names the hub filter misses."""
filter_text = {glossary_key(href): unescape(term) for term, href in HUB_TERM.findall(hub_html)}
filter_text = {glossary_url_key(unescape(href)): unescape(term) for term, href in HUB_TERM.findall(hub_html)}
failures = []
for url_path, html in sorted(term_pages.items()):
match = AKA_LINE.search(html)
Expand Down Expand Up @@ -778,6 +815,7 @@ def main(public_dir):
"20. cluster pages show no literal <i>/<em>/<scp> title markup": check_cluster_title_markup(public_dir),
"21. the clusters map's names are named and counted, its drawing hidden, its panels closed and its links have targets": check_clusters_map(public_dir),
"22. FLoRA page shows the citation": check_flora_citation(public_dir),
"23. translated glossary pages link related terms in their own language": check_glossary_related_language(pages),
"24. the glossary hub's filter finds each term by its other names": check_glossary_aka_filter(public_dir),
}
conditional = check_conditional_assets(pages)
Expand Down
31 changes: 31 additions & 0 deletions scripts/tests/test_check_seo_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,37 @@ def test_missing_citation_or_page_fails(self):
self.assertIn("not found", flora_failures(None)[0])


def related_language_failures(target_alternates, prefix=""):
"""Check 23: a Turkish page links its related term to the English Paywall page."""
with tempfile.TemporaryDirectory() as public:
english = Path(public) / "glossary/english/paywall"
turkish = Path(public) / "glossary/turkish/abstract"
english.mkdir(parents=True)
turkish.mkdir(parents=True)
links = "".join("<link rel='alternate' hreflang='x' href='%s'>" % h for h in target_alternates)
(english / "index.html").write_text("<html><head>%s</head></html>" % links, encoding="utf-8")
(turkish / "index.html").write_text(
"<section class='term-related'><h2>Related term</h2><ul class='term-cards'>"
"<li><a href='%s/glossary/english/paywall/'>Paywall</a><p>Definition</p></li></ul></section>"
"<p><a href='/glossary/english/paywall/'>elsewhere</a></p>" % prefix, encoding="utf-8")
return seo.check_glossary_related_language(seo.load_pages(public))


class GlossaryRelatedLanguageCheck(unittest.TestCase):
def test_preview_prefix_is_ignored(self):
failures = related_language_failures(["https://forrt.org/rebrand-doors/glossary/turkish/odeme_duvar%C4%B1_paywall_/"],
prefix="/rebrand-doors")
self.assertEqual(len(failures), 1)

def test_link_to_english_when_no_translation_exists_passes(self):
self.assertEqual(related_language_failures(["https://forrt.org/glossary/german/bezahlschranke/"]), [])

def test_link_to_english_when_translation_exists_fails(self):
failures = related_language_failures(["https://forrt.org/glossary/turkish/odeme_duvar%C4%B1_paywall_/"])
self.assertEqual(len(failures), 1)
self.assertIn("glossary/turkish/abstract", failures[0])


class GlossaryAkaFilterCheck(unittest.TestCase):
HUB = ('<li data-term="type i error · false-positive · alpha error">\n <a href="/glossary/english/type_i_error/">Type I error</a></li>'
'<li data-term="p-hacking · fishing · p-value fishing"><a href="/glossary/english/p_hacking/">p-hacking</a></li>')
Expand Down
Loading