From 5083ea05ca25212e4b42e02668e74303bf4b8f34 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sun, 30 May 2021 16:18:08 +0100
Subject: [PATCH] Start on #55, but because Minetest blocks our read attempts
 to Chat-Command-Reference (symlinks don't help), we can't continue this

---
 worldeditadditions_commands/doc/init.lua      | 18 ++++++++
 .../doc/parse_reference.lua                   | 45 +++++++++++++++++++
 worldeditadditions_commands/init.lua          | 12 +++++
 3 files changed, 75 insertions(+)
 create mode 100644 worldeditadditions_commands/doc/init.lua
 create mode 100644 worldeditadditions_commands/doc/parse_reference.lua

diff --git a/worldeditadditions_commands/doc/init.lua b/worldeditadditions_commands/doc/init.lua
new file mode 100644
index 0000000..9ab9a55
--- /dev/null
+++ b/worldeditadditions_commands/doc/init.lua
@@ -0,0 +1,18 @@
+-- ██████   ██████   ██████ ███████
+-- ██   ██ ██    ██ ██      ██
+-- ██   ██ ██    ██ ██      ███████
+-- ██   ██ ██    ██ ██           ██
+-- ██████   ██████   ██████ ███████
+
+-- This directory contains support for the doc mod:
+-- https://content.minetest.net/packages/Wuzzy/doc/
+-- API docs: https://repo.or.cz/minetest_doc.git/blob/HEAD:/API.md
+
+-- The strategy here is not to have duplicate content, but to pull data from 
+-- existing sources.
+-- Long-form article descriptions: Chat-Command-Reference.md
+-- Short descriptions: Undecided, but maybe worldedit.registered_commands
+
+worldeditadditions.doc = {}
+
+dofile(worldeditadditions_commands.modpath.."/doc/parse_reference.lua")
diff --git a/worldeditadditions_commands/doc/parse_reference.lua b/worldeditadditions_commands/doc/parse_reference.lua
new file mode 100644
index 0000000..e774618
--- /dev/null
+++ b/worldeditadditions_commands/doc/parse_reference.lua
@@ -0,0 +1,45 @@
+
+
+local function get_reference()
+	local lines = {}
+	for line in io.lines(worldeditadditions_commands.modpath.."/Chat-Command-Reference.md") do
+		table.insert(lines, line)
+	end
+	return lines
+end
+
+local function group_by_heading(lines, max_level)
+	local groups = {}
+	local acc = {}
+	
+	for i,line in ipairs(lines) do
+		if worldeditadditions.str_starts(line, "#") then
+			local _, _, heading, headingtext = line:find("(#+)%s*(.*)")
+			if #heading <= max_level then
+				table.insert(groups, {
+					level = #heading,
+					heading = headingtext,
+					text = table.concat(acc, "\n")
+				})
+				acc = {}
+			end
+		else
+			table.insert(acc, line)
+		end
+	end
+	return groups
+end
+
+function worldeditadditions.doc.parse_reference()
+	local lines = get_reference()
+	local headings = worldeditadditions.table_filter(
+		group_by_heading(lines, 2),
+		function(item, i) return item.level ~= 2 end
+	)
+	for i,value in ipairs(headings) do
+		print(i, "level", level, "heading", heading, "text", text)
+	end
+end
+
+
+worldeditadditions.doc.parse_reference()
diff --git a/worldeditadditions_commands/init.lua b/worldeditadditions_commands/init.lua
index f880a9e..e1b9eb0 100644
--- a/worldeditadditions_commands/init.lua
+++ b/worldeditadditions_commands/init.lua
@@ -62,6 +62,18 @@ else
 	minetest.log("action", "[WorldEditAdditions] bonemeal mod not detected: //bonemeal and //forest commands not registered (if you see this message and you're using an alternative mod that provides bonemeal, please get in touch by opening an issue)")
 end
 
+-- Minetest doesn't allow you to read from files outside the mod
+-- directory - even if you're part of a modpack you can't read from the main
+-- modpack directory. Furthermore, symlinks don't help.
+-- If you have a solution to this issue, please comment on this GitHub issue:
+-- https://github.com/sbrl/Minetest-WorldEditAdditions/issues/55
+-- NOTE TO SELF: When uncommenting this, also add "doc?" to depends.txt
+-- if minetest.get_modpath("doc") then
+-- 	dofile(we_c.modpath.."/doc/init.lua")
+-- else
+-- 	minetest.log("action", "[WorldEditAdditions] doc mod not detected: not registering doc category with extended help")
+-- end
+
 
 worldedit.alias_command("smoothadv", "convolve")
 worldedit.alias_command("conv", "convolve")
