diff --git a/Development/talagan_ReaImGui Markdown.lua b/Development/talagan_ReaImGui Markdown.lua index 03736de04..903bcf4c9 100644 --- a/Development/talagan_ReaImGui Markdown.lua +++ b/Development/talagan_ReaImGui Markdown.lua @@ -1,13 +1,16 @@ --[[ @description ReaImGui Markdown : A Markdown rendering library for ReaImGui -@version 0.1.13 +@version 0.1.14 @author Ben 'Talagan' Babut @license MIT @donation https://www.paypal.com/donate/?business=3YEZMY9D6U8NC&no_recurring=1¤cy_code=EUR @links Forum Thread https://forum.cockos.com/showthread.php?t=301055 @changelog - - [Bug Fix] Fix potential style parameter gardening for dynamic styles (wrong deepCopy implementation) + - [Feature] Better handling of consecutive empty lines + - [Rework] Reworked default style + - [Bug Fix] List bullet not sized accordingly to list entry + - [Bug Fix] Code block would always add empty line at the end @metapackage @provides [nomain] talagan_ReaImGui Markdown/reaimgui_markdown/**/*.lua diff --git a/Development/talagan_ReaImGui Markdown/actions/talagan_ReaImGui Markdown Demo.lua b/Development/talagan_ReaImGui Markdown/actions/talagan_ReaImGui Markdown Demo.lua index f459d3ae9..5727c5424 100644 --- a/Development/talagan_ReaImGui Markdown/actions/talagan_ReaImGui Markdown Demo.lua +++ b/Development/talagan_ReaImGui Markdown/actions/talagan_ReaImGui Markdown Demo.lua @@ -55,12 +55,6 @@ if use_debugger then end -if do_unit_tests then - -- Reqiore stuff for the profiler to work. - local UnitTest = require "reaimgui_markdown/markdown-test" - UnitTest() -end - -- We override Reaper's defer method for two reasons : -- We want the full trace on errors -- We want the debugger to pause on errors @@ -75,6 +69,13 @@ reaper.defer = function(c) end) end +if do_unit_tests then + -- Reqiore stuff for the profiler to work. + local UnitTest = require "reaimgui_markdown/markdown-test" + UnitTest() +end + + local entry = [[ # This is a header level 1 ## This is a header level 2 diff --git a/Development/talagan_ReaImGui Markdown/reaimgui_markdown/markdown-ast.lua b/Development/talagan_ReaImGui Markdown/reaimgui_markdown/markdown-ast.lua index bc332c77e..45356f23a 100644 --- a/Development/talagan_ReaImGui Markdown/reaimgui_markdown/markdown-ast.lua +++ b/Development/talagan_ReaImGui Markdown/reaimgui_markdown/markdown-ast.lua @@ -658,6 +658,7 @@ local function ParseMarkdown(markdown) local table_headers = {} local expected_columns = 0 local raw_rows = {} + local consecutive_empty_lines = 0 local function finalize_table() if in_table and (#raw_rows > 0 or #table_headers > 0) then @@ -702,7 +703,13 @@ local function ParseMarkdown(markdown) -- Code block if trimmed_line:match(RX_CODEBLOCK) then finalize_table() + consecutive_empty_lines = 0 if in_code_block then + -- Remove trailing newline when closing code block + local code_node = ast.children[#ast.children] + if code_node.value:sub(-1) == "\n" then + code_node.value = code_node.value:sub(1, -2) + end in_code_block = false else in_code_block = true @@ -711,10 +718,12 @@ local function ParseMarkdown(markdown) i = i + 1 elseif in_code_block then ast.children[#ast.children].value = ast.children[#ast.children].value .. line .. "\n" + consecutive_empty_lines = 0 i = i + 1 -- Headers elseif trimmed_line:match(RX_HEADER) then finalize_table() + consecutive_empty_lines = 0 local level = #trimmed_line:match("^#+") if level <= 6 then local content = trimmed_line:match(RX_HEADER_CONTENT) @@ -725,6 +734,7 @@ local function ParseMarkdown(markdown) -- Blockquote - use trimmed_line for detection but pass original lines elseif trimmed_line:match(RX_BLOCKQUOTE) then finalize_table() + consecutive_empty_lines = 0 -- Store original lines but with leading spaces trimmed for blockquote parsing local trimmed_lines = {} for idx, l in ipairs(lines) do @@ -738,6 +748,7 @@ local function ParseMarkdown(markdown) -- Lists elseif trimmed_line:match(RX_UNORDERED_LIST) or trimmed_line:match(RX_ORDERED_LIST) then finalize_table() + consecutive_empty_lines = 0 local lists, new_i = parse_list(lines, i, #lines, 0, markdown) for _, list in ipairs(lists) do table.insert(ast.children, list) @@ -750,6 +761,7 @@ local function ParseMarkdown(markdown) end -- Table elseif trimmed_line:match(RX_TABLE_LINE) then + consecutive_empty_lines = 0 local cells = {} for cell in line:gmatch("[^|]+") do table.insert(cells, trim(cell)) @@ -786,14 +798,21 @@ local function ParseMarkdown(markdown) i = i + 1 elseif trimmed_line:match(RX_SEPARATOR) then finalize_table() + consecutive_empty_lines = 0 table.insert(ast.children, new_node("Separator")) i = i + 1 - elseif line == "" then + elseif trimmed_line == "" then finalize_table() + consecutive_empty_lines = consecutive_empty_lines + 1 + -- Only create VerticalSpace from the 2nd consecutive empty line onwards + if consecutive_empty_lines >= 2 then + table.insert(ast.children, new_node("VerticalSpace")) + end i = i + 1 else -- Paragraph finalize_table() + consecutive_empty_lines = 0 local paragraph_lines = {} local j = i diff --git a/Development/talagan_ReaImGui Markdown/reaimgui_markdown/markdown-html.lua b/Development/talagan_ReaImGui Markdown/reaimgui_markdown/markdown-html.lua index 9a91f3d9a..580481f95 100644 --- a/Development/talagan_ReaImGui Markdown/reaimgui_markdown/markdown-html.lua +++ b/Development/talagan_ReaImGui Markdown/reaimgui_markdown/markdown-html.lua @@ -40,6 +40,8 @@ local function ASTToHtml(ast) return '' .. node.attributes.alt .. '' elseif node.type == "LineBreak" then return "
\n" + elseif node.type == "VerticalSpace" then + return "
\n" elseif node.type == "Separator" then return "
\n" elseif node.type == "UnorderedList" then diff --git a/Development/talagan_ReaImGui Markdown/reaimgui_markdown/markdown-imgui.lua b/Development/talagan_ReaImGui Markdown/reaimgui_markdown/markdown-imgui.lua index 5d7b8df89..4da44974b 100644 --- a/Development/talagan_ReaImGui Markdown/reaimgui_markdown/markdown-imgui.lua +++ b/Development/talagan_ReaImGui Markdown/reaimgui_markdown/markdown-imgui.lua @@ -61,19 +61,19 @@ local DEFAULT_COLOR = 0xCCCCCCFF local DEFAULT_STYLE = { default = { font_family = "Arial", font_size = 13, base_color = "#CCCCCC", bold_color = "white", autopad = 5 }, - h1 = { font_family = "Arial", font_size = 23, padding_left = 0, padding_top = 3, padding_bottom = 5, line_spacing = 0, base_color = "#288efa", bold_color = "#288efa" }, - h2 = { font_family = "Arial", font_size = 21, padding_left = 5, padding_top = 3, padding_bottom = 5, line_spacing = 0, base_color = "#4da3ff", bold_color = "#4da3ff" }, - h3 = { font_family = "Arial", font_size = 19, padding_left = 10, padding_top = 3, padding_bottom = 4, line_spacing = 0, base_color = "#65acf7", bold_color = "#65acf7" }, - h4 = { font_family = "Arial", font_size = 17, padding_left = 15, padding_top = 3, padding_bottom = 3, line_spacing = 0, base_color = "#85c0ff", bold_color = "#85c0ff" }, - h5 = { font_family = "Arial", font_size = 15, padding_left = 20, padding_top = 3, padding_bottom = 3, line_spacing = 0, base_color = "#9ecdff", bold_color = "#9ecdff" }, + h1 = { font_family = "Arial", font_size = 23, padding_left = 0, padding_top = 0, padding_bottom = 0, line_spacing = 0, base_color = "#288efa", bold_color = "#288efa" }, + h2 = { font_family = "Arial", font_size = 21, padding_left = 5, padding_top = 0, padding_bottom = 0, line_spacing = 0, base_color = "#4da3ff", bold_color = "#4da3ff" }, + h3 = { font_family = "Arial", font_size = 19, padding_left = 10, padding_top = 0, padding_bottom = 0, line_spacing = 0, base_color = "#65acf7", bold_color = "#65acf7" }, + h4 = { font_family = "Arial", font_size = 17, padding_left = 15, padding_top = 0, padding_bottom = 0, line_spacing = 0, base_color = "#85c0ff", bold_color = "#85c0ff" }, + h5 = { font_family = "Arial", font_size = 15, padding_left = 20, padding_top = 0, padding_bottom = 0, line_spacing = 0, base_color = "#9ecdff", bold_color = "#9ecdff" }, - paragraph = { font_family = "Arial", font_size = 13, padding_left = 30, padding_top = 3, padding_bottom = 7, line_spacing = 0, padding_in_blockquote = 6 }, - list = { font_family = "Arial", font_size = 13, padding_left = 40, padding_top = 5, padding_bottom = 7, line_spacing = 0, padding_indent = 5 }, + paragraph = { font_family = "Arial", font_size = 13, padding_left = 30, padding_top = 3, padding_bottom = 3, line_spacing = 0, padding_in_blockquote = 6 }, + list = { font_family = "Arial", font_size = 13, padding_left = 40, padding_top = 3, padding_bottom = 3, line_spacing = 0, padding_indent = 5 }, - table = { font_family = "Arial", font_size = 13, padding_left = 30, padding_top = 3, padding_bottom = 7, line_spacing = 0 }, + table = { font_family = "Arial", font_size = 13, padding_left = 30, padding_top = 3, padding_bottom = 3, line_spacing = 0 }, - code = { font_family = "monospace", font_size = 13, padding_left = 30, padding_top = 3, padding_bottom = 7, line_spacing = 4, padding_in_blockquote = 6 }, - blockquote = { font_family = "Arial", font_size = 13, padding_left = 0, padding_top = 5, padding_bottom = 10, line_spacing = 2, padding_indent = 10 }, + code = { font_family = "monospace", font_size = 13, padding_left = 30, padding_top = 3, padding_bottom = 3, line_spacing = 4, padding_in_blockquote = 6 }, + blockquote = { font_family = "Arial", font_size = 13, padding_left = 0, padding_top = 5, padding_bottom = 5, line_spacing = 2, padding_indent = 10 }, link = { base_color = "orange", bold_color = "tomato"}, @@ -374,6 +374,8 @@ local function ASTToImgui(ctx, ast, fonts, style, options) return "link" elseif node.type == "LineBreak" then return "paragraph" + elseif node.type == "VerticalSpace" then + return "paragraph" elseif (node.type == "UnorderedList") or (node.type == "OrderedList") then return "list" elseif node.type == "ListItem" then @@ -658,7 +660,14 @@ local function ASTToImgui(ctx, ast, fonts, style, options) ImGui.NewLine(ctx) ImGui.AlignTextToFramePadding(ctx) + elseif node.type == "VerticalSpace" then + ImGui.PushStyleVar(ctx, ImGui.StyleVar_ItemSpacing, 0, 0) + ImGui.Dummy(ctx, 0, 0) + ImGui.PopStyleVar(ctx) + num_on_line = 0 + num_line = num_line + 1 + ImGui.AlignTextToFramePadding(ctx) elseif (node.type == "UnorderedList") or (node.type == "OrderedList") then local nstyle = style.list @@ -699,7 +708,7 @@ local function ASTToImgui(ctx, ast, fonts, style, options) if node.parent_list.type == "UnorderedList" then -- Render a bullet with the default font - ImGui.PushFont(ctx, nil, style.default.font_size) + ImGui.PushFont(ctx, nil, style.list.font_size) ImGui.Text(ctx, "• ") ImGui.PopFont(ctx) num_on_line = 0 diff --git a/Development/talagan_ReaImGui Markdown/reaimgui_markdown/markdown-test.lua b/Development/talagan_ReaImGui Markdown/reaimgui_markdown/markdown-test.lua index 739826704..5882b8202 100644 --- a/Development/talagan_ReaImGui Markdown/reaimgui_markdown/markdown-test.lua +++ b/Development/talagan_ReaImGui Markdown/reaimgui_markdown/markdown-test.lua @@ -140,8 +140,7 @@ This is pure code **and this** should not try to do |clever|things| local wanted = [[ -
This is pure code **and this** should not try to do |clever|things|
-
+
This is pure code **and this** should not try to do |clever|things|
]] dotest(entry, wanted) @@ -656,11 +655,9 @@ code line 2 [x] not checkbox ```]], expected_html = [[
code line 1
-code line 2
-
+code line 2
**not bold**
-[x] not checkbox
-
+[x] not checkbox ]] },