scribble/base: handle special characters in URLs

For characters in the URL that give LaTeX trouble, convert
them to % encoding.

original commit: 967021ca6305dc70488a277c90bed0bb4d6b3e02
This commit is contained in:
Matthew Flatt 2013-05-14 11:33:19 -06:00
parent b66e924310
commit 378d6ee9c9
2 changed files with 28 additions and 10 deletions

View File

@ -374,7 +374,7 @@
[(italic) (wrap e "textit" tt?)] [(italic) (wrap e "textit" tt?)]
[(bold) (wrap e "textbf" tt?)] [(bold) (wrap e "textbf" tt?)]
[(tt) (wrap e "Scribtexttt" #t)] [(tt) (wrap e "Scribtexttt" #t)]
[(url) (wrap e "nolinkurl" 'exact)] [(url) (wrap e "Snolinkurl" 'url)]
[(no-break) (wrap e "mbox" tt?)] [(no-break) (wrap e "mbox" tt?)]
[(sf) (wrap e "textsf" #f)] [(sf) (wrap e "textsf" #f)]
[(subscript) (wrap e "textsub" #f)] [(subscript) (wrap e "textsub" #f)]
@ -422,12 +422,15 @@
(let ([v (car l)]) (let ([v (car l)])
(cond (cond
[(target-url? v) [(target-url? v)
(define target (regexp-replace* #rx"%" (define target (let* ([s (let ([p (target-url-addr v)])
(let ([p (target-url-addr v)])
(if (path? p) (if (path? p)
(path->string p) (path->string p)
p)) p))]
"\\\\%")) [s (regexp-replace* #rx"\\\\" s "%5c")]
[s (regexp-replace* #rx"{" s "%7b")]
[s (regexp-replace* #rx"}" s "%7d")]
[s (regexp-replace* #rx"%" s "\\\\%")])
s))
(if (regexp-match? #rx"^[^#]*#[^#]*$" target) (if (regexp-match? #rx"^[^#]*#[^#]*$" target)
;; work around a problem with `\href' as an ;; work around a problem with `\href' as an
;; argument to other macros, such as `\marginpar': ;; argument to other macros, such as `\marginpar':
@ -790,8 +793,20 @@
[else ses]))) [else ses])))
(define/private (display-protected s) (define/private (display-protected s)
(if (eq? (rendering-tt) 'exact) (define rtt (rendering-tt))
(display s) (cond
[(eq? rtt 'exact)
(display s)]
[(eq? rtt 'url)
(for ([c (in-string s)])
(case c
[(#\%) (display "\\%")]
[(#\#) (display "\\#")]
[(#\\) (display "\\%5c")]
[(#\{) (display "\\%7b")]
[(#\}) (display "\\%7d")]
[else (display c)]))]
[else
(let ([len (string-length s)]) (let ([len (string-length s)])
(let loop ([i 0]) (let loop ([i 0])
(unless (= i len) (unless (= i len)
@ -1051,7 +1066,7 @@
c)] c)]
[else c])])]) [else c])])])
c)]))) c)])))
(loop (add1 i))))))) (loop (add1 i)))))]))
(define/private (box-character c) (define/private (box-character c)

View File

@ -219,6 +219,9 @@
% within other macros: % within other macros:
\newcommand{\Shref}[3]{\href{#1\##2}{#3}} \newcommand{\Shref}[3]{\href{#1\##2}{#3}}
% For URLs:
\newcommand{\Snolinkurl}[1]{\nolinkurl{#1}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Scribble then generates the following: % Scribble then generates the following: