fix preprocessor

This commit is contained in:
carlospolop 2025-09-29 11:33:02 +02:00
parent 666428142b
commit 3f01d2da43

View File

@ -17,7 +17,7 @@ handler2.setLevel(logging.ERROR)
logger.addHandler(handler2)
def findtitle(search ,obj, key, path=(),):
def findtitle(search, obj, key, path=()):
# logger.debug(f"Looking for {search} in {path}")
if isinstance(obj, dict) and key in obj and obj[key] == search:
return obj, path
@ -54,26 +54,42 @@ def ref(matchobj):
if href.endswith("/"):
href = href+"README.md" # Fix if ref points to a folder
if "#" in href:
chapter, _path = findtitle(href.split("#")[0], book, "source_path")
title = " ".join(href.split("#")[1].split("-")).title()
logger.debug(f'Ref has # using title: {title}')
result = findtitle(href.split("#")[0], book, "source_path")
if result is not None:
chapter, _path = result
title = " ".join(href.split("#")[1].split("-")).title()
logger.debug(f'Ref has # using title: {title}')
else:
raise Exception(f"Chapter not found for path: {href.split('#')[0]}")
else:
chapter, _path = findtitle(href, book, "source_path")
logger.debug(f'Recursive title search result: {chapter["name"]}')
title = chapter['name']
result = findtitle(href, book, "source_path")
if result is not None:
chapter, _path = result
logger.debug(f'Recursive title search result: {chapter["name"]}')
title = chapter['name']
else:
raise Exception(f"Chapter not found for path: {href}")
except Exception as e:
dir = path.dirname(current_chapter['source_path'])
rel_path = path.normpath(path.join(dir,href))
try:
logger.debug(f'Not found chapter title from: {href} -- trying with relative path {rel_path}')
if "#" in href:
chapter, _path = findtitle(path.normpath(path.join(dir,href.split('#')[0])), book, "source_path")
title = " ".join(href.split("#")[1].split("-")).title()
logger.debug(f'Ref has # using title: {title}')
result = findtitle(path.normpath(path.join(dir,href.split('#')[0])), book, "source_path")
if result is not None:
chapter, _path = result
title = " ".join(href.split("#")[1].split("-")).title()
logger.debug(f'Ref has # using title: {title}')
else:
raise Exception(f"Chapter not found for relative path: {path.normpath(path.join(dir,href.split('#')[0]))}")
else:
chapter, _path = findtitle(path.normpath(path.join(dir,href.split('#')[0])), book, "source_path")
title = chapter["name"]
logger.debug(f'Recursive title search result: {chapter["name"]}')
result = findtitle(path.normpath(path.join(dir,href)), book, "source_path")
if result is not None:
chapter, _path = result
title = chapter["name"]
logger.debug(f'Recursive title search result: {chapter["name"]}')
else:
raise Exception(f"Chapter not found for relative path: {path.normpath(path.join(dir,href))}")
except Exception as e:
logger.debug(e)
logger.error(f'Error getting chapter title: {rel_path}')