-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdfile.lua
More file actions
164 lines (158 loc) · 3.67 KB
/
mdfile.lua
File metadata and controls
164 lines (158 loc) · 3.67 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
Class=require("./luagy/grammar/class")
stringx=require("pl.stringx")
if(string.startswith==nil) then
stringx.import()
end
local MDFile=Class(function(self,path)
self.path=path
self.isexist=true
self:init()
end)
function MDFile:init()
infile=io.open(self.path,"rb")
if(infile==nil) then
self.isexist=false
return 0
end
self.filecontant={};
for l in infile:lines() do
l=l:gsub("\n","")
l=l:gsub("\r","")
table.insert( self.filecontant, l );
end
infile:close()
self.filelen=#self.filecontant
self.cur=1
self:getHead()
self:findDirMark()
self:findFileMark()
self:findOpsMark()
self:getDirInfo()
self:getAllDirLine()
self:getAllFileLine()
--[[
self:getDirs()
self:getFiles()
self:getOps()
]]
end
function MDFile:getHead()
firstline=self.filecontant[1]
if(firstline:startswith("---")) then
self.hasHead=true
else
self.hasHead=false
return
end
--todo:完善头部信息解析,目前用不到
--skip head
len=#self.filecontant
self.cur=self.cur+1
while(self.cur<=len)do
if(self.filecontant[self.cur]:startswith("---"))then
self.cur=self.cur+1
self.headend=self.cur-1
return
end
self.cur=self.cur+1
end
self:parseError(cur,"解析头部错误,找不到结束标记")
end
function MDFile:getcurline()
return self.filecontant[self.cur];
end
function MDFile:inc()
self.cur=self.cur+1
end
function MDFile:getDirInfo()
self.dirname=self:getcurline()
self:inc()
self:inc()--skip ==========
self.dirdesc=self:getcurline()
self:inc()
m=0
if(self.hasdirmark) then
m=self.dirmark-2
elseif(self.hasfilemark) then
m=self.filemark-2
elseif(self.hasopsmark) then
m=self.opsmark-2
else
m=self.filelen
end
--todo
--skip dirusage
self.dirusageend=m
self.cur=m+1
end
function MDFile:findline(start,con)
ends=self.filelen
cur=start
while(cur<=ends)do
res=self.filecontant[cur]==con
if(res==true) then
return cur
end
cur=cur+1
end
return -1
end
function MDFile:findDirMark()
res=self:findline(self.cur,"----- -")
if(res>0) then
self.hasdirmark=true
self.dirmark=res
return res
end
return 0
end
function MDFile:findFileMark()
res=self:findline(self.cur,"----- --")
if(res>0) then
self.hasfilemark=true
self.filemark=res
return res
end
return 0
end
function MDFile:findOpsMark()
res=self:findline(self.cur,"----- ---")
if(res>0) then
self.hasopsmark=true
self.opsmark=res
return res
end
return 0
end
function MDFile:getAllDirLine()
self.dirline={}
if(self.hasdirmark ~= true) then
return false
end
endl= (self.hasfilemark and self.filemark-2) or
(self.hasopsmark and self.opsmark-2) or
self.filelen
for i=self.dirmark ,endl do
if(self.filecontant[i]:startswith("*") )then
table.insert(self.dirline,self.filecontant[i])
end
end
end
function MDFile:getAllFileLine()
self.fileline={}
if(self.hasfilemark ~= true) then
return false
end
endl= (self.hasopsmark and self.opsmark-2) or
self.filelen
for i=self.filemark ,endl do
if(self.filecontant[i]:startswith("*") )then
table.insert(self.fileline,self.filecontant[i])
end
end
end
function MDFile:parseError(line,msg)
self.error=true
print("[ERROR]: "..self.path.."["..line.."] : "..msg)
end
return MDFile