git.iko.soy/typeable/pandoc
on master help sign in
Man writer: updated to use Pretty.
fd07db1John MacFarlane
src/Text/Pandoc/Writers/Man.hs+22−18
@@ -35,7 +35,7 @@ import Text.Pandoc.Shared
35import Text.Pandoc.Readers.TeXMath
35import Text.Pandoc.Readers.TeXMath
36import Text.Printf ( printf )
36import Text.Printf ( printf )
37import Data.List ( isPrefixOf, intersperse, intercalate )
37import Data.List ( isPrefixOf, intersperse, intercalate )
38−import Text.PrettyPrint.HughesPJ hiding ( Str )
38+import Text.Pandoc.Pretty
39import Control.Monad.State
39import Control.Monad.State
40
40
41type Notes = [[Block]]
41type Notes = [[Block]]
@@ -52,27 +52,31 @@ pandocToMan opts (Pandoc (Meta title authors date) blocks) = do
52 titleText <- inlineListToMan opts title
52 titleText <- inlineListToMan opts title
53 authors' <- mapM (inlineListToMan opts) authors
53 authors' <- mapM (inlineListToMan opts) authors
54 date' <- inlineListToMan opts date
54 date' <- inlineListToMan opts date
55+ let colwidth = if writerWrapText opts
56+ then Just $ writerColumns opts
57+ else Nothing
58+ let render' = render colwidth
55− let (cmdName, rest) = break (== ' ') $ render titleText
59+ let (cmdName, rest) = break (== ' ') $ render' titleText
56 let (title', section) = case reverse cmdName of
60 let (title', section) = case reverse cmdName of
57 (')':d:'(':xs) | d `elem` ['0'..'9'] ->
61 (')':d:'(':xs) | d `elem` ['0'..'9'] ->
58 (text (reverse xs), char d)
62 (text (reverse xs), char d)
59− xs -> (text (reverse xs), doubleQuotes empty)
63+ xs -> (text (reverse xs), doubleQuotes empty)
60 let description = hsep $
64 let description = hsep $
61 map (doubleQuotes . text . removeLeadingTrailingSpace) $
65 map (doubleQuotes . text . removeLeadingTrailingSpace) $
62 splitBy (== '|') rest
66 splitBy (== '|') rest
63 body <- blockListToMan opts blocks
67 body <- blockListToMan opts blocks
64 notes <- liftM stNotes get
68 notes <- liftM stNotes get
65 notes' <- notesToMan opts (reverse notes)
69 notes' <- notesToMan opts (reverse notes)
66− let main = render $ body $$ notes' $$ text ""
70+ let main = render' $ body $$ notes' $$ text ""
67 hasTables <- liftM stHasTables get
71 hasTables <- liftM stHasTables get
68 let context = writerVariables opts ++
72 let context = writerVariables opts ++
69 [ ("body", main)
73 [ ("body", main)
70− , ("title", render title')
74+ , ("title", render' title')
71− , ("section", render section)
75+ , ("section", render' section)
72− , ("date", render date')
76+ , ("date", render' date')
73− , ("description", render description) ] ++
77+ , ("description", render' description) ] ++
74 [ ("has-tables", "yes") | hasTables ] ++
78 [ ("has-tables", "yes") | hasTables ] ++
75− [ ("author", render a) | a <- authors' ]
79+ [ ("author", render' a) | a <- authors' ]
76 if writerStandalone opts
80 if writerStandalone opts
77 then return $ renderTemplate context $ writerTemplate opts
81 then return $ renderTemplate context $ writerTemplate opts
78 else return main
82 else return main
@@ -89,7 +93,7 @@ notesToMan opts notes =
89noteToMan :: WriterOptions -> Int -> [Block] -> State WriterState Doc
93noteToMan :: WriterOptions -> Int -> [Block] -> State WriterState Doc
90noteToMan opts num note = do
94noteToMan opts num note = do
91 contents <- blockListToMan opts note
95 contents <- blockListToMan opts note
92− let marker = text "\n.SS [" <> text (show num) <> char ']'
96+ let marker = cr <> text ".SS " <> brackets (text (show num))
93 return $ marker $$ contents
97 return $ marker $$ contents
94
98
95-- | Association list of characters to escape.
99-- | Association list of characters to escape.
@@ -136,14 +140,13 @@ blockToMan :: WriterOptions -- ^ Options
136 -> State WriterState Doc
140 -> State WriterState Doc
137blockToMan _ Null = return empty
141blockToMan _ Null = return empty
138blockToMan opts (Plain inlines) =
142blockToMan opts (Plain inlines) =
139− liftM vcat $ mapM (wrapIfNeeded opts (inlineListToMan opts)) $
143+ liftM vcat $ mapM (inlineListToMan opts) $ splitSentences inlines
140− splitSentences inlines
141blockToMan opts (Para inlines) = do
144blockToMan opts (Para inlines) = do
142− contents <- liftM vcat $ mapM (wrapIfNeeded opts (inlineListToMan opts)) $
145+ contents <- liftM vcat $ mapM (inlineListToMan opts) $
143 splitSentences inlines
146 splitSentences inlines
144 return $ text ".PP" $$ contents
147 return $ text ".PP" $$ contents
145blockToMan _ (RawHtml _) = return empty
148blockToMan _ (RawHtml _) = return empty
146−blockToMan _ HorizontalRule = return $ text $ ".PP\n * * * * *"
149+blockToMan _ HorizontalRule = return $ text ".PP" $$ text " * * * * *"
147blockToMan opts (Header level inlines) = do
150blockToMan opts (Header level inlines) = do
148 contents <- inlineListToMan opts inlines
151 contents <- inlineListToMan opts inlines
149 let heading = case level of
152 let heading = case level of
@@ -256,7 +259,7 @@ definitionListItemToMan opts (label, defs) = do
256 mapM (\item -> blockToMan opts item) rest
259 mapM (\item -> blockToMan opts item) rest
257 first' <- blockToMan opts first
260 first' <- blockToMan opts first
258 return $ first' $$ text ".RS" $$ rest' $$ text ".RE"
261 return $ first' $$ text ".RS" $$ rest' $$ text ".RE"
259− return $ text ".TP\n.B " <> labelText $+$ contents
262+ return $ text ".TP" $$ text ".B " <> labelText $$ contents
260
263
261-- | Convert list of Pandoc block elements to man.
264-- | Convert list of Pandoc block elements to man.
262blockListToMan :: WriterOptions -- ^ Options
265blockListToMan :: WriterOptions -- ^ Options
@@ -309,11 +312,12 @@ inlineToMan _ (Str str) = return $ text $ escapeString str
309inlineToMan opts (Math InlineMath str) = inlineListToMan opts $ readTeXMath str
312inlineToMan opts (Math InlineMath str) = inlineListToMan opts $ readTeXMath str
310inlineToMan opts (Math DisplayMath str) = do
313inlineToMan opts (Math DisplayMath str) = do
311 contents <- inlineListToMan opts $ readTeXMath str
314 contents <- inlineListToMan opts $ readTeXMath str
312− return $ text ".RS" $$ contents $$ text ".RE"
315+ return $ cr <> text ".RS" $$ contents $$ text ".RE"
313inlineToMan _ (TeX _) = return empty
316inlineToMan _ (TeX _) = return empty
314inlineToMan _ (HtmlInline _) = return empty
317inlineToMan _ (HtmlInline _) = return empty
315−inlineToMan _ (LineBreak) = return $ text "\n.PD 0\n.P\n.PD\n"
318+inlineToMan _ (LineBreak) = return $
319+ cr <> text ".PD 0" $$ text ".P" $$ text ".PD" <> cr
316−inlineToMan _ Space = return $ char ' '
320+inlineToMan _ Space = return space
317inlineToMan opts (Link txt (src, _)) = do
321inlineToMan opts (Link txt (src, _)) = do
318 linktext <- inlineListToMan opts txt
322 linktext <- inlineListToMan opts txt
319 let srcSuffix = if isPrefixOf "mailto:" src then drop 7 src else src
323 let srcSuffix = if isPrefixOf "mailto:" src then drop 7 src else src
tests/tables.man+1−1
@@ -264,4 +264,4 @@ T}@T{
264Here\[aq]s another one.
264Here\[aq]s another one.
265Note the blank line between rows.
265Note the blank line between rows.
266T}
266T}
267−.TE
267+.TE
tests/writer.man+19−27
@@ -26,8 +26,8 @@ Here's a regular paragraph.
26In Markdown 1.0.0 and earlier.
26In Markdown 1.0.0 and earlier.
27Version 8.
27Version 8.
28This line turns into a list item.
28This line turns into a list item.
29−Because a hard-wrapped line in the middle of a paragraph looked
29+Because a hard-wrapped line in the middle of a paragraph looked like a list
30−like a list item.
30+item.
31.PP
31.PP
32Here's one with a bullet.
32Here's one with a bullet.
33* criminey.
33* criminey.
@@ -531,8 +531,8 @@ Superscripts: a^bc^d a^\f[I]hello\f[]^ a^hello\ there^.
531.PP
531.PP
532Subscripts: H~2~O, H~23~O, H~many\ of\ them~O.
532Subscripts: H~2~O, H~23~O, H~many\ of\ them~O.
533.PP
533.PP
534−These should not be superscripts or subscripts, because of the
534+These should not be superscripts or subscripts, because of the unescaped
535−unescaped spaces: a^b c^d, a~b c~d.
535+spaces: a^b c^d, a~b c~d.
536.PP
536.PP
537 * * * * *
537 * * * * *
538.SH Smart quotes, ellipses, dashes
538.SH Smart quotes, ellipses, dashes
@@ -547,8 +547,8 @@ So is `pine.'
547.PP
547.PP
548`He said, \[lq]I want to go.\[rq]' Were you alive in the 70's?
548`He said, \[lq]I want to go.\[rq]' Were you alive in the 70's?
549.PP
549.PP
550−Here is some quoted `\f[C]code\f[]' and a
550+Here is some quoted `\f[C]code\f[]' and a \[lq]quoted
551−\[lq]quoted link (http://example.com/?foo=1&bar=2)\[rq].
551+link (http://example.com/?foo=1&bar=2)\[rq].
552.PP
552.PP
553Some dashes: one\[em]two \[em] three\[em]four \[em] five.
553Some dashes: one\[em]two \[em] three\[em]four \[em] five.
554.PP
554.PP
@@ -641,7 +641,7 @@ Greater-than: >
641.PP
641.PP
642Hash: #
642Hash: #
643.PP
643.PP
644−Period: \&.
644+Period: .
645.PP
645.PP
646Bang: !
646Bang: !
647.PP
647.PP
@@ -701,11 +701,9 @@ Foo bar (/url/).
701Foo biz (/url/).
701Foo biz (/url/).
702.SS With ampersands
702.SS With ampersands
703.PP
703.PP
704−Here's a
705−link with an ampersand in the URL (http://example.com/?foo=1&bar=2).
704+Here's a link with an ampersand in the URL (http://example.com/?foo=1&bar=2).
706.PP
705.PP
707−Here's a link with an amersand in the link text:
706+Here's a link with an amersand in the link text: AT&T (http://att.com/).
708−AT&T (http://att.com/).
709.PP
707.PP
710Here's an inline link (/script?foo=1&bar=2).
708Here's an inline link (/script?foo=1&bar=2).
711.PP
709.PP
@@ -746,9 +744,9 @@ Here is a movie [IMAGE: movie (movie.jpg)] icon.
746 * * * * *
744 * * * * *
747.SH Footnotes
745.SH Footnotes
748.PP
746.PP
749−Here is a footnote reference,[1] and another.[2] This should
747+Here is a footnote reference,[1] and another.[2] This should \f[I]not\f[] be a
750−\f[I]not\f[] be a footnote reference, because it contains a
748+footnote reference, because it contains a space.[^my note] Here is an inline
751−space.[^my note] Here is an inline note.[3]
749+note.[3]
752.RS
750.RS
753.PP
751.PP
754Notes can go in quotes.[4]
752Notes can go in quotes.[4]
@@ -756,23 +754,20 @@ Notes can go in quotes.[4]
756.IP "1." 3
754.IP "1." 3
757And in list items.[5]
755And in list items.[5]
758.PP
756.PP
759−This paragraph should not be part of the note, as it is not
757+This paragraph should not be part of the note, as it is not indented.
760−indented.
761.SH NOTES
758.SH NOTES
762−
763.SS [1]
759.SS [1]
764.PP
760.PP
765Here is the footnote.
761Here is the footnote.
766It can go anywhere after the footnote reference.
762It can go anywhere after the footnote reference.
767It need not be placed at the end of the document.
763It need not be placed at the end of the document.
768−
769.SS [2]
764.SS [2]
770.PP
765.PP
771Here's the long note.
766Here's the long note.
772This one contains multiple blocks.
767This one contains multiple blocks.
773.PP
768.PP
774−Subsequent blocks are indented to show that they belong to the
769+Subsequent blocks are indented to show that they belong to the footnote (as
775−footnote (as with list items).
770+with list items).
776.IP
771.IP
777.nf
772.nf
778\f[C]
773\f[C]
@@ -780,19 +775,16 @@ footnote (as with list items).
780\f[]
775\f[]
781.fi
776.fi
782.PP
777.PP
783−If you want, you can indent every line, but you can also be lazy
778+If you want, you can indent every line, but you can also be lazy and just
784−and just indent the first line of each block.
779+indent the first line of each block.
785−
786.SS [3]
780.SS [3]
787.PP
781.PP
788This is \f[I]easier\f[] to type.
782This is \f[I]easier\f[] to type.
789−Inline notes may contain links (http://google.com) and \f[C]]\f[]
783+Inline notes may contain links (http://google.com) and \f[C]]\f[] verbatim
790−verbatim characters, as well as [bracketed text].
784+characters, as well as [bracketed text].
791−
792.SS [4]
785.SS [4]
793.PP
786.PP
794In quote.
787In quote.
795−
796.SS [5]
788.SS [5]
797.PP
789.PP
798In list.
790In list.