From 913ca874b31eaf87ed5996b2856a45e7512f028f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Stein?= Date: Sat, 13 May 2017 23:47:19 +0200 Subject: [PATCH] Include only a portion of a file (issue #8) --- IncludeFilter.hs | 18 ++++++++++++++++++ test/input.md | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/IncludeFilter.hs b/IncludeFilter.hs index 7599b03..00d209b 100644 --- a/IncludeFilter.hs +++ b/IncludeFilter.hs @@ -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] diff --git a/test/input.md b/test/input.md index 000f668..544bcb3 100644 --- a/test/input.md +++ b/test/input.md @@ -10,5 +10,10 @@ gamma.md ```include beta.md ``` +```cropped +alpha.md +2 +1 +``` text