Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented #168 #3670

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/Text/Pandoc/Readers/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ block = do
, bulletList
, header
, lhsCodeBlock
, divBlock
, divHtml
, htmlBlock
, table
Expand Down Expand Up @@ -745,6 +746,8 @@ birdTrackLine c = try $ do
char c
-- allow html tags on left margin:
when (c == '<') $ notFollowedBy letter
-- allow inplace-images on left margin:
when (c == '!') $ notFollowedBy (char '[')
anyLine

--
Expand Down Expand Up @@ -775,6 +778,82 @@ blockQuote = do
contents <- parseFromString parseBlocks $ (intercalate "\n" raw) ++ "\n\n"
return $ B.blockQuote <$> contents

--
-- div blocks
--

openingDiv :: PandocMonad m => MarkdownParser m Attr
openingDiv = do
string ":::"
skipMany (char ':')
skipMany spaceChar
mclass <- optionMaybe $ do
c <- many1 alphaNum
skipMany1 spaceChar
skipMany1 (char ':')
return c
attr' <- option nullAttr $ do
skipMany spaceChar
attributes
let addClass :: Attr -> String -> Attr
addClass (id',cs, kv) c = (id', (c:cs), kv)
anyLine
return $ maybe attr' (addClass attr') mclass -- if we have mclass, add it, otherwise leave it unchanged

closingDiv :: PandocMonad m => MarkdownParser m ()
closingDiv = do
string ":::"
skipMany spaceChar
((char '\n' >> return ()) <|> eof)

divBlockBirdTrack :: PandocMonad m => MarkdownParser m (F Blocks)
divBlockBirdTrack = try $ do
pos <- getPosition
when (sourceColumn pos /= 1) $ fail "Not in first colum"
attr' <- option nullAttr (attributes <* (skipSpaces >> newline))
lns <- many1 $ birdTrackLine '!'
-- if (as is normal) there is always a space after !, drop it
let lns' = if all (\ln -> null ln || take 1 ln == " ") lns
then map (drop 1) lns
else lns
-- recursively parse lns' we collected
bs <- parseFromString parseBlocks (intercalate "\n" lns' ++ "\n")
return $ B.divWith attr' <$> bs

divBlockBeginEnd :: PandocMonad m => MarkdownParser m (F Blocks)
divBlockBeginEnd = try $ do
pos <- getPosition
when (sourceColumn pos /= 1) $ fail "Not in first column"
attr' <- openingDiv
bs <- divCounter 1 --look for closingDiv and count nesting-depth
bs' <- parseFromString parseBlocks (intercalate "\n" (init bs) ++ "\n")
-- last entry in bs ^^ is the closingDiv, drop it for recursive parsing
return $ B.divWith attr' <$> bs'

divCounter :: PandocMonad m => Int -> MarkdownParser m [String]
divCounter 0 = return []
divCounter i = do
closed <- option False (lookAhead (try closingDiv) >> return True)
case closed of
True -> do
x <- anyLine
xs <- divCounter (i-1)
return (x:xs)
False -> do
opening <- option False (lookAhead (try openingDiv) >> return True)
case opening of
True -> do
x <- anyLine
xs <- divCounter (i+1)
return (x:xs)
False -> do
x <- anyLine
xs <- divCounter i
return (x:xs)

divBlock :: PandocMonad m => MarkdownParser m (F Blocks)
divBlock = divBlockBirdTrack <|> divBlockBeginEnd <?> "divBlock"

--
-- list blocks
--
Expand Down
78 changes: 78 additions & 0 deletions test/command/168.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

```
% pandoc -f markdown -t html
:::{.class}
foo
:::
^D
<div class="class">
foo
</div>
```

```
% pandoc -f markdown -t html
:::::: info ::
foo
:::
^D
<div class="info">
foo
</div>
```

```
% pandoc -f markdown -t html
:::::: info :: {.combine}
foo
:::
^D
<div class="info combine">
foo
</div>
```

```
% pandoc -f markdown -t html
:::::: {#id .class}
foo
:::
^D
<div id="id" class="class">
foo
</div>
```

```
% pandoc -f markdown -t html
{#id .class}
! foo
^D
<div id="id" class="class">
foo
</div>
```

```
% pandoc -f markdown -t html
! foo
!
! {.attr}
! ! nested
^D
<div>
<p>foo</p>
<div class="attr">
nested
</div>
</div>
```

```
% pandoc -f markdown -t html
!noimage
^D
<div>
noimage
</div>
```