summaryrefslogtreecommitdiffstats
path: root/site/cleopatra/Soupault.org
blob: 396f4217c51848e8d2d354ac7f352a6d8c676ce9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#+BEGIN_EXPORT html
<h1><code>soupault</code> Configuration</h1>
#+END_EXPORT

#+TOC: headlines 2

* General Settings

#+NAME: prefix
#+BEGIN_SRC text
~lthms
#+END_SRC

#+BEGIN_SRC toml :tangle soupault.conf :noweb tangle
[settings]
  strict = true
  verbose = false
  debug = false
  site_dir = "site"
  build_dir = "build/<<prefix>>"

  page_file_extensions = ["html"]
  ignore_extensions = [
    "draft", "vo", "vok", "vos", "glob",
    "html~", "org", "aux", "sass",
  ]

  generator_mode = true
  complete_page_selector = "html"
  default_template = "templates/main.html"
  content_selector = "main"
  doctype = "<!DOCTYPE html>"
  clean_urls = false
#+END_SRC

* Widgets

** Setting Page Title

#+BEGIN_SRC toml :tangle soupault.conf
[widgets.page-title]
  widget = "title"
  selector = "h1"
  default = "~lthms"
  prepend = "~lthms: "
#+END_SRC

** Acknowledging ~soupault~

#+BEGIN_SRC toml :tangle soupault.conf
[widgets.generator-meta]
  widget = "insert_html"
  html = '<meta name="generator" content="soupault 1.5">'
  selector = "head"
#+END_SRC

** Generating Table of Contents

#+BEGIN_SRC toml :tangle soupault.conf
[widgets.table-of-contents]
  widget = "toc"
  selector = "div#generate-toc"
  action = "replace_element"
  min_level = 2
  numbered_list = true
#+END_SRC

** Rewriting URLs

#+BEGIN_SRC lua :tangle plugins/urls-rewriting.lua
function prefix_urls (links, attr, prefix_url)
  index, link = next(links)

  while index do
    href = HTML.get_attribute(link, attr)

    if href then
      -- remove prefix sometimes introduced by org
      href = Regex.replace(href, "^file://", "")

      -- Check if URL starts with a leading "/"
      if Regex.match(href, "^/") then
        href = Regex.replace(href, "^/*", "")
        href = prefix_url .. href
        HTML.set_attribute(link, attr, href)
      end
    end
    index, link = next(links, index)
  end
end

prefix_url = config["prefix_url"]

if not prefix_url then
  prefix_url = ""
end

if not Regex.match(prefix_url, "^/(.*)") then
  prefix_url = "/" .. prefix_url
end

if not Regex.match(prefix_url, "(.*)/$") then
  prefix_url = prefix_url .. "/"
end

prefix_urls(HTML.select(page, "a"), "href", prefix_url)
prefix_urls(HTML.select(page, "link"), "href", prefix_url)
prefix_urls(HTML.select(page, "img"), "src", prefix_url)
prefix_urls(HTML.select(page, "script"), "src", prefix_url)
#+END_SRC

#+BEGIN_SRC toml :tangle soupault.conf :noweb tangle
[plugins.urls-rewriting]
  file = "plugins/urls-rewriting.lua"

[widgets.urls-rewriting]
  widget = "urls-rewriting"
  prefix_url = "<<prefix>>"
#+END_SRC

** Marking External Links

#+BEGIN_SRC lua :tangle plugins/external-urls.lua
function mark(name)
  return '<i class="url-mark fa fa-' .. name ..
         '" aria-hidden="true"></i>'
end

links = HTML.select(page, "a")

index, link = next(links)

while index do
  href = HTML.get_attribute(link, "href")

  if href then
    if Regex.match(href, "^https?://github.com") then
      icon = HTML.parse(mark('github'))
      HTML.append_child(link, icon)
    elseif Regex.match(href, "^https?://") then
      icon = HTML.parse(mark('external-link'))
      HTML.append_child(link, icon)
    end
  end

  index, link = next(links, index)
end
#+END_SRC

#+BEGIN_SRC sass :tangle site/style/plugins.sass
.url-mark.fa
    display: inline
    font-size: 90%
    width: 1em

