irongit

A Neovim plugin enabling you to solve LeetCode problems.

fix: cleanup

kawreauthored
parent 67fe948commit 90ba5218e90dc9d9b9d0c2020307960ca963c4f3Browse files

3 files changed, +25 -35

+17-8lua/leetcode/parser/html.lua
@@ -112,7 +112,8 @@ function Html:handle_list(tags)
112112 local li_type = get_list_type()
113113 local li_count = vim.fn.count(tags, "li")
114114 local leftpad = string.rep("\t", li_count)
115- local li_icons = { "", "", "" }
115+ -- local li_icons = { "", "", "" }
116+ local li_icons = { "*", "-", "+" }
116117
117118 local text
118119 if li_type == "ul" then
@@ -152,7 +153,6 @@ function Html:handle_link(text, tag_data)
152153
153154 link = href.value or ""
154155 elseif tag == "img" then
155- log.info(tag_data.attrs)
156156 local alt = vim.tbl_filter(function(attr)
157157 if attr.name == "alt" then return attr end
158158 end, tag_data.attrs)[1] or {}
@@ -248,28 +248,38 @@ end
248248 function Html:normalize()
249249 log.debug(self.str)
250250
251- self.str = self.str
251+ self.str = self
252+ .str
252253 :gsub("​", "")
253254 :gsub("\r\n", "\n")
254255 :gsub("\t*<(/?li)>", "<%1>")
255256 :gsub("\t*<(/?ul)>", "<%1>")
256257 :gsub("\t*<(/?ol)>", "<%1>")
258+ -- :gsub(
259+ -- "\t*<(li[^>]*)>(.-)\t*</(li)>",
260+ -- "<%1>%2</%3>\n"
261+ -- )
262+ -- :gsub("\t*<(ul[^>]*)>(.-)\t*</(ul)>", "<%1>%2</%3>\n")
263+ -- :gsub("\t*<(ol[^>]*)>(.-)\t*</(ol)>", "<%1>%2</%3>\n")
257264 :gsub(
258- "<p><strong[^>]*>(Example%s*%d+:)</strong></p>(\n*)",
265+ "<p><strong[^>]*>(Example%s*%d*:)%s*</strong></p>\n*",
259266 "\n\n<example>󰛨 %1</example>\n\n"
260267 )
261268 :gsub(
262- "<p><strong[^>]*>(Constraints:)</strong></p>(\n*)",
269+ "<p><strong[^>]*>(Constraints:)%s*</strong></p>\n*",
263270 "\n\n<constraints> %1</constraints>\n\n"
264271 )
265- :gsub("<img([^>]*)/>\n*", "<img%1>img</img>")
272+ :gsub("<(img[^>]*)/>\n*", "<%1>img</img>")
266273 :gsub("<pre>\n*(.-)\n*</pre>", "<pre>\n%1</pre>")
267274 :gsub("\n*<p>&nbsp;</p>\n*", "&lcpad;")
268275 :gsub("\n", "&lcnl;")
269276 :gsub("\t", "&lctab;")
270277 :gsub("%s", "&nbsp;")
271278 :gsub("<[^>]*>", function(match) return match:gsub("&nbsp;", " ") end)
272- :gsub("<a[^>]*>(.-)</a>", function(match) return match:gsub("&nbsp;", " ") end) .. "&lcend;"
279+ :gsub("<a[^>]*>(.-)</a>", function(match)
280+ match = match:gsub("&#?%w+;", utils.entity)
281+ return match:gsub("&nbsp;", " ")
282+ end) .. "&lcend;"
273283 end
274284
275285 -- Trim excessive lines
@@ -296,7 +306,6 @@ function Html:parse(html)
296306 newline_count = 0,
297307 ol_count = {},
298308 }, self)
299- log.debug("siema")
300309
301310 self:normalize()
302311 local ok, parser = pcall(self.ts.get_string_parser, self.str, "html")
+1-1lua/leetcode/theme/default.lua
@@ -41,7 +41,7 @@ M.get = function()
4141 constraints = { fg = hl("DiagnosticInfo").fg },
4242 indent = { fg = hl("Comment").fg },
4343 link = { fg = hl("Function").fg, underline = true },
44- list = { fg = hl("Tag").fg },
44+ list = { fg = hl("Statement").fg },
4545 }
4646 end
4747
+7-26lua/leetcode/ui/question.lua
@@ -32,21 +32,21 @@ end
3232
3333 function Question:get_snippet()
3434 local snippets = self.q.code_snippets ~= vim.NIL and self.q.code_snippets or {}
35- return vim.tbl_filter(function(snip) return snip.lang_slug == self.lang end, snippets)[1] or {}
35+ return vim.tbl_filter(function(snip) return snip.lang_slug == self.lang end, snippets)[1]
3636 end
3737
3838 ---@private
3939 function Question:create_file()
4040 local lang = utils.get_lang(is_sql(self.q) and config.sql or config.lang)
41- local suffix = lang.sql and "-" .. lang.short or ""
41+ local suffix = lang.sql and string.format("-%s", lang.short) or ""
4242 local fn = string.format("%s.%s%s.%s", self.q.frontend_id, self.q.title_slug, suffix, lang.ft)
4343
4444 self.file = config.home:joinpath(fn)
45+ if not self.file:exists() then self.file:write(self:get_snippet().code, "w") end
4546 end
4647
4748 function Question:mount()
4849 self:create_file()
49- if not self.file:exists() then self.file:write(self:get_snippet().code, "w") end
5050
5151 vim.api.nvim_set_current_dir(config.home:absolute())
5252 vim.cmd("$tabe " .. self.file:absolute())
@@ -59,30 +59,9 @@ function Question:mount()
5959 self.console = Console:init(self)
6060 self.hints = Hints:init(self)
6161
62- self:autocmds()
6362 return self
6463 end
6564
66-function Question:autocmds()
67- -- local group_id = vim.api.nvim_create_augroup("leetcode_questions", { clear = true })
68-
69- -- local q_group_id = vim.api.nvim_create_augroup("leetcode_question", {})
70- -- vim.api.nvim_create_autocmd("BufWinEnter", {
71- -- group = q_group_id,
72- -- buffer = self.bufnr,
73- -- callback = function()
74- -- log.info("wtf")
75- -- self.description.split:show()
76- -- end,
77- -- })
78- --
79- -- vim.api.nvim_create_autocmd("BufWinLeave", {
80- -- group = q_group_id,
81- -- buffer = self.bufnr,
82- -- callback = function() self.description.split:hide() end,
83- -- })
84-end
85-
8665 function Question:handle_mount()
8766 if self:get_snippet() then
8867 self:mount()
@@ -100,6 +79,8 @@ end
10079
10180 ---@param problem lc.Cache.Question
10281 function Question:init(problem)
82+ log.debug("Initializing question: " .. problem.frontend_id .. ". " .. problem.title_slug)
83+
10384 local tabp = utils.detect_duplicate_question(problem.title_slug)
10485 if tabp then return pcall(vim.cmd.tabnext, tabp) end
10586
@@ -110,13 +91,13 @@ function Question:init(problem)
11091
11192 local lang = utils.get_lang(is_sql(q) and config.sql or config.lang)
11293
113- local obj = setmetatable({
94+ self = setmetatable({
11495 q = q,
11596 lang = lang.slug,
11697 cache = problem,
11798 }, self)
11899
119- return obj:handle_mount()
100+ return self:handle_mount()
120101 end
121102
122103 return Question