-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDefaultStructure.lua
More file actions
273 lines (238 loc) · 7.1 KB
/
DefaultStructure.lua
File metadata and controls
273 lines (238 loc) · 7.1 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
-- {"id":-1,"ver":"1.0.0","libVer":"1.0.0","author":"","repo":"","dep":["foo","bar"]}
--- Identification number of the extension.
--- Should be unique. Should be consistent in all references.
---
--- Required.
---
--- @type int
local id = -1
--- Name of extension to display to the user.
--- Should match index.
---
--- Required.
---
--- @type string
local name = "Example"
--- Base URL of the extension. Used to open web view in Shosetsu.
---
--- Required.
---
--- @type string
local baseURL = "https://example.web/"
--- URL of the logo.
---
--- Optional, Default is empty.
---
--- @type string
local imageURL = "https://example.web/asset/logo.png"
--- Shosetsu tries to handle cloudflare protection if this is set to true.
---
--- Optional, Default is false.
---
--- @type boolean
local hasCloudFlare = false
--- If the website has search.
---
--- Optional, Default is true.
---
--- @type boolean
local hasSearch = true
--- If the websites search increments or not.
---
--- Optional, Default is true.
---
--- @type boolean
local isSearchIncrementing = true
--- Filters to display via the filter fab in Shosetsu.
---
--- Optional, Default is none.
---
--- @type Filter[] | Array
local searchFilters = {
TextFilter(5, "RANDOM STRING INPUT"),
SwitchFilter(6, "RANDOM SWITCH INPUT"),
CheckboxFilter(7, "RANDOM CHECKBOX INPUT"),
TriStateFilter(8, "RANDOM TRISTATE CHECKBOX INPUT"),
RadioGroupFilter(9, "RANDOM RGROUP INPUT", { "A", "B", "C" }),
DropdownFilter(10, "RANDOM DDOWN INPUT", { "A", "B", "C" })
}
--- Internal settings store.
---
--- Completely optional.
--- But required if you want to save results from [updateSetting].
---
--- Notice, each key is surrounded by "[]" and the value is on the right side.
--- @type table
local settings = {
[1] = "test",
[2] = false,
[3] = false,
[4] = 2,
[5] = "A",
[6] = "B"
}
--- Settings model for Shosetsu to render.
---
--- Optional, Default is empty.
---
--- @type Filter[] | Array
local settingsModel = {
TextFilter(1, "RANDOM STRING INPUT"),
SwitchFilter(2, "RANDOM SWITCH INPUT"),
CheckboxFilter(3, "RANDOM CHECKBOX INPUT"),
TriStateFilter(4, "RANDOM TRISTATE CHECKBOX INPUT"),
RadioGroupFilter(5, "RANDOM RGROUP INPUT", { "A", "B", "C" }),
DropdownFilter(6, "RANDOM DDOWN INPUT", { "A", "B", "C" })
}
--- ChapterType provided by the extension.
---
--- Optional, Default is STRING. But please do HTML.
---
--- @type ChapterType
local chapterType = ChapterType.HTML
--- Index that pages start with. For example, the first page of search is index 1.
---
--- Optional, Default is 1.
---
--- @type number
local startIndex = 1
--- Listings that users can navigate in Shosetsu.
---
--- Required, 1 value at minimum.
---
--- @type Listing[] | Array
local listings = {
Listing("Something", false, function(data)
-- Many sites use the baseURL + some path, you can perform the URL construction here.
-- You can also extract query data from [data]. But do perform a null check, for safety.
local url = baseURL
local document = GETDocument(url)
return {}
end),
Listing("Something (with incrementing pages!)", true, function(data)
--- @type int
local page = data[PAGE]
-- Previous documentation, + appending page
local url = baseURL .. "?p=" .. page
local document = GETDocument(url)
return {}
end),
Listing("Something without any input", false, function()
-- Previous documentation, except no data or appending.
local url = baseURL
local document = GETDocument(url)
return {}
end)
}
--- Shrink the website url down. This is for space saving purposes.
---
--- Required.
---
--- @param url string Full URL to shrink.
--- @param type int Either KEY_CHAPTER_URL or KEY_NOVEL_URL.
--- @return string Shrunk URL.
local function shrinkURL(url, type)
-- Currently the two branches are the same.
-- You can simplify this to just a return with a single substitution.
-- But some websites separate novels & chapters.
-- So a novel is URL/novel/12345,
-- And a chapter is URL/chapter/12345.
-- Thus you would then program two substitutions, one to remove URL/novel/,
-- and one to remove URL/chapter/
if type == KEY_NOVEL_URL then
return url:gsub(".-example%.web", "")
else
return url:gsub(".-example%.web", "")
end
end
--- Expand a given URL.
---
--- Required.
---
--- @param url string Shrunk URL to expand.
--- @param type int Either KEY_CHAPTER_URL or KEY_NOVEL_URL.
--- @return string Full URL.
local function expandURL(url, type)
-- Currently the two branches are the same.
-- Read [shrinkURL] documentation in regards to what you should do.
-- Hint, this is the opposite.
if type == KEY_NOVEL_URL then
return baseURL .. url
else
return baseURL .. url
end
end
--- Get a chapter passage based on its chapterURL.
---
--- Required.
---
--- @param chapterURL string The chapters shrunken URL.
--- @return string Strings in lua are byte arrays. If you are not outputting strings/html you can return a binary stream.
local function getPassage(chapterURL)
local url = expandURL(chapterURL, KEY_CHAPTER_URL)
--- Chapter page, extract info from it.
local document = GETDocument(url)
return ""
end
--- Load info on a novel.
---
--- Required.
---
--- @param novelURL string shrunken novel url.
--- @return NovelInfo
local function parseNovel(novelURL)
local url = shrinkURL(novelURL, KEY_NOVEL_URL)
--- Novel page, extract info from it.
local document = GETDocument(url)
return NovelInfo()
end
--- Called to search for novels off a website.
---
--- Optional, But required if [hasSearch] is true.
---
--- @param data table @of applied filter values [QUERY] is the search query, may be empty.
--- @return Novel[] | Array
local function search(data)
--- Not required if search is not incrementing.
--- @type int
local page = data[PAGE]
--- Get the user text query to pass through.
--- @type string
local query = data[QUERY]
return {}
end
--- Called when a user changes a setting and when the extension is being initialized.
---
--- Optional, But required if [settingsModel] is not empty.
---
--- @param id int Setting key as stated in [settingsModel].
--- @param value any Value pertaining to the type of setting. Int/Boolean/String.
--- @return void
local function updateSetting(id, value)
settings[id] = value
end
-- Return all properties in a lua table.
return {
-- Required
id = id,
name = name,
baseURL = baseURL,
listings = listings, -- Must have at least one listing
getPassage = getPassage,
parseNovel = parseNovel,
shrinkURL = shrinkURL,
expandURL = expandURL,
-- Optional values to change
imageURL = imageURL,
hasCloudFlare = hasCloudFlare,
hasSearch = hasSearch,
isSearchIncrementing = isSearchIncrementing,
searchFilters = searchFilters,
settings = settingsModel,
chapterType = chapterType,
startIndex = startIndex,
-- Required if [hasSearch] is true.
search = search,
-- Required if [settings] is not empty
updateSetting = updateSetting,
}