git.iko.soy/typeable/pandoc
on master help sign in
Ms writer: Support external links.

Also add config options for link color.

commit267e1a13eadec5ae7ca6e951fd9ab650487f0f2a authorJohn MacFarlane <jgm@berkeley.edu> dateSun 26 Mar 2017, 11:04 (10 years ago) parentd9e8e84
3 files changed+165−84
data/templates/default.ms+4 src/Text/Pandoc/Writers/Ms.hs+7−21 test/writer.ms+154−63
data/templates/default.ms+4
@@ -38,6 +38,10 @@
3838.nr FPS (\n[PS] - 2000)
3939.\" color used for strikeout
4040.defcolor strikecolor rgb 0.7 0.7 0.7
41+.\" color for links (rgb)
42+.ds PDFHREF.COLOUR 0.35 0.00 0.60
43+.\" border for links (default none)
44+.ds PDFHREF.BORDER 0 0 0
4145.\" ***************************************************************
4246.\" PDF metadata
4347.pdfinfo /Title "$title-meta$"
src/Text/Pandoc/Writers/Ms.hs+7−21
@@ -29,8 +29,6 @@ Conversion of 'Pandoc' documents to groff ms format.
2929
3030TODO:
3131
32−[ ] external links
33− http://pipeline.lbl.gov/code/3rd_party/licenses.win/groff/1.19.2/pdf/pdfmark.pdf
3432[ ] manually create TOC including internal links and pdf outline
3533 bookmarks? See
3634 http://pipeline.lbl.gov/code/3rd_party/licenses.win/groff/1.19.2/pdf/pdfmark.pdf
@@ -62,7 +60,6 @@ import Network.URI (isURI)
6260data WriterState = WriterState { stHasInlineMath :: Bool
6361 , stFirstPara :: Bool
6462 , stNotes :: [Note]
65− , stInNote :: Bool
6663 , stSmallCaps :: Bool
6764 , stFontFeatures :: Map.Map Char Bool
6865 }
@@ -71,7 +68,6 @@ defaultWriterState :: WriterState
7168defaultWriterState = WriterState{ stHasInlineMath = False
7269 , stFirstPara = True
7370 , stNotes = []
74− , stInNote = False
7571 , stSmallCaps = False
7672 , stFontFeatures = Map.fromList [
7773 ('I',False)
@@ -480,21 +476,12 @@ inlineToMs opts (Link _ txt ('#':ident, _)) = do
480476 doubleQuotes (text "\\c") <> space <> text "\\") <> cr <>
481477 text " -- " <> doubleQuotes (nowrap contents) <> cr <> text "\\&"
482478inlineToMs opts (Link _ txt (src, _)) = do
483− let srcSuffix = fromMaybe src (stripPrefix "mailto:" src)
484− inNote <- gets stInNote
485− case txt of
486− [Str s]
487− | escapeURI s == srcSuffix ->
488− return $ text (escapeString srcSuffix)
489− _ | not (isURI src) -> inlineListToMs opts txt
490− | inNote -> do
491− -- avoid a note in a note!
492− contents <- inlineListToMs opts txt
493− return $ contents <> space <> char '(' <>
494− text (escapeString src) <> char ')'
495− | otherwise -> do
496− let linknote = [Plain [Str src]]
497− inlineListToMs opts (txt ++ [Note linknote])
479+ -- external link
480+ contents <- inlineListToMs' opts $ map breakToSpace txt
481+ return $ text "\\c" <> cr <> nowrap (text ".pdfhref W -D " <>
482+ doubleQuotes (text src) <> text " -A " <>
483+ doubleQuotes (text "\\c") <> space <> text "\\") <> cr <>
484+ text " -- " <> doubleQuotes (nowrap contents) <> cr <> text "\\&"
498485inlineToMs opts (Image attr alternate (source, tit)) = do
499486 let alt = if null alternate then [Str "image"] else alternate
500487 linkPart <- inlineToMs opts (Link attr alt (source, tit))
@@ -509,9 +496,8 @@ handleNotes opts fallback = do
509496 if null notes
510497 then return fallback
511498 else do
512− modify $ \st -> st{ stNotes = [], stInNote = True }
499+ modify $ \st -> st{ stNotes = [] }
513500 res <- vcat <$> mapM (handleNote opts) notes
514− modify $ \st -> st{ stInNote = False }
515501 return res
516502
517503handleNote :: PandocMonad m => WriterOptions -> Note -> MS m Doc
test/writer.ms+154−63
@@ -38,6 +38,10 @@
3838.nr FPS (\n[PS] - 2000)
3939.\" color used for strikeout
4040.defcolor strikecolor rgb 0.7 0.7 0.7
41+.\" color for links (rgb)
42+.ds PDFHREF.COLOUR 0.35 0.00 0.60
43+.\" border for links (default none)
44+.ds PDFHREF.BORDER 0 0 0
4145.\" ***************************************************************
4246.\" PDF metadata
4347.pdfinfo /Title "Pandoc Test Suite"
@@ -64,7 +68,10 @@ John Gruber's markdown test suite.
6468Headers
6569.pdfhref M "headers"
6670.SH 2
67−Level 2 with an embedded link
71+Level 2 with an \c
72+.pdfhref W -D "/url" -A "\c" \
73+ -- "embedded link"
74+\&
6875.pdfhref M "level-2-with-an-embedded-link"
6976.SH 3
7077Level 3 with \f[I]emphasis\f[]
@@ -597,7 +604,10 @@ This is \f[I]emphasized\f[], and so \f[I]is this\f[].
597604.PP
598605This is \f[B]strong\f[], and so \f[B]is this\f[].
599606.PP
600−An \f[I]emphasized link\f[].
607+An \f[I]\c
608+.pdfhref W -D "/url" -A "\c" \
609+ -- "emphasized link"
610+\&\f[].
601611.PP
602612\f[B]\f[BI]This is strong and em.\f[B]\f[]
603613.PP
@@ -634,10 +644,10 @@ So is `pine.'
634644`He said, \[lq]I want to go.\[rq]' Were you alive in the
63564570's?
636646.PP
637−Here is some quoted `\f[C]code\f[]' and a \[lq]quoted link\**\[rq].
638−.FS
639−http://example.com/?foo=1&bar=2
640−.FE
647+Here is some quoted `\f[C]code\f[]' and a \[lq]\c
648+.pdfhref W -D "http://example.com/?foo=1&bar=2" -A "\c" \
649+ -- "quoted link"
650+\&\[rq].
641651.PP
642652Some dashes: one\[em]two \[em] three\[em]four \[em] five.
643653.PP
@@ -746,45 +756,93 @@ Links
746756Explicit
747757.pdfhref M "explicit"
748758.LP
749−Just a URL.
750−.PP
751−URL and title.
752−.PP
753−URL and title.
754−.PP
755−URL and title.
756−.PP
757−URL and title
758−.PP
759−URL and title
760−.PP
761−with_underscore
762−.PP
763−Email link\**
764−.FS
765−mailto:nobody\@nowhere.net
766−.FE
767−.PP
768−Empty.
759+Just a \c
760+.pdfhref W -D "/url/" -A "\c" \
761+ -- "URL"
762+\&.
763+.PP
764+\c
765+.pdfhref W -D "/url/" -A "\c" \
766+ -- "URL and title"
767+\&.
768+.PP
769+\c
770+.pdfhref W -D "/url/" -A "\c" \
771+ -- "URL and title"
772+\&.
773+.PP
774+\c
775+.pdfhref W -D "/url/" -A "\c" \
776+ -- "URL and title"
777+\&.
778+.PP
779+\c
780+.pdfhref W -D "/url/" -A "\c" \
781+ -- "URL and title"
782+\&
783+.PP
784+\c
785+.pdfhref W -D "/url/" -A "\c" \
786+ -- "URL and title"
787+\&
788+.PP
789+\c
790+.pdfhref W -D "/url/with_underscore" -A "\c" \
791+ -- "with_underscore"
792+\&
793+.PP
794+\c
795+.pdfhref W -D "mailto:nobody@nowhere.net" -A "\c" \
796+ -- "Email link"
797+\&
798+.PP
799+\c
800+.pdfhref W -D "" -A "\c" \
801+ -- "Empty"
802+\&.
769803.SH 2
770804Reference
771805.pdfhref M "reference"
772806.LP
773−Foo bar.
774−.PP
775−Foo bar.
776−.PP
777−Foo bar.
778−.PP
779−With embedded [brackets].
780−.PP
781−b by itself should be a link.
782−.PP
783−Indented once.
784−.PP
785−Indented twice.
786−.PP
787−Indented thrice.
807+Foo \c
808+.pdfhref W -D "/url/" -A "\c" \
809+ -- "bar"
810+\&.
811+.PP
812+Foo \c
813+.pdfhref W -D "/url/" -A "\c" \
814+ -- "bar"
815+\&.
816+.PP
817+Foo \c
818+.pdfhref W -D "/url/" -A "\c" \
819+ -- "bar"
820+\&.
821+.PP
822+With \c
823+.pdfhref W -D "/url/" -A "\c" \
824+ -- "embedded [brackets]"
825+\&.
826+.PP
827+\c
828+.pdfhref W -D "/url/" -A "\c" \
829+ -- "b"
830+\& by itself should be a link.
831+.PP
832+Indented \c
833+.pdfhref W -D "/url" -A "\c" \
834+ -- "once"
835+\&.
836+.PP
837+Indented \c
838+.pdfhref W -D "/url" -A "\c" \
839+ -- "twice"
840+\&.
841+.PP
842+Indented \c
843+.pdfhref W -D "/url" -A "\c" \
844+ -- "thrice"
845+\&.
788846.PP
789847This should [not][] be a link.
790848.IP
@@ -794,42 +852,66 @@ This should [not][] be a link.
794852\f[]
795853.fi
796854.LP
797−Foo bar.
855+Foo \c
856+.pdfhref W -D "/url/" -A "\c" \
857+ -- "bar"
858+\&.
798859.PP
799−Foo biz.
860+Foo \c
861+.pdfhref W -D "/url/" -A "\c" \
862+ -- "biz"
863+\&.
800864.SH 2
801865With ampersands
802866.pdfhref M "with-ampersands"
803867.LP
804−Here's a link with an ampersand in the URL\**.
805−.FS
806−http://example.com/?foo=1&bar=2
807−.FE
808−.PP
809−Here's a link with an amersand in the link text: AT&T\**.
810−.FS
811−http://att.com/
812−.FE
813−.PP
814−Here's an inline link.
815−.PP
816−Here's an inline link in pointy braces.
868+Here's a \c
869+.pdfhref W -D "http://example.com/?foo=1&bar=2" -A "\c" \
870+ -- "link with an ampersand in the URL"
871+\&.
872+.PP
873+Here's a link with an amersand in the link text: \c
874+.pdfhref W -D "http://att.com/" -A "\c" \
875+ -- "AT&T"
876+\&.
877+.PP
878+Here's an \c
879+.pdfhref W -D "/script?foo=1&bar=2" -A "\c" \
880+ -- "inline link"
881+\&.
882+.PP
883+Here's an \c
884+.pdfhref W -D "/script?foo=1&bar=2" -A "\c" \
885+ -- "inline link in pointy braces"
886+\&.
817887.SH 2
818888Autolinks
819889.pdfhref M "autolinks"
820890.LP
821−With an ampersand: http://example.com/?foo=1&bar=2
891+With an ampersand: \c
892+.pdfhref W -D "http://example.com/?foo=1&bar=2" -A "\c" \
893+ -- "http://example.com/?foo=1&bar=2"
894+\&
822895.IP \[bu] 2
823896In a list?
824897.IP \[bu] 2
825−http://example.com/
898+\c
899+.pdfhref W -D "http://example.com/" -A "\c" \
900+ -- "http://example.com/"
901+\&
826902.IP \[bu] 2
827903It should.
828904.LP
829−An e\-mail address: nobody\@nowhere.net
905+An e\-mail address: \c
906+.pdfhref W -D "mailto:nobody@nowhere.net" -A "\c" \
907+ -- "nobody\@nowhere.net"
908+\&
830909.RS
831910.LP
832−Blockquoted: http://example.com/
911+Blockquoted: \c
912+.pdfhref W -D "http://example.com/" -A "\c" \
913+ -- "http://example.com/"
914+\&
833915.RE
834916.LP
835917Auto\-links should not occur here: \f[C]<http://example.com/>\f[]
@@ -846,9 +928,15 @@ Images
846928.LP
847929From \[lq]Voyage dans la Lune\[rq] by Georges Melies (1902):
848930.PP
849−[IMAGE: lalune]
931+[IMAGE: \c
932+.pdfhref W -D "lalune.jpg" -A "\c" \
933+ -- "lalune"
934+\&]
850935.PP
851−Here is a movie [IMAGE: movie] icon.
936+Here is a movie [IMAGE: \c
937+.pdfhref W -D "movie.jpg" -A "\c" \
938+ -- "movie"
939+\&] icon.
852940.HLINE
853941.SH 1
854942Footnotes
@@ -885,7 +973,10 @@ contains a space.[^my note] Here is an inline note.\**
885973This
886974is \f[I]easier\f[] to type.
887975Inline notes may contain
888−links (http://google.com) and \f[C]]\f[] verbatim characters,
976+\c
977+.pdfhref W -D "http://google.com" -A "\c" \
978+ -- "links"
979+\& and \f[C]]\f[] verbatim characters,
889980as well as [bracketed text].
890981.FE
891982.RS