예제 #1
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)
예제 #2
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)
예제 #3
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)
예제 #4
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)