added options for dumping the default template and default stylesheet
This commit is contained in:
parent
91919dd324
commit
0d1e2cc9a0
|
@ -14,18 +14,20 @@ from pathlib import Path
|
||||||
|
|
||||||
from .config import Context
|
from .config import Context
|
||||||
from .mparse import create_markdown_parser
|
from .mparse import create_markdown_parser
|
||||||
from .style import write_default_stylesheet
|
from .style import write_default_stylesheet, print_stylesheet as do_print_stylesheet
|
||||||
from .template import create_template_environment, template_vars
|
from .template import create_template_environment, template_vars, print_default as do_print_default
|
||||||
from .tree import SourceIndex, generate_list
|
from .tree import SourceIndex, generate_list
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
# The command line parser
|
# The command line parser
|
||||||
parser = argparse.ArgumentParser(prog='dragonglass')
|
parser = argparse.ArgumentParser(prog='dragonglass')
|
||||||
parser.add_argument('source_dir', help='Source directory (Obsidian vault) for the conversion.')
|
parser.add_argument('source_dir', nargs='?', help='Source directory (Obsidian vault) for the conversion.')
|
||||||
parser.add_argument('dest_dir', help='Destination directory 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('-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('-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')
|
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)
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
context = Context()
|
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()
|
context.source_dir = Path(args.source_dir).resolve()
|
||||||
if not (context.source_dir.exists() and context.source_dir.is_dir()):
|
if not (context.source_dir.exists() and context.source_dir.is_dir()):
|
||||||
logger.error(f"{context.source_dir} is not a valid directory")
|
logger.error(f"{context.source_dir} is not a valid directory")
|
||||||
|
|
|
@ -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:
|
def write_default_stylesheet(ctxt: Context, tenv: Environment, dest_dir: Path) -> None:
|
||||||
"""
|
"""
|
||||||
Writes the default stylesheet data to the destination directory.
|
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.
|
tenv (Environment): Template engine used to render the default stylesheet data.
|
||||||
dest_dir (Path): The destination directory to write the stylesheet to.
|
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
|
to_file = dest_dir / ctxt.default_stylesheet
|
||||||
tmpl = tenv.from_string(STYLESHEET_DATA)
|
data = render_stylesheet(ctxt, tenv)
|
||||||
data = tmpl.render(vars)
|
|
||||||
with to_file.open("wt") as f:
|
with to_file.open("wt") as f:
|
||||||
f.write(data)
|
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))
|
||||||
|
|
|
@ -58,10 +58,11 @@ def _create_loader(ctxt: Context) -> BaseLoader:
|
||||||
Returns:
|
Returns:
|
||||||
BaseLoader: A loader to be passed to the template engine.
|
BaseLoader: A loader to be passed to the template engine.
|
||||||
"""
|
"""
|
||||||
return ChoiceLoader([
|
choices = []
|
||||||
FileSystemLoader(ctxt.template_dir),
|
if ctxt.source_dir:
|
||||||
FunctionLoader(lambda n: DEFAULT_TEMPLATE if n == DEFAULT_TEMPLATE_NAME else None)
|
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:
|
def create_template_environment(ctxt: Context) -> Environment:
|
||||||
|
@ -115,3 +116,8 @@ def template_vars(node: SourceNode, ctxt: Context) -> dict[str, Any]:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return tvars
|
return tvars
|
||||||
|
|
||||||
|
|
||||||
|
def print_default() -> None:
|
||||||
|
"""Prints out the default template."""
|
||||||
|
print(DEFAULT_TEMPLATE)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user