From 674dcaec0f4e38befa31acad3095f6c809c0b937 Mon Sep 17 00:00:00 2001 From: mDuo13 Date: Mon, 8 Mar 2021 14:55:03 -0800 Subject: [PATCH] Use Dactyl mainlined hoverlinks code --- dactyl-config.yml | 4 +- tool/filter_unicode_header_ids.py | 75 ------------------------------- 2 files changed, 2 insertions(+), 77 deletions(-) delete mode 100644 tool/filter_unicode_header_ids.py diff --git a/dactyl-config.yml b/dactyl-config.yml index f1b784debd..ee5e44e467 100644 --- a/dactyl-config.yml +++ b/dactyl-config.yml @@ -26,7 +26,6 @@ filter_paths: default_filters: - multicode_tabs - - unicode_header_ids - buttonize - callouts - badges @@ -66,6 +65,8 @@ default_keys: &defaults # Override them with --vars to change which fork/branch to edit. github_forkurl: https://github.com/ripple/xrpl-dev-portal github_branch: master + # Hover icon markup for permalinking headers + hover_anchors: # Script tags used in a variety of tools & examples. lodash_tag: '' ripple_lib_tag: '' @@ -77,7 +78,6 @@ targets: display_name: XRPL.org <<: *defaults prefix: "" - hover_anchors: link_subs: "./src/api-v3/paths/preparations/payments.ts": "https://github.com/xpring-eng/xrp-api/blob/master/src/api-v3/paths/preparations/payments.ts" readability_goals: diff --git a/tool/filter_unicode_header_ids.py b/tool/filter_unicode_header_ids.py deleted file mode 100644 index c76c5f1f0f..0000000000 --- a/tool/filter_unicode_header_ids.py +++ /dev/null @@ -1,75 +0,0 @@ -################################################################################ -## Unicode-friendly header IDs ## -## Author: Rome Reginelli ## -## Copyright: Ripple Labs, Inc. 2016-2019 ## -## License: MIT https://github.com/ripple/xrpl-dev-portal/blob/master/LICENSE ## -## ## -## Changes the header ID formula to accept more unicode characters, so ## -## non-English, non-Latin-script headers can have nice IDs. Closely matches ## -## the behavior of GitHub-Flavored Markdown. ## -################################################################################ -import re - -from bs4 import BeautifulSoup - -## HTML5's 'id' attribute requirements are: -## - Must be at least one character in length. -## - Must not contain ASCII whitespace. -## - Must be unique within the document. -## - No other requirements. (HTML4 had stricter requirements.) - -## Python-Markdown's default ID formula (as of v3.1.1) is: -## 1. normalize unicode to NFKD form (so it can strip accent marks) -## 2. remove all non-ASCII characters (this is the problem for us) -## 3. remove leading/trailing whitespace -## 4. convert to lowercase -## 5. remove everything except space, hyphen, alphanumerics, and underscores -## 6. replace each block of consecutive whitespace/hyphens with a single hyphen -## 7. add _1, _2 etc. suffixes to non-unique IDs -## Later, the dev portal uses a filter to replace underscores with dashes. - -## The new formula is similar, but has some important differences: -## 1. No Unicode normalization. We'd rather leave things as-is. -## 2. Leave the string as unicode throughout. -## 5. Keep hyphens, unicode "space" and unicode "word" characters (includes -## underscores). Replace everything else with spaces, then strip trailing -## spaces. -## 7. Add -1, -2 suffixes to non-unique IDs. -## The end result is that this standard closely matches GitHub-flavored -## Markdown in almost all cases. (The exceptions are cases where GFM makes -## invalid empty IDs, for example with emoji headers...) - -HOVERANCHOR_FIELD = "hover_anchors" - -def idify(utext): - """Make a string ID-friendly (but more unicode-friendly)""" - utext = re.sub(r'[^\w\s-]', '', utext).strip().lower() - utext = re.sub(r'[\s-]+', '-', utext) - if not len(utext): - # Headers must be non-empty - return '_' - return utext - -def filter_soup(soup, currentpage={}, **kwargs): - """Generate new IDs for all headers""" - uniqIDs = {} - headers = soup.find_all(name=re.compile("h[0-9]")) - for h in headers: - new_id = idify(h.get_text()) - if new_id not in uniqIDs.keys(): - uniqIDs[new_id] = 0 - else: - # not unique, append -1, -2, etc. to this instance - uniqIDs[new_id] += 1 - new_id = "{id}-{n}".format(id=new_id, n=uniqIDs[new_id]) - - h["id"] = new_id - - hoveranchor_contents = currentpage.get("hover_anchors", False) - if hoveranchor_contents: - hoverlink = soup.new_tag("a", attrs={ - "href": "#"+new_id, - "class": "hover_anchor", - "aria-hidden": "true"}) - hoverlink.append(BeautifulSoup(hoveranchor_contents, "html.parser")) - h.append(hoverlink)