Skip to content

Commit

Permalink
Improve blockquote parsing in dokuwiki.
Browse files Browse the repository at this point in the history
Allow for quoted code blocks.
  • Loading branch information
jgm committed Sep 22, 2024
1 parent e452f0c commit 98e77e0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/Text/Pandoc/Readers/DokuWiki.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import Text.Pandoc.Definition
import Text.Pandoc.Options
import Text.Pandoc.Parsing hiding (enclosed)
import Text.Pandoc.Shared (trim, stringify, tshow)
import Data.List (isPrefixOf, isSuffixOf, groupBy, intersperse)
import Data.List (isPrefixOf, isSuffixOf, groupBy)
import qualified Safe

-- | Read DokuWiki from an input string and return a Pandoc document.
Expand Down Expand Up @@ -461,15 +461,30 @@ quote = go <$> many1 blockQuoteLine
where
blockQuoteLine = try $ do
lev <- length <$> many1 (char '>')
contents <- B.trimInlines . mconcat <$> many1Till inline' eol
skipMany spaceChar
contents <- (blockCode <* skipMany spaceChar <* optional eol) <|>
(B.plain . B.trimInlines . mconcat <$> many1Till inline' eol)
pure (lev, contents)
go [] = mempty
go xs = mconcat $ map go' (groupBy (\(x,_) (y,_) -> (x == 0 && y == 0) ||
(x > 0 && y > 0)) xs)
go' [] = mempty
go' xs@((0,_):_) = B.plain . mconcat $
intersperse B.linebreak (map snd xs)
go' xs@((0,_):_) =
let (lns, bls) = F.foldl' consolidatePlains (mempty,mempty) (map snd xs)
in bls <> if lns == mempty
then mempty
else B.plain lns
go' xs = B.blockQuote (go $ map (\(x,y) -> (x - 1, y)) xs)
consolidatePlains (lns, bls) b =
case B.toList b of
[Plain ils] -> ((if lns == mempty
then B.fromList ils
else lns <> B.linebreak <> B.fromList ils), bls)
_ -> (mempty, bls <>
(if lns == lns
then mempty
else B.plain lns)
<> b)

blockRaw :: PandocMonad m => DWParser m B.Blocks
blockRaw = try $ do
Expand Down
16 changes: 16 additions & 0 deletions test/command/dokuwiki-quote.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
```
% pandoc -f dokuwiki -t native
> <code>some
code
</code>
>> ok
> then
> more
^D
[ BlockQuote
[ CodeBlock ( "" , [] , [] ) "some\ncode\n"
, BlockQuote [ Plain [ Str "ok" ] ]
, Plain [ Str "then" , LineBreak , Str "more" ]
]
]
```

0 comments on commit 98e77e0

Please sign in to comment.