Exemple #1
0
    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)
Exemple #2
0
    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)
Exemple #3
0
    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)
Exemple #4
0
    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)
Exemple #5
0
    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)
Exemple #6
0
    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)
Exemple #7
0
    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)
Exemple #8
0
    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)
Exemple #9
0
    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)
Exemple #10
0
    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)
Exemple #11
0
    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)
Exemple #12
0
    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)
Exemple #13
0
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()
Exemple #14
0
 def testVariables_addVariables(self):
     builder = ConfigBuilder().add_variables({
         'var1': 'val1',
         'var2': 'val2',
     })
     self.assertDictEqual({'var1': 'val1', 'var2': 'val2'}, builder.build().variables)
Exemple #15
0
    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)
Exemple #16
0
 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)