Skip to content

Commit d5c0d3f

Browse files
committed
Merge remote-tracking branch 'steindani/issueFixes' into develop
* steindani/issueFixes: Revise fileContentAsString encoding (issue steindani#2) Include only a portion of a file (issue steindani#8) Refactoring, file contents read as UTF-8 Use UTF-8 if requested (issue steindani#2) CodeBlock include and file reader changes
2 parents 18d9765 + 27a7ccb commit d5c0d3f

File tree

5 files changed

+73
-25
lines changed

5 files changed

+73
-25
lines changed

IncludeFilter.hs

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#!/usr/bin/env runhaskell
22

3-
{-# LANGUAGE ViewPatterns #-}
4-
53
{-
64
The MIT License (MIT)
75
@@ -67,18 +65,24 @@ example, if the header is incremented by 1, the title is inserted as a level 1 h
6765
6866
-}
6967

68+
{-# LANGUAGE BangPatterns #-}
69+
{-# LANGUAGE ViewPatterns #-}
70+
71+
7072
import Control.Monad
7173
import Data.List
7274
import qualified Data.Char as C
7375
import qualified Data.Map as Map
7476
import Control.Error (readMay, fromMaybe)
7577
import System.Directory
78+
import System.IO
7679

7780
import Text.Pandoc
7881
import Text.Pandoc.Shared (uniqueIdent, stringify)
7982
import Text.Pandoc.Error
8083
import Text.Pandoc.JSON
8184
import Text.Pandoc.Walk
85+
import qualified Text.Pandoc.Builder as B
8286

8387
stripPandoc :: Int -> Either PandocError Pandoc -> [Block]
8488
stripPandoc _ (Left _) = [Null]
@@ -110,36 +114,63 @@ modifyHeaderLevelBlockWith _ _ x = x
110114
modifyHeaderLevelWith :: Int -> Pandoc -> Pandoc
111115
modifyHeaderLevelWith n = walk (modifyHeaderLevelBlockWith n mempty)
112116

113-
ioReadMarkdown :: String -> IO(Either PandocError Pandoc)
114-
ioReadMarkdown content = return $! readMarkdown def content
117+
fileContentAsString :: String -> IO String
118+
fileContentAsString file = withFile file ReadMode $ \handle -> do
119+
hSetEncoding handle utf8
120+
hGetContents handle
115121

116-
getContent :: Int -> String -> IO [Block]
117-
getContent changeInHeaderLevel file = do
118-
c <- readFile file
119-
p <- ioReadMarkdown c
120-
return $! stripPandoc changeInHeaderLevel p
122+
fileContentAsBlocks :: Int -> String -> IO [Block]
123+
fileContentAsBlocks changeInHeaderLevel file = do
124+
let contents = fileContentAsString file
125+
let p = fmap (readMarkdown def) contents
126+
stripPandoc changeInHeaderLevel <$> p
121127

122-
getProcessableFileList :: String -> IO [String]
128+
getProcessableFileList :: String -> [String]
123129
getProcessableFileList list = do
124130
let f = lines list
125-
let files = filter (\x -> not $ "#" `isPrefixOf` x) f
126-
filterM doesFileExist files
127-
128-
processFiles :: Int -> [String] -> IO [Block]
129-
processFiles changeInHeaderLevel toProcess =
130-
fmap concat (getContent changeInHeaderLevel `mapM` toProcess)
131+
filter (\x -> not $ "#" `isPrefixOf` x) f
132+
133+
simpleInclude :: Int -> String -> [String] -> IO [Block]
134+
simpleInclude changeInHeaderLevel list classes = do
135+
let toProcess = getProcessableFileList list
136+
fmap concat (fileContentAsBlocks changeInHeaderLevel `mapM` toProcess)
137+
138+
includeCodeBlock :: Block -> IO [Block]
139+
includeCodeBlock (CodeBlock (_, classes, _) list) = do
140+
let filePath = head $ lines list
141+
let content = fileContentAsString filePath
142+
let newclasses = filter (\x -> "include" `isPrefixOf` x || "code" `isPrefixOf` x) classes
143+
let blocks = fmap (B.codeBlockWith ("", newclasses, [])) content
144+
fmap B.toList blocks
145+
146+
cropContent :: [String] -> (String, String) -> [String]
147+
cropContent lines (skip, count) =
148+
if not $ null skip then
149+
if not $ null count then
150+
take (read count) (drop (read skip) lines)
151+
else
152+
drop (read skip) lines
153+
else
154+
lines
155+
156+
includeCropped :: Block -> IO [Block]
157+
includeCropped (CodeBlock (_, classes, _) list) = do
158+
let [filePath, skip, count] = lines list
159+
let content = fileContentAsString filePath
160+
let croppedContent = unlines <$> ((cropContent . lines <$> content) <*> pure (skip, count))
161+
fmap (stripPandoc 0 . readMarkdown def) croppedContent
131162

132163
doInclude :: Block -> IO [Block]
133-
doInclude (CodeBlock (_, classes, options) list)
164+
doInclude cb@(CodeBlock (_, classes, options) list)
134165
| "include" `elem` classes = do
135-
let toProcess = getProcessableFileList list
136-
changeInHeaderLevel = fromMaybe 0 $ readMay =<< "header-change" `lookup` options
137-
processFiles changeInHeaderLevel =<< toProcess
138-
| "include-indented" `elem` classes =
166+
let changeInHeaderLevel = fromMaybe 0 $ readMay =<< "header-change" `lookup` options
167+
simpleInclude changeInHeaderLevel list classes
168+
| "include-indented" `elem` classes = do
169+
let newClasses = ("include" :) . delete "include-indented" $ classes
170+
let newOptions = ("header-change","1") : options
139171
doInclude $ CodeBlock ("", newClasses, newOptions) list
140-
where
141-
newClasses = ("include" :) . delete "include-indented" $ classes
142-
newOptions = ("header-change","1") : options
172+
| "code" `elem` classes = includeCodeBlock cb
173+
| "cropped" `elem` classes = includeCropped cb
143174
doInclude x = return [x]
144175

145176
main :: IO ()

pandoc-include.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- This file has been generated from package.yaml by hpack version 0.15.0.
1+
-- This file has been generated from package.yaml by hpack version 0.18.1.
22
--
33
-- see: https://github.com/sol/hpack
44

test/encoding/include.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* четыре
2+
* пять
3+
* шесть

test/encoding/test.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Тест
2+
3+
* один
4+
* два
5+
* три
6+
7+
```include utf8
8+
include.md
9+
```

test/input.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ gamma.md
1010
```include
1111
beta.md
1212
```
13+
```cropped
14+
alpha.md
15+
2
16+
1
17+
```
1318

1419
```include-indented
1520
alpha.md

0 commit comments

Comments
 (0)