Skip to content

Commit

Permalink
add: convert chapters to list
Browse files Browse the repository at this point in the history
  • Loading branch information
TOsmanov committed Nov 18, 2024
1 parent f3d9d8c commit 457b9c1
Showing 1 changed file with 37 additions and 15 deletions.
52 changes: 37 additions & 15 deletions foliant/preprocessors/includes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 457b9c1

Please sign in to comment.