Skip to main content

Generate HTML or XML from YAML or JSON

Project description

Empugn – Generate HTML or XML from YAML or JSON

More an experiment than a serious attempt at making a HTML template engine. But if you're going to use it, you may find it works well together with Emrichen.

An Empugn template looks like this:

html:
  - head:
      - title: Empugn example
      - link:
          rel: stylesheet
          href: foo.css
      - script: |
          alert('Hello, World');
  - body:
      - h1: Sole text child may be specified as-is
      - p:
          - "Separating links by "
          - a:
              href: https://google.com
              target: _blank
              children: space
          - " may require quoting"

Or with Emrichen:

!Defaults
title: Empugn example with Emrichen
greeting: Hello, Emrichen
---
html:
  - head:
      - title: !Var title
      - link:
          rel: stylesheet
          href: foo.css
      - script: !Format "alert({greeting!r});"
  - body:
      - h1: !Var title

Installation

Python 3.5+ required. Python 2 is not and will not be supported.

Optional dependencies:

  • YAML support requires PyYAML
  • Emrichen support requires… well… Emrichen

The recommended way to instal Empugn is

pip install empugn[emrichen]

Plain empugn gives you JSON support only. For Empugn with YAML support but no Emrichen, use empugn[yaml].

CLI

usage: empugn [-h] [--template-format {yaml,json}] [--output-file OUTPUT_FILE]
              [--indent INDENT] [--use-emrichen] [--var-file VAR_FILE]
              [--define VAR=VALUE] [--include-env]
              [template_file]

Generate HTML or XML from YAML or JSON

positional arguments:
  template_file         The YAML or JSON template to process. If unspecified,
                        the template is read from stdin.

optional arguments:
  -h, --help            show this help message and exit
  --template-format {yaml,json}
                        Template format. If unspecified, attempted to be
                        autodetected from the input filename (but defaults to
                        YAML).
  --output-file OUTPUT_FILE, -o OUTPUT_FILE
                        Output file. If unspecified, the HTML/XML output is
                        written into stdout.
  --indent INDENT, -i INDENT
                        Indent output. If integer, this many spaces will be
                        used. If string, will be used literally. To disable
                        indentation, pass in the empty string.
  --use-emrichen, -r    Use Emrichen to process tags, making Empugn a full
                        featured HTML template engine. Emrichen must be
                        installed.
  --var-file VAR_FILE, -f VAR_FILE
                        (Implies --use-emrichen) A YAML file containing an
                        object whose top-level keys will be defined as
                        variables. May be specified multiple times.
  --define VAR=VALUE, -D VAR=VALUE
                        (Implies --use-emrichen) Defines a single variable.
                        May be specified multiple times.
  --include-env, -e     (Implies --use-emrichen) Expose process environment
                        variables to the template.

Python API

Simple usage:

from empugn import empugn

# In goes parsed JSON/YAML, out comes string
empugn({'html': []})  # -> '<html></html>'

Usage with Emrichen:

from empugn import empugn
from emrichen import Context, Template

template_yaml = '…'
template = Template.parse(template)  # or Template(…)
context = Context({'var': 'value'})
data, = Template.enrich(context)
empugn(data)

Why is it called Empugn?

The name Empugn is a play on Emrichen, Pug and impugn.

License

The MIT License (MIT)

Copyright © 2018–2019 Santtu Pajukanta
Copyright © 2018–2019 Aarni Koskela

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

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

empugn-0.1.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

empugn-0.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file empugn-0.1.0.tar.gz.

File metadata

  • Download URL: empugn-0.1.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.19.1 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.0

File hashes

Hashes for empugn-0.1.0.tar.gz
Algorithm Hash digest
SHA256 91b50ebd989819248892ca5a33e45fe3b066ed94a3d4e7f186a121e7babb55d7
MD5 c4ac99edf73aa6190bcb4ad8a986fa1f
BLAKE2b-256 c26a756c6b06f859d2e19d7eb47f67663f77512cea75f727c760f3338a3b5b21

See more details on using hashes here.

File details

Details for the file empugn-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: empugn-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.19.1 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.0

File hashes

Hashes for empugn-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 80dea33ebc929ae9f299aa4a9330776da87d4631750174795b7b35c01404427e
MD5 41fd820244d1d9a8dda9513779070db5
BLAKE2b-256 d6180263257b8b0156894dbd9b768db0871458162bdf6c027a121d0dafd30468

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