Skip to content

Commit

Permalink
Always wrap p in sidenotes. Remove p from first list paragraph.
Browse files Browse the repository at this point in the history
  • Loading branch information
treeman committed Jun 2, 2020
1 parent 5ed64ae commit 416753f
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 31 deletions.
2 changes: 1 addition & 1 deletion TODO.diff
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ http://apex.infogridpacific.com/df/epub-type-epubpackaging8.html
- uncensorable donations formatting
- better formatting for unbanked maps

- remove paragraph from beginning of list item
- sidenotes could use epub3 tags? for styling?
- sidenote-group may sometimes create two divs but one would suffice

Need a designer for a cover
+ Find similar covers for the genre
Expand Down
4 changes: 3 additions & 1 deletion rkt/decode.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

(require pollen/tag pollen/decode txexpr)
(require "string-process.rkt")
(require "ebook-transform.rkt")
(require racket/pretty)

(provide (all-defined-out))

(define (std-decode args)
(decode-elements args
#:txexpr-elements-proc decode-paragraphs
#:txexpr-elements-proc (compose1 decode-paragraphs
ebook-transform)
#:string-proc string-proc
#:exclude-tags `(figure pre)))

48 changes: 48 additions & 0 deletions rkt/ebook-transform.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#lang racket/base

(require txexpr)

(provide (all-defined-out))

(define (ebook-transform x)
(map transform-expr x))

(define (transform-expr e)
;(printf "e: ~a~n" e)
(if (txexpr? e)
(if (eq? (get-tag e) 'li)
(txexpr 'li
(get-attrs e)
(strip-first-p (get-elements e)))
e)
e))

(define (strip-first-p es)
;(printf "es: ~a~n" es)
;(printf "car: ~a~n" (car es))
;(printf "car: ~a~n" (cdr es))
(if (null? es)
es
(append (strip-p (car es))
(cdr es))))

(define (strip-p x)
;(printf "strip ~a~n" x)
(if (txexpr? x)
(if (eq? (get-tag x) 'p)
(get-elements x)
(list x))
(list x)))

(module+ test
(require rackunit)
(check-equal? (ebook-transform `((div "foo")))
`((div "foo")))
(check-equal? (ebook-transform `((li (p "one") (p "two"))))
`((li "one" (p "two"))))
(check-equal? (ebook-transform `((li "x")))
`((li "x")))
(check-equal? (ebook-transform `((li "a" "b" "c")))
`((li "a" "b" "c")))
)

2 changes: 1 addition & 1 deletion rkt/post-process.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(require txexpr pollen/decode)
(require racket/match racket/list racket/string)

(provide elem-remover style-remover)
(provide elem-remover style-remover matches-class)


;; Remove all elements with matching class.
Expand Down
174 changes: 151 additions & 23 deletions rkt/sidenotes.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
(require racket/match racket/string racket/list)
(require "string-process.rkt")
(require "decode.rkt")
(require "post-process.rkt")
(require racket/pretty)


(provide ndef sn mn note-pos decode-sidenotes clear-sidenotes)

(define (ref-symbol ref-in)
Expand Down Expand Up @@ -140,9 +142,10 @@

(define (decode-and-append-txexpr x acc)
(let ((decoded (decode-txexpr x)))
(append (car decoded)
(append (expand-sidenote-defs (cdr decoded))
acc))))
(merge-sidenote-groups
(append (car decoded)
(append (expand-sidenote-defs (cdr decoded))
acc)))))

;; Decode a txexpr into
;; ((list decoded-txexpr) . (list notes-to-place-after))
Expand Down Expand Up @@ -217,15 +220,36 @@

(cons decoded-list notes-after))

(define (sidenote-group? x)
(matches-class x `("sidenote-group")))

(define (merge-sidenote-groups xs)
(reverse
(foldl (λ (x acc)
(if (sidenote-group? x)
(if (empty? acc)
`(,x)
(let ((prev (car acc)))
(if (sidenote-group? prev)
(cons
`(div ((class "sidenote-group"))
,@(get-elements prev)
,@(get-elements x)
)
(cdr acc))
(cons x acc))))
(cons x acc)))
`()
xs)))

(define (expand-sidenote-defs #:sort [sort? #t] notes)
(if (empty? notes)
`()
(std-decode
`((div ((class "sidenote-group"))
,@(map expand-sidenote
(if sort?
(sort notes note<)
notes)))))))
`((div ((class "sidenote-group"))
,@(map expand-sidenote
(if sort?
(sort notes note<)
notes))))))

(define (note< a b)
(define a-sign (note-sign a))
Expand Down Expand Up @@ -294,9 +318,24 @@
(define def (note-def note))
(define attrs `((class "sidenote")))

(define decoded
(std-decode `(,@(label note) ,@def)))
(define expanded
(ensure-p decoded))

`(div ,attrs
,@(label note)
,@def))
,@expanded))

(define (ensure-p xs)
(if (empty? xs)
xs
(if (and (pair? xs) (txexpr? (car xs)))
(let ((tag (get-tag (car xs))))
(if (equal? tag 'p)
xs
`((p ,@xs))))
`((p ,@xs)))))


;; A larger test to test the sidenote placement.
(module+ test
Expand Down Expand Up @@ -326,21 +365,110 @@
;; but that's too much work for me now. Meh.
(define expected
`((p "One."
(span ((class "sidenote-label")) "1")
" Two." (span ((class "sidenote-label")) "2")
" Three." (span ((class "sidenote-label")) "3"))
(div ((class "sidenote")) (span ((class "sidenote-number")) "1")
"1st")
(div ((class "sidenote")) (span ((class "sidenote-number")) "2")
"2nd")
(sup ((class "sidenote-number")) "1")
" Two."
(sup ((class "sidenote-number")) "2")
" Three."
(sup ((class "sidenote-number")) "3"))
(div ((class "sidenote-group"))
(div ((class "sidenote"))
(p (sup ((class "sidenote-number")) "1")
"1st"))
(div ((class "sidenote"))
(p (sup ((class "sidenote-number")) "2")
"2nd")))
(ol
(li "a" (span ((class "sidenote-label")) "4"))
(li "a" (sup ((class "sidenote-number")) "4"))
(li "b"))
(div ((class "sidenote")) (span ((class "sidenote-number")) "4")
"In list.")
(div ((class "sidenote")) (span ((class "sidenote-number")) "3")
"3rd")))
(div ((class "sidenote-group"))
(div ((class "sidenote"))
(p (sup ((class "sidenote-number")) "4")
"In list."))
(div ((class "sidenote"))
(p (sup ((class "sidenote-number")) "3")
"3rd")))))

(check-equal? (decode-sidenotes input) expected)

;; Another test
(clear-sidenotes)

(ndef "one" "one")
(ndef "two" "two")
(ndef "three" "one" "\n\n" "two" "\n\n" "three")

(define one (sn "one"))
(define two (mn "two"))
(define three (sn "three"))

(define input2
`((p "One." ,one " Two." ,two " Three." ,three)))

(define expected2
`((p "One."
(sup ((class "sidenote-number")) "1")
" Two."
" Three."
(sup ((class "sidenote-number")) "2"))
(div
((class "sidenote-group"))
(div ((class "sidenote"))
(p (sup ((class "sidenote-number")) "2") "one")
(p "two")
(p "three"))
(div ((class "sidenote"))
(p "two"))
(div ((class "sidenote"))
(p (sup ((class "sidenote-number")) "1") "one")))
)
)

(check-equal? (decode-sidenotes input2) expected2)

(check-equal? (ensure-p
`((p (sup ((class "sidenote-number")) "1")
"one")
(p "two")))
`((p (sup ((class "sidenote-number")) "1")
"one")
(p "two")))
(check-equal? (ensure-p
`((sup ((class "sidenote-number")) "1")
"one"))
`((p (sup ((class "sidenote-number")) "1")
"one")))
(check-equal? (ensure-p `("two"))
`((p "two")))
(check-equal? (ensure-p
`())
`())

(check-equal? (sidenote-group?
`(div
((class "sidenote-group"))
(div ((class "sidenote"))
(p (sup ((class "sidenote-number")) "1") "one"))))
#t)
(check-equal? (sidenote-group?
`(div
((class "sidenote"))
"one"))
#f)

(check-equal? (merge-sidenote-groups
`((div
((class "sidenote-group"))
(div ((class "sidenote"))
(p (sup ((class "sidenote-number")) "1") "one")))
(div
((class "sidenote-group"))
(div ((class "sidenote"))
(p (sup ((class "sidenote-number")) "2") "two")))))
`((div
((class "sidenote-group"))
(div ((class "sidenote"))
(p (sup ((class "sidenote-number")) "1") "one"))
(div ((class "sidenote"))
(p (sup ((class "sidenote-number")) "2") "two")))))
)

5 changes: 0 additions & 5 deletions rkt/tags.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,6 @@
`(h3
(a [[id ,(to-name x)]] ,x)))

(define (li-plus . txt)
`(li ((class "plus")) ,@txt))
(define (li-neg . txt)
`(li ((class "neg")) ,@txt))


(define (stable #:header [header #t]
#:centered [centered #t]
Expand Down

0 comments on commit 416753f

Please sign in to comment.