Skip to content

Commit 614bd69

Browse files
authored
Update ReaImGui Markdown v0.1.14 > v0.1.15 (#1692)
1 parent 4a6fb0a commit 614bd69

File tree

3 files changed

+58
-18
lines changed

3 files changed

+58
-18
lines changed

Development/talagan_ReaImGui Markdown.lua

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
--[[
22
@description ReaImGui Markdown : A Markdown rendering library for ReaImGui
3-
@version 0.1.14
3+
@version 0.1.15
44
@author Ben 'Talagan' Babut
55
@license MIT
66
@donation https://www.paypal.com/donate/?business=3YEZMY9D6U8NC&no_recurring=1&currency_code=EUR
77
@links
88
Forum Thread https://forum.cockos.com/showthread.php?t=301055
99
@changelog
10-
- [Feature] Better handling of consecutive empty lines
11-
- [Rework] Reworked default style
12-
- [Bug Fix] List bullet not sized accordingly to list entry
13-
- [Bug Fix] Code block would always add empty line at the end
10+
- [Feature] Text renderer can output with line feeds
11+
- [Optim] Factorize fonts with context scope instead of widget scope
1412
@metapackage
1513
@provides
1614
[nomain] talagan_ReaImGui Markdown/reaimgui_markdown/**/*.lua

Development/talagan_ReaImGui Markdown/reaimgui_markdown.lua

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ end
5959
-- height (0 == auto, use remaining)
6060
function ReaImGuiMd:_initialize(ctx, id, options, partial_style)
6161
self.id = id
62-
self.options = { wrap = true, horizontal_scrollbar = true , width = 0, height = 0 }
62+
self.options = { wrap = true, horizontal_scrollbar = true , width = 0, height = 0, additional_window_flags = 0 }
6363
self.style = deepCopy(ImGuiMdCore.DEFAULT_STYLE)
6464
self.text = ""
6565
self.ast = {}
@@ -85,20 +85,23 @@ function ReaImGuiMd:updateCtx(ctx)
8585
self:_createFontsIfNeeded(ctx)
8686
end
8787

88+
local font_repository = {} -- ctx -> font_name -> font
8889
function ReaImGuiMd:_createFontsIfNeeded(ctx)
8990

90-
-- If already initialized in the same context, be happy
91-
if self.fonts and ctx == self.font_ctx then return end
91+
if not font_repository[ctx] then
92+
font_repository[ctx] = {}
93+
end
94+
95+
local fr = font_repository[ctx]
9296

9397
-- Else recreate all fonts in the new context
94-
local fonts = {}
9598
local style = self.style
9699

97100
for class_name, _ in pairs(ImGuiMdCore.DEFAULT_STYLE) do
98101
-- 0 is for normal text, 1 for h1, 2 for h2, etc
99102
local fontfam = style[class_name].font_family
100103

101-
if fontfam and not fonts[fontfam] then
104+
if fontfam and not fr[fontfam] then
102105
local font = {
103106
normal = ImGui.CreateFont(fontfam),
104107
bold = ImGui.CreateFont(fontfam, ImGui.FontFlags_Bold),
@@ -111,11 +114,11 @@ function ReaImGuiMd:_createFontsIfNeeded(ctx)
111114
ImGui.Attach(ctx, font.italic)
112115
ImGui.Attach(ctx, font.bolditalic)
113116

114-
fonts[fontfam] = font
117+
fr[fontfam] = font
115118
end
116119
end
117120

118-
self.fonts = fonts
121+
-- Remember font ctx
119122
self.font_ctx = ctx
120123
end
121124

@@ -132,15 +135,15 @@ function ReaImGuiMd:render(ctx)
132135
error("Developer error : ImGui's context has changed but you forgot to update ReaImGuiMd fonts !")
133136
end
134137

135-
local window_flags = 0
138+
local window_flags = 0 | self.options.additional_window_flags
136139
local child_flags = 0
137140

138141
if self.options.horizontal_scrollbar then
139142
window_flags = window_flags | ImGui.WindowFlags_HorizontalScrollbar
140143
end
141144

142145
if ImGui.BeginChild(ctx, "##" .. self.id, self.options.width, self.options.height, child_flags, window_flags) then
143-
self.max_x, self.max_y, self.interaction = ImGuiMdCore.ASTToImgui(ctx, self.ast, self.fonts, self.style, self.options)
146+
self.max_x, self.max_y, self.interaction = ImGuiMdCore.ASTToImgui(ctx, self.ast, font_repository[ctx], self.style, self.options)
144147
ImGui.EndChild(ctx)
145148
end
146149

Development/talagan_ReaImGui Markdown/reaimgui_markdown/markdown-text.lua

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
-- @about This is a part of ReaImGui:Markdown
55

66
-- Text generation from AST, this is for inspecting and debugging
7-
87
local function render_node_inpect(obj, indent, visited, lines)
98
indent = indent or 0
109
visited = visited or {}
@@ -101,8 +100,10 @@ local function ASTInspect(obj, indent)
101100
return render_node_inpect(obj,indent,{},{})
102101
end
103102

103+
local function ASTToPlainText(nodes, options)
104+
options = options or {}
105+
local add_newlines = options.newlines or false
104106

105-
local function ASTToPlainText(nodes)
106107
if not nodes then
107108
return ""
108109
end
@@ -112,8 +113,29 @@ local function ASTToPlainText(nodes)
112113
nodes = {nodes}
113114
end
114115

116+
-- If we receive a Document node, use its children instead
117+
if #nodes == 1 and nodes[1].type == "Document" then
118+
nodes = nodes[1].children
119+
end
120+
115121
local result = {}
116122

123+
-- Block-level elements that add newlines after
124+
local block_types = {
125+
Paragraph = true,
126+
Header = true,
127+
ListItem = true,
128+
CodeBlock = true,
129+
Blockquote = true
130+
}
131+
132+
-- Elements that become newlines
133+
local newline_types = {
134+
LineBreak = true,
135+
VerticalSpace = true,
136+
Separator = true
137+
}
138+
117139
local function traverse(node)
118140
if node.type == "Text" then
119141
table.insert(result, node.value)
@@ -130,15 +152,32 @@ local function ASTToPlainText(nodes)
130152
table.insert(result, "[ ]")
131153
end
132154

155+
elseif node.type == "CodeBlock" then
156+
table.insert(result, node.value)
157+
if add_newlines then
158+
table.insert(result, "\n")
159+
end
160+
161+
elseif add_newlines and newline_types[node.type] then
162+
table.insert(result, "\n")
163+
133164
elseif node.children then
134165
for _, child in ipairs(node.children) do
135166
traverse(child)
136167
end
168+
-- Add newline after block-level elements
169+
if add_newlines and block_types[node.type] then
170+
table.insert(result, "\n")
171+
end
137172
end
138173
end
139174

140-
for _, node in ipairs(nodes) do
175+
for i, node in ipairs(nodes) do
141176
traverse(node)
177+
-- Add extra newline between blocks to separate them (double \n total)
178+
if add_newlines and i < #nodes and block_types[node.type] then
179+
table.insert(result, "\n")
180+
end
142181
end
143182

144183
return table.concat(result, "")
@@ -148,4 +187,4 @@ end
148187
return {
149188
ASTInspect = ASTInspect,
150189
ASTToPlainText = ASTToPlainText
151-
}
190+
}

0 commit comments

Comments
 (0)