FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
librime-lua-script/lua/tools/pattern.lua at main · shewer/librime-lua-script · GitHub
shewer
librime-lua-script
Repository navigation
Code
Issues
4
(4)
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
librime-lua-script
/
lua
/
tools
/
pattern.lua
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
105 lines (93 loc) · 2.5 KB
Breadcrumbs
librime-lua-script
/
lua
/
tools
/
pattern.lua
Copy path
File metadata and controls
executable file
·
105 lines (93 loc) · 2.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
96
97
98
99
100
101
102
103
#!
/usr/bin/env lua
--
--
pfilter.lua
--
Copyright (C) 2020 Shewer Lu <shewer@gmail.com>
--
--
Distributed under terms of the MIT license.
--
--
PFilter extend Filter
--
PFilter obj PFilter:new( pattern_str ,init_status )
--
pattern_str : string 參見 schema xlit xfrom xderive .....
--
init_status : bool
--
return PFilter object
--
method
--
:onn()
--
:off()
--
:toggle()
--
:status()
--
:set_status( bool)
--
:filter(string)
--
--
PFilter.Make_pattern( pattern_str) -- class method
--
return filter func
--
pattern_str : string
--
--
example:
--
PFilter.Make_pattern( "xlit|abcd|ABCD|" )
--
--
require
'
tools/string
'
local
List
=
require
'
tools/list
'
local
function
make_xlit
(
str1
,
str2
)
local
tab1
=
List
(
str1
:
split
(
"
"
))
local
tab2
=
str2
:
split
(
"
"
)
--
如果 字串長度不一 return str str
if
#
tab1
~=
#
tab2
then
return
function
(
str
)
return
str
,
str
end
end
local
strar
=
{}
tab1
:
each_with_index
(
function
(
v
,
i
)
strar
[
v
]
=
tab2
[
i
]
end
)
--
xlit 主要轉檔程式
local
function
xlit_pattern
(
str
)
str
=
str
or
"
"
return
List
(
str
:
split
(
"
"
)):
map
(
function
(
v
)
return
strar
[
v
]
or
v
end
)
:
concat
() ,
str
--
reterun newstr , str
end
return
xlit_pattern
,
false
,
nil
end
local
function
token
(
__str
)
--
local str = ( __str == type(__str) and __str) or ""
if
"
string
"
~=
type
(
__str
)
then
return
"
"
,
"
"
,
"
"
end
local
word
,
chr
,
substr
=
__str
:
gsub
(
"
[$
\\
]([0-9acdlpsuwxz])
"
,
"
%%%1
"
)
:
gsub
(
"
[$
\\
]U
"
,
"
%%u
"
)
:
match
(
"
%s*(%w*)(.)(.*[^%s])%s*
"
)
if
substr
then
--
return word,substr:match("(.*)" .. chr .."(.*)")
local
str1
,
str2
=
table.unpack
(
substr
:
split
(
chr
) )
return
word
,
str1
or
"
"
,
str2
or
"
"
end
return
"
"
,
"
"
,
"
"
end
local
function
make_pattern_func
(
pattern_str
)
local
word
,
str1
,
str2
=
token
(
pattern_str
)
local
func
if
word
==
"
xform
"
then
func
=
function
(
str
)
return
str
:
gsub
(
str1
,
str2
) ,
str
end
elseif
word
==
"
erase
"
then
func
=
function
(
str
)
return
str
:
gsub
(
str1
,
"
"
) ,
str
end
elseif
word
==
"
derive
"
then
func
=
function
(
str
)
return
str
..
"
"
..
str
:
gsub
(
str1
,
str2
),
str
end
elseif
word
==
"
xlit
"
then
func
=
make_xlit
(
str1
,
str2
)
elseif
word
==
"
func
"
then
--
未完成
--
func= loadstring(func_table[str] .."()")
end
if
func
then
return
func
,
false
,
nil
else
return
nil
,
true
,
string.format
(
"
(not match /xlit/xform/erase/derive/func/ ): pattern:%s
"
,
str
)
end
end
return
make_pattern_func
Back
|
FazBrowse Home
|
New Git URL