Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .moban.cd/moban.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ organisation: moremoban
author: C. W.
contact: wangc_2011@hotmail.com
license: MIT
version: 0.3.3
current_version: 0.3.3
version: 0.3.4
current_version: 0.3.4
release: 0.3.3
branch: master
command_line_interface: "moban"
Expand All @@ -19,5 +19,6 @@ dependencies:
- jinja2>=2.7.1
- lml==0.0.4
- crayons
- pybars3
description: Yet another jinja2 cli command for static text generation
scm_host: github.com
29 changes: 21 additions & 8 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
# The short X.Y version
version = u'0.3.3'
# The full version, including alpha/beta/rc tags
release = u'0.3.3'
release = u'0.3.4'


# -- General configuration ---------------------------------------------------
Expand All @@ -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']
Expand All @@ -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 -------------------------------------------------
Expand Down Expand Up @@ -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 ---------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion moban/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.3.3"
__version__ = "0.3.4"
__author__ = "C. W."
20 changes: 0 additions & 20 deletions moban/base_engine.py
Original file line number Diff line number Diff line change
@@ -1,20 +0,0 @@
import moban.reporter as reporter


class BaseEngine(object):
def __init__(self):
self.templated_count = 0
self.file_count = 0

def report(self):
if self.templated_count == 0:
reporter.report_no_action()
elif self.templated_count == self.file_count:
reporter.report_full_run(self.file_count)
else:
reporter.report_partial_run(
self.templated_count, self.file_count
)

def number_of_templated_files(self):
return self.templated_count
104 changes: 0 additions & 104 deletions moban/engine_factory.py

This file was deleted.

93 changes: 26 additions & 67 deletions moban/engine_handlebars.py
Original file line number Diff line number Diff line change
@@ -1,78 +1,29 @@
import os
import sys

from lml.plugin import PluginInfo

import moban.utils as utils
import moban.reporter as reporter
import moban.constants as constants
import moban.exceptions as exceptions
from lml.plugin import PluginInfo
from moban.base_engine import BaseEngine
from moban.engine_factory import (
Context,
verify_the_existence_of_directories,
Strategy,
)
from moban import plugins
from pybars import Compiler


@PluginInfo(
constants.TEMPLATE_ENGINE_EXTENSION, tags=["handlebars", "hbs"]
)
class EngineHandlebars(BaseEngine):
def __init__(self, template_dirs, context_dirs):
BaseEngine.__init__(self)
plugins.refresh_plugins()
template_dirs = list(
plugins.expand_template_directories(template_dirs))
verify_the_existence_of_directories(template_dirs)
context_dirs = plugins.expand_template_directory(context_dirs)
self.context = Context(context_dirs)
self.template_dirs = template_dirs

def find_template_file(self, template_file):
for directory in self.template_dirs:
if os.path.exists(os.path.join(directory, template_file)):
return os.path.abspath(os.path.join(directory, template_file))
raise exceptions.FileNotFound(template_file)

def render_to_files(self, array_of_param_tuple):
sta = Strategy(array_of_param_tuple)
sta.process()
choice = sta.what_to_do()
if choice == Strategy.DATA_FIRST:
self._render_with_finding_data_first(sta.data_file_index)
else:
self._render_with_finding_template_first(sta.template_file_index)

def _file_permissions_copy(self, template_file, output_file):
true_template_file = template_file
for a_template_dir in self.template_dirs:
true_template_file = os.path.join(a_template_dir, template_file)
if os.path.exists(true_template_file):
break
utils.file_permissions_copy(true_template_file, output_file)
@PluginInfo(constants.TEMPLATE_ENGINE_EXTENSION, tags=["handlebars", "hbs"])
class EngineHandlebars(plugins.BaseEngine):

def render_to_file(self, template_file, data_file, output_file):
template_file = self.find_template_file(template_file)
with open(template_file, "r") as source:
if sys.version_info[0] < 3:
template = Compiler().compile(unicode(source.read())) # noqa
else:
template = Compiler().compile(source.read())
data = self.context.get_data(data_file)
template_file, template = self._get_hbr_template(template_file)
self._apply_template(template_file, template, data, output_file)
reporter.report_templating(template_file, output_file)

