Skip to content

Commit 53b0d17

Browse files
committed
CodeBlock include and file reader changes
1 parent 5090512 commit 53b0d17

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

IncludeFilter.hs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,44 +53,56 @@ Note: the metadata from the included source files are discarded.
5353
5454
-}
5555

56+
{-# LANGUAGE BangPatterns #-}
57+
5658
import Control.Monad
5759
import Data.List
5860
import System.Directory
61+
import System.IO
5962

6063
import Text.Pandoc
6164
import Text.Pandoc.Error
6265
import Text.Pandoc.JSON
66+
import qualified Text.Pandoc.Builder as B
67+
6368

6469
stripPandoc :: Either PandocError Pandoc -> [Block]
6570
stripPandoc p =
6671
case p of
6772
Left _ -> [Null]
6873
Right (Pandoc _ blocks) -> blocks
6974

70-
ioReadMarkdown :: String -> IO(Either PandocError Pandoc)
71-
ioReadMarkdown content = return $! readMarkdown def content
72-
7375
getContent :: String -> IO [Block]
7476
getContent file = do
75-
c <- readFile file
76-
p <- ioReadMarkdown c
77-
return $! stripPandoc p
77+
let handle = openFile file ReadMode
78+
!contents <- fmap hGetContents handle
79+
fmap hClose handle
80+
let p = fmap (readMarkdown def) contents
81+
fmap stripPandoc p
7882

79-
getProcessableFileList :: String -> IO [String]
83+
getProcessableFileList :: String -> [String]
8084
getProcessableFileList list = do
8185
let f = lines list
82-
let files = filter (\x -> not $ "#" `isPrefixOf` x) f
83-
filterM doesFileExist files
86+
filter (\x -> not $ "#" `isPrefixOf` x) f
8487

8588
processFiles :: [String] -> IO [Block]
8689
processFiles toProcess =
8790
fmap concat (mapM getContent toProcess)
8891

92+
simpleInclude :: String -> IO [Block]
93+
simpleInclude list = do
94+
let toProcess = getProcessableFileList list
95+
processFiles toProcess
96+
8997
doInclude :: Block -> IO [Block]
9098
doInclude (CodeBlock (_, classes, _) list)
91-
| "include" `elem` classes = do
92-
let toProcess = getProcessableFileList list
93-
processFiles =<< toProcess
99+
| "code" `elem` classes = do
100+
let filePath = head $ lines list
101+
let content = withFile filePath ReadMode hGetContents
102+
let newclasses = filter (\x -> "include" `isPrefixOf` x || "code" `isPrefixOf` x) classes
103+
let blocks = fmap (B.codeBlockWith ("", newclasses, [])) content
104+
fmap B.toList blocks
105+
| "include" `elem` classes = simpleInclude list
94106
doInclude x = return [x]
95107

96108
main :: IO ()

0 commit comments

Comments
 (0)