Tweak parsing of /placeschem
[minetest_schemedit.git] / make_readme.lua
blob4137bcfd28d1f203a9b55f0dca3c7cc593fd3d0d
1 -- This file adds a command for generating the schemedit usage help readme
2 -- file, in Markdown format. The text is extracted from the metadata of
3 -- the items. This is only used for development purposes, after the
4 -- help text of any of the items was changed.
6 -- How to use: Temporarily set MAKE_README to true in init.lua, then
7 -- start the game as admin and run “/make_schemedit_readme”. Copy the
8 -- generated file back to the mod directory (USAGE.md).
10 -- Everything here is intentionally NOT translated because it is for text
11 -- files only.
14 -- Extract text from item definition
15 local get_text = function(item, field)
16 local text = minetest.registered_items[item][field]
18 -- Remove translation escapes
19 text = string.gsub(text, "\x1BE", "")
20 text = string.gsub(text, "\x1B%(T@schemedit%)", "")
22 -- Fix Markdown syntax error
23 text = string.gsub(text, "schematic_override", "`schematic_override`")
24 return text
25 end
28 -- Schemedit items to generate the readme from
29 local items = { "creator", "probtool", "void" }
31 minetest.register_chatcommand("make_schemedit_readme", {
32 description = "Generate the schemedit usage help readme file",
33 privs = {server=true},
34 func = function(name, param)
36 local readme = "## Usage help".."\n"
37 readme = readme .. "In this section you'll learn how to use the items of this mod.".."\n"
38 readme = readme .. "Note: If you have the `doc` and `doc_items` mods installed, you can also access the same help texts in-game (possibly translated).".."\n\n"
40 local entries = {}
41 for i=1, #items do
42 local item = items[i]
43 local desc = get_text("schemedit:"..item, "description")
44 local longdesc = get_text("schemedit:"..item, "_doc_items_longdesc")
45 local usagehelp = get_text("schemedit:"..item, "_doc_items_usagehelp")
47 readme = readme .. "### "..desc.."\n"
48 readme = readme .. longdesc .."\n\n"
49 readme = readme .. "#### Usage\n"
50 readme = readme .. usagehelp
51 if i < #items then
52 readme = readme .. "\n\n\n"
53 end
54 end
56 local path = minetest.get_worldpath().."/schemedit_readme.md"
57 local file = io.open(path, "w")
58 if not file then
59 return false, "Failed to open file!"
60 end
61 local ok = file:write(readme)
62 file:close()
63 if ok then
64 return true, "File written to: "..path
65 else
66 return false, "Failed to write file!"
67 end
68 end