.url-mark.fa-github::before
    content: "\00a0\f09b"

.url-mark.fa-external-link::before
    content: "\00a0\f08e"
#+END_SRC

#+BEGIN_SRC toml :tangle soupault.conf
[plugins.external_links]
  file = "plugins/external-urls.lua"

[widgets.mark-external-urls]
  after = "generate-history"
  widget = "external_links"
#+END_SRC

** Generating Per-File Revisions Tables

*** Users Instructions

This widgets allows to generate a so-called “revisions table” of the filename
contained in a DOM element of id ~history~, based on its history. Paths should
be relative to the directory from which you start the build process (typically,
the root of your repository). The revisions table notably provides hyperlinks to
a ~git~ webview for each commit.

For instance, considering the following HTML snippet

#+BEGIN_SRC html
<div id="history">site/posts/FooBar.org</div>
#+END_SRC

will replace the content of this ~<div>~ with the revisions table of
~site/posts/FooBar.org~.

*** Implementations Details

#+BEGIN_TODO
This plugin fails silently.
#+END_TODO

The base of the URL webview for the document you are currently reading
—afterwards abstracted with the ~<<repo>>~ noweb reference— is

#+NAME: repo
#+BEGIN_SRC text
https://code.soap.coffee/writing/lthms.git
#+END_SRC

This plugin proceeds as follows:

1. Using an ad-hoc script, it generates a JSON containing for each revision
   - The subject, date, hash, and abbreviated hash of the related commit
   - The name of the file at the time of this commit
2. This JSON is passed to a mustache engine (~haskell-mustache~) with a
   proper template
3. The content of the selected DOM element is replaced with the output of
   ~haskell-mustache~

#+BEGIN_SRC bash :tangle scripts/history.sh :tangle-mode (identity #o755)
#!/usr/bin/bash

FORMAT="{\"subject\":\"%s\",\"abbr_hash\":\"%h\",\"hash\":\"%H\",\"date\":\"%cs\""

function generate_history_json () {
    local file="${1}"

    local logs=$(git --no-pager log --follow --pretty=format:"${FORMAT}" "${file}")

    if [ ! $? -eq 0 ]; then
        exit 1
    fi

    local count=0
    local name="${file}"

    while read -r line; do
        local hash=$(echo "${line}}" | jq -j '.hash')

        local pre_name="$(git --no-pager show --stat=10000 ${hash} | sed -e 's/ *\(.*\){\(.*\) => \(.*\)}/\1\2 => \1\3/'  | grep "=> ${name}" | xargs | cut -d' ' -f1)"

        if [[ ${count} -eq 0 ]]; then
            echo -n "[ "
        else
            echo -n ", "
        fi

        echo "${line}, \"filename\":\"${name}\"}"

        if [[ ! -z "${pre_name}" ]]; then
            name="$(echo ${pre_name})"
        fi

        count=$(( ${count} + 1 ))
    done < <(echo "${logs}")

    echo -n "]"
}

function generate_json () {
  local file="${1}"

  echo "{"
  echo "  \"file\" : \"${file}\","
  echo "  \"history\" : $(generate_history_json "${file}")"
  echo "}"
}

FILE=`cat`

tmp_file=$(mktemp)
generate_json ${FILE} > ${tmp_file}
haskell-mustache ${1} ${tmp_file}
rm ${tmp_file}
#+END_SRC

