Make the list of special characters user-extensible .

This commit is contained in:
Vincent St-Amour 2016-03-10 14:50:56 -06:00
parent 78a517a34d
commit 525b72ca4c
2 changed files with 252 additions and 238 deletions

View File

@ -2,6 +2,7 @@
@(require scribble/manual @(require scribble/manual
"utils.rkt" "utils.rkt"
(for-label racket/class (for-label racket/class
racket/dict
scribble/render scribble/render
scribble/xref)) scribble/xref))
@ -396,6 +397,13 @@ are own their own pages. A value of @racket[0] is treated the same as
Specializes a @racket[render<%>] class for generating Latex input.}} Specializes a @racket[render<%>] class for generating Latex input.}}
@defparam[extra-character-conversions convs (dictof char? string?)]{
Maps (special) characters to strings corresponding to the Latex code that
should be used to render them. Scribble already converts many special
characters to the proper Latex commands. This parameter should be used in case
you need characters it does not support yet.
}
@; ---------------------------------------- @; ----------------------------------------
@section{PDF Renderer} @section{PDF Renderer}

View File

@ -3,6 +3,7 @@
"latex-properties.rkt" "latex-properties.rkt"
"private/render-utils.rkt" "private/render-utils.rkt"
racket/class racket/class
racket/dict
racket/runtime-path racket/runtime-path
racket/port racket/port
racket/string racket/string
@ -11,7 +12,8 @@
setup/collects setup/collects
file/convertible) file/convertible)
(provide render-mixin (provide render-mixin
make-render-part-mixin) make-render-part-mixin
extra-character-conversions)
(define current-table-mode (make-parameter #f)) (define current-table-mode (make-parameter #f))
(define rendering-tt (make-parameter #f)) (define rendering-tt (make-parameter #f))
@ -47,6 +49,8 @@
(define-runtime-path skull-tex "scribble-skull.tex") (define-runtime-path skull-tex "scribble-skull.tex")
(define skull-style (make-style #f (list (tex-addition skull-tex)))) (define skull-style (make-style #f (list (tex-addition skull-tex))))
(define extra-character-conversions (make-parameter (make-hash)))
(define (render-mixin % #:image-mode [image-mode #f]) (define (render-mixin % #:image-mode [image-mode #f])
(class % (class %
(super-new) (super-new)
@ -942,6 +946,7 @@
(define/private (display-protected s) (define/private (display-protected s)
(define rtt (rendering-tt)) (define rtt (rendering-tt))
(define convs (extra-character-conversions))
(cond (cond
[(eq? rtt 'exact) [(eq? rtt 'exact)
(display s)] (display s)]
@ -995,6 +1000,8 @@
[(#\uDF) "{\\ss}"] [(#\uDF) "{\\ss}"]
[else [else
(if ((char->integer c) . > . 127) (if ((char->integer c) . > . 127)
;; first, try user-defined conversions
(or (dict-ref convs c #f)
;; latex-prefix.rkt enables utf8 input, but this does not work for ;; latex-prefix.rkt enables utf8 input, but this does not work for
;; all the characters below (e.g. ∞). Some parts of the table ;; all the characters below (e.g. ∞). Some parts of the table
;; below are therefore necessary, but some parts probably are not. ;; below are therefore necessary, but some parts probably are not.
@ -1022,7 +1029,6 @@
[(#\u039B) "$\\Lambda$"] [(#\u039B) "$\\Lambda$"]
[(#\u03BC) "$\\mu$"] [(#\u03BC) "$\\mu$"]
[(#\u03C0) "$\\pi$"] [(#\u03C0) "$\\pi$"]
[(#\ϖ) "$\\varpi$"]
[(#\) "{`}"] [(#\) "{`}"]
[(#\) "{'}"] [(#\) "{'}"]
[(#\“) "{``}"] [(#\“) "{``}"]
@ -1230,7 +1236,7 @@
(= 1 (string-length base))) (= 1 (string-length base)))
(format combiner (char-loop (string-ref base 0))) (format combiner (char-loop (string-ref base 0)))
c)] c)]
[else c])])]) [else c])])]))
c)]))) c)])))
(loop (add1 i))))))])) (loop (add1 i))))))]))