Page 1 of 1
Function to print indexed text from file?
Posted: Sat Sep 05, 2015 1:28 pm
by Nyyrazzilyss
Before I just write it myself: Is there any prexisting commands or function sets to echo segments of a file to the screen? For example, if I have a text file of the below format:
;;id,index name
This is a text message
This is a text message
This is a text message
;;id,index name
(etc)
I would pass id/or index name to the function, and it would respond by echoing the associated contents. Even better would be rendering html, and allowing use of formatting/image linking/etc
Re: Function to print indexed text from file?
Posted: Tue Sep 08, 2015 2:42 am
by Nyyrazzilyss
Hadn't heard anything one way or the other, so wrote a quick function to do so - It's extremely basic, just text to screen/doesn't handle colors/html/etc. Quite likely has bugs, I haven't used it excessively yet. The main thing is I need to write some usage documentation for my mudlet package, and this lets me easily organize/edit it in a single file.
--function mainpath(xname)
-- This function returns the full pathname inside the profile of a specific file
--end
function showHelp(xindex)
local index, name
local found=false
for line in io.lines(mainpath("helpfile.dat")) do
if string.find(line, ";;") ~= nil then
if found == false then
index,name = string.match(line, ";;([0-9]+),([a-zA-Z ]+)")
else
return
end
else
if index == tostring(xindex) or name == xindex then
found=true
echo(line .. "\n")
end
end
end
if found == false then
echo("[No help information found for: " .. xindex .. "]\n")
end
end
if matches[2] == "" then
showHelp("index")
else
showHelp(matches[2])
end
The above code is inside a '#help' alias, the pattern for it is ^#help ?(.*)?
The datafile is of the format
;;number,item name
text
text
;;number,item name
etc
Usage: typing '#help' prints the contents of 'index'. '#help anythingelse' searches the file for the 'anythingelse'' entry, and echoes that to the screen.