rendered_content = ''.join(template(data))
utils.write_file_out(output_file, rendered_content)
self._file_permissions_copy(template_file, output_file)

def _render_with_finding_template_first(self, template_file_index):
for (template_file, data_output_pairs) in template_file_index.items():
template_file = self.find_template_file(template_file)
template_file, template = self._get_hbr_template(template_file)
for (data_file, output) in data_output_pairs:
data = self.context.get_data(data_file)
self._apply_template(template_file, data, output)
self._apply_template(template_file, template, data, output)
reporter.report_templating(template_file, output)
self.templated_count += 1
self.file_count += 1
Expand All @@ -81,23 +32,31 @@ def _render_with_finding_data_first(self, data_file_index):
for (data_file, template_output_pairs) in data_file_index.items():
data = self.context.get_data(data_file)
for (template_file, output) in template_output_pairs:
template_file = self.find_template_file(template_file)
self._apply_template(template_file, data, output)
template_file, template = self._get_hbr_template(template_file)
self._apply_template(template_file, template, data, output)
reporter.report_templating(template_file, output)
self.templated_count += 1
self.file_count += 1

def _apply_template(self, template, data, output):
template_file = self.find_template_file(template)
with open(template_file, "r") as source:
if sys.version_info[0] < 3:
template = Compiler().compile(unicode(source.read())) # noqa
else:
template = Compiler().compile(source.read())
rendered_content = ''.join(template(data))
def _apply_template(self, template_file, template, data, output):
rendered_content = "".join(template(data))
rendered_content = utils.strip_off_trailing_new_lines(rendered_content)
rendered_content = rendered_content.encode("utf-8")
utils.write_file_out(
output, rendered_content, strip=False, encode=False
)
utils.file_permissions_copy(template_file, output)

def _get_hbr_template(self, template_file):
actual_template_file = self.find_template_file(template_file)
with open(actual_template_file, "r") as source:
if sys.version_info[0] < 3:
hbr_template = Compiler().compile(
unicode(source.read()) # noqa: F821
)
else:
hbr_template = Compiler().compile(source.read())
return actual_template_file, hbr_template

def find_template_file(self, template_file):
return utils.get_template_path(self.template_dirs, template_file)
40 changes: 2 additions & 38 deletions moban/extensions.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
from lml.plugin import PluginInfo, PluginManager
from lml.plugin import PluginInfo

import moban.constants as constants


class PluginMixin:
def get_all(self):
for name in self.registry.keys():
# only the first matching one is returned
the_filter = self.load_me_now(name)
yield (name, the_filter)


class JinjaFilterManager(PluginManager, PluginMixin):
def __init__(self):
super(JinjaFilterManager, self).__init__(
constants.JINJA_FILTER_EXTENSION
)
from moban import constants


class JinjaFilter(PluginInfo):
Expand All @@ -26,11 +11,6 @@ def tags(self):
yield self.cls.__name__


class JinjaTestManager(PluginManager, PluginMixin):
def __init__(self):
super(JinjaTestManager, self).__init__(constants.JINJA_TEST_EXTENSION)


class JinjaTest(PluginInfo):
def __init__(self, test_name=None):
super(JinjaTest, self).__init__(constants.JINJA_TEST_EXTENSION)
Expand All @@ -48,22 +28,6 @@ def jinja_tests(**keywords):
JinjaTest(key)(value)


class JinjaGlobalsManager(PluginManager, PluginMixin):
def __init__(self):
super(JinjaGlobalsManager, self).__init__(
constants.JINJA_GLOBALS_EXTENSION
)


def jinja_global(identifier, dict_obj):
plugin = PluginInfo(constants.JINJA_GLOBALS_EXTENSION, tags=[identifier])
plugin(dict_obj)


class LibraryManager(PluginManager):
def __init__(self):
super(LibraryManager, self).__init__(constants.LIBRARY_EXTENSION)

def resource_path_of(self, library_name):
library = self.get_a_plugin(library_name)
return library.resources_path
File renamed without changes.
Loading