Skip to content

Commit a525a61

Browse files
refactor: create a renderer parent class, initialize renderers in handler constructor
1 parent 74502e5 commit a525a61

File tree

9 files changed

+82
-50
lines changed

9 files changed

+82
-50
lines changed

doc/render-markdown.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*render-markdown.txt* For 0.10.0 Last change: 2024 August 23
1+
*render-markdown.txt* For 0.10.0 Last change: 2024 August 24
22

33
==============================================================================
44
Table of Contents *render-markdown-table-of-contents*

lua/render-markdown/handler/markdown.lua

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local Context = require('render-markdown.context')
22
local NodeInfo = require('render-markdown.node_info')
33
local RenderCode = require('render-markdown.render.code')
44
local RenderHeading = require('render-markdown.render.heading')
5+
local RenderQuote = require('render-markdown.render.quote')
56
local RenderTable = require('render-markdown.render.table')
67
local component = require('render-markdown.component')
78
local list = require('render-markdown.list')
@@ -14,6 +15,7 @@ local str = require('render-markdown.str')
1415
---@field private marks render.md.Marks
1516
---@field private config render.md.BufferConfig
1617
---@field private context render.md.Context
18+
---@field private renderers table<string, render.md.Renderer>
1719
local Handler = {}
1820
Handler.__index = Handler
1921

@@ -25,6 +27,12 @@ function Handler.new(buf)
2527
self.marks = list.new_marks()
2628
self.config = state.get_config(buf)
2729
self.context = Context.get(buf)
30+
self.renderers = {
31+
code = RenderCode.new(buf, self.marks, self.config, self.context),
32+
heading = RenderHeading.new(buf, self.marks, self.config, self.context),
33+
quote = RenderQuote.new(buf, self.marks, self.config, self.context),
34+
table = RenderTable.new(buf, self.marks, self.config, self.context),
35+
}
2836
return self
2937
end
3038

@@ -34,32 +42,20 @@ function Handler:parse(root)
3442
self.context:query(root, state.markdown_query, function(capture, node)
3543
local info = NodeInfo.new(self.buf, node)
3644
logger.debug_node_info(capture, info)
37-
if capture == 'section' then
45+
46+
local renderer = self.renderers[capture]
47+
if renderer ~= nil then
48+
renderer:render(info)
49+
elseif capture == 'section' then
3850
self:section(info)
39-
elseif capture == 'heading' then
40-
RenderHeading.new(self.buf, self.marks, self.config, self.context):render(info)
4151
elseif capture == 'dash' then
4252
self:dash(info)
43-
elseif capture == 'code' then
44-
RenderCode.new(self.buf, self.marks, self.config, self.context):render(info)
4553
elseif capture == 'list_marker' then
4654
self:list_marker(info)
4755
elseif capture == 'checkbox_unchecked' then
4856
self:checkbox(info, self.config.checkbox.unchecked)
4957
elseif capture == 'checkbox_checked' then
5058
self:checkbox(info, self.config.checkbox.checked)
51-
elseif capture == 'quote' then
52-
self.context:query(node, state.markdown_quote_query, function(nested_capture, nested_node)
53-
local nested_info = NodeInfo.new(self.buf, nested_node)
54-
logger.debug_node_info(nested_capture, nested_info)
55-
if nested_capture == 'quote_marker' then
56-
self:quote_marker(nested_info, info)
57-
else
58-
logger.unhandled_capture('markdown quote', nested_capture)
59-
end
60-
end)
61-
elseif capture == 'table' then
62-
RenderTable.new(self.buf, self.marks, self.config, self.context):render(info)
6359
else
6460
logger.unhandled_capture('markdown', capture)
6561
end
@@ -187,25 +183,6 @@ function Handler:checkbox(info, checkbox)
187183
})
188184
end
189185

190-
---@private
191-
---@param info render.md.NodeInfo
192-
---@param block_quote render.md.NodeInfo
193-
function Handler:quote_marker(info, block_quote)
194-
local quote = self.config.quote
195-
if not quote.enabled then
196-
return
197-
end
198-
local callout = component.callout(self.buf, block_quote.text, 'contains')
199-
local highlight = callout ~= nil and callout.highlight or quote.highlight
200-
self.marks:add(true, info.start_row, info.start_col, {
201-
end_row = info.end_row,
202-
end_col = info.end_col,
203-
virt_text = { { info.text:gsub('>', quote.icon), highlight } },
204-
virt_text_pos = 'overlay',
205-
virt_text_repeat_linebreak = quote.repeat_linebreak or nil,
206-
})
207-
end
208-
209186
---@class render.md.handler.Markdown: render.md.Handler
210187
local M = {}
211188

