Skip to content

Commit

Permalink
✨ Support extensions for templating language
Browse files Browse the repository at this point in the history
  • Loading branch information
ayan-b committed Jan 15, 2019
1 parent d24c6ce commit 74abba6
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions moban/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
LABEL_MOBANFILE = "mobanfile"
LABEL_FORCE = "force"
LABEL_REQUIRES = "requires"
LABEL_EXTENSION = "extensions"

DEFAULT_CONFIGURATION_DIRNAME = ".moban.cd"
DEFAULT_TEMPLATE_DIRNAME = ".moban.td"
Expand Down
9 changes: 8 additions & 1 deletion moban/jinja2/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"moban.jinja2.filters.text",
"moban.jinja2.tests.files",
]
JINJA2_THIRD_PARTY_EXENSIONS = [
'jinja2.ext.do',
'jinja2.ext.loopcontrols',
]


class PluginMixin:
Expand Down Expand Up @@ -49,7 +53,7 @@ def __init__(self):
constants.TEMPLATE_ENGINE_EXTENSION, tags=["jinja2", "jinja", "jj2", "j2"]
)
class Engine(object):
def __init__(self, template_dirs):
def __init__(self, template_dirs, extensions):
"""
Contruct a jinja2 template engine
Expand All @@ -60,11 +64,14 @@ def __init__(self, template_dirs):
load_jinja2_extensions()
self.template_dirs = template_dirs
template_loader = FileSystemLoader(template_dirs)
if extensions:
JINJA2_THIRD_PARTY_EXENSIONS.extend(extensions)
self.jj2_environment = Environment(
loader=template_loader,
keep_trailing_newline=True,
trim_blocks=True,
lstrip_blocks=True,
extensions=JINJA2_THIRD_PARTY_EXENSIONS,
)
for filter_name, filter_function in FILTERS.get_all():
self.jj2_environment.filters[filter_name] = filter_function
Expand Down
4 changes: 4 additions & 0 deletions moban/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def create_parser():
parser.add_argument(
"-m", "--%s" % constants.LABEL_MOBANFILE, help="custom moban file"
)
parser.add_argument(
"-e", "--%s" % constants.LABEL_EXTENSION,
help="extensions for the templating language",
)
return parser


Expand Down
9 changes: 7 additions & 2 deletions moban/mobanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def find_default_moban_file():
def handle_moban_file_v1(moban_file_configurations, command_line_options):
merged_options = None
target = extract_target(command_line_options)
extensions = []
if constants.LABEL_EXTENSION in moban_file_configurations:
extensions = moban_file_configurations[constants.LABEL_EXTENSION]
if constants.LABEL_CONFIG in moban_file_configurations:
merged_options = merge(
command_line_options,
Expand All @@ -58,7 +61,8 @@ def handle_moban_file_v1(moban_file_configurations, command_line_options):
# if command line option exists, append its template to targets
# issue 30
targets += target
number_of_templated_files = handle_targets(merged_options, targets)
number_of_templated_files = handle_targets(merged_options, extensions,
targets)
else:
number_of_templated_files = 0

Expand Down Expand Up @@ -87,7 +91,7 @@ def handle_copy(template_dirs, copy_config):
return copier.number_of_copied_files()


def handle_targets(merged_options, targets):
def handle_targets(merged_options, extensions, targets):
list_of_templating_parameters = parse_targets(merged_options, targets)
list_of_templating_parameters = expand_directories(
list_of_templating_parameters,
Expand All @@ -110,6 +114,7 @@ def handle_targets(merged_options, targets):
template_type,
merged_options[constants.LABEL_TMPL_DIRS],
merged_options[constants.LABEL_CONFIG_DIR],
extensions,
)
engine.render_to_files(jobs_for_each_engine[template_type])
engine.report()
Expand Down
10 changes: 6 additions & 4 deletions moban/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ def resource_path_of(self, library_name):


class BaseEngine(object):
def __init__(self, template_dirs, context_dirs, engine_cls):
def __init__(self, template_dirs, context_dirs, engine_cls, extensions):
# 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)
self.context = Context(context_dirs)
self.template_dirs = template_dirs
self.engine = engine_cls(self.template_dirs)
self.extensions = extensions
self.engine = engine_cls(self.template_dirs, self.extensions)
self.engine_cls = engine_cls
self.templated_count = 0
self.file_count = 0
Expand Down Expand Up @@ -121,9 +122,10 @@ def __init__(self):
constants.TEMPLATE_ENGINE_EXTENSION
)

def get_engine(self, template_type, template_dirs, context_dirs):
def get_engine(self, template_type, template_dirs, context_dirs,
extensions):
engine_cls = self.load_me_now(template_type)
return BaseEngine(template_dirs, context_dirs, engine_cls)
return BaseEngine(template_dirs, context_dirs, engine_cls, extensions)

def all_types(self):
return list(self.registry.keys())
Expand Down

0 comments on commit 74abba6

Please sign in to comment.