Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions Development/talagan_ReaImGui Markdown.lua
Original file line number Diff line number Diff line change
@@ -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&currency_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
Expand Down
21 changes: 12 additions & 9 deletions Development/talagan_ReaImGui Markdown/reaimgui_markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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),
Expand All @@ -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

Expand All @@ -132,15 +135,15 @@ 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
window_flags = window_flags | ImGui.WindowFlags_HorizontalScrollbar
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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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, "")
Expand All @@ -148,4 +187,4 @@ end
return {
ASTInspect = ASTInspect,
ASTToPlainText = ASTToPlainText
}
}