lua/render-markdown/handler/markdown_inline.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function Handler:parse(root)
3131
self.context:query(root, state.inline_query, function(capture, node)
3232
local info = NodeInfo.new(self.buf, node)
3333
logger.debug_node_info(capture, info)
34+
3435
if capture == 'code' then
3536
self:code(info)
3637
elseif capture == 'shortcut' then

lua/render-markdown/node_info.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ end
77

88
---@class render.md.NodeInfo
99
---@field private buf integer
10-
---@field private node TSNode
10+
---@field node TSNode
1111
---@field type string
1212
---@field text string
1313
---@field start_row integer

lua/render-markdown/render/code.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,8 @@ function Parser.get_width(config, context, widths)
8585
end
8686
end
8787

88-
---@class render.md.render.Code
89-
---@field private buf integer
90-
---@field private marks render.md.Marks
88+
---@class render.md.render.Code: render.md.Renderer
9189
---@field private config render.md.Code
92-
---@field private context render.md.Context
9390
local Render = {}
9491
Render.__index = Render
9592

lua/render-markdown/render/heading.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ local list = require('render-markdown.list')
33
local str = require('render-markdown.str')
44
local util = require('render-markdown.render.util')
55

6-
---@class render.md.render.Heading
7-
---@field private buf integer
8-
---@field private marks render.md.Marks
6+
---@class render.md.render.Heading: render.md.Renderer
97
---@field private config render.md.Heading
10-
---@field private context render.md.Context
118
local Render = {}
129
Render.__index = Render
1310

lua/render-markdown/render/quote.lua

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
local NodeInfo = require('render-markdown.node_info')
2+
local component = require('render-markdown.component')
3+
local logger = require('render-markdown.logger')
4+
local state = require('render-markdown.state')
5+
6+
---@class render.md.render.Quote: render.md.Renderer
7+
---@field private config render.md.Quote
8+
local Render = {}
9+
Render.__index = Render
10+
11+
---@param buf integer
12+
---@param marks render.md.Marks
13+
---@param config render.md.BufferConfig
14+
---@param context render.md.Context
15+
---@return render.md.render.Quote
16+
function Render.new(buf, marks, config, context)
17+
local self = setmetatable({}, Render)
18+
self.buf = buf
19+
self.marks = marks
20+
self.config = config.quote
21+
self.context = context
22+
return self
23+
end
24+
25+
---@param info render.md.NodeInfo
26+
function Render:render(info)
27+
self.context:query(info.node, state.markdown_quote_query, function(capture, node)
28+
local nested_info = NodeInfo.new(self.buf, node)
29+
logger.debug_node_info(capture, nested_info)
30+
31+
if capture == 'quote_marker' then
32+
self:quote_marker(nested_info, info)
33+
else
34+
logger.unhandled_capture('markdown quote', capture)
35+
end
36+
end)
37+
end
38+
39+
---@private
40+
---@param info render.md.NodeInfo
41+
---@param block_quote render.md.NodeInfo
42+
function Render:quote_marker(info, block_quote)
43+
if not self.config.enabled then
44+
return
45+
end
46+
local callout = component.callout(self.buf, block_quote.text, 'contains')
47+
local highlight = callout ~= nil and callout.highlight or self.config.highlight
48+
self.marks:add(true, info.start_row, info.start_col, {
49+
end_row = info.end_row,
50+
end_col = info.end_col,
51+
virt_text = { { info.text:gsub('>', self.config.icon), highlight } },
52+
virt_text_pos = 'overlay',
53+
virt_text_repeat_linebreak = self.config.repeat_linebreak or nil,
54+
})
55+
end
56+
57+
return Render

lua/render-markdown/render/table.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,8 @@ function Parser.row_data(info, cell_type)
176176
return { pipes = pipes, cells = cells }
177177
end
178178

179-
---@class render.md.render.Table
180-
---@field private buf integer
181-
---@field private marks render.md.Marks
179+
---@class render.md.render.Table: render.md.Renderer
182180
---@field private config render.md.PipeTable
183-
---@field private context render.md.Context
184181
local Render = {}
185182
Render.__index = Render
186183

lua/render-markdown/render/util.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ local colors = require('render-markdown.colors')
22
local state = require('render-markdown.state')
33
local str = require('render-markdown.str')
44

5+
---@class render.md.Renderer
6+
---@field protected buf integer
7+
---@field protected marks render.md.Marks
8+
---@field protected context render.md.Context
9+
---@field render fun(self: render.md.Renderer, info: render.md.NodeInfo)
10+
511
---@class render.md.render.Util
612
local M = {}
713

0 commit comments

Comments
 (0)