Refactor `create_optional_css` - Original logic was kept unchanged - The result is now accumulated as a `seq[string]` and joined into a string just once at function return - `style_tag` is now a template
Zoom zoomrmc+git@gmail.com
Sun, 17 Sep 2023 22:48:04 +0400
2 files changed,
20 insertions(+),
22 deletions(-)
M
src/hastyscribe.nim
→
src/hastyscribe.nim
@@ -353,39 +353,37 @@ except CatchableError:
warn obj & " not found: " & key proc create_optional_css*(hs: HastyScribe, document: string): string = - result = "" - let html = document.parseHtml + let html = document.parseHtml() + var rules: seq[string] # Check icons - let iconRules = html.querySelectorAll("span[class^=fa-]") - .mapIt(getTableValue(hs.iconStyles, it.attr("class"), "Icon")) - result &= iconRules.join("\n") + for icon in html.querySelectorAll("span[class^=fa-]"): + rules.add getTableValue(hs.iconStyles, icon.attr("class"), "Icon") # Check badges - let badgeRules = html.querySelectorAll("span[class^=badge-]") - .mapIt(getTableValue(hs.badgeStyles, it.attr("class"), "Badge")) - result &= badgeRules.join("\n") + for badge in html.querySelectorAll("span[class^=badge-]"): + rules.add getTableValue(hs.badgeStyles, badge.attr("class"), "Badge") # Check notes - let noteRules = html.querySelectorAll("div.tip, div.warning, div.note, div.sidebar") - .mapIt(getTableValue(hs.noteStyles, it.attr("class"), "Note")) - result &= noteRules.join("\n") + for note in html.querySelectorAll("div.tip, div.warning, div.note, div.sidebar"): + rules.add getTableValue(hs.noteStyles, note.attr("class"), "Note") # Check links - let linkHrefs = html.querySelectorAll("a[href]") - .mapIt(it.attr("href")) - var linkRules = newSeq[string]() + let linkHrefs = html.querySelectorAll("a[href]").mapIt(it.attr("href")) + var linkRulesSet: CritBitTree[void] # Add #document-top rule because it is always needed and added at the end. - linkRules.add hs.linkStyles["^='#document-top"] + rules.add hs.linkStyles["^='#document-top"] + for href in linkHrefs: for (key, val) in hs.linkStyles.pairs: - if val notin linkRules: + if val notin linkRulesSet: let op = key[0..1] let value = key[3..^1] # Skip first ' - # Save matches in order of priority + # TODO: debug logic + # Save matches in order of priority (?) if (op == "$=" and href.endsWith(value)) or (op == "*=" and href.contains(value)) or (op == "^=" and href.startsWith(value)): - linkRules.add val + linkRulesSet.incl val + rules.add val break - result &= linkRules.join("\n") - result = result.style_tag + rules.join("\n").style_tag() # Public API
M
src/hastyscribepkg/utils.nim
→
src/hastyscribepkg/utils.nim
@@ -8,8 +8,8 @@
import consts -proc style_tag*(css: string): string = - result = "<style>$1</style>" % [css] +template style_tag*(css: string): string = + "<style>" & css & "</style>" proc style_link_tag*(css: string): string = result = "<link rel=\"stylesheet\" href=\"$1\"/>" % [css]