scansion/pretty.lua

changeset 174
662bd8c5ae28
parent 152
ba8219ac7484
child 178
e547ddf8b64d
equal deleted inserted replaced
173:14ed4cb241f4 174:662bd8c5ae28
1 require "verse" 1 require "verse"
2 local xml = require "scansion.xml"; 2 local xml = require "scansion.xml";
3 local s_format, s_gsub = string.format, string.gsub; 3 local s_format, s_gsub = string.format, string.gsub;
4
5 local unsorted_pairs = pairs;
6 local sorted_pairs = require "util.iterators".sorted_pairs;
4 7
5 local escape_table = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;" }; 8 local escape_table = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;" };
6 local function xml_escape(str) return (s_gsub(str, "['&<>\"]", escape_table)); end 9 local function xml_escape(str) return (s_gsub(str, "['&<>\"]", escape_table)); end
7 10
8 -- Dummy functions compatible with util.termcolours, 11 -- Dummy functions compatible with util.termcolours,
16 end 19 end
17 20
18 local default_config = { 21 local default_config = {
19 indent = 2; 22 indent = 2;
20 preserve_whitespace = false; 23 preserve_whitespace = false;
24 sorted = true;
21 }; 25 };
22 26
23 local function new(user_config) 27 local function new(user_config)
24 local config = setmetatable({}, { __index = function (_, k) return user_config[k] or default_config[k]; end }); 28 local config = setmetatable({}, { __index = function (_, k) return user_config[k] or default_config[k]; end });
25 local style_attrk = getstyle("yellow"); 29 local style_attrk = getstyle("yellow");
26 local style_attrv = getstyle("red"); 30 local style_attrv = getstyle("red");
27 local style_tagname = getstyle("red"); 31 local style_tagname = getstyle("red");
28 local style_punc = getstyle("magenta"); 32 local style_punc = getstyle("magenta");
33
34 local pairs = user_config.sorted and sorted_pairs or unsorted_pairs;
29 35
30 local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'"); 36 local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'");
31 local open_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">"); 37 local open_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">");
32 local close_tag_format = getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">"); 38 local close_tag_format = getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">");
33 local tag_format = open_tag_format.."%s"..close_tag_format; 39 local tag_format = open_tag_format.."%s"..close_tag_format;

mercurial