@@ -31,7 +31,7 @@ TODO:
3131
3232[ ] warning for non-rendered raw content
3333[ ] is there a better way to do strikeout?
34−[ ] strong + em doesn't seem to work
34+[x] strong + em doesn't seem to work
3535[ ] super + subscript don't seem to work
3636[ ] options for hyperlink rendering (currently footnote)
3737[ ] avoid note-in-note (which we currently get easily with
@@ -43,10 +43,23 @@ TODO:
4343 they exist in the groff manual PDF version)
4444[ ] use a custom macro for defn lists so they're configurable
4545[ ] better handling of accented characters and other non-ascii
46− characters (e.g. curly quotes) -- we shouldn't rely on a
47− utf8 compatible groff
46+ characters (e.g. curly quotes).
47+ Note: recent versions of groff (more recent than standard
48+ on many systems) include a -k option which runs preconv.
49+ preconv basically converts non-ascii characters
50+ to \[uXXXX] entities. Since we can't assume that the local
51+ groff has the -k option, we could have any invocation of
52+ groff in Text.Pandoc.PDF filter the input through a Haskell
53+ function that does what preconv does.
54+ On the other hand: only recent groffs have -Tpdf. so
55+ if we want compatibility with older groffs, we need to to
56+ -Tps and pipe through ps2pdf (can we assume it's available?).
57+ A big advantage of gropdf: it supports the tag
58+ \X'pdf: pdfpic file alignment width height line-length'
59+ and also seems to support bookmarks.
4860[ ] avoid blank line after footnote marker when footnote has a
4961 paragraph
62+[ ] better smallcaps support, see below...
5063[ ] add via groff option to PDF module
5164[ ] better handling of autolinks?
5265[ ] better handling of images, perhaps converting to eps when
@@ -63,7 +76,8 @@ import Text.Pandoc.Writers.Shared
6376import Text.Pandoc.Options
6477import Text.Pandoc.Writers.Math
6578import Text.Printf ( printf )
66−import Data.List ( stripPrefix, intersperse, intercalate )
79+import qualified Data.Map as Map
80+import Data.List ( stripPrefix, intersperse, intercalate, sort )
6781import Data.Maybe (fromMaybe)
6882import Text.Pandoc.Pretty
6983import Text.Pandoc.Builder (deleteMeta)
@@ -73,7 +87,20 @@ import Data.Char ( isDigit )
7387import Text.TeXMath (writeEqn)
7488
7589data WriterState = WriterState { stHasInlineMath :: Bool
76− , stNotes :: [Note] }
90+ , stNotes :: [Note]
91+ , stFontFeatures :: Map.Map Char Bool
92+ }
93+
94+defaultWriterState :: WriterState
95+defaultWriterState = WriterState{ stHasInlineMath = False
96+ , stNotes = []
97+ , stFontFeatures = Map.fromList [
98+ ('I',False)
99+ , ('B',False)
100+ , ('C',False)
101+ ]
102+ }
103+
77104type Note = [Block]
78105
79106type MS = StateT WriterState
@@ -81,7 +108,7 @@ type MS = StateT WriterState
81108-- | Convert Pandoc to Ms.
82109writeMs :: PandocMonad m => WriterOptions -> Pandoc -> m String
83110writeMs opts document =
84− evalStateT (pandocToMs opts document) (WriterState False [])
111+ evalStateT (pandocToMs opts document) defaultWriterState
85112
86113-- | Return groff man representation of document.
87114pandocToMs :: PandocMonad m => WriterOptions -> Pandoc -> MS m String
@@ -342,12 +369,10 @@ inlineListToMs' opts lst = do
342369-- | Convert Pandoc inline element to man.
343370inlineToMs :: PandocMonad m => WriterOptions -> Inline -> MS m Doc
344371inlineToMs opts (Span _ ils) = inlineListToMs opts ils
345−inlineToMs opts (Emph lst) = do
346− contents <- inlineListToMs opts lst
347− return $ text "\\f[I]" <> contents <> text "\\f[]"
348−inlineToMs opts (Strong lst) = do
349− contents <- inlineListToMs opts lst
350− return $ text "\\f[B]" <> contents <> text "\\f[]"
372+inlineToMs opts (Emph lst) =
373+ withFontFeature 'I' (inlineListToMs opts lst)
374+inlineToMs opts (Strong lst) =
375+ withFontFeature 'B' (inlineListToMs opts lst)
351376inlineToMs opts (Strikeout lst) = do
352377 contents <- inlineListToMs opts lst
353378 return $ text "[STRIKEOUT:" <> contents <> char ']'
@@ -358,6 +383,8 @@ inlineToMs opts (Subscript lst) = do
358383 contents <- inlineListToMs opts lst
359384 return $ char '~' <> contents <> char '~'
360385inlineToMs opts (SmallCaps lst) = inlineListToMs opts lst -- not supported
386+-- but see https://lists.gnu.org/archive/html/groff/2015-01/msg00016.html
387+-- for a way to fake them
361388inlineToMs opts (Quoted SingleQuote lst) = do
362389 contents <- inlineListToMs opts lst
363390 return $ char '`' <> contents <> char '\''
@@ -367,7 +394,7 @@ inlineToMs opts (Quoted DoubleQuote lst) = do
367394inlineToMs opts (Cite _ lst) =
368395 inlineListToMs opts lst
369396inlineToMs _ (Code _ str) =
370− return $ text $ "\\f[C]" ++ escapeCode str ++ "\\f[]"
397+ withFontFeature 'C' (return $ text $ escapeCode str)
371398inlineToMs _ (Str str) = return $ text $ escapeString str
372399inlineToMs opts (Math InlineMath str) = do
373400 modify $ \st -> st{ stHasInlineMath = True }
@@ -424,3 +451,17 @@ handleNote opts bs = do
424451 contents <- blockListToMs opts bs
425452 return $ cr <> text ".FS" $$ contents $$ text ".FE" <> cr
426453
454+fontChange :: PandocMonad m => MS m Doc
455+fontChange = do
456+ features <- gets stFontFeatures
457+ let filling = sort [c | (c,True) <- Map.toList features]
458+ return $ text $ "\\f[" ++ filling ++ "]"
459+
460+withFontFeature :: PandocMonad m => Char -> MS m Doc -> MS m Doc
461+withFontFeature c action = do
462+ modify $ \st -> st{ stFontFeatures = Map.adjust not c $ stFontFeatures st }
463+ begin <- fontChange
464+ d <- action
465+ modify $ \st -> st{ stFontFeatures = Map.adjust not c $ stFontFeatures st }
466+ end <- fontChange
467+ return $ begin <> d <> end