irongit

A Neovim plugin enabling you to solve LeetCode problems.

fix: `cn` `&lcpad;`

kawreauthored
parent 8b77a96commit 4a54e80e2fed84b12d4ac56bf4e350d07c65063cBrowse files

5 files changed, +49 -31

+12-2.neoconf.json
@@ -3,13 +3,23 @@
33 "library": {
44 "enabled": true,
55 "plugins": [
6- "nvim-treesitter",
76 "telescope.nvim",
87 "plenary.nvim",
98 "nui.nvim",
109 "nvim-web-devicons",
11- "image.nvim"
10+ "image.nvim",
11+ "nvim-treesitter"
1212 ]
1313 }
14+ },
15+ "neoconf": {
16+ "plugins": {
17+ "lua_ls": {
18+ "enabled": true
19+ }
20+ }
21+ },
22+ "lspconfig": {
23+ "sumneko_lua": {}
1424 }
1525 }
+7-17lua/leetcode-plugins/cn/normalizer.lua
@@ -11,16 +11,10 @@ function Normalizer:cleanup()
1111 :gsub(" ", " ")
1212 :gsub("<(/?)b([^>]*)>", "<%1strong%2>")
1313 :gsub(":", ":")
14- -- :gsub("<=", "&le;")
15- -- :gsub(">", "&le;")
16- -- :gsub("<", "&le;")
17- -- :gsub("> =", "&le;")
18-
19- -- for e, char in pairs(u.entities) do
20- -- self.text = self.text:gsub(e, char)
21- -- end
2214 end
2315
16+-- :gsub("<strong>(提示:%s*)</strong>", "<followup>%1</followup>") -- Note
17+
2418 function Normalizer:tags() --
2519 self.text = self
2620 .text --
@@ -28,25 +22,21 @@ function Normalizer:tags() --
2822 :gsub("<strong>(输出:?%s*)</strong>", "<output>%1</output>") -- output
2923 :gsub("<strong>(解释:?%s*)</strong>", "<explanation>%1</explanation>") -- explanation
3024 :gsub("<strong>(进阶:?%s*)</strong>", "<followup>%1</followup>") -- Note
31- -- :gsub("<strong>(提示:%s*)</strong>", "<followup>%1</followup>") -- Note
32- :gsub(
33- "<strong>(注意:%s*)</strong>",
34- "<followup>%1</followup>"
35- ) -- Followup
25+ :gsub("<strong>(注意:?%s*)</strong>", "<followup>%1</followup>") -- Followup
3626 :gsub(
37- "\n*<p><strong[^>]*>(示例%s*%d*:?)%s*</strong></p>\n*",
27+ "\n*<p>%s*<strong[^>]*>(示例%s*%d*:?)%s*</strong>%s*</p>\n*",
3828 "\n\n<example>󰛨 %1</example>\n\n"
3929 )
4030 :gsub(
41- "\n*<p><strong[^>]*>(提示:?)%s*</strong></p>\n*",
31+ "\n*<p>%s*<strong[^>]*>(提示:?%s*)</strong>%s*</p>\n*",
4232 "\n\n<constraints> %1</constraints>\n\n"
4333 )
4434 :gsub(
45- "\n*<p><strong[^>]*>(限制:?)%s*</strong></p>\n*",
35+ "\n*<p>%s*<strong[^>]*>(限制:?%s*)</strong>%s*</p>\n*",
4636 "\n\n<constraints> %1</constraints>\n\n"
4737 )
4838 :gsub(
49- "\n*<p>%s*<strong[^>]*>(提示:?)%s*</strong>%s*</p>\n*",
39+ "\n*<p>%s*<strong[^>]*>(提示:?%s*)</strong>%s*</p>\n*",
5040 "\n\n<constraints> %1</constraints>\n\n"
5141 )
5242 end
+23-2lua/leetcode-ui/group/tag/init.lua
@@ -1,13 +1,12 @@
11 local theme = require("leetcode.theme")
22 local u = require("leetcode-ui.utils")
3+local ts = vim.treesitter
34
45 local utils = require("leetcode.parser.utils")
56 local Group = require("leetcode-ui.group")
67 local Indent = require("nui.text")
78 local Normalizer = require("leetcode.parser.normalizer")
89
9-local ts = vim.treesitter
10-
1110 local log = require("leetcode.logger")
1211
1312 ---@class lc.ui.Tag : lc.ui.Group
@@ -233,6 +232,7 @@ local LeetTag = Tag
233232
234233 ---@param text string
235234 function Tag.static:parse(text) --
235+ ---@type string
236236 local normalized = Normalizer:norm(text)
237237
238238 local ok, parser = pcall(ts.get_string_parser, normalized, "html")
@@ -243,7 +243,28 @@ function Tag.static:parse(text) --
243243 end
244244
245245 local root = parser:parse()[1]:root()
246+
247+ local query = ts.query.parse(
248+ "html",
249+ [[
250+ (element
251+ (start_tag
252+ (tag_name) @stag (#any-of? @stag "strong" "b"))
253+ (text) @text (#match? @text
254+ "(Input|Output|Explanation|Note|Follow-up):?")
255+ (end_tag
256+ (tag_name) @etag)) @el
257+ ]]
258+ )
259+
260+ for id, capture, md in query:iter_captures(root, normalized) do
261+ log.info(md)
262+ end
263+
246264 return LeetTag(normalized, { spacing = 3, hl = "leetcode_normal" }, root, {})
247265 end
248266
267+-- parser = ts.get_string_parser(normalized, "html")
268+-- root = parser:parse()[1]:root()
269+
249270 return LeetTag
+2-1lua/leetcode-ui/split/description.lua
@@ -135,7 +135,8 @@ function Description:populate()
135135 todo = { "", "leetcode_alt" },
136136 }
137137 if user_status[self.question.cache.status] then
138- header:append(" "):append(table.unpack(user_status[self.question.cache.status]))
138+ local s = user_status[self.question.cache.status]
139+ header:append(" "):append(s[1], s[2])
139140 end
140141
141142 header:append(" | ")
+5-9lua/leetcode/parser/normalizer.lua
@@ -14,6 +14,7 @@ function Normalizer:cleanup() --
1414 :gsub("<br%s*/>", "\n")
1515 :gsub("<meta[^>]*/>", "")
1616 :gsub("&nbsp;", " ")
17+ :gsub("\u{00A0}", " ")
1718 :gsub("<(/?)b([^>]*)>", "<%1strong%2>")
1819 -- :gsub("&#?%w+;", function(e) --
1920 -- return vim.tbl_keys({})
@@ -55,21 +56,16 @@ function Normalizer:tags() --
5556 end
5657
5758 function Normalizer:entities()
59+ log.debug(self.text)
5860 self.text = self
5961 .text --
60- :gsub("\n*<p>%s+</p>\n*", "&lcpad;")
62+ :gsub("\n*<p>%s*</p>\n*", "&lcpad;")
63+ :gsub("\n*<p> *</p>\n*", "&lcpad;")
6164 :gsub("<([^>]+)>(%s*)</%1>", "%2")
6265 :gsub("\n", "&lcnl;")
6366 :gsub("\t", "&lctab;")
6467 :gsub("%s", "&nbsp;")
65- -- :gsub(
66- -- "(%s*)(<[^>]+>)(%s*)",
67- -- function(m1, m2, m3) return m1:gsub(".", "&nbsp;") .. m2 .. m3:gsub(".", "&nbsp;") end
68- -- )
69- :gsub(
70- "<[^>]+>",
71- function(match) return match:gsub("&nbsp;", " ") end
72- )
68+ :gsub("<[^>]+>", function(match) return match:gsub("&nbsp;", " ") end)
7369 end
7470
7571 ---@param text string