Skip to content

Commit e923626

Browse files
committed
piratify: use some magic to generate Pirates TOC matcornic#469
1 parent b50f114 commit e923626

File tree

5 files changed

+140
-111
lines changed

5 files changed

+140
-111
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
{{- $context := .context }}
2+
{{- $content := .content }}
3+
{{- $pagefield := cond (ne $content nil) nil (.pagefield | default "Content") }}
4+
{{- $writenotice := cond (ne $content nil) false (cond (eq $pagefield "Content") true false) }}
5+
{{- $langtrg := "pir" }}
6+
{{- $langsrc := cond (eq $context.Page.Lang $langtrg) (.langsrc | default "en") $context.Page.Lang }}
7+
{{- $baseURL := urls.Parse site.BaseURL }}
8+
{{- $baseURLpath := $baseURL.Path | default "/" }}
9+
{{- $l := "" }}
10+
{{- $c := "" }}
11+
{{- range $context.Page.AllTranslations }}
12+
{{- if eq .Language.Lang $langsrc }}
13+
{{- $l = .RelPermalink }}
14+
{{- if ne $content nil }}
15+
{{- $c = $content }}
16+
{{- else if eq $pagefield "Content" }}
17+
{{- $c = .Content }}
18+
{{- else if eq $pagefield "TableOfContents" }}
19+
{{- $c = .TableOfContents }}
20+
{{- end }}
21+
{{- break }}
22+
{{- end }}
23+
{{- end }}
24+
{{- if eq $context.Page.Lang $langtrg }}
25+
{{- if $writenotice }}
26+
{{- partial "shortcodes/notice.html" (dict
27+
"context" .
28+
"content" "<p>Fello' pirrrates, wrrrit'n are not fer us as yer can see! Also be awarrre <b>some stuff may not work</b> in this trrranslat'n. Like see'n <b>Merrrmaids</b>, do'n <b>math or chemistrrry</b> and stuff.</p>\n"
29+
"icon" "skull-crossbones"
30+
"style" "warning"
31+
"title" "Arrr! Pirrrates"
32+
) }}
33+
{{- end }}
34+
{{- $words := dict
35+
"Hugo" "Cap'n Hugo"
36+
"Info" "Ahoi"
37+
"Note" "Avast"
38+
"Tip" "Smarrrt Arrrse"
39+
"Warning" "Arrr"
40+
"good" "bloody"
41+
"shortcodes" "shorrrtcodes"
42+
"Shortcodes" "Shorrrtcodes"
43+
"Mermaid" "Merrrmaid"
44+
"Markdown" "Marrrkdown"
45+
"Markup" "Marrrkup"
46+
"markup" "marrrkup"
47+
"for" "fer"
48+
"Your" "Yer"
49+
"your" "yer"
50+
"You" "Ye"
51+
"you" "ye"
52+
"the" "th'"
53+
"The" "Th'"
54+
"is" "be"
55+
"Is" "Be"
56+
"are" "be"
57+
"Are" "Be"
58+
"Of" "O'"
59+
"of" "o'"
60+
"To" "T'"
61+
"to" "t'"
62+
"in" "'n"
63+
"With" "Wit'"
64+
"with" "wit'"
65+
"Where" "Whar'"
66+
"where" "whar'"
67+
"After" "Aft"
68+
"after" "aft"
69+
"And" "An'"
70+
"and" "an'"
71+
"Load" "Board"
72+
"load" "board"
73+
"Loaded" "Boarded"
74+
"loaded" "boarded"
75+
"Content" "Rrrambling"
76+
"content" "rrrambling"
77+
"icon" "ay'con"
78+
"Icon" "Ay'con"
79+
"icons" "ay'cons"
80+
"Icons" "Ay'cons"
81+
"syntax" "rules"
82+
"Syntax" "Rules"
83+
"Site" "Ship"
84+
"site" "ship"
85+
"Page" "Plank"
86+
"page" "plank"
87+
"Pages" "Planks"
88+
"pages" "planks"
89+
"Relearn" "Relearrrn"
90+
"Learn" "Learrrn"
91+
-}}
92+
{{- $specials := dict
93+
"(\\w)ing([\\s\\n<.,;?!:])" "'n"
94+
"(\\w)ings([\\s\\n<.,;?!:])" "'ns"
95+
"(\\w)tion([\\s\\n<.,;?!:])" "t'n"
96+
"(\\w)tions([\\s\\n<.,;?!:])" "t'ns"
97+
"(\\w)(?:[aeiou])ble([\\s\\n<.,;?!:])" "'ble"
98+
"(\\w)(?:[aeiou])mize([\\s\\n<.,;?!:])" "'mize"
99+
"(\\w)(?:[aeiou])mizes([\\s\\n<.,;?!:])" "'mizes"
100+
"(\\w)(?:[aeiou])nize([\\s\\n<.,;?!:])" "'nize"
101+
"(\\w)(?:[aeiou])nizes([\\s\\n<.,;?!:])" "'nizes"
102+
(printf "(.)=\"%s([^\"]*?\")" $baseURLpath) (printf "=\"%s%s/" $baseURLpath $langtrg)
103+
-}}
104+
{{- $fix := dict
105+
"warn'n" "warning"
106+
"sect'n" "section"
107+
"n Cap'n" "n"
108+
"Avast right o' John" "Note right of John"
109+
-}}
110+
{{- range $from, $to := $words }}
111+
{{- $c = replaceRE (printf "([\\s\\n>])%s([\\s\\n<.,;?!:])" $from) (printf "${1}%s${2}" $to) $c }}
112+
{{- end }}
113+
{{- range $from, $to := $specials }}
114+
{{- $c = replaceRE $from (printf "${1}%s${2}" $to) $c }}
115+
{{- end }}
116+
{{- range $from, $to := $fix }}
117+
{{- $c = replace $c $from $to }}
118+
{{- end }}
119+
{{- if (int (len $l)) }}
120+
{{- $c = replaceRE "(src|href)=\"(images/[^\"]*?\")" (printf "${1}=\"%s${2}" $l) $c }}
121+
{{- end }}
122+
{{- $c = replaceRE "(src|href)=\"([^\"]*?)/pir/([^\"]*?.files/[^\"]*?\")" "${1}=\"${2}/${3}" $c }}
123+
{{- end }}
124+
{{ $c | safeHTML }}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div class="default-animation progress">
2+
<div class="toc-wrapper" dir="{{ T "Reading-direction" | default "ltr" }}">
3+
{{- partial "shortcodes/piratify.html" (dict
4+
"context" .
5+
"pagefield" "TableOfContents"
6+
) }}
7+
</div>
8+
</div>
Lines changed: 4 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,4 @@
1-
{{- $langsrc := .Get 1 | default "en" }}
2-
{{- $langtrg := .Page.Language.Lang }}
3-
{{- $baseURL := urls.Parse .Site.BaseURL }}
4-
{{- $baseURLpath := $baseURL.Path | default "/" }}
5-
{{- $words := dict
6-
"Hugo" "Cap'n Hugo"
7-
"Info" "Ahoi"
8-
"Note" "Avast"
9-
"Tip" "Smarrrt Arrrse"
10-
"Warning" "Arrr"
11-
"good" "bloody"
12-
"shortcodes" "shorrrtcodes"
13-
"Shortcodes" "Shorrrtcodes"
14-
"Mermaid" "Merrrmaid"
15-
"Markdown" "Marrrkdown"
16-
"Markup" "Marrrkup"
17-
"markup" "marrrkup"
18-
"for" "fer"
19-
"Your" "Yer"
20-
"your" "yer"
21-
"You" "Ye"
22-
"you" "ye"
23-
"the" "th'"
24-
"The" "Th'"
25-
"is" "be"
26-
"Is" "Be"
27-
"are" "be"
28-
"Are" "Be"
29-
"Of" "O'"
30-
"of" "o'"
31-
"To" "T'"
32-
"to" "t'"
33-
"in" "'n"
34-
"With" "Wit'"
35-
"with" "wit'"
36-
"Where" "Whar'"
37-
"where" "whar'"
38-
"After" "Aft"
39-
"after" "aft"
40-
"And" "An'"
41-
"and" "an'"
42-
"Load" "Board"
43-
"load" "board"
44-
"Loaded" "Boarded"
45-
"loaded" "boarded"
46-
"Content" "Rrrambling"
47-
"content" "rrrambling"
48-
"icon" "ay'con"
49-
"Icon" "Ay'con"
50-
"icons" "ay'cons"
51-
"Icons" "Ay'cons"
52-
"syntax" "rules"
53-
"Syntax" "Rules"
54-
"Site" "Ship"
55-
"site" "ship"
56-
"Page" "Plank"
57-
"page" "plank"
58-
"Pages" "Planks"
59-
"pages" "planks"
60-
"Relearn" "Relearrrn"
61-
"Learn" "Learrrn"
62-
-}}
63-
{{- $specials := dict
64-
"(\\w)ing([\\s\\n<.,;?!:])" "'n"
65-
"(\\w)ings([\\s\\n<.,;?!:])" "'ns"
66-
"(\\w)tion([\\s\\n<.,;?!:])" "t'n"
67-
"(\\w)tions([\\s\\n<.,;?!:])" "t'ns"
68-
"(\\w)(?:[aeiou])ble([\\s\\n<.,;?!:])" "'ble"
69-
"(\\w)(?:[aeiou])mize([\\s\\n<.,;?!:])" "'mize"
70-
"(\\w)(?:[aeiou])mizes([\\s\\n<.,;?!:])" "'mizes"
71-
"(\\w)(?:[aeiou])nize([\\s\\n<.,;?!:])" "'nize"
72-
"(\\w)(?:[aeiou])nizes([\\s\\n<.,;?!:])" "'nizes"
73-
(printf "(.)=\"%s([^\"]*?\")" $baseURLpath) (printf "=\"%s%s/" $baseURLpath $langtrg)
74-
-}}
75-
{{- $fix := dict
76-
"warn'n" "warning"
77-
"sect'n" "section"
78-
"n Cap'n" "n"
79-
"Avast right o' John" "Note right of John"
80-
-}}
81-
{{- $c := "" }}
82-
{{- range .Page.Translations }}
83-
{{- if eq .Language.Lang $langsrc }}
84-
{{- $l := .RelPermalink }}
85-
{{- $c = .Content }}
86-
{{- range $from, $to := $words }}
87-
{{- $c = replaceRE (printf "([\\s\\n>])%s([\\s\\n<.,;?!:])" $from) (printf "${1}%s${2}" $to) $c }}
88-
{{- end }}
89-
{{- range $from, $to := $specials }}
90-
{{- $c = replaceRE $from (printf "${1}%s${2}" $to) $c }}
91-
{{- end }}
92-
{{- range $from, $to := $fix }}
93-
{{- $c = replace $c $from $to }}
94-
{{- end }}
95-
{{- $c = replaceRE "(src|href)=\"(images/[^\"]*?\")" (printf "${1}=\"%s${2}" $l) $c }}
96-
{{- $c = replaceRE "(src|href)=\"([^\"]*?)/pir/([^\"]*?.files/[^\"]*?\")" "${1}=\"${2}/${3}" $c }}
97-
{{- end }}
98-
{{- end }}
99-
{{- partial "shortcodes/notice.html" (dict
100-
"context" .
101-
"content" "<p>Fello' pirrates, be awarrre <b>some stuff may not work</b> fer us in this trrranslat'n. Like <b>table of rrramblings</b>, see'ng <b>Merrrmaids</b>, do'ng <b>math or chemistrrry</b> and stuff.</p>\n"
102-
"icon" "skull-crossbones"
103-
"style" "warning"
104-
"title" "Arrr! Pirrrates"
105-
) }}
106-
107-
{{ $c | safeHTML }}
1+
{{- partial "shortcodes/piratify.html" (dict
2+
"context" .
3+
"pagefield" "Content"
4+
) }}

layouts/partials/header.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@
108108
{{- end }}
109109
{{- $defaultDisableToc := .Site.Params.disableToc | default false }}
110110
{{- $currentDisableToc := .Params.disableToc | default $defaultDisableToc }}
111-
{{- $hastoc := not (eq 0 (int (len (.TableOfContents | plainify)))) }}
111+
{{- $toc_content := partial "toc.html" . }}
112+
{{- $hastoc := not (eq 0 (int (len ($toc_content | plainify)))) }}
112113
{{- $toc := and $hastoc (not $currentDisableToc) (eq $outputFormat "html") }}
113114
<div id="breadcrumbs">
114115
<span id="sidebar-toggle-span">
@@ -129,7 +130,7 @@
129130
{{- end }}
130131
</div>
131132
{{- if $toc }}
132-
{{- partial "toc.html" . }}
133+
{{ $toc_content }}
133134
{{- end }}
134135
</div>
135136
</nav>

layouts/partials/toc.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
21
<div class="default-animation progress">
32
<div class="toc-wrapper" dir="{{ T "Reading-direction" | default "ltr" }}">
4-
{{ .TableOfContents }}
3+
{{- .TableOfContents }}
54
</div>
65
</div>

0 commit comments

Comments
 (0)