From 4f5e97caf4cae86cfe3fcfa0329c63554c095c12 Mon Sep 17 00:00:00 2001 From: chfw Date: Mon, 1 Jun 2020 21:22:23 +0100 Subject: [PATCH] :books: update readme about custom template engine. #60 --- .moban.d/moban_readme.jj2 | 36 ++++++++++++++++++ README.rst | 38 ++++++++++++++++++- .../README.rst | 2 +- 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/.moban.d/moban_readme.jj2 b/.moban.d/moban_readme.jj2 index dbc7580c..c42fe59c 100644 --- a/.moban.d/moban_readme.jj2 +++ b/.moban.d/moban_readme.jj2 @@ -191,6 +191,42 @@ installed, {% include "handlebars_example.rst.jj2" %} +Can I write my own template engine? +-------------------------------------- + +Yes and please click `here `_ for more details. + +Given the following template type function, and saved in custom-plugin dir: + +.. code-block:: python + + from moban.core.content_processor import ContentProcessor + + + @ContentProcessor("de-duplicate", "De-duplicating", "De-duplicated") + def de_duplicate(content: str) -> str: + """ + Does no templating, works like 'copy'. + + """ + lines = content.split(b'\n') + new_lines = [] + for line in lines: + if line not in new_lines: + new_lines.append(line) + return b'\n'.join(new_lines) + + +You can start using it like this: + +.. code-block::bash + + $ moban --template-type de-duplicate -pd custom-plugin -t duplicated_content.txt + De-duplicating duplicated_content.txt to moban.output + De-duplicating 1 file. + Everything is up to date! + + TOML data format ---------------------- diff --git a/README.rst b/README.rst index eaaedfbb..5c48034d 100644 --- a/README.rst +++ b/README.rst @@ -219,6 +219,42 @@ Let's continue with a bit more fancy feature: $ moban --template-type handlebars -c data.json "{{#with person}}{{firstname}} {{lastname}} {{/with}}" +Can I write my own template engine? +-------------------------------------- + +Yes and please click `here `_ for more details. + +Given the following template type function, and saved in custom-plugin dir: + +.. code-block:: python + + from moban.core.content_processor import ContentProcessor + + + @ContentProcessor("de-duplicate", "De-duplicating", "De-duplicated") + def de_duplicate(content: str) -> str: + """ + Does no templating, works like 'copy'. + + """ + lines = content.split(b'\n') + new_lines = [] + for line in lines: + if line not in new_lines: + new_lines.append(line) + return b'\n'.join(new_lines) + + +You can start using it like this: + +.. code-block::bash + + $ moban --template-type de-duplicate -pd custom-plugin -t duplicated_content.txt + De-duplicating duplicated_content.txt to moban.output + De-duplicating 1 file. + Everything is up to date! + + TOML data format ---------------------- @@ -282,7 +318,7 @@ moban gives you a promise of any location which `python-anyconfig` does not supp **Why do it mean 'any location'?** Thanks to `pyfilesystem 2 `_, -moban allow to read data back from `git repo `_, `pypi `_ package, `http(s) `_, zip, +moban is able to read data back from `git repo `_, `pypi `_ package, `http(s) `_, zip, tar, ftp, `s3 `_ or `you name it `_. diff --git a/tests/regression_tests/level-7-b-template-engine-plugin/README.rst b/tests/regression_tests/level-7-b-template-engine-plugin/README.rst index 86c10881..37ddc516 100644 --- a/tests/regression_tests/level-7-b-template-engine-plugin/README.rst +++ b/tests/regression_tests/level-7-b-template-engine-plugin/README.rst @@ -1,4 +1,4 @@ -Level 7: Custom jinja filters, tests and globals on cli +Level 7 b: Custom template engine ================================================================================ We will test this on '-pd' cli option with custom template engine.