-
Notifications
You must be signed in to change notification settings - Fork 10
/
t-webfilter.lua
105 lines (78 loc) · 2.81 KB
/
t-webfilter.lua
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
thirddata = thirddata or {}
thirddata.webfilter = thirddata.webfilter or {}
local webfilter = thirddata.webfilter
local http = require("socket.http")
local ltn12 = require("ltn12")
local url = require("socket.url")
local report_webfilter = logs.new("thirddata.webfilter")
local trace_webfilter = false
trackers.register("thirddata.webfilter", function(v) trace_webfilter = v end)
local match = string.match
local gsub = string.gsub
local tinsert = table.insert
local tconcat = table.concat
function webfilter.processwebfilter(name, transform, prefix, suffix, figuresetup)
local content = webfilter.transform[transform](name)
local url = prefix .. content .. suffix
if trace_webfilter then
report_webfilter("downloading url %s", url)
end
local specification = resolvers.splitmethod(url)
local file = resolvers.finders['http'](specification) or ""
if trace_webfilter then
if file and file ~= "" then
report_webfilter("saving file %s", file)
else
report_webfilter("download failed")
end
end
context.externalfigure({file}, {figuresetup})
end
-- Useful data transformation
thirddata.webfilter.transform = {}
-- The default transformation:
-- Dedent the buffer and remove empty lines
local function default(name, separator)
if trace_webfilter then
report_webfilter("joining %s buffer with separator %s", name, separator or "no")
end
local content = buffers.getcontent(name)
local lines = string.splitlines(content)
local indent = match(lines[1], '^ +') or ''
local result = {}
local pattern = '^' .. indent
for i=1,#lines do
lines[i] = gsub(lines[i],pattern,"")
-- remove empty lines
if gsub(lines[i], '^%s*$', '') ~= "" then
tinsert(result,lines[i])
end
end
content = tconcat(result,separator or ",")
return content
end
local ampersand = function(s) return default(s, "&") end
-- Transform for http://www.websequence.com
local function websequence (name)
local content = buffers.getcontent(name)
local style = "modern-blue" -- TODO: make configurable
local body = "style=" .. style .. "&message=" .. url.escape(content)
local response = {}
local status, message = http.request {
method = 'POST',
url = "http://www.websequencediagrams.com",
source = ltn12.source.string(body),
sink = ltn12.sink.table(response),
headers = {
["Content-Length"] = string.len(body),
},
}
local quot = lpeg.P('"')
local other = 1 - quot
local img = other^0 * quot * lpeg.Cs(other^0) * quot * other
local location = lpeg.match(img,response[1])
return location
end
thirddata.webfilter.transform['default'] = default
thirddata.webfilter.transform['ampersand'] = ampersand
thirddata.webfilter.transform['websequence'] = websequence