diff --git a/alt_scroll.lua b/alt_scroll.lua new file mode 100644 index 0000000..a15fb1b --- /dev/null +++ b/alt_scroll.lua @@ -0,0 +1,114 @@ +-- This mod add a function to add written books to the treasurer +-- + +local modname = minetest.get_current_modname() +local modpath = minetest.get_modpath(modname) +local worldpath = minetest.get_worldpath() + +-- Load support for intllib. +local S, NS +if intllib and intllib.make_gettext_pair then +S, NS = dofile(modpath.."/intllib.lua") +else S, NS = function(s) return s, s end +end + +local max_title_size = 80 +local short_title_size = 35 +local lpp = 14 --14 -- Lines per book's page +local no_title = "Untitled Book" +local no_owner = minetest.formspec_escape("...") + + +local line_parse = function(line) + local op = "," + local pt = { + {"^# ","#FF9292,"}, + {"^## ","#9292DB,"}, + {"^### ","#92FF92,"}, + {"^#### ","#FFFF00,"}, + } + for _,v in ipairs(pt) do + if string.match(line,v[1]) then + line = string.gsub(line,v[1],"") + op = v[2] + break + end + end + line = op .. minetest.formspec_escape(line) + return line +end + +-- --[[ Alternate scrollable book unsing table +written_books.register_book_alernate.scroll = function(dt, prob, pre, booktype) + if not ( dt and dt.title and dt.filetable ) then return end + local text + -- Get a stack of one book + local bookstack = ItemStack(modname..":scroll") + -- Open stack meta + local meta = bookstack:get_meta() + local text = dt.filetable + + -- Set book meta + local data = {} +-- local data = meta:to_table().fields + local title = dt.title:sub(1, max_title_size) + local text = text or {} + for n,l in ipairs(text) do text[n] = line_parse(l) end + local author = dt.owner + + local short_title = dt.title + -- Don't bother triming the title if the trailing dots would make it longer + if #short_title > short_title_size + 3 then + short_title = short_title:sub(1, short_title_size) .. "..." + end + local crd = "" + if author ~= no_owner then crd = "by "..author end + data.description = "\""..short_title.."\""..crd + + local deco = default.gui_bg .. default.gui_bg_img + local formspec = "size[7,7,true]" .. deco + -- Title + formspec = formspec .. "tablecolumns[color;text]" + .. "tableoptions[background=#00000000;highlight=#00000000;border=false]" + .. "table[0.4,0;7,0.5;title;#FFFF00," .. minetest.formspec_escape(title) .. "]" + .. "label[0.5,0.5;by " .. author .. "]" + + -- Content + formspec = formspec .. "tablecolumns[color;text,align=inline]" --,width=5 + .. "tableoptions[color=#FFF;background=#494949;highlight=#494949;border=true]" + .. "table[0,1;6.7,6;doc;" .. table.concat(text, ",") .. ",] " + +-- formspec = formspec .. "image_button_exit[6,6.5;0.8,0.8;power.png;close;;false;false;power.png]" + + + data.owner = author + data.formspec = formspec + meta:from_table({ fields = data }) + + -- Convert back to itemstack (string) + local wbook = bookstack:to_string() + + -- Add to list + written_books.add_book_to_list(wbook, prob, pre, booktype) +end +--]] + +local function book_on_use(itemstack, user) + local player_name = user:get_player_name() + --if not user then return end + --local username = user:get_player_name() + if not player_name or player_name == "" then return end + local meta = itemstack:get_meta() + local data = meta:to_table().fields + print(dump(data)) + if data.formspec then minetest.show_formspec(player_name, modname..":book", data.formspec) end + return itemstack +end + +minetest.register_craftitem(modname..":scroll", { + description = S("Scroll"), + inventory_image = "written_books_scroll.png", + groups = {book = 1, not_in_creative_inventory = 1, flammable = 3}, + stack_max = 1, + on_use = book_on_use, +}) diff --git a/depends.txt b/depends.txt new file mode 100644 index 0000000..7e72579 --- /dev/null +++ b/depends.txt @@ -0,0 +1,3 @@ +treasurer +default +inttlib? diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..8014680 --- /dev/null +++ b/init.lua @@ -0,0 +1,234 @@ +-- This mod add a function to add written books to the treasurer +-- + +local modname = minetest.get_current_modname() +local modpath = minetest.get_modpath(modname) +local worldpath = minetest.get_worldpath() + +-- Load support for intllib. +local S, NS +if intllib and intllib.make_gettext_pair then +S, NS = dofile(modpath.."/intllib.lua") +else S, NS = function(s) return s, s end +end + +-- Request read/write access to files +local ie = minetest.request_insecure_environment() +assert(ie, "You must allow `"..modname.."` in `secure.trusted_mods`") + +local private = { } +private.open = ie.io.open +private.mkdir = ie.core.mkdir + +-- Define langage (code from intllib mod) +local LANG = minetest.settings:get("language") +if not (LANG and (LANG ~= "")) then LANG = os.getenv("LANG") end +if not (LANG and (LANG ~= "")) then LANG = "en" end +LANG = LANG:sub(1, 2) + + +written_books = {} +local bookslist = {} +local max_text_size = 10000 +local max_title_size = 80 +local short_title_size = 35 +local lpp = 14 --14 -- Lines per book's page +local patterns = { + { "title", "-- title : " }, + { "author", "-- author : " }, +-- { "description", "-- description : "}, + } +local bookpath = modpath.."/books" +local no_title = "Untitled Book" +local no_owner = minetest.formspec_escape("...") + + + +-- Add books not based on default book to bookslist +written_books.add_book_to_list = function(itemstack, prob, pre, booktype) + if not itemstack then return end + if not prob then prob = 0.1 end + if not pre then pre = 4 end + if not booktype then booktype = "default" end + bookslist[#bookslist+1] = { itemstack, prob, pre, booktype } +end + +-- Add written book to bookslist +written_books.register_book = function(dt, prob, pre, booktype) + if not ( dt and dt.title and dt.text ) then return end + local text + -- Get a stack of one book + local bookstack = ItemStack("default:book_written") + -- Open stack meta + local meta = bookstack:get_meta() + -- Set book meta + local data = meta:to_table().fields + data.title = dt.title:sub(1, max_title_size) + text = dt.text:sub(1, max_text_size) + text = text:gsub("\r\n", "\n"):gsub("\r", "\n") + text = string.gsub(text,"", "") -- Remove comments + text = string.gsub(text,"%!%[.-%]%(.-%)", "") -- Remove images links + data.text = text + data.owner = dt.owner + + local short_title = dt.title + -- Don't bother triming the title if the trailing dots would make it longer + if #short_title > short_title_size + 3 then + short_title = short_title:sub(1, short_title_size) .. "..." + end + local crd = "" + if data.owner ~= no_owner then crd = "by "..data.owner end + data.description = "\""..short_title.."\""..crd + data.page = 1 + data.page_max = math.ceil((#data.text:gsub("[^\n]", "") + 1) / lpp) + + meta:from_table({ fields = data }) + -- Convert back to itemstack (string) + local wbook = bookstack:to_string() + -- Send to treasurer + -- (name, rarity, preciousness, count, wear, groups) + -- treasurer.register_treasure("default:book",0.1,4) + -- treasurer.register_treasure(wbook,0.1,4,nil) + written_books.add_book_to_list(wbook, prob, pre, booktype) +end + +written_books.register_book_alernate = {} +dofile(modpath.."/alt_scroll.lua") + +-- Add written book from file content +written_books.register_book_from_file = function(fileinfo, prob, pre, booktype) + local filetext, filepath, f, getinfos, alernate_book, owner, title + local headcomment = true + + if type(fileinfo) == "string" then filepath = fileinfo + elseif type(fileinfo) == "table" and fileinfo.basename then + local fbasename = fileinfo.basename + local fpath = fileinfo.path or bookpath + local fext = fileinfo.ext or ".md" + local lg = fileinfo.lang or LANG + local fp = fpath..'/'..fbasename..'.'..lg..'.'..fext + filepath = fpath..'/'..fbasename..'.'..fext + alernate_book = fileinfo.alernate + headcomment = fileinfo.header or true + f = private.open(fp) + else return end + + local filetable = {} + local infos = {} + + -- Read file to get its content + if not f then f = private.open(filepath)end + if not f then filetext = "Sorry, there was a problem while trying to read the file..." + else + -- + -- Get infos + -- + local l = 1 + local lmax = 6 + + local maxlen = 59 + for line in f:lines() do + -- Search for file infos at the beginning of the file + if headcomment and l < lmax then + -- Look for pattern indicating infos + for _,v in ipairs(patterns) do + if string.match(line,v[2]) then + local k = v[1] + infos[k] = string.gsub(line,v[2],"") + break + end + end + -- Count lines to stop after a few lines + l = l + 1 + end + --if comment then next end + --elseif string.match("