steed/stead/format.lua

51 lines
1.4 KiB
Lua
Raw Normal View History

2010-07-02 20:57:09 +03:00
format = {
2010-10-31 12:26:32 +02:00
nam = 'format';
object_type = true;
system_type = true;
2010-07-02 21:37:59 +03:00
para = false;
2010-07-02 20:57:09 +03:00
para_space = ' ';
2010-07-02 21:37:59 +03:00
quotes = false;
dash = false;
2010-10-30 15:54:41 +03:00
filter = nil;
nopara = '_';
2010-10-31 12:26:32 +02:00
save = function(self, name, h, need)
local k,v
for k,v in pairs(self) do
if k == 'para' or k == 'para_space' or k == 'quotes' or
k == 'dash' or k == 'nopara' then
local s = stead.tostring(v)
h:write(stead.string.format("format[%q] = %s;\n", k, s))
end
end
end;
2010-07-02 20:57:09 +03:00
}
stead.fmt = stead.hook(stead.fmt, function(f, ...)
local utf8
2011-02-23 12:11:27 +02:00
local r = f(...)
2010-07-02 20:57:09 +03:00
if game.codepage == 'UTF-8' or game.codepage == 'utf-8' then
utf8 = true
end
2010-07-03 15:38:27 +03:00
if type(r) == 'string' and stead.state then
2010-07-02 22:29:18 +03:00
if type(format.filter) == 'function' and stead.state then
r = format.filter(r);
end
2010-07-03 08:36:47 +03:00
if call_bool(format, 'dash') and utf8 then
2010-07-02 20:57:09 +03:00
r = r:gsub('([^-])%-%-([^-])', '%1—%2');
r = r:gsub('^%-%-([^-])', '—%1');
end
2010-07-03 08:36:47 +03:00
if call_bool(format, 'quotes') and utf8 then
2010-07-02 21:25:51 +03:00
r = r:gsub('_"','«'):gsub('"_',"»");
2010-07-02 20:57:09 +03:00
r = r:gsub('"([^"]*)"','«%1»');
2010-07-03 15:38:27 +03:00
r = r:gsub(',,',''):gsub("''",'');
2010-07-02 20:57:09 +03:00
end
2010-07-03 15:38:27 +03:00
if call_bool(format, 'para') then
2010-10-30 16:27:57 +03:00
r = r:gsub('\n([^\n])', '<&para;>%1'):gsub('<&para;>[ \t]*'..format.nopara,''):gsub('<&para;>[ \t]*', '\n'..txtnb(format.para_space));
r = r:gsub('^[ \t]*', '<&para;>'):gsub('<&para;>[ \t]*'..format.nopara,''):gsub('<&para;>[ \t]*', txtnb(format.para_space));
2010-07-02 20:57:09 +03:00
end
end
return r;
end)
2010-07-06 13:15:27 +03:00
-- vim:ts=4