Skip to main content

Markdown based html generator

Project description

Markdown based html generator.

Generation requires: HTML template and markdown template.
The generator has additional meta extensions that allow you to do metaprogramming in HTML.

Installation

pip install mdhtmlgen

Usage

$ python -m mdhtmlgen --help
Usage: python -m mdhtmlgen [options]

Options:
  -h, --help            show this help message and exit
  -S SOURCE, --source=SOURCE
                        Markdown source filename (*.md)
  -H HTML, --html=HTML  HTML template filename (*.t)
  -t, --trace           Print diagnostic traces
  -o OUTPUT, --output=OUTPUT
                        Set output file
  -m MARKDOWN_EXT, --markdown-ext=MARKDOWN_EXT
                        Set markdown extension list, coma separated, e.g.
                        meta,toc,footnotes
  -d DATE_FMT, --date-fmt=DATE_FMT
                        Set date format, e.g. %d-%m-%Y %H:%M:%S
  -e EXT, --ext=EXT     Set extension list, e.g. meta,glob,filename,date
  -a PARAMS, --add=PARAMS
                        Add parameter in format name:value
  -g GIT_DIR, --git-dir=GIT_DIR
                        Set GIT directory location, e.g. /home/user/repo/.git

Tools

We use python as the template engine for html and markdown, and its ingenious feature is string formatting using a dictionary. A simple example:

print('Dear %(name)s, I am interested in the %(post)s position at your company…' % {'name': 'Oliver', 'post': 'Sales Manager'})

Here name and post are template parameters. If you understand how this example works, then there will be no problems with the rest.

Markdown itself does not generate html. Markdown is responsible for the content, so we use an additional page template to generate the html.

Example

Let's create a simple page template - html.t:

<!DOCTYPE html>
<html>
	<head>
		<title>%(title)s</title>
		<meta charset="utf-8" />
		<link rel="stylesheet" href="styles.css" />
	</head>
	<body>%(body)s</body>
</html>

Now let's add a content template for this page - example.md:

---
title: Example Page
---

## Task scenario

You want to buy traveler’s checks with your credit card. Which percentage rate applies to the purchase?

*Possible answers:*

* The Standard APR of 10.99%
* The Cash Advance APR of 24.24%[^*]
* The Penalty APR of 29.99%
* I don’t know

[^*]: This is the correct answer, based on my own credit card company’s cardmember agreement.

The last step is to combine 2 templates into 1 html:

$ python -m mdhtmlgen -S example.md -H html.t -o example.html

The same can be done via a redirect:

$ python -m mdhtmlgen -S example.md -H html.t >example.html

Whichever is more convenient - the result will be the same.

As you can see, In HTML template was added styles.css. For our example, we can take ready styles, for example from the sindresorhus/github-markdown-css project, they cover the markdown functionality. But if it is not enough for you, you can always extend html.t, add links to additional styles and scripts.

Extensions

Without extensions, markdown is very limited. A list of basic extensions can be found on the markdown github page. Into mdhtmlgen, you can pass a list of extensions to be used during generation using the -m or --markdown-ext option.

Besides the markdown extensions, mdhtmlgen has its own meta extensions. At the moment there are 6 of them:

filename

Adds path information to the template for the source:

  • %(input-path)s - full source path
  • %(input-name)s - name of source file
  • %(input-ext)s - extension of source filename
  • %(input-basename)s - basename of source

and similar information about the output:

  • %(output-path)s
  • %(output-name)s
  • %(output-ext)s
  • %(output-basename)s

stat

Adds information about the file statistics of the source to the template:

  • %(input-date-update)s - date and time when the file was last modified
  • %(input-date-create)s - file creation date and time (if supported by FS)
  • %(input-owner)s - file owner name

git

Adds information about the statistics of a file in the Git repository to the template:

  • %(input-date-commit)s - date and time when the file was last modified
  • %(input-commiter)s - name of the last editor of the file
  • %(input-date-add)s - date and time when the file was added to the repository
  • %(input-author)s - the name of the first editor of the file

For this extension, you need to specify the path to the repository using the --git-dir option (a similar option exists in Git, see man git).

custom

Allows you to add arbitrary meta-parameters to the template using the option -a or --add

meta

Imports a meta-parameter from another source into a template, example: %(input-file-date:other-example.md)s.
Here we have extracted %(input-file-date)s from the other-example.md source

glob

Concatenates meta parameters from different sources, example: %(glob:row:*.md)s.
Here we have concatenated %(row)s from all sources using the search pattern *.md.

Native mdhtmlgen extensions can be enabled with the -e or --ext options.

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

mdhtmlgen-0.8.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

mdhtmlgen-0.8-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file mdhtmlgen-0.8.tar.gz.

File metadata

  • Download URL: mdhtmlgen-0.8.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.1.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.9

File hashes

Hashes for mdhtmlgen-0.8.tar.gz
Algorithm Hash digest
SHA256 408a97230f4ff304057ea11b8d6c4461dff199144f18179cfeec7d55be2c94d4
MD5 69e67831fc77aaf1da5c5f1840236134
BLAKE2b-256 d9bca98855d9c527b118552558d23630fa165e1d1d8ca8baea1d073ac1ff7636

See more details on using hashes here.

File details

Details for the file mdhtmlgen-0.8-py3-none-any.whl.

File metadata

  • Download URL: mdhtmlgen-0.8-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.1.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.9

File hashes

Hashes for mdhtmlgen-0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 3f6e7ad095ff216902c5697a4944f4a9489eade0d8ebb90aa700cf9d6894a1e6
MD5 27509229e3581ff688a0fcff7ca2913a
BLAKE2b-256 96617bc5a48a39add48b2d7ed7f9e386e3d6ed513840d66c0caceefd61af1518

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