├── .gitignore ├── CHANGELOG.md ├── IncludeFilter.hs ├── LICENSE ├── README.md ├── Setup.hs ├── pandoc-include.cabal └── test ├── alpha.md ├── beta.md ├── gamma.md └── input.md /.gitignore: -------------------------------------------------------------------------------- 1 | dist/ 2 | .idea 3 | *.iml 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## v0.0.1 – 2015-12-08 4 | * Project restructured 5 | * License 6 | * Initial documentation 7 | * Cabal package file 8 | -------------------------------------------------------------------------------- /IncludeFilter.hs: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env runhaskell 2 | 3 | {- 4 | The MIT License (MIT) 5 | 6 | Copyright (c) 2015 Dániel Stein 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining 9 | a copy of this software and associated documentation files (the 10 | "Software"), to deal in the Software without restriction, including 11 | without limitation the rights to use, copy, modify, merge, publish, 12 | distribute, sublicense, and/or sell copies of the Software, and to 13 | permit persons to whom the Software is furnished to do so, subject to 14 | the following conditions: 15 | 16 | The above copyright notice and this permission notice shall be included 17 | in all copies or substantial portions of the Software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 22 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 23 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 24 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 25 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 | -} 27 | 28 | {-| 29 | A Pandoc filter that replaces include labeled Code Blocks with the contents of 30 | the referenced files. Even nested, recursive includes. 31 | 32 | Based on the scripting tutorial for Pandoc: 33 | http://pandoc.org/scripting.html#include-files 34 | 35 | The Code Blocks like the following will include every file in a new line. The 36 | reference paths should be either absolute or relative to the folder where the 37 | pandoc command will be executed. 38 | 39 | > ```include 40 | > /absolute/file/path.md 41 | > relative/to/the/command/root.md 42 | > #do/not/include/this.md 43 | > ``` 44 | 45 | If the file does not exist, it will be skipped completely. No warnings, no 46 | residue, nothing. Putting an # as the first character in the line will make the 47 | filter skip that file. 48 | 49 | For now the nested includes only work for two levels, after that the source 50 | will be inserted and not parsed. 51 | 52 | Note: the metadata from the included source files are discarded. 53 | 54 | -} 55 | 56 | import Control.Monad 57 | import Data.List 58 | import System.Directory 59 | 60 | import Text.Pandoc 61 | import Text.Pandoc.Error 62 | import Text.Pandoc.JSON 63 | 64 | stripPandoc :: Either PandocError Pandoc -> [Block] 65 | stripPandoc p = 66 | case p of 67 | Left _ -> [Null] 68 | Right (Pandoc _ blocks) -> blocks 69 | 70 | ioReadMarkdown :: String -> IO(Either PandocError Pandoc) 71 | ioReadMarkdown content = return $! readMarkdown def content 72 | 73 | getContent :: String -> IO [Block] 74 | getContent file = do 75 | c <- readFile file 76 | p <- ioReadMarkdown c 77 | return $! stripPandoc p 78 | 79 | getProcessableFileList :: String -> IO [String] 80 | getProcessableFileList list = do 81 | let f = lines list 82 | let files = filter (\x -> not $ "#" `isPrefixOf` x) f 83 | filterM doesFileExist files 84 | 85 | processFiles :: [String] -> IO [Block] 86 | processFiles toProcess = 87 | fmap concat (mapM getContent toProcess) 88 | 89 | doInclude :: Block -> IO [Block] 90 | doInclude (CodeBlock (_, classes, _) list) 91 | | "include" `elem` classes = do 92 | let toProcess = getProcessableFileList list 93 | processFiles =<< toProcess 94 | doInclude x = return [x] 95 | 96 | main :: IO () 97 | main = toJSONFilter doInclude 98 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Dániel Stein 4 | 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining 7 | a copy of this software and associated documentation files (the 8 | "Software"), to deal in the Software without restriction, including 9 | without limitation the rights to use, copy, modify, merge, publish, 10 | distribute, sublicense, and/or sell copies of the Software, and to 11 | permit persons to whom the Software is furnished to do so, subject to 12 | the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included 15 | in all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pandoc-include 2 | A Pandoc filter that replaces include labeled Code Blocks with the contents of 3 | the referenced files. Even nested, recursive includes. 4 | 5 | Based on the scripting tutorial for Pandoc: 6 | http://pandoc.org/scripting.html#include-files 7 | 8 | ## Format 9 | The Code Blocks like the following will include every file in a new line. The 10 | reference paths should be either absolute or relative to the folder where the 11 | pandoc command will be executed. 12 | 13 | ```markdown 14 | ```include 15 | /absolute/file/path.md 16 | relative/to/the/command/root.md 17 | #do/not/include/this.md 18 | ``` 19 | ``` 20 | 21 | If the file does not exist, it will be skipped completely. No warnings, no 22 | residue, nothing. Putting an `#` as the first character in the line will make the 23 | filter skip that file. 24 | 25 | For now the nested includes only work for two levels, after that the source 26 | will be inserted and not parsed. 27 | 28 | *Note: the metadata from the included source files are discarded.* 29 | 30 | ## Installation 31 | One could either install it using the Cabal packaging system by running: 32 | 33 | ``` 34 | cabal update 35 | cabal install pandoc-include 36 | ``` 37 | 38 | Or it is also possible to use the pipe method using the source code described in the *Usage* section. 39 | 40 | ## Usage 41 | In order to use this Pandoc filter, one has to include it into the Pandoc transformation workflow. This can be done by using the `--filter` parameter, like so: 42 | 43 | ``` 44 | pandoc --from markdown --to latex --filter pandoc-include input.md 45 | ``` 46 | 47 | All this does in the background is pipelining the output of Pandoc and the last filter into the standard input of the include filter and using its output as the next filter's input: 48 | 49 | ``` 50 | pandoc --from markdown --to json input.md | runhaskell IncludeFilter.hs | pandoc --from json --to latex 51 | ``` 52 | 53 | ## License 54 | Copyright ©2015 [Dániel Stein](https://twitter.com/steindani) 55 | 56 | Source code is released under the Terms and Conditions of [MIT License](http://opensource.org/licenses/MIT). 57 | -------------------------------------------------------------------------------- /Setup.hs: -------------------------------------------------------------------------------- 1 | import Distribution.Simple 2 | main = defaultMain 3 | -------------------------------------------------------------------------------- /pandoc-include.cabal: -------------------------------------------------------------------------------- 1 | Name: pandoc-include 2 | Version: 0.0.1 3 | Synopsis: Include other Markdown files 4 | Description: A Pandoc filter that replaces include labeled 5 | Code Blocks with the contents of the referenced 6 | Markdown files. Even nested, recursive includes. 7 | Homepage: https://github.com/steindani/pandoc-include 8 | Bug-Reports: https://github.com/steindani/pandoc-include/issues 9 | License: MIT 10 | License-File: LICENSE 11 | Author: Dániel Stein 12 | Maintainer: Dániel Stein 13 | Copyright: (c) 2015 Dániel Stein 14 | Stability: alpha 15 | Category: Text 16 | Build-Type: Simple 17 | Data-Files: README.md 18 | Extra-Source-Files: CHANGELOG.md 19 | Cabal-Version: >=1.10 20 | Source-repository head 21 | type: git 22 | location: git://github.com/steindani/pandoc-include.git 23 | 24 | Library 25 | Build-Depends: base >= 4.6 && < 5, 26 | text >= 0.11, 27 | pandoc >= 1.13.0.0, 28 | pandoc-types >= 1.12.0.0, 29 | directory 30 | Hs-Source-Dirs: src 31 | Default-Extensions: CPP 32 | Exposed-Modules: 33 | Buildable: True 34 | Default-Language: Haskell2010 35 | 36 | Executable pandoc-include 37 | Build-Depends: base >= 4.6, 38 | text >= 0.11, 39 | pandoc >= 1.13.0.0, 40 | pandoc-types >= 1.12.0.0, 41 | directory 42 | Hs-Source-Dirs: . 43 | Main-Is: IncludeFilter.hs 44 | Buildable: True 45 | Default-Language: Haskell2010 46 | -------------------------------------------------------------------------------- /test/alpha.md: -------------------------------------------------------------------------------- 1 | # Alpha! 2 | 3 | Text from alpha. 4 | 5 | ```include 6 | beta.md 7 | ``` 8 | 9 | Text after beta include. 10 | -------------------------------------------------------------------------------- /test/beta.md: -------------------------------------------------------------------------------- 1 | BETA!!! 2 | 3 | ```include 4 | gamma.md 5 | ``` 6 | -------------------------------------------------------------------------------- /test/gamma.md: -------------------------------------------------------------------------------- 1 | Gamma. 2 | -------------------------------------------------------------------------------- /test/input.md: -------------------------------------------------------------------------------- 1 | text 2 | 3 | ```include 4 | alpha.md 5 | alpha.md 6 | beta.md 7 | gamma.md 8 | #alpha.md 9 | ``` 10 | ```include 11 | beta.md 12 | ``` 13 | 14 | text 15 | --------------------------------------------------------------------------------