scansion/pretty.lua

changeset 174
662bd8c5ae28
parent 152
ba8219ac7484
child 178
e547ddf8b64d
--- a/scansion/pretty.lua	Thu Mar 23 15:09:10 2023 +0000
+++ b/scansion/pretty.lua	Thu Mar 23 15:12:30 2023 +0000
@@ -2,6 +2,9 @@
 local xml = require "scansion.xml";
 local s_format, s_gsub = string.format, string.gsub;
 
+local unsorted_pairs = pairs;
+local sorted_pairs = require "util.iterators".sorted_pairs;
+
 local escape_table = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;" };
 local function xml_escape(str) return (s_gsub(str, "['&<>\"]", escape_table)); end
 
@@ -18,6 +21,7 @@
 local default_config = {
 	indent = 2;
 	preserve_whitespace = false;
+	sorted = true;
 };
 
 local function new(user_config)
@@ -27,6 +31,8 @@
 	local style_tagname = getstyle("red");
 	local style_punc = getstyle("magenta");
 
+	local pairs = user_config.sorted and sorted_pairs or unsorted_pairs;
+
 	local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'");
 	local open_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">");
 	local close_tag_format = getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">");

mercurial