#+BEGIN_SRC html :tangle templates/history.html :noweb tangle
<details class="history">
  <summary>Revisions</summary>
  <p>
    This revisions table has been automatically generated from
    <a href="<<repo>>">the
    <code>git</code> history of this website repository</a>,
    and the change descriptions may not always be as useful as
    they should. You can consult the source of this file
    in its current version
    <a href="<<repo>>/tree/{{file}}">here</a>.
  </p>

  <table>
  {{#history}}
  <tr>
    <td class="date">{{date}}</a></td>
    <td class="subject">{{subject}}</a></td>
    <td class="commit">
      <a href="<<repo>>/commit/{{filename}}/?id={{hash}}">
        {{abbr_hash}}
      </a>
    </td>
  </tr>
  {{/history}}
  </table>
</details>
#+END_SRC

#+BEGIN_SRC sass :tangle site/style/plugins.sass
#history
  summary
    color: $primary-color
    font-weight: bold

  table
    border-top: 2px solid $primary-color
    border-bottom: 2px solid $primary-color
    border-collapse: collapse;

  td
    border-bottom: 1px solid $primary-color
    padding: .5em
    vertical-align: top

  td.commit
    font-size: smaller

  td.commit
    font-family: 'Fira Code', monospace
    color: $code-fg-color
    font-size: 80%
    white-space: nowrap;
#+END_SRC

#+BEGIN_SRC toml :tangle soupault.conf
[widgets.generate-history]
  widget = "preprocess_element"
  selector = "#history"
  command = "./scripts/history.sh \"templates/history.html\""
  action = "replace_content"
#+END_SRC

** Rendering Equations Offline

*** Users instructions

Inline equations written in the DOM under the class src_css{.imath} and using
the @@html:<span class="imath">\LaTeX</span>@@ syntax can be rendered once and
for all by ~soupault~. User For instance, ~<span class="imath">\LaTeX</span>~ is
rendered @@html:<span class="imath">\LaTeX</span>@@.

Using this widgets requires being able to inject raw HTML in input files.

*** Implementation details

We will use [[https://katex.org][@@html:<span class="imath">\KaTeX</span>@@]] to render equations
offline. @@html:<span class="imath">\KaTeX</span>@@ availability on most systems
is unlikely, but it is part of [[https://www.npmjs.com/package/katex][npm]], so we can define a minimal ~package.json~
file to fetch it automatically.

#+BEGIN_SRC json :tangle package.json
{
  "private": true,
  "devDependencies": {
    "katex": "^0.11.1"
  }
}
#+END_SRC

We introduce a Makefile recipe to call ~npm install~. This command produces a
file called ~package-lock.json~ that we add to ~GENFILES~ to ensure @@html:<span
class="imath">\KaTeX</span>@@ will be available when ~soupault~ is called.

#+BEGIN_REMARK
If ~Soupault.org~ has been modified since the last generation, Babel will
generate ~package.json~ again. However, if the modifications of ~Soupault.org~
do not concern ~package.json~, then ~npm install~ will not modify
~package-lock.json~ and its “last modified” time will not be updated. This means
that the next time ~make~ will be used, it will replay this recipe again. As a
consequence, we systematically ~touch~ ~packase-lock.json~ to satisfy ~make~.
#+END_REMARK

#+BEGIN_SRC makefile :tangle soupault.mk
package-lock.json : package.json
	@echo "    init  npm packages"
	@npm install
	@touch $@

GENFILES += package-lock.json
GENAUX += node_modules/
#+END_SRC

Once installed and available, @@html:<span class="imath">\KaTeX</span>@@ is
really simple to use. The following script reads (synchronously!) the standard
input, renders it using @@html:<span class="imath">\KaTeX</span>@@ and outputs
the resut to the standard output.

#+BEGIN_TODO
This script should be generalized to handle both display and inline
mode. Currently, only inline mode is supported.
#+END_TODO

#+BEGIN_SRC js :tangle scripts/katex.js
var katex = require("katex");
var fs = require("fs");
var input = fs.readFileSync(0);

var html = katex.renderToString(String.raw`${input}`, {
    throwOnError: false
});

console.log(html)
#+END_SRC

We reuse once again the =preprocess_element= widget. The selector is ~.imath~
(~i~ stands for inline in this context), and we replace the previous content
with the result of our script.

#+BEGIN_SRC toml :tangle soupault.conf
[widgets.inline-math]
  widget = "preprocess_element"
  selector = ".imath"
  command = "node scripts/katex.js"
  action = "replace_content"
#+END_SRC

The @@html:<span class="imath">\KaTeX</span>@@ font is bigger than the serif
font used for this website, so we reduce it a bit with a dedicated SASS rule.

#+BEGIN_SRC sass :tangle site/style/plugins.sass
.imath
  font-size: smaller
#+END_SRC