2 个版本

0.1.1 2021 年 4 月 20 日
0.1.0 2021 年 4 月 18 日

#339 in 模板引擎

Apache-2.0

23KB
425 代码行

Fillout

Crates.io Changelog

Fillout 通过在模板中填充占位符来生成文件。

我制作了 fillout,因为我需要生成包含一个 区域文件 和一个调查问卷文件的临时 DNS 测试。然而,其设计并没有特别针对区域文件或调查问卷。

功能

  • 简单的模板语法。占位符用双大括号标记。模板语法旨在与您的平均群邮件软件相媲美。
  • 验证。如果您的模板中包含的数据中不存在占位符,则会报告错误,并且不会写入文件。
  • 集成。数据文件从 STDIN 读取,以简化从动态生成数据生成文件的流程。

安装

安装稳定版 Rust 和 Cargo:

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

使用 Cargo 编译和安装 fillout

$ cargo install fillout

用法

为了演示目的,创建一些示例数据

$ mkdir templates
$ echo 'Lorem {{alpha}} {{beta}} sit.' > templates/first.txt.tmpl
$ echo 'Duis {{gamma}} irure {{beta}}.' > templates/second.txt.tmpl
$ echo 'alpha, ipsum' > input.data
$ echo 'beta, dolor' >> input.data
$ echo 'gamma, aute' >> input.data

在输出目录中生成一组文件

$ mkdir output
$ fillout -o output templates/* < input.data
$ ls -l output/
total 8K
-rw------- 1 mattias mattias 23 apr 18 12:35 first.txt
-rw------- 1 mattias mattias 23 apr 18 12:35 second.txt
$ cat output/first.txt
Lorem ipsum dolor sit.
$ cat output/second.txt
Duis aute irure dolor.

现在让我们尝试一下验证功能。添加另一个带有拼写错误的占位符的模板,并尝试生成一组新文件

$ echo 'Dolor magna eget est lorem {{ahpla}}.' > templates/third.txt.tmpl
$ fillout -o output templates/* < input.data
Error: Failed to validate data file against template-files ["/tmp/templates/first.txt.tmpl", "/tmp/templates/second.txt.tmpl", "/tmp/templates/third.txt.tmpl"]

Caused by:
    Placeholders used in templates are not defined in data-file: ["ahpla"]
$ ls -l output/
total 8K
-rw------- 1 mattias mattias 23 apr 18 12:35 first.txt
-rw------- 1 mattias mattias 23 apr 18 12:35 second.txt

请注意,现在没有创建新文件,现有的文件保持不变(如它们的最后修改时间和文件大小所示)。

最后,让我们看一下 fillout 的自身用法文档

$ fillout --help
fillout 0.1.1
Fill out placeholders in templates

Reads a data file and a set of template-files and writes a
set of output-files corresponding to the templates files.

Placeholders in the template-files are replaced with values
from the data file in the output-files. Each placeholder is
marked up in the template-files by its name in double
braces. E.g. {{ recipient }}. Whitespace surrounding the
name is optional. A single placeholder name may optionally
recur multiple times in multiple in multiple template-files.
If so, they are replaced with the same value each time.

The data file is a headerless CSV file, read from STDIN.
Each record must have two fields - a placeholder-name and a
value. The delimiter is an ASCII comma. Leading and trailing
whitespace are trimmed from both placeholder-names and
values. Records are terminated with CR, LF or CRLF. Fields
may be quoted with ASCII double quote characters. If you
need to use an ASCII double quote you can escape it by
doubling it. If a record starts with a hash character, this
line is ignored.

USAGE:
    fillout [OPTIONS] [TEMPLATE-FILE]...

FLAGS:
    -h, --help       
            Prints help information

    -V, --version    
            Prints version information


OPTIONS:
    -o, --output-dir <DIR>
            The directory where output-files are written -
            if none is given each output-file is written to
            the directory of its corresponding template-file

ARGS:
    <TEMPLATE-FILE>...
            A template-file - must have the .tmpl file
            extension

联系方式

要提问、报告错误或建议功能,请使用 问题跟踪器

依赖项

~5MB
~71K SLoC