跳转到主要内容

从YAML或JSON生成HTML或XML

项目描述

Empugn – 从YAML或JSON生成HTML或XML

与其说是严肃的HTML模板引擎尝试,不如说是更多的一次实验。但如果你要使用它,你可能会发现它与Emrichen一起工作得很好。

Empugn模板看起来像这样

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"

或者与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

安装

需要Python 3.5+。Python 2不受支持,也不会受到支持。

可选依赖项

安装Empugn的推荐方法是

pip install empugn[emrichen]

empugn只提供JSON支持。对于具有YAML支持但无Emrichen的Empugn,请使用empugn[yaml]

命令行界面

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

简单用法

from empugn import empugn

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

与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)

为什么叫Empugn?

Empugn这个名字是Emrichen、Pugimpugn的谐音。

许可证

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.

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定要选择哪个,请了解更多关于安装包的信息。

源分布

empugn-0.1.0.tar.gz (6.0 kB 查看哈希值)

上传时间:

构建分布

empugn-0.1.0-py3-none-any.whl (6.7 kB 查看哈希值)

上传时间: Python 3

支持

AWSAWS 云计算和安全赞助商 DatadogDatadog 监控 FastlyFastly CDN GoogleGoogle 下载分析 MicrosoftMicrosoft PSF赞助商 PingdomPingdom 监控 SentrySentry 错误日志 StatusPageStatusPage 状态页面