Ms writer: Support external links.

Also add config options for link color.
This commit is contained in:
John MacFarlane 2017-03-26 11:04:33 +02:00
parent d9e8e84be0
commit 267e1a13ea
3 changed files with 147 additions and 66 deletions

View file

@ -38,6 +38,10 @@
.nr FPS (\n[PS] - 2000)
.\" color used for strikeout
.defcolor strikecolor rgb 0.7 0.7 0.7
.\" color for links (rgb)
.ds PDFHREF.COLOUR 0.35 0.00 0.60
.\" border for links (default none)
.ds PDFHREF.BORDER 0 0 0
.\" ***************************************************************
.\" PDF metadata
.pdfinfo /Title "$title-meta$"

View file

@ -29,8 +29,6 @@ Conversion of 'Pandoc' documents to groff ms format.
TODO:
[ ] external links
http://pipeline.lbl.gov/code/3rd_party/licenses.win/groff/1.19.2/pdf/pdfmark.pdf
[ ] manually create TOC including internal links and pdf outline
bookmarks? See
http://pipeline.lbl.gov/code/3rd_party/licenses.win/groff/1.19.2/pdf/pdfmark.pdf
@ -62,7 +60,6 @@ import Network.URI (isURI)
data WriterState = WriterState { stHasInlineMath :: Bool
, stFirstPara :: Bool
, stNotes :: [Note]
, stInNote :: Bool
, stSmallCaps :: Bool
, stFontFeatures :: Map.Map Char Bool
}
@ -71,7 +68,6 @@ defaultWriterState :: WriterState
defaultWriterState = WriterState{ stHasInlineMath = False
, stFirstPara = True
, stNotes = []
, stInNote = False
, stSmallCaps = False
, stFontFeatures = Map.fromList [
('I',False)
@ -480,21 +476,12 @@ inlineToMs opts (Link _ txt ('#':ident, _)) = do
doubleQuotes (text "\\c") <> space <> text "\\") <> cr <>
text " -- " <> doubleQuotes (nowrap contents) <> cr <> text "\\&"
inlineToMs opts (Link _ txt (src, _)) = do
let srcSuffix = fromMaybe src (stripPrefix "mailto:" src)
inNote <- gets stInNote
case txt of
[Str s]
| escapeURI s == srcSuffix ->
return $ text (escapeString srcSuffix)
_ | not (isURI src) -> inlineListToMs opts txt
| inNote -> do
-- avoid a note in a note!
contents <- inlineListToMs opts txt
return $ contents <> space <> char '(' <>
text (escapeString src) <> char ')'
| otherwise -> do
let linknote = [Plain [Str src]]
inlineListToMs opts (txt ++ [Note linknote])
-- external link
contents <- inlineListToMs' opts $ map breakToSpace txt
return $ text "\\c" <> cr <> nowrap (text ".pdfhref W -D " <>
doubleQuotes (text src) <> text " -A " <>
doubleQuotes (text "\\c") <> space <> text "\\") <> cr <>
text " -- " <> doubleQuotes (nowrap contents) <> cr <> text "\\&"
inlineToMs opts (Image attr alternate (source, tit)) = do
let alt = if null alternate then [Str "image"] else alternate
linkPart <- inlineToMs opts (Link attr alt (source, tit))
@ -509,9 +496,8 @@ handleNotes opts fallback = do
if null notes
then return fallback
else do
modify $ \st -> st{ stNotes = [], stInNote = True }
modify $ \st -> st{ stNotes = [] }
res <- vcat <$> mapM (handleNote opts) notes
modify $ \st -> st{ stInNote = False }
return res
handleNote :: PandocMonad m => WriterOptions -> Note -> MS m Doc

View file

@ -38,6 +38,10 @@
.nr FPS (\n[PS] - 2000)
.\" color used for strikeout
.defcolor strikecolor rgb 0.7 0.7 0.7
.\" color for links (rgb)
.ds PDFHREF.COLOUR 0.35 0.00 0.60
.\" border for links (default none)
.ds PDFHREF.BORDER 0 0 0
.\" ***************************************************************
.\" PDF metadata
.pdfinfo /Title "Pandoc Test Suite"
@ -64,7 +68,10 @@ John Gruber's markdown test suite.
Headers
.pdfhref M "headers"
.SH 2
Level 2 with an embedded link
Level 2 with an \c
.pdfhref W -D "/url" -A "\c" \
-- "embedded link"
\&
.pdfhref M "level-2-with-an-embedded-link"
.SH 3
Level 3 with \f[I]emphasis\f[]
@ -597,7 +604,10 @@ This is \f[I]emphasized\f[], and so \f[I]is this\f[].
.PP
This is \f[B]strong\f[], and so \f[B]is this\f[].
.PP
An \f[I]emphasized link\f[].
An \f[I]\c
.pdfhref W -D "/url" -A "\c" \
-- "emphasized link"
\&\f[].
.PP
\f[B]\f[BI]This is strong and em.\f[B]\f[]
.PP
@ -634,10 +644,10 @@ So is `pine.'
`He said, \[lq]I want to go.\[rq]' Were you alive in the
70's?
.PP
Here is some quoted `\f[C]code\f[]' and a \[lq]quoted link\**\[rq].
.FS
http://example.com/?foo=1&bar=2
.FE
Here is some quoted `\f[C]code\f[]' and a \[lq]\c
.pdfhref W -D "http://example.com/?foo=1&bar=2" -A "\c" \
-- "quoted link"
\&\[rq].
.PP
Some dashes: one\[em]two \[em] three\[em]four \[em] five.
.PP
@ -746,45 +756,93 @@ Links
Explicit
.pdfhref M "explicit"
.LP
Just a URL.
Just a \c
.pdfhref W -D "/url/" -A "\c" \
-- "URL"
\&.
.PP
URL and title.
\c
.pdfhref W -D "/url/" -A "\c" \
-- "URL and title"
\&.
.PP
URL and title.
\c
.pdfhref W -D "/url/" -A "\c" \
-- "URL and title"
\&.
.PP
URL and title.
\c
.pdfhref W -D "/url/" -A "\c" \
-- "URL and title"
\&.
.PP
URL and title
\c
.pdfhref W -D "/url/" -A "\c" \
-- "URL and title"
\&
.PP
URL and title
\c
.pdfhref W -D "/url/" -A "\c" \
-- "URL and title"
\&
.PP
with_underscore
\c
.pdfhref W -D "/url/with_underscore" -A "\c" \
-- "with_underscore"
\&
.PP
Email link\**
.FS
mailto:nobody\@nowhere.net
.FE
\c
.pdfhref W -D "mailto:nobody@nowhere.net" -A "\c" \
-- "Email link"
\&
.PP
Empty.
\c
.pdfhref W -D "" -A "\c" \
-- "Empty"
\&.
.SH 2
Reference
.pdfhref M "reference"
.LP
Foo bar.
Foo \c
.pdfhref W -D "/url/" -A "\c" \
-- "bar"
\&.
.PP
Foo bar.
Foo \c
.pdfhref W -D "/url/" -A "\c" \
-- "bar"
\&.
.PP
Foo bar.
Foo \c
.pdfhref W -D "/url/" -A "\c" \
-- "bar"
\&.
.PP
With embedded [brackets].
With \c
.pdfhref W -D "/url/" -A "\c" \
-- "embedded [brackets]"
\&.
.PP
b by itself should be a link.
\c
.pdfhref W -D "/url/" -A "\c" \
-- "b"
\& by itself should be a link.
.PP
Indented once.
Indented \c
.pdfhref W -D "/url" -A "\c" \
-- "once"
\&.
.PP
Indented twice.
Indented \c
.pdfhref W -D "/url" -A "\c" \
-- "twice"
\&.
.PP
Indented thrice.
Indented \c
.pdfhref W -D "/url" -A "\c" \
-- "thrice"
\&.
.PP
This should [not][] be a link.
.IP
@ -794,42 +852,66 @@ This should [not][] be a link.
\f[]
.fi
.LP
Foo bar.
Foo \c
.pdfhref W -D "/url/" -A "\c" \
-- "bar"
\&.
.PP
Foo biz.
Foo \c
.pdfhref W -D "/url/" -A "\c" \
-- "biz"
\&.
.SH 2
With ampersands
.pdfhref M "with-ampersands"
.LP
Here's a link with an ampersand in the URL\**.
.FS
http://example.com/?foo=1&bar=2
.FE
Here's a \c
.pdfhref W -D "http://example.com/?foo=1&bar=2" -A "\c" \
-- "link with an ampersand in the URL"
\&.
.PP
Here's a link with an amersand in the link text: AT&T\**.
.FS
http://att.com/
.FE
Here's a link with an amersand in the link text: \c
.pdfhref W -D "http://att.com/" -A "\c" \
-- "AT&T"
\&.
.PP
Here's an inline link.
Here's an \c
.pdfhref W -D "/script?foo=1&bar=2" -A "\c" \
-- "inline link"
\&.
.PP
Here's an inline link in pointy braces.
Here's an \c
.pdfhref W -D "/script?foo=1&bar=2" -A "\c" \
-- "inline link in pointy braces"
\&.
.SH 2
Autolinks
.pdfhref M "autolinks"
.LP
With an ampersand: http://example.com/?foo=1&bar=2
With an ampersand: \c
.pdfhref W -D "http://example.com/?foo=1&bar=2" -A "\c" \
-- "http://example.com/?foo=1&bar=2"
\&
.IP \[bu] 2
In a list?
.IP \[bu] 2
http://example.com/
\c
.pdfhref W -D "http://example.com/" -A "\c" \
-- "http://example.com/"
\&
.IP \[bu] 2
It should.
.LP
An e\-mail address: nobody\@nowhere.net
An e\-mail address: \c
.pdfhref W -D "mailto:nobody@nowhere.net" -A "\c" \
-- "nobody\@nowhere.net"
\&
.RS
.LP
Blockquoted: http://example.com/
Blockquoted: \c
.pdfhref W -D "http://example.com/" -A "\c" \
-- "http://example.com/"
\&
.RE
.LP
Auto\-links should not occur here: \f[C]<http://example.com/>\f[]
@ -846,9 +928,15 @@ Images
.LP
From \[lq]Voyage dans la Lune\[rq] by Georges Melies (1902):
.PP
[IMAGE: lalune]
[IMAGE: \c
.pdfhref W -D "lalune.jpg" -A "\c" \
-- "lalune"
\&]
.PP
Here is a movie [IMAGE: movie] icon.
Here is a movie [IMAGE: \c
.pdfhref W -D "movie.jpg" -A "\c" \
-- "movie"
\&] icon.
.HLINE
.SH 1
Footnotes
@ -885,7 +973,10 @@ contains a space.[^my note] Here is an inline note.\**
This
is \f[I]easier\f[] to type.
Inline notes may contain
links (http://google.com) and \f[C]]\f[] verbatim characters,
\c
.pdfhref W -D "http://google.com" -A "\c" \
-- "links"
\& and \f[C]]\f[] verbatim characters,
as well as [bracketed text].
.FE
.RS