|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- By Edrx, 2007dec27
-- (find-es "blogme" "parsechangelog-tests")
-- (find-angg "LUA/parsechangelog.lua")
-- (find-es "lua5" "lpeg-quickref")
-- (find-blogme3 "escripts.lua")
loadlpeg()
LF = lpeg.P("\n") -- todo: add support for \r and \r\n
CommentChar = lpeg.S("#")
SpaceChar = lpeg.S( " \t")
HeadChar = 1 - lpeg.S("\r\n# \t")
LineChar = 1 - lpeg.S("\r\n")
NonSpaceChar = 1 - lpeg.S("\r\n \t")
HeadLine = HeadChar * LineChar^0 * LF
CommentLine = CommentChar * LineChar^0 * LF
NonHeadLine = SpaceChar^1 * LineChar^0 * LF
EmptyLine = SpaceChar^0 * LF
Field = HeadLine * (EmptyLine^0 * NonHeadLine)^0
Separator = (EmptyLine + CommentLine)^0
SeparatorT = Separator:Cs()
FieldsT = (Field:C()^1):Ct()
-- For tests, from:
-- http://zumbi/~silas/_/tmp/pr
-- http://zumbi/~silas/_/tmp/pr.html
-- (find-fline "~/tmp/Changelog")
-- (find-es "blogme" "parsechangelog-tests")
--
fields = {
"title: aplicar filtro etc\n",
"state: open\n",
"responsible: andre.luiz@\n",
"class: feature-request\n",
"release: ihmtelm-0-9\n",
"arrival date: 2007dez21\n",
}
-- Iterate over fieldname/fieldvalue pairs.
-- (find-angg "LUA/lua50init.lua" "each2")
-- Note: I'm stripping the spaces after the ":" - is that right?
--
prfields = function (fields)
local i = 1
return function ()
if i <= #fields then
local fieldname, body = fields[i]:match"^([^:]+):%s*(.*)\n$"
i = i + 1
return fieldname, body
end
end
end
-- "#fields - 1" because the last field is always "egnahc" (like a "fi")
changelogfields = function (fields)
local i = 1
return function ()
if i <= #fields - 1 then
local fieldname, body = fields[i]:match"^(%S+)%s*(.*)\n$"
i = i + 1
return fieldname, body
end
end
end
fieldsandvalues = function (fields, iterateoverfields)
local fieldnames = {}
local fieldvalues = {}
for fieldname, fieldvalue in iterateoverfields(fields) do
tinsert(fieldnames, fieldname)
tinsert(fieldvalues, fieldvalue)
end
return fieldnames, fieldvalues
end
prfieldsandvalues = function (fields)
return fieldsandvalues(fields, prfields)
end
changelogfieldsandvalues = function (fields)
return fieldsandvalues(fields, changelogfields)
end
sqlizefieldname = function (fieldname)
return (fieldname:gsub(" ", "_"))
end
sqlizefieldvalue = function (fieldvalue)
return "'"..fieldvalue:gsub("'", "''").."'"
end
sqlizefields = function (tablename, fieldnames, fieldvalues)
local fns = table.concat(map(sqlizefieldname, fieldnames), ", ")
local fvs = table.concat(map(sqlizefieldvalue, fieldvalues), ",\n ")
return format("insert into tbl1 (%s)\n values (\n %s\n );\n", fns, fvs)
end