💾 Archived View for gemini.conman.org › extensions › GLV-1 › handlers › reflow.lua captured on 2020-09-24 at 01:06:37.
-=-=-=-=-=-=-
-- ************************************************************************ -- -- A reflow experiment, -- Copyright 2020 by Sean Conner. All Rights Reserved. -- -- This program is free software: you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by -- the Free Software Foundation, either version 3 of the License, or -- (at your option) any later version. -- -- This program is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- -- You should have received a copy of the GNU General Public License -- along with this program. If not, see <http://www.gnu.org/licenses/>. -- -- Comments, questions and criticisms can be sent to: sean@conman.org -- -- ************************************************************************ -- luacheck: globals init handler -- luacheck: ignore 611 local wrapt = require "org.conman.string".wrapt local syslog = require "org.conman.syslog" local MSG = require "GLV-1/MSG" local io = require "io" local string = require "string" local table = require "table" local ipairs = ipairs local tonumber = tonumber _ENV = {} -- ************************************************************************ function init(conf) if not conf.file then return false,"Missing file directive" end return true end -- ************************************************************************ function handler(conf,_,_,match) local width = tonumber(match[1]) or 80 if width < 10 then return 59,MSG[59],"" end local file,err = io.open(conf.file,"r") if not file then syslog('error',"%s = %s",conf.file,err) return 40,MSG[40],"" end local literal = false local doc = {} for line in file:lines() do if line:match "^%#" then local hdr,title = line:match("^(%#+)%s*(.*)") local text = wrapt(title,width - (#hdr + 1)) table.insert(doc,string.format("%s %s",hdr,text[1])) for i = 2 , #text do table.insert(doc,string.format("%s %s",string.rep(" ",#hdr),text[i])) end elseif line:match "^```$" then literal = not literal elseif line:match "^%=%>" then local url,text = line:match("^%=%>%s*(%S+)%s*(.*)") if text == "" then text = url end table.insert(doc,string.format("=> %s",text:sub(1,width - 3))) else if literal then table.insert(doc,line:sub(1,width)) else local text = wrapt(line,width) for _,l in ipairs(text) do table.insert(doc,l) end end end end return 20,"text/plain",table.concat(doc,"\r\n") .. "\r\n" end -- ************************************************************************ return _ENV