diff --git a/src/dragonglass/dragonglass.py b/src/dragonglass/dragonglass.py index 190fabd..c1a6b3a 100644 --- a/src/dragonglass/dragonglass.py +++ b/src/dragonglass/dragonglass.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import argparse +import logging import shutil from pathlib import Path @@ -9,31 +10,41 @@ from .mparse import create_markdown_parser from .template import create_template_environment from .tree import SourceIndex, generate_list +logger = logging.getLogger(__name__) + # The command line parser parser = argparse.ArgumentParser(prog='dragonglass') parser.add_argument('source_dir', help='Source directory (Obsidian vault) for the conversion.') parser.add_argument('dest_dir', help='Destination directory for the conversion.') parser.add_argument('-C', '--config', help='Specifies an alternate name for the configuration file.') +parser.add_argument('-D', '--debug', action='store_true', help='Enables debug logging') +parser.add_argument('--dump', action='store_true', help='Dumps the parsed data before writing the destination') def main() -> int: args = parser.parse_args() - context = Context() + if args.debug: + logging.basicConfig(level=logging.DEBUG) + else: + logging.basicConfig(level=logging.INFO) + + context = Context() context.source_dir = Path(args.source_dir).resolve() if not (context.source_dir.exists() and context.source_dir.is_dir()): - print(f"{context.source_dir} is not a valid directory") + logger.error(f"{context.source_dir} is not a valid directory") return 1 dest_dir = Path(args.dest_dir).resolve() if dest_dir.exists() and not dest_dir.is_dir(): - print(f"{dest_dir} exists but is not a valid directory") + logger.error(f"{dest_dir} exists but is not a valid directory") return 1 context.load_config(args) nodes = generate_list(context.source_dir) for node in nodes: + logger.info(f"Loading metadata for {node}") context.current_node = node node.load_metadata(context.source_dir) @@ -42,17 +53,19 @@ def main() -> int: mdparse = create_markdown_parser(context) for node in nodes: context.current_node = node + logger.info(f"Parsing {node}") node.parse_markdown(context.source_dir, mdparse) # TEMP - for node in nodes: - print(node) - if node.metadata: - print(f"Metadata: {node.metadata}") - if node.text: - print("----- BEGIN TEXT -----") - print(node.text) - print("------ END TEXT ------") + if args.dump: + for node in nodes: + print(node) + if node.metadata: + print(f"Metadata: {node.metadata}") + if node.text: + print("----- BEGIN TEXT -----") + print(node.text) + print("------ END TEXT ------") if not dest_dir.exists(): dest_dir.mkdir() diff --git a/src/dragonglass/mparse.py b/src/dragonglass/mparse.py index 63da1d7..b01b0cd 100644 --- a/src/dragonglass/mparse.py +++ b/src/dragonglass/mparse.py @@ -165,7 +165,9 @@ class ObsidianImages(Extension): assert self._context.src_index is not None node, _ = self._context.src_index.lookup(name) if node: - return node.link_target(self._context.url_prefix) + return node.link_target(self._context.url_prefix, + self._context.current_node if self._context.relative_links else None, + root_path=self._context.source_dir) return None class ObsidianImageProc(InlineProcessor): @@ -261,7 +263,9 @@ class ObsidianLinks(Extension): if not text: text = contents if node: - return node.link_target(self._context.url_prefix), text + return node.link_target(self._context.url_prefix, + self._context.current_node if self._context.relative_links else None, + root_path=self._context.source_dir), text return None, text class ObsidianLinksProc(InlineProcessor): diff --git a/src/dragonglass/tree.py b/src/dragonglass/tree.py index b5b0508..8a9d656 100644 --- a/src/dragonglass/tree.py +++ b/src/dragonglass/tree.py @@ -4,9 +4,12 @@ from pathlib import Path from typing import Any from urllib.parse import quote as urlquote +import logging import markdown import yaml +logger = logging.getLogger(__name__) + # The paths that are always to be ignored. STATIC_IGNORE = [ '.obsidian', @@ -46,10 +49,13 @@ class SourceNode: p = p.with_suffix('.html') return dest_dir / p - def link_target(self, prefix: str = "/", rel_to: Any = None) -> str: + def link_target(self, prefix: str = "/", rel_to: Any = None, root_path: Path = None) -> str: xpath = self._path.with_suffix('.html') if self._is_md else self._path if rel_to is not None: - return urlquote(xpath.relative_to(rel_to._path, walk_up=True).as_posix()) + xpath = root_path / xpath + rel_path = root_path / rel_to.path + logger.debug(f"*** Computing path of {xpath} relative to {rel_path}") + return urlquote(xpath.relative_to(rel_path.parent, walk_up=True).as_posix()) return urlquote(prefix + xpath.as_posix()) def load_metadata(self, source_dir: Path) -> None: