diff --git a/.moban.cd/changelog.yml b/.moban.cd/changelog.yml index 392eed67..aca0eb33 100644 --- a/.moban.cd/changelog.yml +++ b/.moban.cd/changelog.yml @@ -5,6 +5,10 @@ releases: - action: Added details: - "global variables to store the target and template file names in the jinja2 engine" + - "moban-handlebars is tested to work well with this version and above" + - action: Updated + details: + - Template engine interface has been clarified and documented date: 14-11-2018 version: 0.3.4 - changes: diff --git a/.moban.cd/moban.yml b/.moban.cd/moban.yml index 061a144a..e2e1d7f3 100644 --- a/.moban.cd/moban.yml +++ b/.moban.cd/moban.yml @@ -17,8 +17,7 @@ keywords: dependencies: - pyyaml>=3.11 - jinja2>=2.7.1 - - lml==0.0.4 + - lml>=0.0.7 - crayons - - pybars3 description: Yet another jinja2 cli command for static text generation scm_host: github.com diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f189ae90..d6977c04 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -9,6 +9,12 @@ Added #. global variables to store the target and template file names in the jinja2 engine +#. moban-handlebars is tested to work well with this version and above + +Updated +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +#. Template engine interface has been clarified and documented 0.3.3 - 05-11-2018 -------------------------------------------------------------------------------- diff --git a/docs/README.rst b/docs/README.rst index 37ccfaf4..d3cafab8 100644 --- a/docs/README.rst +++ b/docs/README.rst @@ -13,6 +13,7 @@ This section covers the use cases for moban. Please check them out individually. #. `Pass a folder full of templates`_ #. `Use pypi package as a moban dependency`_ #. `Use git repository as a moban dependency`_ +#. `Use handlebars template with moban`_ .. _Jinja2 command line: level-1-jinja2-cli .. _Template inheritance: level-2-template-inheritance @@ -24,3 +25,4 @@ This section covers the use cases for moban. Please check them out individually. .. _Pass a folder full of templates: level-8-pass-a-folder-full-of-templates .. _Use pypi package as a moban dependency: level-9-moban-dependency-as-pypi-package .. _Use git repository as a moban dependency: level-10-moban-dependency-as-git-repo +.. _Use handlebars template with moban: level-11-use-handlebars diff --git a/docs/conf.py b/docs/conf.py index d4a30bcb..b4ffa141 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -42,12 +42,7 @@ # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. -extensions = [ - 'sphinx.ext.autodoc', - 'sphinx.ext.doctest', - 'sphinx.ext.intersphinx', - 'sphinx.ext.viewcode', -] +extensions = [ 'sphinx.ext.autodoc', 'sphinx.ext.doctest', 'sphinx.ext.intersphinx', 'sphinx.ext.viewcode',] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] @@ -74,7 +69,7 @@ exclude_patterns = [] # The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'sphinx' +pygments_style = None # -- Options for HTML output ------------------------------------------------- @@ -162,6 +157,24 @@ 'Miscellaneous'), ] + +# -- Options for Epub output ------------------------------------------------- + +# Bibliographic Dublin Core info. +epub_title = project + +# The unique identifier of the text. This can be a ISBN number +# or the project homepage. +# +# epub_identifier = '' + +# A unique identification for the text. +# +# epub_uid = '' + +# A list of files that should not be packed into the epub file. +epub_exclude_files = ['search.html'] + # -- Extension configuration ------------------------------------------------- # -- Options for intersphinx extension --------------------------------------- diff --git a/docs/index.rst b/docs/index.rst index a2dc5109..6668ac83 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -25,6 +25,7 @@ examples folder. level-8-pass-a-folder-full-of-templates/README.rst level-9-moban-dependency-as-pypi-package/README.rst level-10-moban-dependency-as-git-repo/README.rst + level-11-use-handlebars/README.rst In pratice, the following use cases were found interesting to go along with. diff --git a/docs/level-11-use-handlebars/.moban.cd/data.base.yaml b/docs/level-11-use-handlebars/.moban.cd/data.base.yaml new file mode 100644 index 00000000..2c16d47a --- /dev/null +++ b/docs/level-11-use-handlebars/.moban.cd/data.base.yaml @@ -0,0 +1,2 @@ +nihao: shijie +hello: shijie \ No newline at end of file diff --git a/docs/level-11-use-handlebars/.moban.td/base.hbs b/docs/level-11-use-handlebars/.moban.td/base.hbs new file mode 100644 index 00000000..ded3b3f7 --- /dev/null +++ b/docs/level-11-use-handlebars/.moban.td/base.hbs @@ -0,0 +1,5 @@ + +{{hello}} + +{{nihao}} + diff --git a/docs/level-11-use-handlebars/.moban.yml b/docs/level-11-use-handlebars/.moban.yml new file mode 100644 index 00000000..62af65fe --- /dev/null +++ b/docs/level-11-use-handlebars/.moban.yml @@ -0,0 +1,6 @@ +requires: + - moban-handlebars + +targets: + - a.output: a.template.handlebars + - b.output: base.hbs diff --git a/docs/level-11-use-handlebars/README.rst b/docs/level-11-use-handlebars/README.rst new file mode 100644 index 00000000..80c1f6b7 --- /dev/null +++ b/docs/level-11-use-handlebars/README.rst @@ -0,0 +1,31 @@ +Level 11: use handlebars +================================================================================ + +moban is extensible via lml. Charlie Liu through Google Code-in 2018 has +kindly contributed moban-handlebars plugin. + + +Evaluation +-------------------------------------------------------------------------------- + +Please go to `docs/level-11-use-handlebars` directory. + + +Here is the `.moban.yml`, which replaces `jj2` with handlebars files in level 4:: + + requires: + - moban-handlebars + + targets: + - a.output: a.template.handlebars + - b.output: base.hbs + + +where `targets` should lead an array of dictionaries, `requires` installs +moban-handlebars extension. You can provide file suffixes: ".handlebars" +or ".hbs" to your handlebars template. + +Here is how to launch it +.. code-block:: bash + + moban diff --git a/docs/level-11-use-handlebars/a.template.handlebars b/docs/level-11-use-handlebars/a.template.handlebars new file mode 100644 index 00000000..2cc9b416 --- /dev/null +++ b/docs/level-11-use-handlebars/a.template.handlebars @@ -0,0 +1 @@ +{{no-inheritance}} diff --git a/docs/level-11-use-handlebars/b.output b/docs/level-11-use-handlebars/b.output new file mode 100644 index 00000000..e91bcf2b --- /dev/null +++ b/docs/level-11-use-handlebars/b.output @@ -0,0 +1,4 @@ + +world + +shijie diff --git a/docs/level-11-use-handlebars/data.yml b/docs/level-11-use-handlebars/data.yml new file mode 100644 index 00000000..dc8c957d --- /dev/null +++ b/docs/level-11-use-handlebars/data.yml @@ -0,0 +1,3 @@ +overrides: data.base.yaml +hello: world +no-inheritance: handlebars does not support inheritance \ No newline at end of file diff --git a/docs/level-4-single-command/README.rst b/docs/level-4-single-command/README.rst index 718290a0..5ad698f6 100644 --- a/docs/level-4-single-command/README.rst +++ b/docs/level-4-single-command/README.rst @@ -12,7 +12,7 @@ Evaluation Please go to `docs/level-4-single-command` directory. -Here is the `.moban.yml`, whihc replaces the command in level 3:: +Here is the `.moban.yml`, which replaces the command in level 3:: targets: - a.output: a.template diff --git a/moban/engine_handlebars.py b/moban/engine_handlebars.py deleted file mode 100644 index 8a190ae2..00000000 --- a/moban/engine_handlebars.py +++ /dev/null @@ -1,26 +0,0 @@ -import codecs - -from lml.plugin import PluginInfo - -import moban.utils as utils -import moban.constants as constants -from pybars import Compiler - - -@PluginInfo(constants.TEMPLATE_ENGINE_EXTENSION, tags=["handlebars", "hbs"]) -class EngineHandlebars(object): - def __init__(self, template_dirs): - self.template_dirs = template_dirs - - def get_template(self, template_file): - actual_file = utils.get_template_path( - self.template_dirs, template_file - ) - with codecs.open(actual_file, "r", encoding="utf-8") as source: - hbr_template = Compiler().compile(source.read()) - return hbr_template - - def apply_template(self, template, data, _): - rendered_content = "".join(template(data)) - rendered_content = rendered_content - return rendered_content diff --git a/moban/plugins.py b/moban/plugins.py index f8a230d0..544dc2a4 100644 --- a/moban/plugins.py +++ b/moban/plugins.py @@ -8,7 +8,7 @@ from moban.strategy import Strategy from moban.hashstore import HASH_STORE -BUILTIN_EXENSIONS = ["moban.jinja2.engine", "moban.engine_handlebars"] +BUILTIN_EXENSIONS = ["moban.jinja2.engine"] class LibraryManager(PluginManager): @@ -24,7 +24,6 @@ class BaseEngine(object): def __init__(self, template_dirs, context_dirs, engine_cls): # pypi-moban-pkg cannot be found if removed make_sure_all_pkg_are_loaded() - template_dirs = list(expand_template_directories(template_dirs)) verify_the_existence_of_directories(template_dirs) context_dirs = expand_template_directory(context_dirs) @@ -62,11 +61,15 @@ def apply_template(self, template_abs_path, template, data, output_file): rendered_content = self.engine.apply_template( template, data, output_file ) + rendered_content = utils.strip_off_trailing_new_lines(rendered_content) + rendered_content = rendered_content.encode("utf-8") flag = HASH_STORE.is_file_changed( - output_file, rendered_content.encode("utf-8"), template_abs_path + output_file, rendered_content, template_abs_path ) if flag: - utils.write_file_out(output_file, rendered_content) + utils.write_file_out( + output_file, rendered_content, strip=False, encode=False + ) utils.file_permissions_copy(template_abs_path, output_file) return flag diff --git a/requirements.txt b/requirements.txt index 0a7e7b8d..cc7ce754 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ pyyaml>=3.11 jinja2>=2.7.1 -lml==0.0.4 +lml>=0.0.7 crayons -pybars3 diff --git a/rnd_requirements.txt b/rnd_requirements.txt new file mode 100644 index 00000000..8ead802a --- /dev/null +++ b/rnd_requirements.txt @@ -0,0 +1 @@ +https://github.com/moremoban/moban-handlebars/archive/master.zip diff --git a/setup.py b/setup.py index 0d5b00dc..8aad5959 100644 --- a/setup.py +++ b/setup.py @@ -28,9 +28,9 @@ DOWNLOAD_URL = '%s/archive/0.3.3.tar.gz' % URL FILES = ['README.rst', 'CONTRIBUTORS.rst', 'CHANGELOG.rst'] KEYWORDS = [ + 'python', 'jinja2', 'moban', - 'python' ] CLASSIFIERS = [ @@ -48,9 +48,8 @@ INSTALL_REQUIRES = [ 'pyyaml>=3.11', 'jinja2>=2.7.1', - 'lml==0.0.4', + 'lml>=0.0.7', 'crayons', - 'pybars3', ] SETUP_COMMANDS = {} diff --git a/tests/fixtures/handlebars_tests/file_tests.json b/tests/fixtures/handlebars_tests/file_tests.json deleted file mode 100644 index 39e8d602..00000000 --- a/tests/fixtures/handlebars_tests/file_tests.json +++ /dev/null @@ -1 +0,0 @@ -{"test": "here"} \ No newline at end of file diff --git a/tests/fixtures/handlebars_tests/file_tests.template b/tests/fixtures/handlebars_tests/file_tests.template deleted file mode 100644 index c905f153..00000000 --- a/tests/fixtures/handlebars_tests/file_tests.template +++ /dev/null @@ -1 +0,0 @@ -{{test}} \ No newline at end of file diff --git a/tests/test_docs.py b/tests/test_docs.py index 795b3793..08fd7aeb 100644 --- a/tests/test_docs.py +++ b/tests/test_docs.py @@ -100,6 +100,11 @@ def test_level_10(self): folder = "level-10-moban-dependency-as-git-repo" self._raw_moban(["moban"], folder, expected, "test.txt") + def test_level_11(self): + expected = "handlebars does not support inheritance\n" + folder = "level-11-use-handlebars" + self._raw_moban(["moban"], folder, expected, "a.output") + def test_misc_1(self): expected = "test file\n" diff --git a/tests/test_engine.py b/tests/test_engine.py index 03590c95..dc47d4f5 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -12,7 +12,6 @@ expand_template_directories, ) from moban.jinja2.engine import Engine -from moban.engine_handlebars import EngineHandlebars @PluginInfo("library", tags=["testmobans"]) @@ -42,11 +41,6 @@ def test_default_template_type(): assert engine.engine_cls == Engine -def test_handlebars_template_type(): - engine = ENGINES.get_engine("hbs", [], "") - assert engine.engine_cls == EngineHandlebars - - def test_default_mako_type(): # fake mako engine = ENGINES.get_engine("mako", [], "") assert engine.engine_cls.__name__ == "MakoEngine" @@ -83,17 +77,6 @@ def test_file_tests(): os.unlink(output) -def test_handlebars_file_tests(): - output = "test.txt" - path = os.path.join("tests", "fixtures", "handlebars_tests") - engine = BaseEngine([path], path, EngineHandlebars) - engine.render_to_file("file_tests.template", "file_tests.json", output) - with open(output, "r") as output_file: - content = output_file.read() - eq_(content, "here") - os.unlink(output) - - def test_global_template_variables(): output = "test.txt" path = os.path.join("tests", "fixtures", "globals") diff --git a/tests/test_handlebar_engine.py b/tests/test_handlebar_engine.py deleted file mode 100644 index 57249fd0..00000000 --- a/tests/test_handlebar_engine.py +++ /dev/null @@ -1,14 +0,0 @@ -import os - -from nose.tools import eq_ -from moban.jinja2.engine import Engine - - -def test_handlebars_template_not_found(): - path = os.path.join("tests", "fixtures", "jinja_tests") - engine = Engine([path]) - template = engine.get_template("file_tests.template") - data = dict(test="here") - result = engine.apply_template(template, data, None) - expected = "yes\nhere" - eq_(expected, result) diff --git a/tests/test_jinja2_engine.py b/tests/test_jinja2_engine.py index fa72dbe7..57249fd0 100644 --- a/tests/test_jinja2_engine.py +++ b/tests/test_jinja2_engine.py @@ -1,14 +1,14 @@ import os from nose.tools import eq_ -from moban.engine_handlebars import EngineHandlebars +from moban.jinja2.engine import Engine def test_handlebars_template_not_found(): - path = os.path.join("tests", "fixtures", "handlebars_tests") - engine = EngineHandlebars([path]) + path = os.path.join("tests", "fixtures", "jinja_tests") + engine = Engine([path]) template = engine.get_template("file_tests.template") data = dict(test="here") result = engine.apply_template(template, data, None) - expected = "here" + expected = "yes\nhere" eq_(expected, result)