From f5a4a8114783c1dbdeea9d778f064db166e17549 Mon Sep 17 00:00:00 2001 From: Guilian Date: Sun, 19 Jan 2025 14:16:42 +0100 Subject: [PATCH] feat: global logger module --- html.lua | 9 ++++----- logging.lua | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 logging.lua diff --git a/html.lua b/html.lua index d317aa5..da43d7f 100644 --- a/html.lua +++ b/html.lua @@ -1,3 +1,4 @@ +local logger = require(".logging") local function trim(str) return str:match("^%s*(.-)%s*$") @@ -232,7 +233,7 @@ function M.tokenise( content ) if TOKENS[#TOKENS] and ( TOKENS[#TOKENS].type == "START_OPENING_TAG") then if RAW_TEXT_TAGS[word] then - print("Warning: "..word.." tags may contain text that would be incorrectly parsed as HTML.") + logger.printerr("Warning: "..word.." tags may contain text that would be incorrectly parsed as HTML.") -- made possible because of the whitespace removal at the start set_skipping_to("" end - --print( node_name ) - local next_indent = indent + 1 if is_pseudo_element and not include_internal_pseudoelements then next_indent = indent diff --git a/logging.lua b/logging.lua new file mode 100644 index 0000000..afabf23 --- /dev/null +++ b/logging.lua @@ -0,0 +1,31 @@ + +local may_print_errors = false +local errors_buffer = {} + +local logger = { + print = function( str ) + print( str ) + end, + printerr = function( str) + if str:sub(#str,#str) ~= "\n" then + str = str .. "\n" + end + + if not may_print_errors then + table.insert(errors_buffer, str) + return + end + + io.stderr:write(str) + end, + enable_printing_errors = function() + may_print_errors = true + + for _, err in ipairs(errors_buffer) do + io.stderr:write(err) + end + end, +} + + +return logger