Skip to content

Commit 718a947

Browse files
committed
Man reader: fixed nested emphasis.
1 parent bc7b4d9 commit 718a947

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

src/Text/Pandoc/Readers/Man.hs

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,52 @@ parseTitle = do
174174
return mempty
175175

176176
linePartsToInlines :: [LinePart] -> Inlines
177-
linePartsToInlines = go
177+
linePartsToInlines = go False
178178

179179
where
180-
go :: [LinePart] -> Inlines
181-
go [] = mempty
182-
go (MacroArg _:xs) = go xs -- shouldn't happen
183-
go (RoffStr s : xs) = text s <> go xs
184-
go (Font _newfonts : xs) = go xs
185-
go (FontSize _fs : xs) = go xs
180+
go :: Bool -> [LinePart] -> Inlines
181+
go _ [] = mempty
182+
go mono (MacroArg _:xs) = go mono xs -- shouldn't happen
183+
go mono (RoffStr s : xs)
184+
| mono = code s <> go mono xs
185+
| otherwise = text s <> go mono xs
186+
go mono (Font fs: xs) =
187+
if litals > 0 && litals >= lbolds && litals >= lmonos
188+
then emph (go mono (Font fs{ fontItalic = False } :
189+
map (adjustFontSpec (\s -> s{ fontItalic = False }))
190+
itals)) <>
191+
go mono italsrest
192+
else if lbolds > 0 && lbolds >= lmonos
193+
then strong (go mono (Font fs{ fontBold = False } :
194+
map (adjustFontSpec (\s -> s{ fontBold = False }))
195+
bolds)) <>
196+
go mono boldsrest
197+
else if lmonos > 0
198+
then go True (Font fs{ fontMonospace = False } :
199+
map (adjustFontSpec (\s -> s { fontMonospace = False }))
200+
monos) <> go mono monosrest
201+
else go mono xs
202+
where
203+
adjustFontSpec f (Font fspec) = Font (f fspec)
204+
adjustFontSpec _ x = x
205+
withFont f (Font fspec) = f fspec
206+
withFont _ _ = False
207+
litals = length itals
208+
lbolds = length bolds
209+
lmonos = length monos
210+
(itals, italsrest) =
211+
if fontItalic fs
212+
then break (withFont (not . fontItalic)) xs
213+
else ([], xs)
214+
(bolds, boldsrest) =
215+
if fontBold fs
216+
then break (withFont (not . fontBold)) xs
217+
else ([], xs)
218+
(monos, monosrest) =
219+
if fontMonospace fs
220+
then break (withFont (not . fontMonospace)) xs
221+
else ([], xs)
222+
go mono (FontSize _fs : xs) = go mono xs
186223

187224
parsePara :: PandocMonad m => ManParser m Blocks
188225
parsePara = para . trimInlines <$> parseInlines

0 commit comments

Comments
 (0)