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
7 changes: 5 additions & 2 deletions Development/talagan_ReaImGui Markdown.lua
Original file line number Diff line number Diff line change
@@ -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&currency_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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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))
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ local function ASTToHtml(ast)
return '<img src="' .. node.attributes.url .. '" alt="' .. node.attributes.alt .. '">'
elseif node.type == "LineBreak" then
return "<br>\n"
elseif node.type == "VerticalSpace" then
return "<br>\n"
elseif node.type == "Separator" then
return "<hr>\n"
elseif node.type == "UnorderedList" then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"},

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ This is pure code **and this** should not try to do |clever|things|

local wanted =
[[
<pre><code>This is pure code **and this** should not try to do |clever|things|
</code></pre>
<pre><code>This is pure code **and this** should not try to do |clever|things|</code></pre>
]]

dotest(entry, wanted)
Expand Down Expand Up @@ -656,11 +655,9 @@ code line 2
[x] not checkbox
```]],
expected_html = [[<pre><code>code line 1
code line 2
</code></pre>
code line 2</code></pre>
<pre><code>**not bold**
[x] not checkbox
</code></pre>
[x] not checkbox</code></pre>
]]
},

Expand Down