From 1ad5663b0973ac1d4fa697bb2ac0e0d3c6389d48 Mon Sep 17 00:00:00 2001 From: BenTalagan Date: Mon, 8 Dec 2025 10:10:32 +0100 Subject: [PATCH] Update ReaImGui Markdown v0.1.14 > v0.1.15 --- Development/talagan_ReaImGui Markdown.lua | 8 ++-- .../reaimgui_markdown.lua | 21 +++++---- .../reaimgui_markdown/markdown-text.lua | 47 +++++++++++++++++-- 3 files changed, 58 insertions(+), 18 deletions(-) diff --git a/Development/talagan_ReaImGui Markdown.lua b/Development/talagan_ReaImGui Markdown.lua index 903bcf4c9..aba08930a 100644 --- a/Development/talagan_ReaImGui Markdown.lua +++ b/Development/talagan_ReaImGui Markdown.lua @@ -1,16 +1,14 @@ --[[ @description ReaImGui Markdown : A Markdown rendering library for ReaImGui -@version 0.1.14 +@version 0.1.15 @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 - - [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 + - [Feature] Text renderer can output with line feeds + - [Optim] Factorize fonts with context scope instead of widget scope @metapackage @provides [nomain] talagan_ReaImGui Markdown/reaimgui_markdown/**/*.lua diff --git a/Development/talagan_ReaImGui Markdown/reaimgui_markdown.lua b/Development/talagan_ReaImGui Markdown/reaimgui_markdown.lua index 760f44ff0..82529073d 100644 --- a/Development/talagan_ReaImGui Markdown/reaimgui_markdown.lua +++ b/Development/talagan_ReaImGui Markdown/reaimgui_markdown.lua @@ -59,7 +59,7 @@ end -- height (0 == auto, use remaining) function ReaImGuiMd:_initialize(ctx, id, options, partial_style) self.id = id - self.options = { wrap = true, horizontal_scrollbar = true , width = 0, height = 0 } + self.options = { wrap = true, horizontal_scrollbar = true , width = 0, height = 0, additional_window_flags = 0 } self.style = deepCopy(ImGuiMdCore.DEFAULT_STYLE) self.text = "" self.ast = {} @@ -85,20 +85,23 @@ function ReaImGuiMd:updateCtx(ctx) self:_createFontsIfNeeded(ctx) end +local font_repository = {} -- ctx -> font_name -> font function ReaImGuiMd:_createFontsIfNeeded(ctx) - -- If already initialized in the same context, be happy - if self.fonts and ctx == self.font_ctx then return end + if not font_repository[ctx] then + font_repository[ctx] = {} + end + + local fr = font_repository[ctx] -- Else recreate all fonts in the new context - local fonts = {} local style = self.style for class_name, _ in pairs(ImGuiMdCore.DEFAULT_STYLE) do -- 0 is for normal text, 1 for h1, 2 for h2, etc local fontfam = style[class_name].font_family - if fontfam and not fonts[fontfam] then + if fontfam and not fr[fontfam] then local font = { normal = ImGui.CreateFont(fontfam), bold = ImGui.CreateFont(fontfam, ImGui.FontFlags_Bold), @@ -111,11 +114,11 @@ function ReaImGuiMd:_createFontsIfNeeded(ctx) ImGui.Attach(ctx, font.italic) ImGui.Attach(ctx, font.bolditalic) - fonts[fontfam] = font + fr[fontfam] = font end end - self.fonts = fonts + -- Remember font ctx self.font_ctx = ctx end @@ -132,7 +135,7 @@ function ReaImGuiMd:render(ctx) error("Developer error : ImGui's context has changed but you forgot to update ReaImGuiMd fonts !") end - local window_flags = 0 + local window_flags = 0 | self.options.additional_window_flags local child_flags = 0 if self.options.horizontal_scrollbar then @@ -140,7 +143,7 @@ function ReaImGuiMd:render(ctx) end if ImGui.BeginChild(ctx, "##" .. self.id, self.options.width, self.options.height, child_flags, window_flags) then - self.max_x, self.max_y, self.interaction = ImGuiMdCore.ASTToImgui(ctx, self.ast, self.fonts, self.style, self.options) + self.max_x, self.max_y, self.interaction = ImGuiMdCore.ASTToImgui(ctx, self.ast, font_repository[ctx], self.style, self.options) ImGui.EndChild(ctx) end diff --git a/Development/talagan_ReaImGui Markdown/reaimgui_markdown/markdown-text.lua b/Development/talagan_ReaImGui Markdown/reaimgui_markdown/markdown-text.lua index 43bc60f0b..7ad07c319 100644 --- a/Development/talagan_ReaImGui Markdown/reaimgui_markdown/markdown-text.lua +++ b/Development/talagan_ReaImGui Markdown/reaimgui_markdown/markdown-text.lua @@ -4,7 +4,6 @@ -- @about This is a part of ReaImGui:Markdown -- Text generation from AST, this is for inspecting and debugging - local function render_node_inpect(obj, indent, visited, lines) indent = indent or 0 visited = visited or {} @@ -101,8 +100,10 @@ local function ASTInspect(obj, indent) return render_node_inpect(obj,indent,{},{}) end +local function ASTToPlainText(nodes, options) + options = options or {} + local add_newlines = options.newlines or false -local function ASTToPlainText(nodes) if not nodes then return "" end @@ -112,8 +113,29 @@ local function ASTToPlainText(nodes) nodes = {nodes} end + -- If we receive a Document node, use its children instead + if #nodes == 1 and nodes[1].type == "Document" then + nodes = nodes[1].children + end + local result = {} + -- Block-level elements that add newlines after + local block_types = { + Paragraph = true, + Header = true, + ListItem = true, + CodeBlock = true, + Blockquote = true + } + + -- Elements that become newlines + local newline_types = { + LineBreak = true, + VerticalSpace = true, + Separator = true + } + local function traverse(node) if node.type == "Text" then table.insert(result, node.value) @@ -130,15 +152,32 @@ local function ASTToPlainText(nodes) table.insert(result, "[ ]") end + elseif node.type == "CodeBlock" then + table.insert(result, node.value) + if add_newlines then + table.insert(result, "\n") + end + + elseif add_newlines and newline_types[node.type] then + table.insert(result, "\n") + elseif node.children then for _, child in ipairs(node.children) do traverse(child) end + -- Add newline after block-level elements + if add_newlines and block_types[node.type] then + table.insert(result, "\n") + end end end - for _, node in ipairs(nodes) do + for i, node in ipairs(nodes) do traverse(node) + -- Add extra newline between blocks to separate them (double \n total) + if add_newlines and i < #nodes and block_types[node.type] then + table.insert(result, "\n") + end end return table.concat(result, "") @@ -148,4 +187,4 @@ end return { ASTInspect = ASTInspect, ASTToPlainText = ASTToPlainText -} +} \ No newline at end of file