Skip to main content

Pandoc filter to allow file and header includes

Project description

pandoc-include

PyPI PyPI - Downloads GitHub

Pandoc filter to allow file and header includes.

The filter script is based on User Guide for Panflute. This repository is to provide a simple way to install and use it.

Features

  • Include as raw blocks
  • Indent and dedent included contents
  • Partial include: Allow including only parts of the file using options
  • Code include: Allow using !include in code blocks
  • Unix style pathname
  • Recursive include: It depends on include-entry header to work
  • Yaml header Merging: When an included file has its header, it will be merged into the current header. If there's a conflict, the original header of the current file remains.
  • Header include: Use !include-header file.yaml to include Yaml header from file.

Installation

pandoc-include requires python and pip.

Then, use pip to install:

pip install --user pandoc-include

After installation, make sure that the pandoc-include executable is put in the directory which is in the PATH environment.

To install the current (development) version hosted on the repository, use

pip install --upgrade --force --no-cache git+https://github.com/DCsunset/pandoc-include

You can use

pip show pandoc-include

to check the version currently installed.

Usage

Command

To use this filter, add to pandoc command

pandoc input.md --filter pandoc-include -o output.pdf

Syntax

Each include statement has its own line and has the syntax:

!include somefolder/somefile

!include-header file.yaml

Or

$include somefolder/somefile

$include-header file.yaml

Each include statement must be in its own paragraph. That is, in its own line and separated by blank lines.

For code include, use !include statement in a code block:

```cpp
!include filename.cpp
```

The path can be either absolute or relative to the current file's directory. Besides, unix-style pathname can used. (If the include statement is used in an included file, then the path is absolute or relative to the included file itself.)

If there are special characters in the filename, use quotes:

!include "filename with space"
!include 'filename"with"quotes'

The second syntax may lead to wrong highlighting when using a markdown editor. If it happens, use the first syntax. Also make sure that there are no circular includes.

The !include command also supports options:

!include`<key1>=<value1>, <key2>=<value2>` some_file

For example, to specify line ranges in options:

!include`startLine=1, endLine=10` some_file

Or to include snippets with enclosed delimiters:

!include`snippetStart="<!-- Start -->", snippetEnd="<!-- End -->"` some_file

where <!-- Start --> and <!-- End --> are two strings occuring in some_file.

If multiple occurences of <!-- Start --> or <!-- End --> are in some_file, then pandoc-include will include all the blocks between the delimiters. If snippetEnd or snippetStart is not found or specified, it will include till the end or from the start.

Supported options:

Key Value Description
startLine int Start line of include (default: 1)
endLine int End line of include (default: number of the last line)
snippetStart str Start delimiter of a snippet
snippetEnd str End delimiter of a snippet
includeSnippetDelimiters bool Whether to include the delimiters (default: False)
incrementSection int Increment (or decrement) section levels of include
dedent int Remove n leading whitespaces of each line where possible (-1 means remove all)
format str The input format of the included file (see pandoc --list-input-formats). It will be automatically deduced from the path if not set
raw str Include as raw block. The arg is the format (latex, html...)

Note: the values above are parsed as Python literals. So str should be quoted like 'xxx' or "xxx"; bool should be True or False.

Header options

---
include-entry: '<path>'
include-order: 'natural'
rewrite-path: true
pandoc-options:
  - --filter=pandoc-include
  - <other options>
---

include-entry

The include-entry option is to make recursive includes work. Its value is a path relative to current working directory or absolute where the entry file (the initial file) locates. It should be placed in the entry file only, not in the included files. It is optional and the default include-entry value is ..

For example, to compile a file in current directory, no header is needed:

pandoc test.md --filter pandoc-include -o test.pdf

However, to compile a file not in current directory, like:

pandoc dir/test.md --filter pandoc-include -o test.pdf

The header should now be set to: include-entry: 'dir'.

include-order

The include-order options is to define the order of included files if the unix-style pathname matches multiple files. The default value is natural, which means using the natural order. Other possible values are alphabetical and default. The default means to keep the order returned by the Python glob module.

rewrite-path

The rewrite-path option is a boolean value to configure whether the relative paths of images should be rewritten to paths relative to the root file. The default value is true.

For example, consider the following directory structure:

main.md
content/
  chapter01.md
  image.png

Suppose chapter01.md uses the image image.png. It should use ![Image](image.png) if rewrite-path is true, or ![Image](content/image.png) if rewrite-path is false.

pandoc-options

The pandoc-options option is a list to specify the pandoc options when recursively processing included files. By default, the included file will inherit the pandoc-options from its parent file, unless specified in its own file.

To make the recursive includes work, --filter=pandoc-include is necessary. The default value of pandoc-options is:

pandoc-options:
  - --filter=pandoc-include

Examples

File include

File include can be used to separate chapters into different files, or include some latex files:

---
title: Article
author: Author
toc: true
---

!include chapters/chap01.md

!include chapters/chap02.md

!include chapters/chap03.md

!include`raw="latex"` data/table.tex

Header include

For header include, it is useful to define a header template and include it in many files.

For example, in the header.yaml, we can define basic info:

name: xxx
school: yyy
email: zzz

In the main.md, we can extend the header:

---
title: Title
---

!include-header header.yaml

# Section

Body

The main.md then is equivalent to the follow markdown:

---
title: Title
name: xxx
school: yyy
email: zzz
---

# Section

Body

Trouble Shooting

The pandoc command-line options are processed in order. If you want some options to be applied in included files, make sure the --filter pandoc-include option is specified before those options.

For example, use bibliography in the included files:

pandoc main.md --filter pandoc-include --citeproc --bibliography=ref.bib -o main.pdf

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pandoc-include-1.2.0.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

pandoc_include-1.2.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file pandoc-include-1.2.0.tar.gz.

File metadata

  • Download URL: pandoc-include-1.2.0.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for pandoc-include-1.2.0.tar.gz
Algorithm Hash digest
SHA256 21d9d3f052fa0740bb302246e03537f15b5717f89a45ea983c2dd782f45fd906
MD5 950abf9b704e6dbf3a5a95b4e7cb4352
BLAKE2b-256 b6b193cf75fad755a90a1fccc7cfb233331f68e9fa7766e3c124c3a7b460f596

See more details on using hashes here.

File details

Details for the file pandoc_include-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: pandoc_include-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for pandoc_include-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c04a843ed73f08fbf4c1fb647b1a4e8f4d92449d3ad7e2a1cdadb3dd26580c85
MD5 263925a838ff3557df05e1cf0c09ee30
BLAKE2b-256 c129e66ef14bfecb5aa6cb56ee9710b20c4abf6ae25d8f7fd7238eceeb161abf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page