| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| local gsub = string.gsub |
| local concat,append = table.concat,table.insert |
| local utils = require 'pl.utils' |
| local bind1,usplit,assert_arg = utils.bind1,utils.split,utils.assert_arg |
| local is_callable = require 'pl.types'.is_callable |
| local unpack = utils.unpack |
|
|
| local function makelist(l) |
| return setmetatable(l, require('pl.List')) |
| end |
|
|
| local function lstrip(str) return (str:gsub('^%s+','')) end |
| local function strip(str) return (lstrip(str):gsub('%s+$','')) end |
| local function split(s,delim) return makelist(usplit(s,delim)) end |
|
|
| local function imap(f,t,...) |
| local res = {} |
| for i = 1,#t do res[i] = f(t[i],...) end |
| return res |
| end |
|
|
| |
| |
| |
|
|
| local text = {} |
|
|
| local function _indent (s,sp) |
| local sl = split(s,'\n') |
| return concat(imap(bind1('..',sp),sl),'\n')..'\n' |
| end |
|
|
| |
| |
| |
| |
| |
| function text.indent (s,n,ch) |
| assert_arg(1,s,'string') |
| assert_arg(2,n,'number') |
| return _indent(s,string.rep(ch or ' ',n)) |
| end |
|
|
| |
| |
| |
| |
| function text.dedent (s) |
| assert_arg(1,s,'string') |
| local sl = split(s,'\n') |
| local i1,i2 = (#sl>0 and sl[1] or ''):find('^%s*') |
| sl = imap(string.sub,sl,i2+1) |
| return concat(sl,'\n')..'\n' |
| end |
|
|
| |
| |
| |
| |
| |
| |
| function text.wrap (s,width) |
| assert_arg(1,s,'string') |
| width = width or 70 |
| s = s:gsub('\n',' ') |
| local i,nxt = 1 |
| local lines,line = {} |
| while i < #s do |
| nxt = i+width |
| if s:find("[%w']",nxt) then |
| nxt = s:find('%W',nxt+1) |
| end |
| line = s:sub(i,nxt) |
| i = i + #line |
| append(lines,strip(line)) |
| end |
| return makelist(lines) |
| end |
|
|
| |
| |
| |
| |
| |
| function text.fill (s,width) |
| return concat(text.wrap(s,width),'\n') .. '\n' |
| end |
|
|
| local Template = {} |
| text.Template = Template |
| Template.__index = Template |
| setmetatable(Template, { |
| __call = function(obj,tmpl) |
| return Template.new(tmpl) |
| end}) |
|
|
| function Template.new(tmpl) |
| assert_arg(1,tmpl,'string') |
| local res = {} |
| res.tmpl = tmpl |
| setmetatable(res,Template) |
| return res |
| end |
|
|
| local function _substitute(s,tbl,safe) |
| local subst |
| if is_callable(tbl) then |
| subst = tbl |
| else |
| function subst(f) |
| local s = tbl[f] |
| if not s then |
| if safe then |
| return f |
| else |
| error("not present in table "..f) |
| end |
| else |
| return s |
| end |
| end |
| end |
| local res = gsub(s,'%${([%w_]+)}',subst) |
| return (gsub(res,'%$([%w_]+)',subst)) |
| end |
|
|
| |
| |
| |
| function Template:substitute(tbl) |
| assert_arg(1,tbl,'table') |
| return _substitute(self.tmpl,tbl,false) |
| end |
|
|
| |
| |
| |
| function Template:safe_substitute(tbl) |
| assert_arg(1,tbl,'table') |
| return _substitute(self.tmpl,tbl,true) |
| end |
|
|
| |
| |
| |
| |
| |
| |
| function Template:indent_substitute(tbl) |
| assert_arg(1,tbl,'table') |
| if not self.strings then |
| self.strings = split(self.tmpl,'\n') |
| end |
| |
| |
| |
| local function subst(line) |
| return line:gsub('(%s*)%$([%w_]+)',function(sp,f) |
| local subtmpl |
| local s = tbl[f] |
| if not s then error("not present in table "..f) end |
| if getmetatable(s) == Template then |
| subtmpl = s |
| s = s.tmpl |
| else |
| s = tostring(s) |
| end |
| if s:find '\n' then |
| s = _indent(s,sp) |
| end |
| if subtmpl then return _substitute(s,tbl) |
| else return s |
| end |
| end) |
| end |
| local lines = imap(subst,self.strings) |
| return concat(lines,'\n')..'\n' |
| end |
|
|
| |
| |
|
|
| function text.format_operator() |
|
|
| local format = string.format |
|
|
| |
| |
| local function formatx (fmt,...) |
| local args = {...} |
| local i = 1 |
| for p in fmt:gmatch('%%.') do |
| if p == '%s' and type(args[i]) ~= 'string' then |
| args[i] = tostring(args[i]) |
| end |
| i = i + 1 |
| end |
| return format(fmt,unpack(args)) |
| end |
|
|
| local function basic_subst(s,t) |
| return (s:gsub('%$([%w_]+)',t)) |
| end |
|
|
| |
| |
| |
| |
| |
| |
| getmetatable("").__mod = function(a, b) |
| if b == nil then |
| return a |
| elseif type(b) == "table" and getmetatable(b) == nil then |
| if #b == 0 then |
| return _substitute(a,b,true) |
| else |
| return formatx(a,unpack(b)) |
| end |
| elseif type(b) == 'function' then |
| return basic_subst(a,b) |
| else |
| return formatx(a,b) |
| end |
| end |
| end |
|
|
| return text |
|
|