💾 Archived View for gemini.conman.org › extensions › GLV-1 › handlers › wrap2.lua captured on 2023-09-28 at 19:24:38.

View Raw

More Information

⬅️ Previous capture (2022-06-04)

-=-=-=-=-=-=-

-- ************************************************************************
--
--    Another text wrapping experiment.
--    Copyright 2019 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 handler
-- luacheck: ignore 611 631
-- RFC-3875

local lpeg     = require "lpeg"
local tonumber = tonumber
local ASCII    = [[ !"#$%&'()*+,-./0123456789:;<=>?]]
              .. [[@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_]]
              .. [[`abcdefghijklmnopqrstuvwxyz{|}~]]
              
_ENV = {}

-- ************************************************************************

local parse = lpeg.P";" * (lpeg.R"09"^1 / tonumber) * lpeg.P(-1)
            + lpeg.P(-1) * lpeg.Cc(80)
            
function handler(_,_,_,pathinfo,ios)
  local width = parse:match(pathinfo)
  if not width or width < 1 then
    ios:write("59\r\n")
    return 59
  end
  
  ios:write("20 text/plain\r\n")
  
  for i = 1 , #ASCII do
    local line = ""
    local p    = i
    
    while #line < width do
      if p > #ASCII then p = p - #ASCII end
      line = line .. (ASCII:sub(p,p + width) .. ASCII:sub(1,p - 1)):sub(1,width)
      p    = p + #ASCII
    end
    ios:write(line:sub(1,width),"\r\n")
  end
  
  return 20
end

-- ************************************************************************

return _ENV