pandoc-include


Namepandoc-include JSON
Version 1.4.1 PyPI version JSON
download
home_pageNone
SummaryPandoc filter to allow file and header includes
upload_time2024-11-11 15:22:43
maintainerNone
docs_urlNone
authorNone
requires_python>=3.7
licenseMIT
keywords pandoc pandocfilters markdown latex
VCS
bugtrack_url
requirements panflute natsort lxml
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # pandoc-include

[![PyPI](https://img.shields.io/pypi/v/pandoc-include)](https://pypi.org/project/pandoc-include/)
[![PyPI - Downloads](https://img.shields.io/pypi/dm/pandoc-include)](https://pypi.org/project/pandoc-include/)
[![GitHub](https://img.shields.io/github/license/DCsunset/pandoc-include?color=blue)](https://github.com/DCsunset/pandoc-include)

Pandoc filter to allow including files and headers in a document.

## 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.

## TODO

- [ ] Write options to a tmp file and pass the filename by environment variable

## Installation

### Using pip

To install the latest published version:

```
pip install --user pandoc-include
```


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

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

To check the version currently installed:

```
pip show pandoc-include
```

### Using Nix

`pandoc-include` is included in the Nixpkgs.
Simply add the package to your NixOS config or use the following command:

```sh
# install in your profile
nix-env -iA nixpkgs.pandoc-include

# Or use it temporarily in a shell
nix-shell -p pandoc-include
```


## Usage

**Note**: you should use `pandoc` with version greater than or equal to `2.17`,
which is the minimum version that is tested.

### 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:

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

The path can be either absolute or relative to the **current** file's directory.
Besides, [unix-style](https://en.wikipedia.org/wiki/Glob_(programming)) 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'
```

If the filename contains special sequences in markdown, use backquotes:
(**Note**: this also applies to include statment in code blocks)

```
!include `__filename__`
```


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
```

Or including `xml` files transforming them with `XSLT`:
```
!include`format="markdown", xslt="xslt/api.xslt"` main_8h.xml
```

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. (Hint: extensions can also be enabled here) |
| raw | `str` | Include as raw block. The arg is the format (`latex`, `html`...) |
| xslt | `str` | `XSLT` file to use for transforming the given `.xml` file (This is intended to be used with Doxygen's `xml` output |

**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'
include-resources: '<path>[:<path>]'
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](https://en.wikipedia.org/wiki/Natural_sort_order).
Other possible values are `alphabetical` and `default`.
The `default` means to keep the order returned by the Python `glob` module.


#### `include-resources`
The `include-resources` can be used to simplify relative paths of include statements by searching in the given paths for files with relative paths when otherwise not found.

Given following directory structure and `pandoc` command:

```
main.md
examples/
    hello-world.c
content/
    chapter1/
        chapter01.md
        image.png
    chapter2/
        chapter02.md
        image.png
```
```
$ pandoc --metadata include-resources=examples/ ... main.md
```

This will make it possible to have following include line in `chapter01.md` and `chapter02.md`
````markdown
```cpp
!include hello-world.c
```
````

Instead of:
````markdown
```cpp
!include ../../examples/hello-world.c
```
````

This is most useful if some resources, e.g. source code or Doxygen output,
is located in an external directory structure.


#### `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:

```markdown

---
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:

```yaml
name: xxx
school: yyy
email: zzz
```

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

```markdown

---
title: Title
---

!include-header header.yaml

# Section

Body

```

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

```markdown

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

# Section

Body

```


### Environment Variables

The following environment variables can be set to change the default behaviour:

| Key                            | Value      | Description                                  |
|--------------------------------|------------|----------------------------------------------|
| PANDOC_INCLUDE_NOT_FOUND_ERROR | `0` or `1` | Emit an error if file not found (default: 0) |


## Development

To setup local dev environment, you can use python `venv` to create a virtual environment from `requirements.txt`.

Or if you use Nix, you can simply run `nix develop`.


## Contributing

Contributions are welcome if you find any bugs or want to add some features.
Please open an issue for discussion first if it's a big change (e.g. a new feature) or if you are uncertain about it.

Please follow [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/)
for your commit messages so that the changelog can be generated automatically.


## Trouble Shooting

### Command-line options

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
```

### Executable not found

For some operating systems, the path may not be set correctly after installation.
Make sure that the `pandoc-include` executable is put in the directory which is in **the PATH environment**.

## License

MIT License

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pandoc-include",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": "pandoc, pandocfilters, markdown, latex",
    "author": null,
    "author_email": "DCsunset <DCsunset@protonmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/05/be/341f65df20be276fd82fccb0662513d09365aa3e75a353eb7ca44d4105e1/pandoc_include-1.4.1.tar.gz",
    "platform": null,
    "description": "# pandoc-include\n\n[![PyPI](https://img.shields.io/pypi/v/pandoc-include)](https://pypi.org/project/pandoc-include/)\n[![PyPI - Downloads](https://img.shields.io/pypi/dm/pandoc-include)](https://pypi.org/project/pandoc-include/)\n[![GitHub](https://img.shields.io/github/license/DCsunset/pandoc-include?color=blue)](https://github.com/DCsunset/pandoc-include)\n\nPandoc filter to allow including files and headers in a document.\n\n## Features\n\n* Include as raw blocks\n* Indent and dedent included contents\n* Partial include: Allow including only parts of the file using options\n* Code include: Allow using `!include` in code blocks\n* Unix style pathname\n* Recursive include: It depends on `include-entry` header to work\n* Yaml header Merging:\nWhen an included file has its header, it will be merged into the current header.\nIf there's a conflict, the original header of the current file remains.\n* Header include: Use `!include-header file.yaml` to include Yaml header from file.\n\n## TODO\n\n- [ ] Write options to a tmp file and pass the filename by environment variable\n\n## Installation\n\n### Using pip\n\nTo install the latest published version:\n\n```\npip install --user pandoc-include\n```\n\n\nTo install the current (development) version hosted on the repository, use\n\n```\npip install --upgrade --force --no-cache git+https://github.com/DCsunset/pandoc-include\n```\n\nTo check the version currently installed:\n\n```\npip show pandoc-include\n```\n\n### Using Nix\n\n`pandoc-include` is included in the Nixpkgs.\nSimply add the package to your NixOS config or use the following command:\n\n```sh\n# install in your profile\nnix-env -iA nixpkgs.pandoc-include\n\n# Or use it temporarily in a shell\nnix-shell -p pandoc-include\n```\n\n\n## Usage\n\n**Note**: you should use `pandoc` with version greater than or equal to `2.17`,\nwhich is the minimum version that is tested.\n\n### Command\n\nTo use this filter, add to pandoc command\n\n```\npandoc input.md --filter pandoc-include -o output.pdf\n```\n\n### Syntax\n\nEach include statement has its own line and has the syntax:\n\n```\n!include somefolder/somefile\n\n!include-header file.yaml\n```\n\nOr\n\n```\n$include somefolder/somefile\n\n$include-header file.yaml\n```\n\nEach include statement must be in its own paragraph. That is, in its own line\nand separated by blank lines.\n\n\nFor code include, use `!include` statement in a code block:\n\n````markdown\n```cpp\n!include filename.cpp\n```\n````\n\nThe path can be either absolute or relative to the **current** file's directory.\nBesides, [unix-style](https://en.wikipedia.org/wiki/Glob_(programming)) pathname can used.\n(If the include statement is used in an included file,\nthen the path is absolute or relative to the included file itself.)\n\nIf there are special characters in the filename, use quotes:\n\n```\n!include \"filename with space\"\n!include 'filename\"with\"quotes'\n```\n\nIf the filename contains special sequences in markdown, use backquotes:\n(**Note**: this also applies to include statment in code blocks)\n\n```\n!include `__filename__`\n```\n\n\nThe second syntax may lead to wrong highlighting when using a markdown editor.\nIf it happens, use the first syntax.\nAlso make sure that there are no circular includes.\n\n\nThe `!include` command also supports options:\n\n```\n!include`<key1>=<value1>, <key2>=<value2>` some_file\n```\n\nFor example, to specify line ranges in options:\n\n```\n!include`startLine=1, endLine=10` some_file\n```\n\nOr to include snippets with enclosed delimiters:\n\n```\n!include`snippetStart=\"<!-- Start -->\", snippetEnd=\"<!-- End -->\"` some_file\n```\n\nOr including `xml` files transforming them with `XSLT`:\n```\n!include`format=\"markdown\", xslt=\"xslt/api.xslt\"` main_8h.xml\n```\n\nwhere `<!-- Start -->` and `<!-- End -->` are two strings occuring in `some_file`.\n\nIf multiple occurences of `<!-- Start -->` or `<!-- End -->` are in `some_file`, then pandoc-include will include all the blocks between the delimiters.\nIf `snippetEnd` or `snippetStart` is not found or specified, it will include till the end or from the start.\n\nSupported options:\n\n| Key | Value | Description |\n| --- | ----- | ----------- |\n| startLine | `int` | Start line of include (default: 1) |\n| endLine | `int` | End line of include (default: number of the last line) |\n| snippetStart | `str` | Start delimiter of a snippet |\n| snippetEnd | `str` | End delimiter of a snippet |\n| includeSnippetDelimiters | `bool` | Whether to include the delimiters (default: `False`) |\n| incrementSection | `int` | Increment (or decrement) section levels of include |\n| dedent | `int` | Remove n leading whitespaces of each line where possible (`-1` means remove all) |\n| 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. (Hint: extensions can also be enabled here) |\n| raw | `str` | Include as raw block. The arg is the format (`latex`, `html`...) |\n| xslt | `str` | `XSLT` file to use for transforming the given `.xml` file (This is intended to be used with Doxygen's `xml` output |\n\n**Note**: the values above are parsed as Python literals. So `str` should be quoted like `'xxx'` or `\"xxx\"`; `bool` should be `True` or `False`.\n\n\n### Header options\n\n```\n---\ninclude-entry: '<path>'\ninclude-order: 'natural'\ninclude-resources: '<path>[:<path>]'\nrewrite-path: true\npandoc-options:\n  - --filter=pandoc-include\n  - <other options>\n---\n```\n\n#### `include-entry`\n\nThe `include-entry` option is to make recursive includes work.\nIts value is a path relative to current working directory or absolute\nwhere the entry file (the initial file) locates.\nIt should be placed in the entry file only, not in the included files.\nIt is optional and the default `include-entry` value is `.`.\n\nFor example, to compile a file in current directory, no header is needed:\n\n```\npandoc test.md --filter pandoc-include -o test.pdf\n```\n\nHowever, to compile a file not in current directory, like:\n\n```\npandoc dir/test.md --filter pandoc-include -o test.pdf\n```\n\nThe header should now be set to: `include-entry: 'dir'`.\n\n\n#### `include-order`\n\nThe `include-order` options is to define the order of included files if the unix-style pathname matches multiple files.\nThe default value is `natural`, which means using the [natural order](https://en.wikipedia.org/wiki/Natural_sort_order).\nOther possible values are `alphabetical` and `default`.\nThe `default` means to keep the order returned by the Python `glob` module.\n\n\n#### `include-resources`\nThe `include-resources` can be used to simplify relative paths of include statements by searching in the given paths for files with relative paths when otherwise not found.\n\nGiven following directory structure and `pandoc` command:\n\n```\nmain.md\nexamples/\n    hello-world.c\ncontent/\n    chapter1/\n        chapter01.md\n        image.png\n    chapter2/\n        chapter02.md\n        image.png\n```\n```\n$ pandoc --metadata include-resources=examples/ ... main.md\n```\n\nThis will make it possible to have following include line in `chapter01.md` and `chapter02.md`\n````markdown\n```cpp\n!include hello-world.c\n```\n````\n\nInstead of:\n````markdown\n```cpp\n!include ../../examples/hello-world.c\n```\n````\n\nThis is most useful if some resources, e.g. source code or Doxygen output,\nis located in an external directory structure.\n\n\n#### `rewrite-path`\n\nThe `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.\nThe default value is `true`.\n\nFor example, consider the following directory structure:\n\n```\nmain.md\ncontent/\n  chapter01.md\n  image.png\n```\n\nSuppose `chapter01.md` uses the image `image.png`.\nIt should use `![Image](image.png)` if `rewrite-path` is `true`,\nor `![Image](content/image.png)` if `rewrite-path` is `false`.\n\n#### `pandoc-options`\n\nThe `pandoc-options` option is **a list** to specify the pandoc options when recursively processing included files.\nBy default, the included file will **inherit** the `pandoc-options` from its parent file, **unless** specified in its own file.\n\nTo make the recursive includes work, `--filter=pandoc-include` is **necessary**.\nThe default value of `pandoc-options` is:\n\n```\npandoc-options:\n  - --filter=pandoc-include\n```\n\n\n## Examples\n\n### File include\n\nFile include can be used to separate chapters into different files,\nor include some latex files:\n\n```markdown\n\n---\ntitle: Article\nauthor: Author\ntoc: true\n---\n\n!include chapters/chap01.md\n\n!include chapters/chap02.md\n\n!include chapters/chap03.md\n\n!include`raw=\"latex\"` data/table.tex\n\n```\n\n### Header include\n\nFor header include, it is useful to define a header template\nand include it in many files.\n\nFor example, in the `header.yaml`, we can define basic info:\n\n```yaml\nname: xxx\nschool: yyy\nemail: zzz\n```\n\nIn the `main.md`, we can extend the header:\n\n```markdown\n\n---\ntitle: Title\n---\n\n!include-header header.yaml\n\n# Section\n\nBody\n\n```\n\nThe `main.md` then is equivalent to the follow markdown:\n\n```markdown\n\n---\ntitle: Title\nname: xxx\nschool: yyy\nemail: zzz\n---\n\n# Section\n\nBody\n\n```\n\n\n### Environment Variables\n\nThe following environment variables can be set to change the default behaviour:\n\n| Key                            | Value      | Description                                  |\n|--------------------------------|------------|----------------------------------------------|\n| PANDOC_INCLUDE_NOT_FOUND_ERROR | `0` or `1` | Emit an error if file not found (default: 0) |\n\n\n## Development\n\nTo setup local dev environment, you can use python `venv` to create a virtual environment from `requirements.txt`.\n\nOr if you use Nix, you can simply run `nix develop`.\n\n\n## Contributing\n\nContributions are welcome if you find any bugs or want to add some features.\nPlease open an issue for discussion first if it's a big change (e.g. a new feature) or if you are uncertain about it.\n\nPlease follow [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/)\nfor your commit messages so that the changelog can be generated automatically.\n\n\n## Trouble Shooting\n\n### Command-line options\n\nThe pandoc command-line options are processed in order.\nIf you want some options to be applied in included files,\nmake sure the `--filter pandoc-include` option is specified before those options.\n\nFor example, use bibliography in the included files:\n\n```\npandoc main.md --filter pandoc-include --citeproc --bibliography=ref.bib -o main.pdf\n```\n\n### Executable not found\n\nFor some operating systems, the path may not be set correctly after installation.\nMake sure that the `pandoc-include` executable is put in the directory which is in **the PATH environment**.\n\n## License\n\nMIT License\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Pandoc filter to allow file and header includes",
    "version": "1.4.1",
    "project_urls": {
        "homepage": "https://github.com/DCsunset/pandoc-include"
    },
    "split_keywords": [
        "pandoc",
        " pandocfilters",
        " markdown",
        " latex"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7ef1ecc50c98dcece5a23db43246ad5ec9806c9e09c418520ebb45f3b788ee25",
                "md5": "88cd22ee10101d8d4f647fbd92a78d14",
                "sha256": "5e43ed5c8887efc4b092aebe33e07e5c1418ae7aedb037e073a3f9099fef5903"
            },
            "downloads": -1,
            "filename": "pandoc_include-1.4.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "88cd22ee10101d8d4f647fbd92a78d14",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 12808,
            "upload_time": "2024-11-11T15:22:41",
            "upload_time_iso_8601": "2024-11-11T15:22:41.778303Z",
            "url": "https://files.pythonhosted.org/packages/7e/f1/ecc50c98dcece5a23db43246ad5ec9806c9e09c418520ebb45f3b788ee25/pandoc_include-1.4.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "05be341f65df20be276fd82fccb0662513d09365aa3e75a353eb7ca44d4105e1",
                "md5": "cf1bdfec31f5bd34445fd69b6bcf0acc",
                "sha256": "7eec973bb6ded74d2663a2f863416fef38b79b2486eb7a812b3f907b9223c020"
            },
            "downloads": -1,
            "filename": "pandoc_include-1.4.1.tar.gz",
            "has_sig": false,
            "md5_digest": "cf1bdfec31f5bd34445fd69b6bcf0acc",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 15658,
            "upload_time": "2024-11-11T15:22:43",
            "upload_time_iso_8601": "2024-11-11T15:22:43.416767Z",
            "url": "https://files.pythonhosted.org/packages/05/be/341f65df20be276fd82fccb0662513d09365aa3e75a353eb7ca44d4105e1/pandoc_include-1.4.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-11 15:22:43",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "DCsunset",
    "github_project": "pandoc-include",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "panflute",
            "specs": []
        },
        {
            "name": "natsort",
            "specs": []
        },
        {
            "name": "lxml",
            "specs": []
        }
    ],
    "lcname": "pandoc-include"
}
        
Elapsed time: 2.48832s