From 25ad72f8e99ed2d4b423a72e3f798097f79f345f Mon Sep 17 00:00:00 2001 From: Reuh Date: Sat, 10 Dec 2016 21:38:24 +0100 Subject: [PATCH] Partial syntax per-paste setting, fixed problems --- changelog.txt | 1 + vrel.lua | 21 +++++++++++---------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/changelog.txt b/changelog.txt index 94c026f..9d52c0a 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,6 +1,7 @@ vrel 0.1.5 (wip): - Doubled the default maximum lifetime (3 months -> 6 months). - Added an optional configuration file. + - Added syntax setting per-paste (wip). - Various luacheck cleaning (a few potential bugs are fixed). It's still complaining about legit things tho. vrel 0.1.4: - Now stores the sender's IP diff --git a/vrel.lua b/vrel.lua index dcbcecc..19a1c87 100644 --- a/vrel.lua +++ b/vrel.lua @@ -132,20 +132,20 @@ httpd = { } -- Vrel -- -- Load data -local data = {} -- { ["name"] = { expire = os.time()+lifetime, burnOnRead = false, senderId = "someuniqueidentifier", data = "Hello\nWorld" } } +local data = {} -- { ["name"] = { expire = os.time()+lifetime, burnOnRead = false, senderId = "someuniqueidentifier", syntax = "lua", data = "Hello\nWorld" } } local sqliteAvailable, sqlite3 = pcall(require, "lsqlite3") if sqliteAvailable then httpd.log("Using SQlite3 storage backend") -- SQlite backend local db = sqlite3.open("database.sqlite3") - db:exec("CREATE TABLE IF NOT EXISTS data (name STRING PRIMARY KEY NOT NULL, expire INTEGER NOT NULL, burnOnRead INTEGER NOT NULL, senderId STRING NOT NULL, data STRING NOT NULL)") + db:exec("CREATE TABLE IF NOT EXISTS data (name STRING PRIMARY KEY NOT NULL UNIQUE, expire INTEGER NOT NULL, burnOnRead INTEGER NOT NULL DEFAULT 0, senderId STRING NOT NULL, syntax STRING NOT NULL DEFAULT 'text', data STRING NOT NULL)") setmetatable(data, { __index = function(self, key) -- data[name]: get paste { expire = integer, burnOnRead = boolean, data = string } - local stmt = db:prepare("SELECT expire, burnOnRead, senderId, data FROM data WHERE name = ?") stmt:bind_values(key) + local stmt = db:prepare("SELECT expire, burnOnRead, senderId, syntax, data FROM data WHERE name = ?") stmt:bind_values(key) local r for row in stmt:nrows() do r = row r.burnOnRead = r.burnOnRead == 1 break end stmt:finalize() return r end, __newindex = function(self, key, value) - if value ~= nil then -- data[name] = { expire = integer, burnOnRead = boolean, data = string }: add paste - local stmt = db:prepare("INSERT INTO data VALUES (?, ?, ?, ?, ?)") stmt:bind_values(key, value.expire, value.burnOnRead, value.senderId, value.data) + if value ~= nil then -- data[name] = { expire = integer, burnOnRead = boolean, syntax = string, data = string }: add paste + local stmt = db:prepare("INSERT INTO data VALUES (?, ?, ?, ?, ?, ?)") stmt:bind_values(key, value.expire, value.burnOnRead, value.senderId, value.syntax, value.data) stmt:step() stmt:finalize() else -- data[name] = nil: delete paste local stmt = db:prepare("DELETE FROM data WHERE name = ?") stmt:bind_values(key) @@ -197,22 +197,23 @@ local function post(paste, request) clean() -- add a paste, will check data and paste.expire = math.min(tonumber(paste.expire) or os.time()+defaultLifetime, os.time()+maxLifetime) paste.burnOnRead = paste.burnOnRead == true paste.senderId = paste.senderId or request.client:getpeername() or "0.0.0.0" + paste.syntax = (paste.syntax or "text"):lower():match("[a-z]*") paste.data = tostring(paste.data) data[name] = paste return name, data[name] end local pygmentsStyle, extraStyle = "monokai", "*{color:#F8F8F2;background-color:#272822;margin:0px;}pre{color:#8D8D8A;}" -- pygments style name, extra css for highlighted blocks (also aply if no pygments) -local function highlight(code, lexer) -- Syntax highlighting; should returns the code block, style and everything included +local function highlight(paste, forceLexer) -- Syntax highlighting; should returns the code block, style and everything included local source = assert(io.open("pygmentize.tmp", "w")) -- Lua can't at the same time write an read from a command, so we need to put one in a file - source:write(code) source:close() - local pygments = assert(io.popen("pygmentize -f html -O linenos=table,style="..pygmentsStyle.." -l "..lexer.." pygmentize.tmp", "r")) + source:write(paste.data) source:close() + local pygments = assert(io.popen("pygmentize -f html -O linenos=table,style="..pygmentsStyle.." -l "..(forceLexer or paste.syntax).." pygmentize.tmp", "r")) local out = assert(pygments:read("*a")) pygments:close() if #out > 0 then -- if pygments available and available lexer (returned something) local style = assert(io.popen("pygmentize -f html -S "..pygmentsStyle, "r")) -- get style data out = out.."" style:close() return out -- no highlighter available, put in
 and escape
-	else return "
"..code:gsub("([\"&<>])",{["\""]=""",["&"]="&",["<"]="<",[">"]=">"}).."
" end + else return "
"..paste.data:gsub("([\"&<>])",{["\""]=""",["&"]="&",["<"]="<",[">"]=">"}).."
" end end -- Start! httpd.start(config.address or "*", config.port or 8155, { -- Pages @@ -241,7 +242,7 @@ httpd.start(config.address or "*", config.port or 8155, { -- Pages
expires in hours (burn on read) vrel
- ]] or highlight((get(name:match("^[^.]+"), request) or {data="paste not found"}).data, name:match("%.(.+)$") or "lua"))..[[ + ]] or highlight(get(name:match("^[^.]+"), request) or {data="paste not found"}, name:match("%.([a-zA-Z]+)$"):lower()))..[[ ]] } end, ["/g/(.+)"] = function(request, name) local d = get(name, request) return d and { "200 OK", {["Content-Type"] = "text"}, d.data } or nil end,