From 378d6ee9c9ea4947f29f0e2a2bbfc5712c8c5533 Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Tue, 14 May 2013 11:33:19 -0600 Subject: [PATCH] scribble/base: handle special characters in URLs For characters in the URL that give LaTeX trouble, convert them to % encoding. original commit: 967021ca6305dc70488a277c90bed0bb4d6b3e02 --- collects/scribble/latex-render.rkt | 35 +++++++++++++++++++++--------- collects/scribble/scribble.tex | 3 +++ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/collects/scribble/latex-render.rkt b/collects/scribble/latex-render.rkt index aeb92524..4a7c5994 100644 --- a/collects/scribble/latex-render.rkt +++ b/collects/scribble/latex-render.rkt @@ -374,7 +374,7 @@ [(italic) (wrap e "textit" tt?)] [(bold) (wrap e "textbf" tt?)] [(tt) (wrap e "Scribtexttt" #t)] - [(url) (wrap e "nolinkurl" 'exact)] + [(url) (wrap e "Snolinkurl" 'url)] [(no-break) (wrap e "mbox" tt?)] [(sf) (wrap e "textsf" #f)] [(subscript) (wrap e "textsub" #f)] @@ -422,12 +422,15 @@ (let ([v (car l)]) (cond [(target-url? v) - (define target (regexp-replace* #rx"%" - (let ([p (target-url-addr v)]) - (if (path? p) - (path->string p) - p)) - "\\\\%")) + (define target (let* ([s (let ([p (target-url-addr v)]) + (if (path? p) + (path->string 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) ;; work around a problem with `\href' as an ;; argument to other macros, such as `\marginpar': @@ -790,8 +793,20 @@ [else ses]))) (define/private (display-protected s) - (if (eq? (rendering-tt) 'exact) - (display s) + (define rtt (rendering-tt)) + (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 loop ([i 0]) (unless (= i len) @@ -1051,7 +1066,7 @@ c)] [else c])])]) c)]))) - (loop (add1 i))))))) + (loop (add1 i)))))])) (define/private (box-character c) diff --git a/collects/scribble/scribble.tex b/collects/scribble/scribble.tex index b957fc45..de3df37e 100644 --- a/collects/scribble/scribble.tex +++ b/collects/scribble/scribble.tex @@ -219,6 +219,9 @@ % within other macros: \newcommand{\Shref}[3]{\href{#1\##2}{#3}} +% For URLs: +\newcommand{\Snolinkurl}[1]{\nolinkurl{#1}} + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Scribble then generates the following: