@@ -72,17 +72,17 @@ stripPandoc p =
72
72
Left _ -> [Null ]
73
73
Right (Pandoc _ blocks) -> blocks
74
74
75
- setEncodingToUTF8 :: IO Handle -> Bool -> IO ()
76
- setEncodingToUTF8 handle useUTF8
77
- | useUTF8 = join $ fmap (`hSetEncoding` utf8) handle
78
- | not useUTF8 = return ()
79
-
80
- getContent :: String -> Bool -> IO [Block ]
81
- getContent file useUTF8 = do
75
+ fileContentAsString :: String -> IO String
76
+ fileContentAsString file = do
82
77
let handle = openFile file ReadMode
83
- setEncodingToUTF8 handle useUTF8
78
+ fmap ( `hSetEncoding` utf8) handle
84
79
! contents <- fmap hGetContents handle
85
80
fmap hClose handle
81
+ contents
82
+
83
+ fileContentAsBlocks :: String -> IO [Block ]
84
+ fileContentAsBlocks file = do
85
+ let contents = fileContentAsString file
86
86
let p = fmap (readMarkdown def) contents
87
87
fmap stripPandoc p
88
88
@@ -91,24 +91,22 @@ getProcessableFileList list = do
91
91
let f = lines list
92
92
filter (\ x -> not $ " #" `isPrefixOf` x) f
93
93
94
- processFiles :: [String ] -> Bool -> IO [Block ]
95
- processFiles toProcess useUTF8 =
96
- fmap concat (mapM (`getContent` useUTF8) toProcess)
97
-
98
94
simpleInclude :: String -> [String ] -> IO [Block ]
99
95
simpleInclude list classes = do
100
96
let toProcess = getProcessableFileList list
101
- let useUTF8 = " utf8" `elem` classes
102
- processFiles toProcess useUTF8
97
+ fmap concat (mapM fileContentAsBlocks toProcess)
98
+
99
+ includeCodeBlock :: Block -> IO [Block ]
100
+ includeCodeBlock (CodeBlock (_, classes, _) list) = do
101
+ let filePath = head $ lines list
102
+ let content = fileContentAsString filePath
103
+ let newclasses = filter (\ x -> " include" `isPrefixOf` x || " code" `isPrefixOf` x) classes
104
+ let blocks = fmap (B. codeBlockWith (" " , newclasses, [] )) content
105
+ fmap B. toList blocks
103
106
104
107
doInclude :: Block -> IO [Block ]
105
- doInclude (CodeBlock (_, classes, _) list)
106
- | " code" `elem` classes = do
107
- let filePath = head $ lines list
108
- let content = withFile filePath ReadMode hGetContents
109
- let newclasses = filter (\ x -> " include" `isPrefixOf` x || " code" `isPrefixOf` x) classes
110
- let blocks = fmap (B. codeBlockWith (" " , newclasses, [] )) content
111
- fmap B. toList blocks
108
+ doInclude cb@ (CodeBlock (_, classes, _) list)
109
+ | " code" `elem` classes = includeCodeBlock cb
112
110
| " include" `elem` classes = simpleInclude list classes
113
111
doInclude x = return [x]
114
112
0 commit comments