moban icon indicating copy to clipboard operation
moban copied to clipboard

Creating jinja2 templates from example files which have static values

Open jayvdb opened this issue 7 years ago • 6 comments

An idea I had was wanted to have some templates which are jinja-ified versions of example files.

The best way to explain it is with a real case.

https://github.com/ogrisel/python-appveyor-demo is an example repo that I have helped build.

While I could possibly moban-ise it, I am not sure the main maintainer would agree, as that repo is supposed to look easy, and moban looks complicated ;-)

I can use the new copy: to obtain the two files in https://github.com/ogrisel/python-appveyor-demo/tree/master/appveyor

However I want a jinja-ified version of https://github.com/ogrisel/python-appveyor-demo/blob/master/appveyor.yml

I could manually build the jinja version, but I would prefer the template to be automatically maintained whenever the source file is updated.

Essentially, I need to go backwards. file -> template

One way to achieve this is for files to be generated from jinja2 global functions, ala https://github.com/moremoban/moban/issues/23

jayvdb avatar May 29 '18 16:05 jayvdb

I have done some similar thing: write change log in yml and get them into changelog.rst. You can check if it is related to your thinking.

Reference:

https://github.com/moremoban/moban/blob/master/mobanfile#L11 https://github.com/moremoban/pypi-mobans/blob/master/templates/CHANGELOG.rst.jj2

chfw avatar May 29 '18 16:05 chfw

https://github.com/moremoban/pypi-mobans/pull/21#discussion_r191616121 is more what I mean --- sometimes an upstream example file, or a upstream template, has some literal text which needs to be removed or replaced. This is a copy+fix type problem, and I'd love to use moban for that.

jayvdb avatar May 30 '18 00:05 jayvdb

this is reverse the direction of moban. What moban does is take a template and a data dict and produce templated output. now, the reverse is: take a templated file and apply some "magic" then produce the template, which can be used in forward direction.

I wondered how to generalize the "magic". When I did CHANGELOG.rst -> changelog.yml + changelog.rst.jj2, the "magic" is specific to CHANGELOG.rst in my style.

chfw avatar May 30 '18 07:05 chfw

Only magic I can think of is allowing a list of regex transforms for each target. While it would be more common to use with copy , regex could also be used after templating as quick fixes to temporarily workaround limitations of any upstream templates.

jayvdb avatar May 30 '18 09:05 jayvdb

This could be done as a template engine, which looks for configuration values and replaces them with the configuration keys.

jayvdb avatar Jun 07 '18 14:06 jayvdb

Sort of the complement of https://github.com/econandrew/untemplate/blob/master/README.md

jayvdb avatar Jun 08 '18 01:06 jayvdb