From 75c3098b68632f3e512b48b66ec43bb08ca3bf83 Mon Sep 17 00:00:00 2001 From: Michal Hoftich Date: Sun, 27 Dec 2020 20:39:23 +0100 Subject: [PATCH] sort element attributes --- h5tk.lua | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/h5tk.lua b/h5tk.lua index f0bb7e0..7049053 100644 --- a/h5tk.lua +++ b/h5tk.lua @@ -121,23 +121,39 @@ function fmt_traverse_aux_attr(tree) end end +-- sort element attributes, in order to achieve identical +-- output with same input values +local function sort_attr(tree) + local t = {} + for k, _ in pairs(tree) do + if type(k) == "string" then + t[#t+1] = k + end + end + table.sort(t) + return t +end + -- queries all values with string keys and -- folds the together to one single string -- using fmt_traverse_aux_attr function fmt_traverse_attr(tree) local attr = buffer_get() - - for k, v in pairs(tree) do + -- ensure alphabetical attribute order + local keys = sort_attr(tree) + for _, k in ipairs(keys) do + local v = tree[k] if type(k) == "string" then buffer_add(attr, " " .. k .. "=\"") buffer_add(attr, fmt_traverse_aux_attr(v)) buffer_add(attr, "\"") end end - + return buffer_tostring(attr) end + function fmt_traverse_data(source) local tree = tree_get()