Skip to content

Commit 00d89ef

Browse files
committed
foonotes: Rework footnotes to work across the entire book, not per page
* A performance optimization in Pollen re-uses the context for running pollen.rkt, this means that the footnotes are shared between pages * Make the footnotes to be a hash table, where the page file name is the key
1 parent dae7cdd commit 00d89ef

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

pollen.rkt

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,23 @@
2525

2626
(define this-book-title "Advanced Bash-Scripting Guide")
2727

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))
3534

3635
;; Init entities per page
3736
(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))
3940

4041
;; footnotes-render:
4142
;; Render footnotes as an x-expression to be placed at the bottom of
4243
;; the page
43-
(define (footnotes-render)
44+
(define (footnotes-render footnotes)
4445
(define (a-footnote note-elements)
4546
(let ([a-index (+ 1 (index-of footnotes note-elements))])
4647
; (printf "~a~n" a-index)
@@ -54,18 +55,21 @@
5455
`((div [[class "footnotes"]]
5556
,@(map a-footnote footnotes)))))
5657

58+
;; Override document root to insert footnotes rendering
5759
(define (root . elements)
5860
(case (current-poly-target)
5961
[(html)
6062
; (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
6973
[else `(root ,@elements)]))
7074

7175
;; Two '\n' mark a paragraph, single '\n' is ignored (doesn't generate
@@ -247,15 +251,20 @@
247251
(string-append* elements)
248252
"}")]
249253
[(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)))]
259268
;; else (txt)
260269
[else (string-append* elements)]))
261270

0 commit comments

Comments
 (0)