@@ -42,6 +42,11 @@ pandoc command will be executed.
4242> #do/not/include/this.md
4343> ```
4444
45+ Alternatively, use the following to increase all the header numbers by one in
46+ the included file.
47+
48+ > ```include-indented
49+
4550If the file does not exist, it will be skipped completely. No warnings, no
4651residue, nothing. Putting an # as the first character in the line will make the
4752filter skip that file.
@@ -60,6 +65,7 @@ import System.Directory
6065import Text.Pandoc
6166import Text.Pandoc.Error
6267import Text.Pandoc.JSON
68+ import Text.Pandoc.Walk
6369
6470stripPandoc :: Either PandocError Pandoc -> [Block ]
6571stripPandoc p =
@@ -70,28 +76,38 @@ stripPandoc p =
7076ioReadMarkdown :: String -> IO (Either PandocError Pandoc )
7177ioReadMarkdown content = return $! readMarkdown def content
7278
73- getContent :: String -> IO [Block ]
74- getContent file = do
79+ getContent :: Int -> String -> IO [Block ]
80+ getContent changeInHeaderLevel file = do
7581 c <- readFile file
7682 p <- ioReadMarkdown c
77- return $! stripPandoc p
83+ return $! stripPandoc (modifyHeaderLevelWith changeInHeaderLevel <$> p)
7884
7985getProcessableFileList :: String -> IO [String ]
8086getProcessableFileList list = do
8187 let f = lines list
8288 let files = filter (\ x -> not $ " #" `isPrefixOf` x) f
8389 filterM doesFileExist files
8490
85- processFiles :: [String ] -> IO [Block ]
86- processFiles toProcess =
87- fmap concat (mapM getContent toProcess)
91+ processFiles :: Int -> [String ] -> IO [Block ]
92+ processFiles changeInHeaderLevel toProcess =
93+ fmap concat (getContent changeInHeaderLevel `mapM` toProcess)
8894
8995doInclude :: Block -> IO [Block ]
9096doInclude (CodeBlock (_, classes, _) list)
9197 | " include" `elem` classes = do
9298 let toProcess = getProcessableFileList list
93- processFiles =<< toProcess
99+ processFiles 0 =<< toProcess
100+ | " include-indented" `elem` classes = do
101+ let toProcess = getProcessableFileList list
102+ processFiles 1 =<< toProcess
94103doInclude x = return [x]
95104
105+ modifyHeaderLevelBlock :: Int -> Block -> Block
106+ modifyHeaderLevelBlock n (Header int att inls) = Header (int + n) att inls
107+ modifyHeaderLevelBlock _ x = x
108+
109+ modifyHeaderLevelWith :: Int -> Pandoc -> Pandoc
110+ modifyHeaderLevelWith n = walk (modifyHeaderLevelBlock n)
111+
96112main :: IO ()
97113main = toJSONFilter doInclude
0 commit comments