Skip to content

Commit

Permalink
Man reader: move spaces outside of emph/strong.
Browse files Browse the repository at this point in the history
This is needed to avoid problems in conversion to Markdown
and some other formats. Closes #9445.
  • Loading branch information
jgm committed Feb 12, 2024
1 parent 8c85fbb commit bd352e4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/Text/Pandoc/Readers/Man.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import Text.Pandoc.Walk (query)
import Text.Pandoc.Readers.Roff -- TODO explicit imports
import qualified Text.Pandoc.Parsing as P
import qualified Data.Foldable as Foldable
import Text.Pandoc.Shared (extractSpaces)

data ManState = ManState { readerOptions :: ReaderOptions
, metadata :: Meta
Expand Down Expand Up @@ -241,12 +242,12 @@ linePartsToInlines = go False
| otherwise = text s <> go mono xs
go mono (Font fs: xs)
| litals > 0 && litals >= lbolds && litals >= lmonos
= emph (go mono (Font fs{ fontItalic = False } :
= extractSpaces emph (go mono (Font fs{ fontItalic = False } :
map (adjustFontSpec (\s -> s{ fontItalic = False }))
itals)) <>
go mono italsrest
| lbolds > 0 && lbolds >= lmonos
= strong (go mono (Font fs{ fontBold = False } :
= extractSpaces strong (go mono (Font fs{ fontBold = False } :
map (adjustFontSpec (\s -> s{ fontBold = False }))
bolds)) <>
go mono boldsrest
Expand Down Expand Up @@ -305,12 +306,12 @@ handleInlineMacro mname args _pos =
"RI" -> parseAlternatingFonts [id, emph] args
"BR" -> parseAlternatingFonts [strong, id] args
"RB" -> parseAlternatingFonts [id, strong] args
"SY" -> return $ strong $ mconcat $ intersperse B.space
"SY" -> return $ extractSpaces strong $ mconcat $ intersperse B.space
$ map linePartsToInlines args
"YS" -> return mempty
"OP" -> case args of
(x:ys) -> return $ B.space <> str "[" <> B.space <>
mconcat (strong (linePartsToInlines x) :
mconcat (extractSpaces strong (linePartsToInlines x) :
map ((B.space <>) . linePartsToInlines) ys)
<> B.space <> str "]"
[] -> return mempty
Expand All @@ -319,16 +320,18 @@ handleInlineMacro mname args _pos =
parseBold :: PandocMonad m => [Arg] -> ManParser m Inlines
parseBold [] = do
TextLine lparts <- mline
return $ strong $ linePartsToInlines lparts
return $ extractSpaces strong $ linePartsToInlines lparts
parseBold args = return $
strong $ mconcat $ intersperse B.space $ map linePartsToInlines args
extractSpaces strong $
mconcat $ intersperse B.space $ map linePartsToInlines args

parseItalic :: PandocMonad m => [Arg] -> ManParser m Inlines
parseItalic [] = do
TextLine lparts <- mline
return $ emph $ linePartsToInlines lparts
return $ extractSpaces emph $ linePartsToInlines lparts
parseItalic args = return $
emph $ mconcat $ intersperse B.space $ map linePartsToInlines args
extractSpaces emph $
mconcat $ intersperse B.space $ map linePartsToInlines args

parseAlternatingFonts :: [Inlines -> Inlines]
-> [Arg]
Expand Down
6 changes: 6 additions & 0 deletions test/command/9445.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```
% pandoc -f man -t markdown
\fB--c \fI<file>
^D
**\--c** *\<file\>*
```

0 comments on commit bd352e4

Please sign in to comment.