Skip to content

Commit beab4ba

Browse files
committed
Refactoring, file contents read as UTF-8
1 parent 99d0b02 commit beab4ba

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

IncludeFilter.hs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,17 @@ stripPandoc p =
7272
Left _ -> [Null]
7373
Right (Pandoc _ blocks) -> blocks
7474

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
8277
let handle = openFile file ReadMode
83-
setEncodingToUTF8 handle useUTF8
78+
fmap (`hSetEncoding` utf8) handle
8479
!contents <- fmap hGetContents handle
8580
fmap hClose handle
81+
contents
82+
83+
fileContentAsBlocks :: String -> IO [Block]
84+
fileContentAsBlocks file = do
85+
let contents = fileContentAsString file
8686
let p = fmap (readMarkdown def) contents
8787
fmap stripPandoc p
8888

@@ -91,24 +91,22 @@ getProcessableFileList list = do
9191
let f = lines list
9292
filter (\x -> not $ "#" `isPrefixOf` x) f
9393

94-
processFiles :: [String] -> Bool -> IO [Block]
95-
processFiles toProcess useUTF8 =
96-
fmap concat (mapM (`getContent` useUTF8) toProcess)
97-
9894
simpleInclude :: String -> [String] -> IO [Block]
9995
simpleInclude list classes = do
10096
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
103106

104107
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
112110
| "include" `elem` classes = simpleInclude list classes
113111
doInclude x = return [x]
114112

0 commit comments

Comments
 (0)