Replace custom template syntax with Jinja2 #88
Labels
No labels
bug
contribution welcome
duplicate
enhancement
good first issue
help wanted
invalid
question
upstream
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Reference
marvin8/feed2fedi#88
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
The template system has grown organically — first basic
{var}substitution, then conditional prefix/suffix syntax ({[prefix]var[suffix]}, #69), and now a fallback syntax is being requested (#87). Each addition requires extending a hand-rolled regex parser, and the direction of travel is clear: we're building a template language one issue at a time.Rather than continue patching the custom syntax, this issue proposes switching to Jinja2 as the template engine. Jinja2 is mature, well-documented, and already solves everything users have asked for — and everything they're likely to ask for next:
{{ title }}{{ description | default(title) }}{% if author %} by {{ author }}{% endif %}{{ title | truncate(100) }}This is a breaking change: existing
{var}templates will need to be updated to{{ var }}syntax. The plan is to ship this with a major version bump and provide a migration guide in the docs.Issue #87 (fallback syntax) will serve as the concrete use case to implement and validate against during development.
Findings and plan of attack
Explored the current template system in full. Summary of what changes:
Current system (to be removed)
_format_conditional_template()inpublish.py— regex parser handling{[prefix]var[suffix]}patternsstr.format(**params)fallback for standard{var}substitution\\npreprocessing hack (post_template.replace("\\\\n", "\n")) because JSON strings can't express newlines naturallybot_post_template(inline string) onConfiguration;post_template(inline string) onFeedInfoNew system
default()for issue #87's fallback use case), and anything else users might need.j2files only — no inline template strings in config. This lets users write natural multiline templates without the\\nhackbot_post_template_file(bot level),post_template_file(feed level) — paths resolved relative to the config file's locationbot_post_template,post_template) raise a clear error pointing to the migration utility{{ title }}\n\n{{ link }})Migration utility
New CLI subcommand:
feed2fedi migrate-templates <config-file>--applyto execute{var}→{{ var }},{[prefix]var[suffix]}→{% if var and var.strip() %}prefix{{ var }}suffix{% endif %},\\n→ real newlinebot_post_template.j2and{sanitized_url}_post_template.j2alongside the configCommit sequence (TDD)
migrate-templatesmigrate-templatesCLI subcommandAddressed as part of version 4.0.0... which is a breaking change! The config file has subtley changed, however there is a migration helper included (
feed2fedi-migrate-templates)