-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathnormalizer.lua
More file actions
95 lines (82 loc) · 2.73 KB
/
Copy pathnormalizer.lua
File metadata and controls
95 lines (82 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
local Object = require("nui.object")
local u = require("leetcode.parser.utils")
local log = require("leetcode.logger")
---@class lc.Normalizer
local Normalizer = Object("LeetNormalizer")
function Normalizer:cleanup() --
self.text = self
.text --
:gsub("", "")
:gsub("\r\n", "\n")
:gsub("<br%s*/>", "\n")
:gsub("<meta[^>]*/>", "")
:gsub(" ", " ")
-- :gsub("<(/?)b([^>]*)>", "<%1strong%2>")
:gsub("<(/?)b>", "<%1strong>")
-- :gsub("&#?%w+;", function(e) --
-- return vim.tbl_keys({})
-- end)
-- for e, char in pairs(u.entities) do
-- self.text = self.text:gsub(e, char)
-- end
end
function Normalizer:fix_indent()
self.text = self
.text --
:gsub("(\n+)(\t+)", "%1")
:gsub("<(/?li)>\n*", "<%1>\n\n")
:gsub("\n*(<ul[^>]*>)\n*", "\n\n%1\n")
:gsub("\n*(<ol[^>]*>)\n*", "\n\n%1\n")
:gsub("\n*(<pre[^>]*>)", "\n\n%1\n")
:gsub("\n*(<table[^>]*>)\n*", "\n\n%1\n")
:gsub("\n*(</table>)\n*", "\n%1\n\n")
:gsub("\n*(<img[^>]*/?>)\n*", "\n\n%1\n\n")
end
function Normalizer:tags() --
self.text = self
.text --
:gsub("<strong>(Input:?%s*)</strong>", "<input>%1</input>")
:gsub("<strong>(Output:?%s*)</strong>", "<output>%1</output>")
:gsub("<strong>(Explanation:?%s*)</strong>", "<explanation>%1</explanation>")
:gsub("<strong>(Note:?%s*)</strong>", "<explanation>%1</explanation>")
:gsub("<strong>(Follow-up:?%s*)</strong>", "<followup>%1</followup>")
:gsub("<strong>(Follow up:?%s*)</strong>", "<followup>%1</followup>")
:gsub(
"\n*<p>%s*<strong[^>]*>(Example%s*%d*:?)%s*</strong>%s*</p>\n*",
"\n\n<example> %1</example>\n\n"
)
:gsub(
"\n*<p>%s*<strong[^>]*>(Constraints:?)%s*</strong>%s*</p>\n*",
"\n\n<constraints> %1</constraints>\n\n"
)
end
function Normalizer:entities()
self.text = self
.text --
:gsub("\n*<p>%s*</p>\n*", "&lcpad;")
:gsub("\n*<p> *</p>\n*", "&lcpad;")
:gsub("<([^>]+)>(%s*)</%1>", "%2")
:gsub("\n", "&lcnl;")
:gsub("\t", "&lctab;")
:gsub("%s", " ")
:gsub("<[^>]+>", function(match)
return match:gsub(" ", " ")
end)
end
---@param text string
function Normalizer:init(text) --
log.debug(text)
self.text = text
self:cleanup()
self:fix_indent()
self:tags()
self:entities()
log.debug(self.text:gsub("&lcnl;", "\n"), false)
end
---@type fun(text: string): lc.Normalizer
local LeetNormalizer = Normalizer
---@param text string
function Normalizer:norm(text)
return LeetNormalizer(text).text
end
return LeetNormalizer