Skip to content

Commit 852aa1e

Browse files
committed
Change header level by arbitrary number.
1 parent c6f5192 commit 852aa1e

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

IncludeFilter.hs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@ 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.
45+
Alternatively, use one of the following to increase all the header levels in the
46+
included file. The first option is a shortcut for incrementing the level by 1.
47+
The second demonstrates an increase of 2.
4748
4849
> ```include-indented
4950
51+
> ```{ .include header-change=2 }
52+
5053
If the file does not exist, it will be skipped completely. No warnings, no
5154
residue, nothing. Putting an # as the first character in the line will make the
5255
filter skip that file.
@@ -60,6 +63,7 @@ Note: the metadata from the included source files are discarded.
6063

6164
import Control.Monad
6265
import Data.List
66+
import Control.Error (readMay, fromMaybe)
6367
import System.Directory
6468

6569
import Text.Pandoc
@@ -93,13 +97,16 @@ processFiles changeInHeaderLevel toProcess =
9397
fmap concat (getContent changeInHeaderLevel `mapM` toProcess)
9498

9599
doInclude :: Block -> IO [Block]
96-
doInclude (CodeBlock (_, classes, _) list)
100+
doInclude (CodeBlock (_, classes, options) list)
97101
| "include" `elem` classes = do
98102
let toProcess = getProcessableFileList list
99-
processFiles 0 =<< toProcess
100-
| "include-indented" `elem` classes = do
101-
let toProcess = getProcessableFileList list
102-
processFiles 1 =<< toProcess
103+
changeInHeaderLevel = fromMaybe 0 $ readMay =<< "header-change" `lookup` options
104+
processFiles changeInHeaderLevel =<< toProcess
105+
| "include-indented" `elem` classes =
106+
doInclude $ CodeBlock ("", newClasses, newOptions) list
107+
where
108+
newClasses = ("include" :) . delete "include-indented" $ classes
109+
newOptions = ("header-change","1") : options
103110
doInclude x = return [x]
104111

105112
modifyHeaderLevelBlock :: Int -> Block -> Block

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ pandoc command will be executed.
1616
#do/not/include/this.md
1717
```
1818

19-
Alternatively, use the following to increase all the header numbers by one in
20-
the included file.
19+
Alternatively, use one of the following to increase all the header levels in the
20+
included file. The first option is a shortcut for incrementing the level by 1.
21+
The second demonstrates an increase of 2.
2122

2223
```include-indented
2324

25+
```{ .include header-change=2 }
26+
2427
If the file does not exist, it will be skipped completely. No warnings, no
2528
residue, nothing. Putting an `#` as the first character in the line will make the
2629
filter skip that file.

pandoc-include.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Source-repository head
2323

2424
Library
2525
Build-Depends: base >= 4.6 && < 5,
26+
errors >= 2.0.0,
2627
text >= 0.11,
2728
pandoc >= 1.13.0.0,
2829
pandoc-types >= 1.12.0.0,
@@ -35,6 +36,7 @@ Library
3536

3637
Executable pandoc-include
3738
Build-Depends: base >= 4.6,
39+
errors >= 2.0.0,
3840
text >= 0.11,
3941
pandoc >= 1.13.0.0,
4042
pandoc-types >= 1.12.0.0,

test/input.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ beta.md
1515
alpha.md
1616
```
1717

18+
```{ .include header-change=2 }
19+
alpha.md
20+
```
21+
1822
text

0 commit comments

Comments
 (0)