added options for dumping the default template and default stylesheet

This commit is contained in:
Amy G. Bowersox 2024-08-12 01:25:52 -06:00
parent 91919dd324
commit 0d1e2cc9a0
3 changed files with 67 additions and 17 deletions

View File

@ -14,18 +14,20 @@ from pathlib import Path
from .config import Context
from .mparse import create_markdown_parser
from .style import write_default_stylesheet
from .template import create_template_environment, template_vars
from .style import write_default_stylesheet, print_stylesheet as do_print_stylesheet
from .template import create_template_environment, template_vars, print_default as do_print_default
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('source_dir', nargs='?', help='Source directory (Obsidian vault) for the conversion.')
parser.add_argument('dest_dir', nargs='?', 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('--print-default', action='store_true', help='Prints out the default template and exits.')
parser.add_argument('--print-stylesheet', action='store_true', help='Prints out the default stylesheet and exits.')
parser.add_argument('--dump', action='store_true', help='Dumps the parsed data before writing the destination')
@ -45,6 +47,23 @@ def main() -> int:
logging.basicConfig(level=logging.INFO)
context = Context()
if args.print_default:
do_print_default()
return 0
if args.print_stylesheet:
do_print_stylesheet(context, create_template_environment(context))
return 0
if not args.source_dir:
logger.error('source directory not specified')
return 1
if not args.dest_dir:
logger.error('destination directory not specified')
return 1
context.source_dir = Path(args.source_dir).resolve()
if not (context.source_dir.exists() and context.source_dir.is_dir()):
logger.error(f"{context.source_dir} is not a valid directory")

View File

@ -230,6 +230,28 @@ span.linenos.special { color: #000000; background-color: #ffffc0; padding-left:
"""
def render_stylesheet(ctxt: Context, tenv: Environment) -> str:
"""
Renders the default stylesheet data as a string.
Args:
ctxt (Context): The context for the operation, which includes the configuration.
tenv (Environment): Template engine used to render the default stylesheet data.
Returns:
str: The default stylesheet data.
"""
vars = {}
# Fill in all the class names as variables.
for n in KNOWN_CLASSNAMES:
vname = n.replace("-", '_')
vars[vname] = ctxt.get_classname(n)
tmpl = tenv.from_string(STYLESHEET_DATA)
return tmpl.render(vars)
def write_default_stylesheet(ctxt: Context, tenv: Environment, dest_dir: Path) -> None:
"""
Writes the default stylesheet data to the destination directory.
@ -239,15 +261,18 @@ def write_default_stylesheet(ctxt: Context, tenv: Environment, dest_dir: Path) -
tenv (Environment): Template engine used to render the default stylesheet data.
dest_dir (Path): The destination directory to write the stylesheet to.
"""
vars = {}
# Fill in all the class names as variables.
for n in KNOWN_CLASSNAMES:
vname = n.replace("-", '_')
vars[vname] = ctxt.get_classname(n)
to_file = dest_dir / ctxt.default_stylesheet
tmpl = tenv.from_string(STYLESHEET_DATA)
data = tmpl.render(vars)
data = render_stylesheet(ctxt, tenv)
with to_file.open("wt") as f:
f.write(data)
def print_stylesheet(ctxt: Context, tenv: Environment) -> None:
"""
Prints out the default stylesheet.
Args:
ctxt (Context): The context for the operation, which includes the configuration.
tenv (Environment): Template engine used to render the default stylesheet data.
"""
print(render_stylesheet(ctxt, tenv))

View File

@ -58,10 +58,11 @@ def _create_loader(ctxt: Context) -> BaseLoader:
Returns:
BaseLoader: A loader to be passed to the template engine.
"""
return ChoiceLoader([
FileSystemLoader(ctxt.template_dir),
FunctionLoader(lambda n: DEFAULT_TEMPLATE if n == DEFAULT_TEMPLATE_NAME else None)
])
choices = []
if ctxt.source_dir:
choices.append(FileSystemLoader(ctxt.template_dir))
choices.append(FunctionLoader(lambda n: DEFAULT_TEMPLATE if n == DEFAULT_TEMPLATE_NAME else None))
return ChoiceLoader(choices)
def create_template_environment(ctxt: Context) -> Environment:
@ -115,3 +116,8 @@ def template_vars(node: SourceNode, ctxt: Context) -> dict[str, Any]:
pass
return tvars
def print_default() -> None:
"""Prints out the default template."""
print(DEFAULT_TEMPLATE)