irongit

A Neovim plugin enabling you to solve LeetCode problems.

feat(parser): improved highlighting

kawreauthored
parent 7e5b466commit 3d1238cc8b89978ce66ce3518456fd1c47e71d02Browse files

3 files changed, +21 -30

+8-27lua/leetcode/parser/html.lua
@@ -166,7 +166,7 @@ function Html:handle_link(text, tag_data)
166166 end
167167
168168 local line = NuiLine()
169- line:append(text, "leetcode_list")
169+ line:append(text, "leetcode_ref")
170170 line:append("->(", "leetcode_normal")
171171 line:append(link, "leetcode_link")
172172 line:append(")", "leetcode_normal")
@@ -174,26 +174,6 @@ function Html:handle_link(text, tag_data)
174174 return line
175175 end
176176
177-function Html:handle_img(tag_data)
178- local tag = tag_data.tag
179- if tag ~= "img" then return end
180- local line = NuiLine()
181-
182- local src, alt
183- for _, attr in ipairs(tag_data.attrs) do
184- if attr.name == "src" then src = attr.value end
185- if attr.name == "alt" then alt = attr.value end
186- end
187- if not src then return end
188- alt = alt or ""
189-
190- line:append(string.format("[%s](", alt ~= "" and alt or "img"), "leetcode_indent")
191- line:append(src, "leetcode_link")
192- line:append(")", "leetcode_indent")
193-
194- self.text:append(line)
195-end
196-
197177 ---@private
198178 ---
199179 ---@param node TSNode
@@ -256,6 +236,9 @@ function Html:normalize()
256236 :gsub("\t*<(/?li)>", "<%1>")
257237 :gsub("\t*<(/?ul)>", "<%1>")
258238 :gsub("\t*<(/?ol)>", "<%1>")
239+ :gsub("<strong>(Input:?%s*)</strong>", "<input>%1</input>")
240+ :gsub("<strong>(Output:?%s*)</strong>", "<output>%1</output>")
241+ :gsub("<strong>(Explanation:?%s*)</strong>", "<explanation>%1</explanation>")
259242 -- :gsub(
260243 -- "\t*<(li[^>]*)>(.-)\t*</(li)>",
261244 -- "<%1>%2</%3>\n"
@@ -263,11 +246,11 @@ function Html:normalize()
263246 -- :gsub("\t*<(ul[^>]*)>(.-)\t*</(ul)>", "<%1>%2</%3>\n")
264247 -- :gsub("\t*<(ol[^>]*)>(.-)\t*</(ol)>", "<%1>%2</%3>\n")
265248 :gsub(
266- "<p><strong[^>]*>(Example%s*%d*:)%s*</strong></p>\n*",
249+ "<p><strong[^>]*>(Example%s*%d*:?)%s*</strong></p>\n*",
267250 "\n\n<example>󰛨 %1</example>\n\n"
268251 )
269252 :gsub(
270- "<p><strong[^>]*>(Constraints:)%s*</strong></p>\n*",
253+ "<p><strong[^>]*>(Constraints:?)%s*</strong></p>\n*",
271254 "\n\n<constraints> %1</constraints>\n\n"
272255 )
273256 :gsub("<(img[^>]*)/>\n*", "<%1>img</img>")
@@ -277,10 +260,8 @@ function Html:normalize()
277260 :gsub("\t", "&lctab;")
278261 :gsub("%s", "&nbsp;")
279262 :gsub("<[^>]*>", function(match) return match:gsub("&nbsp;", " ") end)
280- :gsub("<a[^>]*>(.-)</a>", function(match)
281- match = match:gsub("&#?%w+;", utils.entity)
282- return match:gsub("&nbsp;", " ")
283- end) .. "&lcend;"
263+ :gsub("<a[^>]*>(.-)</a>", function(match) return match:gsub("&#?%w+;", utils.entity) end)
264+ .. "&lcend;"
284265 end
285266
286267 -- Trim excessive lines
+9-2lua/leetcode/parser/utils.lua
@@ -37,17 +37,24 @@ local entities = {
3737 ["&lccode;"] = "`",
3838 }
3939
40+local xd = "header"
41+
4042 local highlights = {
4143 strong = "bold",
4244 b = "bold",
4345 em = "italic",
4446 i = "italic",
4547 code = "code",
46- example = "example",
47- constraints = "constraints",
4848 a = "link",
4949 u = "underline",
5050
51+ example = "example",
52+ constraints = "constraints",
53+
54+ input = xd,
55+ output = xd,
56+ explanation = xd,
57+
5158 -- pre = "",
5259 -- span = "",
5360 -- p = "",
+4-1lua/leetcode/theme/default.lua
@@ -33,8 +33,8 @@ M.get = function()
3333 hint = { fg = hl("DiagnosticHint").fg },
3434 error = { fg = hl("DiagnosticError").fg },
3535
36+ normal = { fg = hl("Conceal").fg },
3637 alt = { fg = hl("Comment").fg },
37- normal = { fg = hl("Delimiter").fg },
3838 italic = { fg = hl("Normal").fg, italic = true },
3939 bold = { fg = hl("Normal").fg, bold = true },
4040 underline = { underline = true },
@@ -42,9 +42,12 @@ M.get = function()
4242 code = { fg = hl("Type").fg, bg = hl("Normal").bg },
4343 example = { fg = hl("DiagnosticHint").fg },
4444 constraints = { fg = hl("DiagnosticInfo").fg },
45+ header = { fg = hl("SpecialChar").fg, bold = true },
46+
4547 indent = { fg = hl("Comment").fg },
4648 link = { fg = hl("Function").fg, underline = true },
4749 list = { fg = hl("SpecialChar").fg },
50+ ref = { fg = hl("Tag").fg },
4851 }
4952 end
5053