From 5399a0f302029a10b1fe5616981e323699ef36cc Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Tue, 22 Dec 2020 12:37:29 +0100 Subject: [PATCH] Schema: Generate Markdown & format Readme --- mod.lua | 16 ++++++++++++++++ schema.lua | 6 +++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/mod.lua b/mod.lua index fcaf1e7..f815fb0 100644 --- a/mod.lua +++ b/mod.lua @@ -100,6 +100,22 @@ function configuration(modname) conf = schema:load(overrides, {convert_strings = true, error_message = true}) end modlib.file.ensure_content(get_resource(modname, "settingtypes.txt"), schema:generate_settingtypes()) + local readme_path = get_resource(modname, "Readme.md") + local readme = modlib.file.read(readme_path) + if readme then + local modified = false + readme = readme:gsub("" .. "(.-)" .. "", function(level, content) + schema._md_level = assert(tonumber(level)) + 1 + local markdown = schema:generate_markdown() + if content ~= markdown then + modified = true + return "" .. markdown .. "" + end + end, 1) + if modified then + assert(modlib.file.write(readme_path, readme)) + end + end if conf == nil then return schema:load({}, {error_message = true}) end diff --git a/schema.lua b/schema.lua index 0f9eafb..2662718 100644 --- a/schema.lua +++ b/schema.lua @@ -77,7 +77,7 @@ function generate_markdown(self) -- TODO address redundancies local typ = self.type self.title = self.title or field_name_to_title(self._md_name) - self._level = self._level or 1 + self._md_level = self._md_level or 1 if typ == "table" then local handled = {} local settings = {} @@ -88,8 +88,8 @@ function generate_markdown(self) handled[key] = true value_scheme._md_name = key value_scheme.title = value_scheme.title or self.title .. " " .. field_name_to_title(key) - value_scheme._level = self._level + 1 - table.insert(settings, table.concat(modlib.table.repetition("#", self._level)) .. " `" .. key .. "`") + value_scheme._md_level = self._md_level + 1 + table.insert(settings, table.concat(modlib.table.repetition("#", self._md_level)) .. " `" .. key .. "`") table.insert(settings, generate_markdown(value_scheme)) table.insert(settings, "") end