commit be49325e0fb1bf05618d0abfb3bcf2cf6f7f22a3
parent 720beec5df9f1b850b06aef59341778e62675c26
Author: corndog <cauchyn@firemail.cc>
Date: Sun, 14 Apr 2019 13:13:21 -0700
Add table of contents filter
Retool replacement filters (wordcount, toc) to
replace similarly typed element with particular
id.
Place filters in a filter directory.
Diffstat:
9 files changed, 75 insertions(+), 48 deletions(-)
diff --git a/config b/config
@@ -7,8 +7,8 @@ URL = https://grgr.me/
*.md:
config
inner-template
- external pandoc --to html --lua-filter wordcount.lua --lua-filter anchor.lua --lua-filter external.lua
- external python3 youtube-embed.py
+ external pandoc --to html --lua-filter filter/wordcount.lua --lua-filter filter/toc.lua --lua-filter filter/anchor.lua --lua-filter filter/external.lua
+ external python3 filter/youtube-embed.py
template page
ext .html
diff --git a/anchor.lua b/filter/anchor.lua
diff --git a/external.lua b/filter/external.lua
diff --git a/filter/toc.lua b/filter/toc.lua
@@ -0,0 +1,21 @@
+sections = {}
+
+function collect(header)
+ if header.level == 2 then
+ section = {}
+ link = pandoc.Link(header.content, "#" .. header.identifier)
+ table.insert(section, link)
+ table.insert(sections, pandoc.Plain(section))
+ end
+end
+
+function replace(el)
+ if el.identifier == "toc" then
+ toc = pandoc.BulletList(sections)
+ return toc
+ else
+ return el
+ end
+end
+
+return {{Header = collect}, {Block = replace}}
diff --git a/filter/wordcount.lua b/filter/wordcount.lua
@@ -0,0 +1,43 @@
+-- adapted from https://pandoc.org/lua-filters.html#counting-words-in-a-document
+-- license: GPL v2 per https://github.com/jgm/pandoc-website/blob/0001df4a44901b60d37ea0d06f0212bdc5283a21/CONTRIBUTING.md#patches-and-pull-requests
+-- see: http://www.gnu.org/copyleft/gpl.html
+-- Copyright 2006-2017 John MacFarlane, Copyright 2018 grgr.me
+words = 0
+minutes = 0
+wpm = 150
+
+wordcount = {
+ Str = function(el)
+ if el.text:match("%P") then
+ words = words + 1
+ end
+ end,
+
+ Code = function(el)
+ _,n = el.text:gsub("%S+", "")
+ words = words + n
+ end,
+
+ CodeBlock = function(el)
+ _,n = el.text:gsub("%S+", "")
+ words = words + n
+ end
+}
+
+function count(el)
+ pandoc.walk_block(pandoc.Div(el.blocks), wordcount)
+ minutes = math.ceil(words / wpm)
+end
+
+function replace(el)
+ if el.identifier == "time-to-read" then
+ tooltip = words .. " words at a speed of " .. wpm .. " wpm."
+ attr = pandoc.Attr("", {}, {{"class", "bold-on-hover"}, {"title", tooltip}})
+ time_to_read = "~" .. minutes .. " minutes of text*"
+ el.attr = attr
+ el.content = { pandoc.Str(time_to_read) }
+ end
+ return el
+end
+
+return {{Pandoc = count}, {Span = replace}}
diff --git a/youtube-embed.py b/filter/youtube-embed.py
diff --git a/in/me/index.md b/in/me/index.md
@@ -45,6 +45,8 @@ I believe this will be a useful (and historically interesting) exercise and inte
collect links here I would lose track of otherwise. This list is non-exhaustive and
may never be completed.
+{{ template "toc" }}
+
## Fighting Games
- [Rare footage of Daigo actually angry (Moment
diff --git a/site.tmpl b/site.tmpl
@@ -33,12 +33,16 @@
<span class="bold-on-hover">
Last modified: <time datetime="{{ .Format "2006-01-02" }}">{{ .Format "02 Jan 2006" }}</time>
</span>
-{{end}}
+{{ end }}
{{ define "proseheader" }}
<small>[{{ template "moddate" . }}]
-[<span class="bold-on-hover">\$timetoread\$</span>]</small>
-{{end}}
+[<span class="bold-on-hover" id="time-to-read"></span>]</small>
+{{ end }}
+
+{{ define "toc" }}
+<div id="toc"></div>
+{{ end }}
{{ define "page" }}
{{ template "header" . }}
diff --git a/wordcount.lua b/wordcount.lua
@@ -1,43 +0,0 @@
--- adapted from https://pandoc.org/lua-filters.html#counting-words-in-a-document
--- license: GPL v2 per https://github.com/jgm/pandoc-website/blob/0001df4a44901b60d37ea0d06f0212bdc5283a21/CONTRIBUTING.md#patches-and-pull-requests
--- see: http://www.gnu.org/copyleft/gpl.html
--- Copyright 2006-2017 John MacFarlane, Copyright 2018 grgr.me
-words = 0
-minutes = 0
-wpm = 150
-
-wordcount = {
- Str = function(el)
- if el.text:match("%P") then
- words = words + 1
- end
- end,
-
- Code = function(el)
- _,n = el.text:gsub("%S+", "")
- words = words + n
- end,
-
- CodeBlock = function(el)
- _,n = el.text:gsub("%S+", "")
- words = words + n
- end
-}
-
-function count(el)
- pandoc.walk_block(pandoc.Div(el.blocks), wordcount)
- minutes = math.ceil(words / wpm)
-end
-
-function replace(el)
- tooltip = words .. " words at a speed of " .. wpm .. " wpm."
- tooltip = pandoc.Attr("", {}, {{"title", tooltip}})
- time_to_read = "~" .. minutes .. " minutes of text*"
- if el.text == "$timetoread$" then
- return pandoc.Span(time_to_read, tooltip)
- else
- return el
- end
-end
-
-return {{Pandoc = count}, {Str = replace}}