def test_chapter_name(self):
        ch_path = TEST_DATA_PATH / 'chapter.md'
        ch_name = 'Chapter name'
        chapter = get_meta_for_chapter(ch_path, ch_name)

        self.assertEqual(chapter.name, ch_name)
        chapter_no_name = get_meta_for_chapter(ch_path)
        self.assertEqual(chapter_no_name.name, str(ch_path))
    def test_chapter(self):
        ch_path = TEST_DATA_PATH / 'chapter.md'
        ch_name = 'Chapter name'
        chapter = get_meta_for_chapter(ch_path, ch_name)
        expected_main_section_data = {
            'field1': 'value1',
            'field2': ['li1', 'li2'],
            'field3': True
        }
        expected_child1_data = {'field1': 'val1'}
        expected_child2_data = {
            'field1': {
                'key1': 'val1',
                'key2': 'val2'
            },
            'field2': ['li1', 'li2', 'li3']
        }

        self.assertEqual(chapter.filename, str(ch_path))
        main_section = chapter.main_section
        self.assertEqual(main_section.data, expected_main_section_data)
        self.assertEqual(len(main_section.children), 1)
        child1 = main_section.children[0]
        self.assertEqual(child1.data, expected_child1_data)
        self.assertEqual(len(child1.children), 1)
        child2 = child1.children[0]
        self.assertEqual(child2.data, expected_child2_data)
        self.assertEqual(len(child2.children), 0)
    def test_chapter_only_meta_tag(self):
        ch_path = TEST_DATA_PATH / 'chapter_only_meta_tag.md'
        expected_data = {'field1': 'value1', 'field2': True}

        chapter = get_meta_for_chapter(ch_path)
        main_section = chapter.main_section
        self.assertEqual(main_section.data, expected_data)
        self.assertEqual(len(main_section.children), 0)
    def process_template_tag(self, block) -> str:
        """
        Function for processing tag. Send the contents to the corresponging
        template engine along with parameters from tag and config, and
        <content_file> path. Replace the tag with output from the engine.
        """
        tag_options = Options(
            self.get_options(block.group('options')),
            validators={'engine': validate_in(self.engines.keys())})
        options = CombinedOptions({
            'config': self.options,
            'tag': tag_options
        },
                                  priority='tag')

        tag = block.group('tag')
        if tag == 'template':  # if "template" tag is used — engine must be specified
            if 'engine' not in options:
                self._warning(
                    'Engine must be specified in the <template> tag. Skipping.',
                    self.get_tag_context(block))
                return block.group(0)
            engine = self.engines[options['engine']]
        else:
            engine = self.engines[tag]

        current_pos = block.start()
        chapter = get_meta_for_chapter(self.current_filepath)
        section = chapter.get_section_by_offset(current_pos)
        _foliant_vars = {
            'meta': section.data,
            'meta_object': self.meta,
            'config': self.config,
            'target': self.context['target'],
            'backend': self.context['backend'],
            'project_path': self.context['project_path']
        }

        context = {}

        # external context is loaded first, it has lowest priority
        if 'ext_context' in options:
            context.update(
                self.load_external_context(options['ext_context'], options))

        # all unrecognized params are redirected to template engine params
        context.update(
            {p: options[p]
             for p in options if p not in self.tag_params})

        # add options from "context" param
        context.update(options.get('context', {}))

        template = engine(block.group('body'), context,
                          options.get('engine_params', {}),
                          self.current_filepath, _foliant_vars)
        return template.build()
 def test_empty_chapter(self):
     ch_path = TEST_DATA_PATH / 'empty_chapter.md'
     chapter = get_meta_for_chapter(ch_path)
     main_section = chapter.main_section
     self.assertEqual(main_section.data, {})
     self.assertEqual(len(main_section.children), 0)
 def test_main_section_title(self):
     ch_path = TEST_DATA_PATH / 'chapter.md'
     chapter = get_meta_for_chapter(ch_path)
     main_section = chapter.main_section
     self.assertEqual(main_section.title, 'First heading')
 def test_nonexisting_file(self):
     path = Path('some_nonexistant_path.exe')
     self.assertFalse(path.exists())
     self.assertIsNone(get_meta_for_chapter(path))