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) .nr FPS (\n[PS] - 2000)
.\" color used for strikeout .\" color used for strikeout
.defcolor strikecolor rgb 0.7 0.7 0.7 .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 .\" PDF metadata
.pdfinfo /Title "$title-meta$" .pdfinfo /Title "$title-meta$"

View file

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

View file

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