|
25 | 25 |
|
26 | 26 | (define this-book-title "Advanced Bash-Scripting Guide")
|
27 | 27 |
|
28 |
| -;; Collect a list footnotes in a page |
29 |
| -;; (Lis of a lists, each note is an x-expression) |
30 |
| -(define footnotes null) |
31 |
| - |
32 |
| -;; Footnotes need to be reset at the beginning of each page |
33 |
| -(define (footnotes-reset) |
34 |
| - (set! footnotes null)) |
| 28 | +;; All footnotes of the book are collected into a hash table |
| 29 | +;; |
| 30 | +;; key: the 'here-path of each individual page |
| 31 | +;; value: the list of all footnotes for that page, each footnote is an |
| 32 | +;; x-expression |
| 33 | +(define footnotes-book (make-hash)) |
35 | 34 |
|
36 | 35 | ;; Init entities per page
|
37 | 36 | (define (page-init)
|
38 |
| - (footnotes-reset)) |
| 37 | + ;; This function was used some time ago, it is not used anymore; it |
| 38 | + ;; is still on every page -- return a harmless value |
| 39 | + `(span)) |
39 | 40 |
|
40 | 41 | ;; footnotes-render:
|
41 | 42 | ;; Render footnotes as an x-expression to be placed at the bottom of
|
42 | 43 | ;; the page
|
43 |
| -(define (footnotes-render) |
| 44 | +(define (footnotes-render footnotes) |
44 | 45 | (define (a-footnote note-elements)
|
45 | 46 | (let ([a-index (+ 1 (index-of footnotes note-elements))])
|
46 | 47 | ; (printf "~a~n" a-index)
|
|
54 | 55 | `((div [[class "footnotes"]]
|
55 | 56 | ,@(map a-footnote footnotes)))))
|
56 | 57 |
|
| 58 | +;; Override document root to insert footnotes rendering |
57 | 59 | (define (root . elements)
|
58 | 60 | (case (current-poly-target)
|
59 | 61 | [(html)
|
60 | 62 | ; (printf "~a~n" elements)
|
61 |
| - `(root |
62 |
| - ,@(decode-elements elements |
63 |
| - #:txexpr-elements-proc decode-paragraphs-flow |
64 |
| - #:exclude-tags '(script)) |
65 |
| - ,@(footnotes-render))] |
66 |
| - ;; (txexpr 'root '() (decode-elements elements |
67 |
| - ;; #:txexpr-elements-proc decode-paragraphs-flow))] |
68 |
| - ;; `else' -- passthrough without changes |
| 63 | + (let* ([cur-page-fpath (hash-ref (current-metas) 'here-path)] |
| 64 | + [cur-page-footnotes (hash-ref footnotes-book cur-page-fpath '())]) |
| 65 | + `(root |
| 66 | + ;; Render the page elements |
| 67 | + ,@(decode-elements elements |
| 68 | + #:txexpr-elements-proc decode-paragraphs-flow |
| 69 | + #:exclude-tags '(script)) |
| 70 | + ;; Followed by the footnotes (if any) |
| 71 | + ,@(footnotes-render cur-page-footnotes)))] |
| 72 | + ;; 'else' -- passthrough without changes |
69 | 73 | [else `(root ,@elements)]))
|
70 | 74 |
|
71 | 75 | ;; Two '\n' mark a paragraph, single '\n' is ignored (doesn't generate
|
|
247 | 251 | (string-append* elements)
|
248 | 252 | "}")]
|
249 | 253 | [(html)
|
250 |
| - ;; Collect into footnotes |
251 |
| - (set! footnotes (append footnotes (list elements))) |
252 |
| - ;; Render a link to jump to the footnote |
253 |
| - ; (printf "~a: ~a~n" (length footnotes) footnotes) |
254 |
| - (let* ([fn-index (length footnotes)] |
255 |
| - [fn-index-id (format "_footnoteref_~a" fn-index)] |
256 |
| - [fn-index-jump (format "#_footnotedef_~a" fn-index)] |
257 |
| - [fn-index-str (format "[~a]" fn-index)]) |
258 |
| - `(a [[id ,fn-index-id] [href ,f\n-index-jump]] ,fn-index-str))] |
| 254 | + ;; Collect into footnotes-book |
| 255 | + ;; --- |
| 256 | + (let* ([cur-page-fpath (hash-ref (current-metas) 'here-path)] |
| 257 | + ;; cur-page-footnotes: all collected or empty list |
| 258 | + [cur-page-footnotes (hash-ref footnotes-book cur-page-fpath '())] |
| 259 | + [new-footnote (append cur-page-footnotes (list elements))] |
| 260 | + [fn-index (add1 (length cur-page-footnotes))]) |
| 261 | + ;; Override entry in hash table with the new list |
| 262 | + (hash-set! footnotes-book cur-page-fpath new-footnote) |
| 263 | + ; (printf "~a: [~a] ~a~n" cur-page-fpath fn-index new-footnote) |
| 264 | + (let ([fn-index-id (format "_footnoteref_~a" fn-index)] |
| 265 | + [fn-index-jump (format "#_footnotedef_~a" fn-index)] |
| 266 | + [fn-index-str (format "[~a]" fn-index)]) |
| 267 | + `(a [[id ,fn-index-id] [href ,f\n-index-jump]] ,fn-index-str)))] |
259 | 268 | ;; else (txt)
|
260 | 269 | [else (string-append* elements)]))
|
261 | 270 |
|
|
0 commit comments