File: format.lua

package info (click to toggle)
instead 3.5.2%2Bdfsg-0.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,316 kB
  • sloc: ansic: 28,336; sh: 452; makefile: 236
file content (49 lines) | stat: -rw-r--r-- 1,441 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
format = {
	nam = 'format';
	object_type = true;
	system_type = true;
	para = false;
	para_space = '    ';
	quotes = false;
	dash = false;
	filter = nil;
	nopara = '_';
	save = function(self, name, h, need)
		for k, v in stead.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;
}

stead.fmt = stead.hook(stead.fmt, function(f, ...)
	local utf8
	local r = f(...)
	if game.codepage == 'UTF-8' or game.codepage == 'utf-8' then
		utf8 = true
	end
	if stead.type(r) == 'string' and stead.state then
		if stead.type(format.filter) == 'function' and stead.state then
			r = format.filter(r);
		end
		if stead.call_bool(format, 'dash') and utf8 then
			r = r:gsub('([^-])%-%-([^-])', '%1—%2');
			r = r:gsub('^%-%-([^-])', '—%1');
		end
		if stead.call_bool(format, 'quotes') and utf8 then
			r = r:gsub('_"','«'):gsub('"_',"»");
			r = r:gsub('"([^"]*)"','«%1»');
			r = r:gsub(',,','„'):gsub("''",'”');
		end
		if stead.call_bool(format, 'para') then
			r = r:gsub('\n([^\n])', '<&para;>%1'):gsub('<&para;>[ \t]*'..format.nopara,'\n'):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));
		end
	end
	return r;
end)

-- vim:ts=4