|
1 | 1 | #!/usr/bin/env runhaskell |
2 | | --- includes.hs |
3 | 2 |
|
4 | | -module Text.Pandoc.Include where |
| 3 | +{- |
| 4 | +The MIT License (MIT) |
5 | 5 |
|
6 | | -import Control.Monad |
7 | | -import Data.List |
8 | | -import System.Directory |
| 6 | +Copyright (c) 2015 Dániel Stein <[email protected]> |
9 | 7 |
|
10 | | -import Text.Pandoc.JSON |
11 | | -import Text.Pandoc |
12 | | -import Text.Pandoc.Error |
| 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: |
13 | 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 |
14 | 63 |
|
15 | 64 | stripPandoc :: Either PandocError Pandoc -> [Block] |
16 | 65 | stripPandoc p = |
|
0 commit comments