def testRecursiveEvaluateJinjaExpressions(self): builder = ConfigBuilder() # Default should be False. self.assertFalse(builder.build().recursively_evaluate_jinja_expressions) builder.set_recursively_evaluate_jinja_expressions(True) self.assertTrue(builder.build().recursively_evaluate_jinja_expressions)
def testTemplateDirs(self): with mock.patch('os.path.isdir', lambda s: True): builder = ConfigBuilder().add_template_dirs('template/path1', 'template/path2') self.assertSequenceEqual( ['template/path1', 'template/path2'], builder.build().template_dirs) builder.clear_template_dirs() self.assertSequenceEqual([], builder.build().template_dirs)
def testCompilerRules(self): rule1, rule2 = Rule(), Rule() builder = ConfigBuilder().append_compiler_rules(rule1, rule2) self.assertSequenceEqual([rule1, rule2], builder.build().compiler_rules) rule3, rule4 = Rule(), Rule() builder.prepend_compiler_rules(rule3, rule4) self.assertSequenceEqual([rule3, rule4, rule1, rule2], builder.build().compiler_rules) builder.clear_compiler_rules() self.assertSequenceEqual([], builder.build().compiler_rules)
def testPostprocessRules(self): rule1, rule2 = Rule(), Rule() builder = ConfigBuilder().append_postprocess_rules(rule1, rule2) self.assertSequenceEqual([rule1, rule2], builder.build().postprocess_rules) rule3, rule4 = Rule(), Rule() builder.prepend_postprocess_rules(rule3, rule4) self.assertSequenceEqual([rule3, rule4, rule1, rule2], builder.build().postprocess_rules) builder.clear_postprocess_rules() self.assertSequenceEqual([], builder.build().postprocess_rules)
def testOnlyTemplate_success(self): template_loader = jinja2.DictLoader({ 'some/path': self.join_lines( '<title>{{ title }}</title>', '<ul>', '{% for user in users -%}', '<li><a href="{{ user.url }}">{{ user.username }}</a></li>', '{% endfor -%}', '</ul>') }) jinja_env = jinja2.Environment(loader=template_loader) class User(object): def __init__(self, url, username): self.url = url self.username = username # Build Templar config. config_builder = ConfigBuilder() config_builder.add_variable('title', 'Test') config_builder.add_variable('users', [User('url1', 'user1'), User('url2', 'user2')]) config = config_builder.build() result = publish(config, template='some/path', jinja_env=jinja_env, no_write=True) self.assertEqual( self.join_lines( '<title>Test</title>', '<ul>', '<li><a href="url1">user1</a></li>', '<li><a href="url2">user2</a></li>', '</ul>'), result)
def testOnlySource_withRulesAndBlocks(self): file_map = { 'docA.md': self.join_lines( 'outer content', '<block blockA>', 'inner content', '</block blockA>', 'outer content'), } class AppliedRule(Rule): counter = 0 def apply(self, content): self.counter += 1 return 'segment {}: '.format(self.counter) + content config_builder = ConfigBuilder() config_builder.append_postprocess_rules(AppliedRule()) with self.mock_open(file_map): result = publish(config_builder.build(), source='docA.md', no_write=True) self.assertEquals( self.join_lines( 'segment 1: outer content', 'segment 2: inner content', 'segment 3: outer content'), result)
def testOnlySource_withRulesAndBlocks(self): file_map = { 'docA.md': self.join_lines('outer content', '<block blockA>', 'inner content', '</block blockA>', 'outer content'), } class AppliedRule(Rule): counter = 0 def apply(self, content): self.counter += 1 return 'segment {}: '.format(self.counter) + content config_builder = ConfigBuilder() config_builder.append_postprocess_rules(AppliedRule()) with self.mock_open(file_map): result = publish(config_builder.build(), source='docA.md', no_write=True) self.assertEquals( self.join_lines('segment 1: outer content', 'segment 2: inner content', 'segment 3: outer content'), result)
def testConfigIsImmutable(self): with mock.patch('os.path.isdir', lambda s: True): builder = ConfigBuilder().add_template_dirs('template/path1', 'template/path2') builder.add_variable('var1', 'val1') config = builder.build() # Verify config was constructed correctly. self.assertSequenceEqual(['template/path1', 'template/path2'], config.template_dirs) self.assertDictEqual({'var1': 'val1'}, config.variables) new_builder = config.to_builder() new_builder.clear_template_dirs() new_builder.add_variable('var2', 'val2') # Verify previously built config was not affected by changes to new_builder. self.assertSequenceEqual(['template/path1', 'template/path2'], config.template_dirs) self.assertDictEqual({'var1': 'val1'}, config.variables)
def testSourceAndTemplate(self): file_map = { 'docA.md': self.join_lines( '~ title: Test', # Creates a variable called 'title' 'outside block', '<block first>', 'inside block', '</block first>'), } template_loader = jinja2.DictLoader({ 'some/path': self.join_lines( '<title>{{ title }}</title>', # Use the variable defined in the content. '<p>', '{{ blocks.first }}', # Use the block defined in the content. '{{ var }}', # Use the variable defined by VarRule, below. '</p>') }) jinja_env = jinja2.Environment(loader=template_loader) # Test rule application. class UpperCaseRule(Rule): def apply(self, content): return content.upper() class VarRule(VariableRule): def extract(self, content): return {'var': 'val'} config_builder = ConfigBuilder() config_builder.append_preprocess_rules(UpperCaseRule(), VarRule()) with self.mock_open(file_map): result = publish( config_builder.build(), source='docA.md', template='some/path', jinja_env=jinja_env, no_write=True) self.assertEquals( self.join_lines( '<title>Test</title>', '<p>', 'INSIDE BLOCK', 'val', '</p>'), result)
def testOnlySource_withRules(self): file_map = { 'docA.md': 'original content', } class AppliedRule(Rule): def apply(self, content): return content + ' rule1' class NotAppliedRule(Rule): def apply(self, content): return content + 'rule2' config_builder = ConfigBuilder() config_builder.append_preprocess_rules( AppliedRule(src=r'\.md'), NotAppliedRule(dst=r'\.html')) config_builder.append_postprocess_rules(AppliedRule(), NotAppliedRule(src=r'\.py')) with self.mock_open(file_map): result = publish(config_builder.build(), source='docA.md', no_write=True) self.assertEquals('original content rule1 rule1', result)
def testSourceAndTemplate(self): file_map = { 'docA.md': self.join_lines( '~ title: Test', # Creates a variable called 'title' 'outside block', '<block first>', 'inside block', '</block first>'), } template_loader = jinja2.DictLoader({ 'some/path': self.join_lines( '<title>{{ title }}</title>', # Use the variable defined in the content. '<p>', '{{ blocks.first }}', # Use the block defined in the content. '{{ var }}', # Use the variable defined by VarRule, below. '</p>') }) jinja_env = jinja2.Environment(loader=template_loader) # Test rule application. class UpperCaseRule(Rule): def apply(self, content): return content.upper() class VarRule(VariableRule): def extract(self, content): return {'var': 'val'} config_builder = ConfigBuilder() config_builder.append_preprocess_rules(UpperCaseRule(), VarRule()) with self.mock_open(file_map): result = publish(config_builder.build(), source='docA.md', template='some/path', jinja_env=jinja_env, no_write=True) self.assertEquals( self.join_lines('<title>Test</title>', '<p>', 'INSIDE BLOCK', 'val', '</p>'), result)
def testOnlySource_withRules(self): file_map = { 'docA.md': 'original content', } class AppliedRule(Rule): def apply(self, content): return content + ' rule1' class NotAppliedRule(Rule): def apply(self, content): return content + 'rule2' config_builder = ConfigBuilder() config_builder.append_preprocess_rules(AppliedRule(src=r'\.md'), NotAppliedRule(dst=r'\.html')) config_builder.append_postprocess_rules(AppliedRule(), NotAppliedRule(src=r'\.py')) with self.mock_open(file_map): result = publish(config_builder.build(), source='docA.md', no_write=True) self.assertEquals('original content rule1 rule1', result)
from templar.api.config import ConfigBuilder from templar.api.rules.core import SubstitutionRule from templar.api.rules.compiler_rules import MarkdownToHtmlRule from templar.api.rules.table_of_contents import HtmlTableOfContents class ImageRule(SubstitutionRule): pattern = re.compile(r'(<img.*?)>') def substitute(self, match): return match.group(1) + ' class="img-responsive">' _config_builder = ConfigBuilder().add_template_dirs( 'templates', 'projects/templates', 'cs61a/review/templates', ).add_variables({ 'MASTER_DIR': '', 'CS61A_DIR': '/cs61a', 'REVIEW_DIR': '/cs61a/review', 'NOTES_DIR': '/cs61a/notes', 'BLOG_DIR': '/blog', 'PROJECTS_DIR': '/projects', }).append_compiler_rules(MarkdownToHtmlRule()).append_postprocess_rules( HtmlTableOfContents(), ImageRule(dst=r'\.html'), ) config = _config_builder.build()
def testVariables_addVariables(self): builder = ConfigBuilder().add_variables({ 'var1': 'val1', 'var2': 'val2', }) self.assertDictEqual({'var1': 'val1', 'var2': 'val2'}, builder.build().variables)
def testVariables_addVariable(self): builder = ConfigBuilder().add_variable('var1', 'val1').add_variable('var2', 'val2') self.assertDictEqual({'var1': 'val1', 'var2': 'val2'}, builder.build().variables) builder.clear_variables() self.assertDictEqual({}, builder.build().variables)
def testRules(self): rule1, rule2, rule3, = Rule(), Rule(), Rule() builder = ConfigBuilder().append_preprocess_rules(rule1) builder.append_compiler_rules(rule2) builder.append_postprocess_rules(rule3) self.assertSequenceEqual([rule1, rule2, rule3], builder.build().rules)