Commit 433cff3d authored by Jakub Ružička's avatar Jakub Ružička
Browse files

templates: new include_raw tag

Fixes: #63
parent 0fdf56b5
Pipeline #93303 passed with stages
in 21 minutes and 31 seconds
......@@ -7,6 +7,7 @@ import os
from pathlib import Path
from packaging import version
import jinja2
import jinja2.ext
from apkg import adistro
from apkg.log import getLogger
......@@ -42,6 +43,25 @@ DEFAULT_PLAIN_COPY_FILES = [
]
class IncludeRawExtension(jinja2.ext.Extension):
"""
include raw file contents without templating
Usage from template: {% include_raw distro/pkg/foo %}
"""
tags = {"include_raw"}
def parse(self, parser):
lineno = parser.stream.expect("name:include_raw").lineno
fn = jinja2.nodes.Const(parser.parse_expression().value)
result = self.call_method("_render", [fn], lineno=lineno)
return jinja2.nodes.Output([result], lineno=lineno)
def _render(self, filename):
src = self.environment.loader.get_source(self.environment, filename)[0]
return jinja2.Markup(src.rstrip('\n'))
# pylint: disable=too-many-instance-attributes
class PackageTemplate:
def __init__(self, path, style=None, selection=TS_DISTRO,
......@@ -64,7 +84,8 @@ class PackageTemplate:
def setup_env(self):
self.env = jinja2.Environment(
loader=jinja2.FileSystemLoader('.'))
loader=jinja2.FileSystemLoader('.'),
extensions=[IncludeRawExtension])
@property
def path(self):
......
......@@ -56,19 +56,36 @@ available:
* `now`: current date and time in changelog format (provided only by pkgstyles
with changelog dates such as `deb` and `rpm`)
[include] tag is also supported and can be used to include
files relative to project root.
Example Debian `changelog`:
```jinja
{{ 'examples/templates/distro/pkg/deb/changelog' | file_raw }}
```
See [distro in templates] to learn howto use the magical `distro` object in
See [distro in templates] to learn how to use the magical `distro` object in
templates.
### reusing code in templates
Jinja [include] tag can be used to include file content **with** templating:
```jinja
{% include 'distro/common/foo.txt' %}
```
Custom `include_raw` tag is also provided to include file content **without** templating:
```jinja
{% include_raw 'distro/common/foo.txt' %}
```
This is especially useful when including files which might contain jinja control
sequences but no templating is needed in them.
Both `include` and `include_raw` expect file path relative to project root.
## template selection
`apkg` selects package template (from `distro/pkg/`) to use based on [target
......
Included from shared.txt file. Name: {{ name }}
from shared.txt: {{ name }}
......@@ -2,7 +2,8 @@ apkg-example-templates ({{ version }}-{{ release }}~{{ distro.tiny }}) unstable;
* new upstream version {{ version }}
* distro: {{ distro }} / {{ distro.idver }} / {{ distro.tiny }}
* {% include 'distro/common/shared.txt' %}
* include: {% include 'distro/common/shared.txt' %}
* raw include: {% include_raw 'distro/common/shared.txt' %}
{%- if distro.match('debian >= 11', 'ubuntu > 20.04') %}
* new Debian-based
{%- elif distro.match('deb-old') %}
......
......@@ -21,7 +21,8 @@ This is a minimal example of apkg templating features.
* {{ now }} Jakub Ružička <jakub.ruzicka@nic.cz> - {{ version }}-{{ release }}
- new upstream version {{ version }}
- distro: {{ distro }} / {{ distro.idver }} / {{ distro.tiny }}
- {% include 'distro/common/shared.txt' %}
- include: {% include 'distro/common/shared.txt' %}
- raw include: {% include_raw 'distro/common/shared.txt' %}
{%- if distro.match('fedora') %}
- Fedora-specific block
{%- elif distro.match('centos <= 7', 'rhel <= 7') %}
......
......@@ -54,7 +54,8 @@ def test_template_select(proj, distro, name, style):
RE_RPM_CH = r"""\* \w{3} \w{3} \d{2} \d{4} [^-]+- ([^-]+)-(\w+)
- new upstream version (\S+)
- distro: ([^\n]+)
- Included from shared.txt file. Name: apkg-example-templates\n?(.*)\Z"""
- include: from shared.txt: apkg-example-templates
- raw include: from shared.txt: {{ name }}\n?(.*)\Z"""
@pytest.mark.parametrize('distro,extra', [
......@@ -95,7 +96,8 @@ def test_template_render_rpm(proj, capsys, distro, extra):
RE_DEB_CH = r"""apkg-example-templates \(([^-]+)-([^)]+)\) unstable; .*
\* new upstream version (\S+)
\* distro: ([^\n]+)
\* Included from shared.txt file. Name: apkg-example-templates\n?(.*)
\* include: from shared.txt: apkg-example-templates
\* raw include: from shared.txt: {{ name }}\n?(.*)
-- .+? \w{3}, \d+ \w{3} \d{4} [\d:]{8} [+-]?\d{4}"""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment