Skip to main content

A customized pandoc filters set that can be used to generate a useful pandoc python filter.

Project description

PyPI - Python Version PyPI - Version DOI GitHub License

GitHub Actions Workflow Status GitHub Actions Workflow Status GitHub Actions Workflow Status codecov

pandoc-filter

This project supports some useful and highly customized pandoc python filters that based on panflute. They can meet some special requests when using pandoc to

  • convert files from markdown to gfm
  • convert files from markdown to html
  • convert other formats (In the future)

Please see Main Features for the concrete features.

Please see Samples for the recommend usage.

Backgrounds

I'm used to taking notes with markdown and clean markdown syntax. Then, I usually post these notes on my site as web pages. So, I need to convert markdown to html. There were many tools to achieve the converting and I chose pandoc at last due to its powerful features.

But sometimes, I need many more features when converting from markdown to html, where pandoc filters are needed. I have written some pandoc python filters with some advanced features by panflute and many other tools. And now, I think it's time to gather these filters into a combined toolset as this project.

Installation

pip install -i https://pypi.org/simple/ -U pandoc-filter

Main Features

There are 2 supported ways:

  • command-line-mode: use non-parametric filters in command-lines with pandoc.
  • python-mode: use run_filters_pyio function in python.

For an example, md2md_enhance_equation_filter in enhance_equation.py is a filter function as panflute-user-guide . And its registered command-line script is md2md-enhance-equation-filter.

  • So, after the installation, one can use it in command-line-mode:

    pandoc ./input.md -o ./output.md -f markdown -t gfm -s --filter md2md-enhance-equation-filter
    
  • Or, use in python mode

    import pandoc_filter
    file_path = pathlib.Path("./input.md")
    output_path = pathlib.Path("./output.md")
    pandoc_filter.run_filters_pyio(file_path,output_path,'markdown','gfm',[pandoc_filter.md2md_enhance_equation_filter])
    

Runtime status can be recorded. In python mode, any filter function will return a proposed panflute Doc. Some filter functions will add an instance attribute dict runtime_dict to the returned Doc, as a record for runtime status, which may be very useful for advanced users. For an example, md2md_enhance_equation_filter, will add an instance attribute dict runtime_dict to the returned Doc, which may contain a mapping {'math':True} if there is any math element in the Doc.

All filters with corresponding registered command-line scripts, the specific features, and the recorded runtime status are recorded as the following:

[!NOTE]

Since some filters need additional arguments, not all filter functions support command-line-mode, even though they all support python-mode indeed.

All filters support cascaded invoking.

  • pandoc_filter.filters.md2md.convert_github_style_alert_to_hexo_style_alert.run_filter
    • source
    • command-line: md2md-convert-github-style-alert-to-hexo-style-alert-filter
    • main features: Convert the github-style alert to hexo-style alert.
  • pandoc_filter.filters.md2md.enhance_equation.run_filter
    • source
    • command-line: md2md-enhance-equation-filter
    • main features: Enhance math equations.
    • Runtime status (doc.runtime_dict): {'math':< bool >,'equations_count':<some_number>}
  • pandoc_filter.filters.md2md.norm_footnote.run_filter
    • source
    • command-line: md2md-norm-footnote-filter
    • main features: Normalize the footnotes.
  • pandoc_filter.filters.md2md.norm_internal_link.run_filter
    • source
    • command-line: md2md-norm-internal-link-filter
    • main features: Normalize internal links' URLs.
  • pandoc_filter.filters.md2md.upload_figure_to_aliyun.run_filter
    • source
    • command-line: ==Unsupported.==
    • Additional Arguments: doc_path
    • main features: Auto upload local pictures to Aliyun OSS.
    • Runtime status (doc.runtime_dict): {'doc_path':<doc_path>,'oss_helper':<Oss_Helper>}
  • pandoc_filter.filters.md2html.centralize_figure.run_filter
    • source
    • command-line: md2html-centralize-figure-filter
    • main features: ==Deprecated==
  • pandoc_filter.filters.md2html.enhance_footnote.run_filter
    • source
    • command-line: md2html-enhance-footnote-filter
    • main features: Enhance the footnote.
  • pandoc_filter.filters.md2html.enhance_link_like.run_filter
    • source
    • command-line: md2html-enhance-link-like-filter
    • main features: Enhance the link-like string to a link element.
  • pandoc_filter.filters.md2html.hash_anchor_and_internal_link.run_filter
    • source
    • command-line: md2html-hash-anchor-and-internal-link-filter
    • main features: Hash both the anchor's id and the internal-link's url simultaneously.
    • Runtime status (doc.runtime_dict): {'anchor_count':<anchor_count_dict>,'internal_link_record':<internal_link_record_list>}
  • pandoc_filter.filters.md2html.increase_header_level.run_filter
    • source
    • command-line: md2html-increase-header-level-filter
    • main features: Increase the header level by 1.

Samples

Here are 2 basic types of examples

Convert markdown to markdown (Normalization)

Convert markdown to html

Contribution

Contributions are welcome. But recently, the introduction and documentation are not complete. So, please wait for a while.

A simple way to contribute is to open an issue to report bugs or request new features.

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_filter-0.2.16.tar.gz (33.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pandoc_filter-0.2.16-py3-none-any.whl (38.4 kB view details)

Uploaded Python 3

File details

Details for the file pandoc_filter-0.2.16.tar.gz.

File metadata

  • Download URL: pandoc_filter-0.2.16.tar.gz
  • Upload date:
  • Size: 33.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for pandoc_filter-0.2.16.tar.gz
Algorithm Hash digest
SHA256 f1eea9982b4f725c5da36bbbf98d808ae5d653df17a01a38e5d1b266d6ffa849
MD5 d6d2502af822decc25d5cc559e770ae1
BLAKE2b-256 a445c0e8ab7647251ec5fd0a74900a4de0ef53899e255cfe8e5b451e8a8a91f8

See more details on using hashes here.

File details

Details for the file pandoc_filter-0.2.16-py3-none-any.whl.

File metadata

  • Download URL: pandoc_filter-0.2.16-py3-none-any.whl
  • Upload date:
  • Size: 38.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for pandoc_filter-0.2.16-py3-none-any.whl
Algorithm Hash digest
SHA256 e14ed16ec56756b43f86b21f3a7294ef92296d2309c7149e626c528f1df450e5
MD5 0dbf66afbbae1d018373970b0403b29d
BLAKE2b-256 b52f6ba120bead9423745b1e32470ca56d83fe557aec423798b0ed5af10f51df

See more details on using hashes here.

Supported by

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