Tue, 11 Sep 2018 21:01:15 +0100
main: Throw scansion error on timeout, to avoid unnecessary traceback
112
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 | require "verse" |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 | local xml = require "scansion.xml"; |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 | local s_format, s_gsub = string.format, string.gsub; |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 | |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 | local escape_table = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" }; |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 | local function xml_escape(str) return (s_gsub(str, "['&<>\"]", escape_table)); end |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 | |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 | -- Dummy functions compatible with util.termcolours, |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 | -- just in case we add colour in the future |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 | local function getstyle() return "" end |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 | local function getstring(style, text) |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 | if not style then |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 | text = style; |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 | end |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 | return text; |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 | end |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 | |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 | local default_config = { |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 | indent = 2; |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 | preserve_whitespace = false; |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 | }; |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 | |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 | local function new(user_config) |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 | local config = setmetatable({}, { __index = function (_, k) return user_config[k] or default_config[k]; end }); |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 | local style_attrk = getstyle("yellow"); |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 | local style_attrv = getstyle("red"); |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 | local style_tagname = getstyle("red"); |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 | local style_punc = getstyle("magenta"); |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 | |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 | local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'"); |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 | local open_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">"); |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 | local close_tag_format = getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">"); |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 | local tag_format = open_tag_format.."%s"..close_tag_format; |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 | local short_close_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, "/>"); |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 | local function pretty_print(t, ind) |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 | ind = ind or config.indent; |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 | local children_text = ""; |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 | for i, child in ipairs(t) do |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 | if type(child) == "string" then |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 | if config.preserve_whitespace or child:match("%S") then |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 | children_text = children_text .. "\n"..string.rep(" ", ind) .. xml_escape(child); |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 | end |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 | else |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 | children_text = children_text .. "\n" .. pretty_print(child, ind+config.indent); |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 | end |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 | end |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 | |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 | local attr_string = ""; |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 | if t.attr then |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 | for k, v in pairs(t.attr) do |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 | if type(k) == "string" then |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 | attr_string = attr_string .. s_format(attr_format, k, tostring(v)); |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 | end |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 | end |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 | end |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 | |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 | local use_tag_format = tag_format; |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 | if #t == 0 then |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 | use_tag_format = short_close_tag_format; |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 | end |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 | if children_text ~= "" then |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 | children_text = children_text .. "\n" .. string.rep(" ", ind); |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 | end |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 | |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 | return string.rep(" ", ind)..s_format(use_tag_format, t.name, attr_string, children_text, t.name); |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 | end |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 | |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 | return function (s, ind) |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 | local doc = assert(xml.parse(s)); |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 | return pretty_print(doc, ind); |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 | end |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 | end |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 | |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 | return { |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 | new = new; |
bbb49227c174
scansion.pretty: Utility lib for XML pretty-printing, borrowed from Prosody
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 | } |