Skip to content

Commit

Permalink
Include only a portion of a file (issue #8)
Browse files Browse the repository at this point in the history
  • Loading branch information
steindani committed May 13, 2017
1 parent beab4ba commit 913ca87
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
18 changes: 18 additions & 0 deletions IncludeFilter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,27 @@ includeCodeBlock (CodeBlock (_, classes, _) list) = do
let blocks = fmap (B.codeBlockWith ("", newclasses, [])) content
fmap B.toList blocks

cropContent :: [String] -> (String, String) -> [String]
cropContent lines (skip, count) =
if not $ null skip then
if not $ null count then
take (read count) (drop (read skip) lines)
else
drop (read skip) lines
else
lines

includeCropped :: Block -> IO [Block]
includeCropped (CodeBlock (_, classes, _) list) = do
let [filePath, skip, count] = lines list
let content = fileContentAsString filePath
let croppedContent = unlines <$> ((cropContent . lines <$> content) <*> pure (skip, count))
fmap (stripPandoc . readMarkdown def) croppedContent

doInclude :: Block -> IO [Block]
doInclude cb@(CodeBlock (_, classes, _) list)
| "code" `elem` classes = includeCodeBlock cb
| "cropped" `elem` classes = includeCropped cb
| "include" `elem` classes = simpleInclude list classes
doInclude x = return [x]

Expand Down
5 changes: 5 additions & 0 deletions test/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ gamma.md
```include
beta.md
```
```cropped
alpha.md
2
1
```

text

0 comments on commit 913ca87

Please sign in to comment.