From cdc89f9685e4ad312e9db2abae7452c15333a6b4 Mon Sep 17 00:00:00 2001 From: Greg Hendershott Date: Tue, 11 Dec 2012 14:06:24 -0500 Subject: [PATCH] Render `@hyperlink` in Markdown. Render Scribble like @hyperlink["url" "content"] as Markdown like [content](url) Note that this only works for `@hyperlink`. The motivation is to preserve content the author has explicitly written. (Previously, `markdown-render.rkt` was discarding this; `text-render.rkt` still does so.) This does _not_ attempt to handle everything that `html-render.rkt` would automatically generate and render as ``. It simply can't -- things like hotlinked Racket keywords in code blocks simply won't work in Markdown. original commit: dbffc840a93ceb142c59df4a533ced0b41b8e4fa --- collects/scribble/markdown-render.rkt | 38 ++++++++++++++----- .../tests/scribble/markdown-docs/example.md | 2 + .../scribble/markdown-docs/example.scrbl | 2 + 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/collects/scribble/markdown-render.rkt b/collects/scribble/markdown-render.rkt index 5886e613..eaa55725 100644 --- a/collects/scribble/markdown-render.rkt +++ b/collects/scribble/markdown-render.rkt @@ -1,6 +1,6 @@ #lang racket/base (require "core.rkt" "base-render.rkt" - racket/class racket/port racket/list racket/string + racket/class racket/port racket/list racket/string racket/match scribble/text/wrap) (provide render-mixin) @@ -151,6 +151,12 @@ (newline) null) + (define/private (content-style e) + (cond + [(element? e) (element-style e)] + [(multiarg-element? e) (multiarg-element-style e)] + [else #f])) + (define/override (render-content i part ri) (define tick? (and (zero? (table-ticks-depth)) @@ -164,15 +170,29 @@ (when (zero? (phrase-ticks-depth)) (display "`")) (phrase-ticks-depth (add1 (phrase-ticks-depth)))) + (define properties (let ([s (content-style i)]) + (if (style? s) (style-properties s) '()))) + (define targ (for/or ([p properties]) + (if (target-url? p) p #f))) + (define url (and targ (target-url-addr targ))) (begin0 - (if (and (element? i) - (let ([s (element-style i)]) - (or (eq? 'hspace s) - (and (style? s) - (eq? 'hspace (style-name s)))))) - (parameterize ([current-preserve-spaces #t]) - (super render-content i part ri)) - (super render-content i part ri)) + (cond [url + (define new-i + (match (element-content i) + [(list (? string? s)) + (element (element-style i) + (list (format "[~a](~a)" s url)))] + [else i])) + (super render-content new-i part ri)] + [(and (element? i) + (let ([s (element-style i)]) + (or (eq? 'hspace s) + (and (style? s) + (eq? 'hspace (style-name s)))))) + (parameterize ([current-preserve-spaces #t]) + (super render-content i part ri))] + [else + (super render-content i part ri)]) (when tick? (phrase-ticks-depth (sub1 (phrase-ticks-depth))) (when (zero? (phrase-ticks-depth)) diff --git a/collects/tests/scribble/markdown-docs/example.md b/collects/tests/scribble/markdown-docs/example.md index b613de0b..b08a7a23 100644 --- a/collects/tests/scribble/markdown-docs/example.md +++ b/collects/tests/scribble/markdown-docs/example.md @@ -8,6 +8,8 @@ ## 1. Section +[I am a hyperlink to Racket.](http://www.racket-lang.org/) + Italic. \_Just underlines\_. Bold. \*Just asterisks.\* diff --git a/collects/tests/scribble/markdown-docs/example.scrbl b/collects/tests/scribble/markdown-docs/example.scrbl index 0b27bb82..8041df2e 100644 --- a/collects/tests/scribble/markdown-docs/example.scrbl +++ b/collects/tests/scribble/markdown-docs/example.scrbl @@ -12,6 +12,8 @@ @section{Section} +@hyperlink["http://www.racket-lang.org/" "I am a hyperlink to Racket."] + @italic{Italic}. _Just underlines_.