Skip to content

Commit 8329e6e

Browse files
committed
Working only for 2 level and for the first item
0 parents  commit 8329e6e

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

IncludeFilter.hs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env runhaskell
2+
-- includes.hs
3+
4+
module Text.Pandoc.Include where
5+
6+
import Control.Monad
7+
import Data.List.Split
8+
9+
import Text.Pandoc.JSON
10+
import Text.Pandoc
11+
import Text.Pandoc.Error
12+
13+
14+
stripPandoc :: Either PandocError Pandoc -> [Block]
15+
stripPandoc p =
16+
case p of
17+
Left _ -> [Null]
18+
Right (Pandoc _ blocks) -> blocks
19+
20+
21+
ioReadMarkdown :: String -> IO(Either PandocError Pandoc)
22+
ioReadMarkdown content = return (readMarkdown def content)
23+
24+
25+
getContent :: String -> IO [Block]
26+
getContent file = do
27+
c <- readFile file
28+
p <- ioReadMarkdown c
29+
return (stripPandoc p)
30+
31+
32+
doInclude :: Block -> IO [Block]
33+
doInclude cb@(CodeBlock (_, classes, _) list) =
34+
if "include" `elem` classes
35+
then do
36+
-- msum $ map getContent (splitOn "\n" list)
37+
files <- return $ wordsBy (=='\n') list
38+
contents <- return $ map getContent files
39+
result <- return $ msum contents
40+
result
41+
-- msum $ map (\file ->
42+
-- do
43+
-- c <- readFile file
44+
-- p <- ioReadMarkdown c
45+
-- return (stripPandoc p)
46+
-- ) (wordsBy (=='\n') list)
47+
else
48+
return [cb]
49+
doInclude x = return [x]
50+
51+
52+
main :: IO ()
53+
main = toJSONFilter doInclude

alpha.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Alpha!
2+
3+
Text from alpha.
4+
5+
```include
6+
beta.md
7+
```
8+
9+
Text after beta include.

beta.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BETA!!!

input.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
text
2+
3+
```include
4+
alpha.md
5+
alpha.md
6+
beta.md
7+
gamma.md
8+
#alpha.md
9+
```
10+
```include
11+
beta.md
12+
```
13+
14+
text

0 commit comments

Comments
 (0)