diff options
Diffstat (limited to 'src/smp/builtins.py')
-rw-r--r-- | src/smp/builtins.py | 59 |
1 files changed, 38 insertions, 21 deletions
diff --git a/src/smp/builtins.py b/src/smp/builtins.py index b44cc23..a9e1977 100644 --- a/src/smp/builtins.py +++ b/src/smp/builtins.py @@ -92,8 +92,8 @@ def smp_builtin_add_metadata(macro_processor, metadata: dict[str, Any], overwrit macro_processor.define_macro(macro_name, macro_value) -def smp_builtin_include(macro_processor, filename): - return smp_builtin_read(macro_processor, filename, template_content=None) +def smp_builtin_include_file(macro_processor, filename): + return _smp_builtin_read(macro_processor, filename, template_content=None) def smp_builtin_parse_leading_yaml(macro_processor, content): @@ -171,9 +171,7 @@ def smp_builtin_format_time(macro_processor, format, time): def smp_builtin_html_from_markdown(macro_processor, text, extensions=list()): - # Get rid of quoting, I don't remember why, but the rust implementation does it like this. - for _ in range(2): - text = macro_processor.process_input(text) + text = macro_processor.process_input(text) extensions.append(TableExtension()) extensions.append(FencedCodeExtension()) extensions.append(AutolinkExtension()) @@ -181,24 +179,30 @@ def smp_builtin_html_from_markdown(macro_processor, text, extensions=list()): return markdown.markdown(text, extensions=extensions) -def _smp_builtin_template_content(content): +def _smp_builtin_template_content(): def inner(macro_processor): - """ - This should do some kind of stack thing, so we can track which file we are processing. - entering the CONTENT is fine, the question is how to handle exiting it. + filename, content, extension = macro_processor._get_macro_with_prefix( + "template_stack_content" + )[0] + macro_processor._enter_file_frame(f"[part]{filename}", 0, "") + res = macro_processor.process_input(content, file=filename) - could have a "once" macro or something, that is added to the end of the content. - """ - return content + if extension == "md": + res = smp_builtin_html_from_markdown(macro_processor, res) + + macro_processor._pop_file_frame() + macro_processor._get_macro_with_prefix("template_stack_content").pop(0) + return res return inner def smp_builtin_template(macro_processor, template, content): - return smp_builtin_read(macro_processor, template, template_content=content) + return _smp_builtin_read(macro_processor, template, template_content=content) -def smp_builtin_read(macro_processor, filename, template_content=None): +def _smp_builtin_read(macro_processor, filename, template_content=None): + macro_processor._enter_file_frame(filename, 0, template_content) with open(filename, "r") as f: file_content = f.read() @@ -214,14 +218,27 @@ def smp_builtin_read(macro_processor, filename, template_content=None): macro_processor._get_macro_with_prefix("template_stack").append(filename) macro_processor.macros["CONTENT"] = template_content - content = macro_processor.process_input(file_content) - - if extension == "md": - content = smp_builtin_html_from_markdown(macro_processor, content) + # content = macro_processor.process_input(file_content, file=filename) + content = file_content if (template := macro_processor._get_metadata("template")) is not None: + + template_prefix = macro_processor._get_macro_with_prefix("template_prefix") + if not os.path.exists(template): + template = os.path.join(template_prefix, template) + if template not in macro_processor._get_macro_with_prefix("template_stack"): - return smp_builtin_read(macro_processor, template, content) + macro_processor._get_macro_with_prefix("template_stack_content").append( + (filename, content, extension) + ) + return _smp_builtin_read( + macro_processor, template, _smp_builtin_template_content() + ) + + content = macro_processor.process_input(file_content, file=filename) + + if extension == "md": + content = smp_builtin_html_from_markdown(macro_processor, content) return content @@ -258,12 +275,12 @@ def smp_builtin_wodl(macro_processor, link, timeout_seconds=5): cache[link] = (working_link, r.status, r.reason) if not working_link: macro_processor.log_warning(f"Dead link {link} ({r.status} {r.reason})!") - except urllib.error.URLError as e: + except Exception as e: macro_processor.log_warning(f"Dead link {link} ({e})!") return "" -def smp_builtin_once(macro_processor, content): +def smp_builtin_expand_once(macro_processor, content): if (cache := macro_processor._get_macro_with_prefix("once_cache")) is not None: if (exp := cache.get(content)) is not None: return exp |