git.iko.soy/typeable/pandoc
on master help sign in
Ms writer: Implement header identifiers and internal links.
commit1d659bec015774d0ec8bdd54d4975bdcacf657a2 authorJohn MacFarlane <jgm@berkeley.edu> dateSat 25 Mar 2017, 22:16 (10 years ago) parentce4bb68
4 files changed+60−13
data/templates/default.ms−1 src/Text/Pandoc/PDF.hs+1−1 src/Text/Pandoc/Writers/Ms.hs+27−9 test/writer.ms+32−2
data/templates/default.ms−1
@@ -40,7 +40,6 @@
4040.defcolor strikecolor rgb 0.7 0.7 0.7
4141.\" ***************************************************************
4242.\" PDF metadata
43−.mso pdfmark.tmac
4443.pdfinfo /Title "$title-meta$"
4544.pdfinfo /Author "$author-meta$"
4645$if(adjusting)$
src/Text/Pandoc/PDF.hs+1−1
@@ -110,7 +110,7 @@ makePDF "pdfroff" writer opts verbosity _mediabag doc = liftIO $ do
110110 source <- runIOorExplode $ do
111111 setVerbosity verbosity
112112 writer opts doc
113− let args = ["-ms", "-e", "-t", "-k", "-KUTF-8", "-i"]
113+ let args = ["-ms", "-mpdfmark", "-e", "-t", "-k", "-KUTF-8", "-i"]
114114 ms2pdf verbosity args source
115115makePDF program writer opts verbosity mediabag doc = do
116116 let withTemp = if takeBaseName program == "context"
src/Text/Pandoc/Writers/Ms.hs+27−9
@@ -29,11 +29,14 @@ Conversion of 'Pandoc' documents to groff ms format.
2929
3030TODO:
3131
32+[ ] is there a way to avoid the extra space between internal links
33+ and following punctuation?
34+[ ] manually create TOC including internal links and pdf outline
35+ bookmarks? See
36+ http://pipeline.lbl.gov/code/3rd_party/licenses.win/groff/1.19.2/pdf/pdfmark.pdf
3237[ ] is there a better way to do strikeout?
3338[ ] options for hyperlink rendering (currently footnote)
3439[ ] tight/loose list distinction
35−[ ] internal hyperlinks (this seems to be possible since
36− they exist in the groff manual PDF version)
3740-}
3841
3942module Text.Pandoc.Writers.Ms ( writeMs ) where
@@ -242,20 +245,28 @@ blockToMs _ b@(RawBlock f str)
242245blockToMs _ HorizontalRule = do
243246 resetFirstPara
244247 return $ text ".HLINE"
245−blockToMs opts (Header level _ inlines) = do
248+blockToMs opts (Header level (ident,classes,_) inlines) = do
246249 setFirstPara
247250 contents <- inlineListToMs' opts inlines
251+ let anchor = if null ident
252+ then empty
253+ else nowrap $
254+ text ".pdfhref M " <> doubleQuotes (text ident)
248255 let tocEntry = if writerTableOfContents opts &&
249256 level <= writerTOCDepth opts
250257 then text ".XS" $$
251258 (text (replicate level '\t') <> contents) $$
252259 text ".XE"
253260 else empty
254− let heading = if writerNumberSections opts
261+ let heading = if writerNumberSections opts &&
262+ "unnumbered" `notElem` classes
255263 then ".NH"
256264 else ".SH"
257265 modify $ \st -> st{ stFirstPara = True }
258− return $ text heading <> space <> text (show level) $$ contents $$ tocEntry
266+ return $ anchor $$
267+ (text heading <> space <> text (show level)) $$
268+ contents $$
269+ tocEntry
259270blockToMs _ (CodeBlock _ str) = do
260271 setFirstPara
261272 return $
@@ -388,8 +399,6 @@ blockListToMs opts blocks =
388399inlineListToMs :: PandocMonad m => WriterOptions -> [Inline] -> MS m Doc
389400-- if list starts with ., insert a zero-width character \& so it
390401-- won't be interpreted as markup if it falls at the beginning of a line.
391−inlineListToMs opts lst@(Str ('.':_) : _) = mapM (inlineToMs opts) lst >>=
392− (return . (text "\\&" <>) . hcat)
393402inlineListToMs opts lst = hcat <$> mapM (inlineToMs opts) lst
394403
395404-- This version to be used when there is no further inline content;
@@ -435,10 +444,13 @@ inlineToMs opts (Cite _ lst) =
435444inlineToMs _ (Code _ str) =
436445 withFontFeature 'C' (return $ text $ escapeCode str)
437446inlineToMs _ (Str str) = do
447+ let shim = case str of
448+ '.':_ -> afterBreak "\\&"
449+ _ -> empty
438450 smallcaps <- gets stSmallCaps
439451 if smallcaps
440− then return $ text $ toSmallCaps str
441− else return $ text $ escapeString str
452+ then return $ shim <> text (toSmallCaps str)
453+ else return $ shim <> text (escapeString str)
442454inlineToMs opts (Math InlineMath str) = do
443455 modify $ \st -> st{ stHasInlineMath = True }
444456 res <- convertMath writeEqn InlineMath str
@@ -461,6 +473,12 @@ inlineToMs _ il@(RawInline f str)
461473inlineToMs _ (LineBreak) = return $ cr <> text ".br" <> cr
462474inlineToMs opts SoftBreak = handleNotes opts cr
463475inlineToMs opts Space = handleNotes opts space
476+inlineToMs opts (Link _ txt ('#':ident, _)) = do
477+ -- internal link
478+ contents <- inlineListToMs' opts{ writerWrapText = WrapNone } txt
479+ return $ text "\\c" <> cr <> nowrap (text ".pdfhref L -D " <>
480+ doubleQuotes (text ident) <> space <>
481+ doubleQuotes contents) <> cr
464482inlineToMs opts (Link _ txt (src, _)) = do
465483 let srcSuffix = fromMaybe src (stripPrefix "mailto:" src)
466484 inNote <- gets stInNote
test/writer.ms+32−2
@@ -37,10 +37,9 @@
3737.\" footnote point size
3838.nr FPS (\n[PS] - 2000)
3939.\" color used for strikeout
40−.defcolor strikecolor rgb 0.2 0.2 0.2
40+.defcolor strikecolor rgb 0.7 0.7 0.7
4141.\" ***************************************************************
4242.\" PDF metadata
43−.mso pdfmark.tmac
4443.pdfinfo /Title "Pandoc Test Suite"
4544.pdfinfo /Author "John MacFarlane; Anonymous"
4645.hy
@@ -61,29 +60,39 @@ This is a set of tests for pandoc.
6160Most of them are adapted from
6261John Gruber's markdown test suite.
6362.HLINE
63+.pdfhref M "headers"
6464.SH 1
6565Headers
66+.pdfhref M "level-2-with-an-embedded-link"
6667.SH 2
6768Level 2 with an embedded link
69+.pdfhref M "level-3-with-emphasis"
6870.SH 3
6971Level 3 with \f[I]emphasis\f[]
72+.pdfhref M "level-4"
7073.SH 4
7174Level 4
75+.pdfhref M "level-5"
7276.SH 5
7377Level 5
78+.pdfhref M "level-1"
7479.SH 1
7580Level 1
81+.pdfhref M "level-2-with-emphasis"
7682.SH 2
7783Level 2 with \f[I]emphasis\f[]
84+.pdfhref M "level-3"
7885.SH 3
7986Level 3
8087.LP
8188with no blank line
89+.pdfhref M "level-2"
8290.SH 2
8391Level 2
8492.LP
8593with no blank line
8694.HLINE
95+.pdfhref M "paragraphs"
8796.SH 1
8897Paragraphs
8998.LP
@@ -104,6 +113,7 @@ There should be a hard line break
104113.br
105114here.
106115.HLINE
116+.pdfhref M "block-quotes"
107117.SH 1
108118Block Quotes
109119.LP
@@ -147,6 +157,7 @@ This should not be a block quote: 2
147157.PP
148158And a following paragraph.
149159.HLINE
160+.pdfhref M "code-blocks"
150161.SH 1
151162Code Blocks
152163.LP
@@ -174,8 +185,10 @@ These\ should\ not\ be\ escaped:\ \ \\$\ \\\\\ \\>\ \\[\ \\{
174185\f[]
175186.fi
176187.HLINE
188+.pdfhref M "lists"
177189.SH 1
178190Lists
191+.pdfhref M "unordered"
179192.SH 2
180193Unordered
181194.LP
@@ -226,6 +239,7 @@ Minus 1
226239Minus 2
227240.IP \[bu] 2
228241Minus 3
242+.pdfhref M "ordered"
229243.SH 2
230244Ordered
231245.LP
@@ -275,6 +289,7 @@ back.
275289Item 2.
276290.IP "3." 3
277291Item 3.
292+.pdfhref M "nested"
278293.SH 2
279294Nested
280295.IP \[bu] 2
@@ -319,6 +334,7 @@ Foe
319334.RE
320335.IP "3." 3
321336Third
337+.pdfhref M "tabs-and-spaces"
322338.SH 2
323339Tabs and spaces
324340.IP \[bu] 2
@@ -335,6 +351,7 @@ indented with tabs
335351this is an example list item
336352indented with spaces
337353.RE
354+.pdfhref M "fancy-list-markers"
338355.SH 2
339356Fancy list markers
340357.IP "(2)" 4
@@ -390,6 +407,7 @@ M.A.\ 2007
390407B.
391408Williams
392409.HLINE
410+.pdfhref M "definition-lists"
393411.SH 1
394412Definition Lists
395413.LP
@@ -506,6 +524,7 @@ sublist
506524.IP "2." 3
507525sublist
508526.RE
527+.pdfhref M "html-blocks"
509528.SH 1
510529HTML Blocks
511530.LP
@@ -570,6 +589,7 @@ Code:
570589.LP
571590Hr's:
572591.HLINE
592+.pdfhref M "inline-markup"
573593.SH 1
574594Inline Markup
575595.LP
@@ -599,6 +619,7 @@ Subscripts: H\*<2\*>O, H\*<23\*>O, H\*<many\ of\ them\*>O.
599619These should not be superscripts or subscripts,
600620because of the unescaped spaces: a^b c^d, a~b c~d.
601621.HLINE
622+.pdfhref M "smart-quotes-ellipses-dashes"
602623.SH 1
603624Smart quotes, ellipses, dashes
604625.LP
@@ -624,6 +645,7 @@ Dashes between numbers: 5\[en]7, 255\[en]66, 1987\[en]1999.
624645.PP
625646Ellipses\&...and\&...and\&....
626647.HLINE
648+.pdfhref M "latex"
627649.SH 1
628650LaTeX
629651.IP \[bu] 2
@@ -659,6 +681,7 @@ Escaped \f[C]$\f[]: $73 \f[I]this should be emphasized\f[] 23$.
659681.LP
660682Here's a LaTeX table:
661683.HLINE
684+.pdfhref M "special-characters"
662685.SH 1
663686Special Characters
664687.LP
@@ -716,8 +739,10 @@ Plus: +
716739.PP
717740Minus: \-
718741.HLINE
742+.pdfhref M "links"
719743.SH 1
720744Links
745+.pdfhref M "explicit"
721746.SH 2
722747Explicit
723748.LP
@@ -741,6 +766,7 @@ mailto:nobody\@nowhere.net
741766.FE
742767.PP
743768Empty.
769+.pdfhref M "reference"
744770.SH 2
745771Reference
746772.LP
@@ -771,6 +797,7 @@ This should [not][] be a link.
771797Foo bar.
772798.PP
773799Foo biz.
800+.pdfhref M "with-ampersands"
774801.SH 2
775802With ampersands
776803.LP
@@ -787,6 +814,7 @@ http://att.com/
787814Here's an inline link.
788815.PP
789816Here's an inline link in pointy braces.
817+.pdfhref M "autolinks"
790818.SH 2
791819Autolinks
792820.LP
@@ -812,6 +840,7 @@ or\ here:\ <http://example.com/>
812840\f[]
813841.fi
814842.HLINE
843+.pdfhref M "images"
815844.SH 1
816845Images
817846.LP
@@ -821,6 +850,7 @@ From \[lq]Voyage dans la Lune\[rq] by Georges Melies (1902):
821850.PP
822851Here is a movie [IMAGE: movie] icon.
823852.HLINE
853+.pdfhref M "footnotes"
824854.SH 1
825855Footnotes
826856.LP