From 457b9c161bbc97d22460f9227b6621d0a545c99a Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Mon, 18 Nov 2024 10:25:47 +0300 Subject: [PATCH] add: convert chapters to list --- foliant/preprocessors/includes.py | 52 ++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/foliant/preprocessors/includes.py b/foliant/preprocessors/includes.py index 2169c74..5f4d183 100644 --- a/foliant/preprocessors/includes.py +++ b/foliant/preprocessors/includes.py @@ -56,10 +56,31 @@ def __init__(self, *args, **kwargs): if self.includes_map_enable: self.includes_map = [] + self.chapters = [] + self.chapters_list(self.config["chapters"], self.chapters) # converting chapters to a list + self.logger = self.logger.getChild('includes') self.logger.debug(f'Preprocessor inited: {self.__dict__}') + def chapters_list(self, obj, chapters: list) -> list: + '''Converting chapters to a list + :param config_chapters: Chapters from config + :param chapters: List of chapters + ''' + if isinstance(obj, list): + for item in obj: + if isinstance(item, str): + chapters.append(f"{self.src_dir}/{item}") + else: + self.chapters_list(item, chapters) + elif isinstance(obj, object): + for k, v in obj.items(): + if isinstance(v, str): + chapters.append(f"{self.src_dir}/{v}") + else: + self.chapters_list(v, chapters) + def _find_file( self, file_name: str, @@ -1292,21 +1313,22 @@ def process_includes( if self.includes_map_enable: if donor_md_path: - if not self._exist_in_includes_map(self.includes_map, recipient_md_path): - if not self.includes_map_anchors or len(donor_anchors) == 0: - self.includes_map.append({ 'file': recipient_md_path, "includes": []}) - else: - self.includes_map.append({ 'file': recipient_md_path, "includes": [], 'anchors': []}) - - for i, f in enumerate(self.includes_map): - if f['file'] == recipient_md_path: - self.includes_map[i]['includes'].append(donor_md_path) - - if self.includes_map_anchors: - for anchor in donor_anchors: - if not 'anchors' in self.includes_map[i]: - self.includes_map[i]['anchors'] = [] - self.includes_map[i]['anchors'].append(anchor) + if recipient_md_path in self.chapters: + if not self._exist_in_includes_map(self.includes_map, recipient_md_path): + if not self.includes_map_anchors or len(donor_anchors) == 0: + self.includes_map.append({ 'file': recipient_md_path, "includes": []}) + else: + self.includes_map.append({ 'file': recipient_md_path, "includes": [], 'anchors': []}) + + for i, f in enumerate(self.includes_map): + if f['file'] == recipient_md_path: + self.includes_map[i]['includes'].append(donor_md_path) + + if self.includes_map_anchors: + for anchor in donor_anchors: + if not 'anchors' in self.includes_map[i]: + self.includes_map[i]['anchors'] = [] + self.includes_map[i]['anchors'].append(anchor) else: processed_content_part = content_part