lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> I uploaded Windows binaries and Visual C++ 6 projects for Lua 5.0 at 
> http://philho.multimania.com/#downloads

I updated the Lua 5.0 project files for Visual C++ 6.
They weren't up-to-date with the Lua 4.0.1 files, now they are...
I updated also the binary files.

I changed the LuaExe distributions. For Lua 5, I changed the binaries. For
both, I changed the manual.
I finished my HTML TOC (table of content) generator, and used it against
these manual to make them a bit more practical.

I had to do some minor, invisible changes, so all listed titles allow to
jump to the exact place.
In Lua 4.0 manual, I remove some </Hn> in the link names (?), moved up some
named anchors to fit my algorithm, and added some others.
In Lua 5.0 manual, I had to add a lot of named anchors, mostly for the
functions names, that were references in the previous version but not in this one.
Luiz, Roberto, if you are willing to keep my changes for the next version,
it will avoid some work (or I should write another utility...).

Note I prefer to put the TOC in the same page as the manual itself.
Initially, I just copy/pasted the 4.0 manual TOC in the main page, to avoid jumping
between the pages. I felt the need for a more detailled TOC to avoid jumping
to the string section and scrolling down to the gsub sub-section, for example.
Instead of just adding the corresponding lines, I went to the program
road...

Note that given the time spent on this program, I probably would be faster
by writing the TOCs by hand...
But I had quite some fun doing it, I had a bit more practice in Lua
programming (mostly in regular expression writing), and somehow it is generic enough
to be used on other HTML manuals, if they follow my logic... So I give the
code here, hoping it will be useful for somebody else.

-- 
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--

+++ GMX - Mail, Messaging & more  http://www.gmx.net +++
Bitte lächeln! Fotogalerie online mit GMX ohne eigene Homepage!
#! lua40.exe
-- Create an HTML table of content (TOC) from an HTML file. Made mostly for the Lua manual...
--
-- Take two parameters: the file to process as input, the resulting file as output.
--
-- The program extracts the hierarchical titles (<Hn> tags) and store them with their level.
-- It will then output the list of titles in a hierarchical HTML list.
--
-- Actually, it will also take the latest named link before each title, so the above list
-- can be used to jump to the chosen level.
--
-- Note that the input HTML file must follow this structure for the output to be accurate.
-- I had to make minor, invisible edits to the original Lua manuals,
-- to create anchors or to move them before the title.
--
-- The result must be inserted by hand in the HTML file. If used in a separated page,
-- you must add the destination page name to the links.
--
-- by Philippe Lhoste <PhiLho@GMX.net> http://jove.prohosting.com/~philho/
-- v. 1.1 -- 2003/05/20 -- Output of TOC
-- v. 1.0 -- 2002/07/26 -- Initial code, extraction of data

filenameIn  = (arg and arg[1]) or "-"	-- If absent, use standard input
filenameOut = (arg and arg[2]) or "-"	-- If absent, use standard output

tocList = {}
--lineNumber = 0
refNumber = 0
prevRef = ""
prevLevel = 0

-- Extract useful information from the given line.
function ProcessLine(line)
	if line == nil then
		return nil
	end
--	lineNumber = lineNumber + 1

	-- Remove tags other than those we are looking for.
	-- Replace the interesting tags by control chars, remove all other tags,
	-- and restore the control chars to the corresponding tags.
	line = gsub(line, "<[Hh]", "\1")
	line = gsub(line, "</[Hh]", "\2")
	line = gsub(line, "<[Aa] ", "\3")
	line = gsub(line, "</[Aa]>", "\4")
	line = gsub(line, "<[^>]->", "")
	line = gsub(line, "\1", "<H")
	line = gsub(line, "\2", "</H")
	line, nn = gsub(line, "\3", "<A ")
	line = gsub(line, "\4", "</A>")

	local startRef, endRef, level, ref, title
	local tocData = {}

	-- Capture latest referenced string (by a <A NAME="..."> tag) of the line
	endRef = 0
	repeat
		startRef, endRef, ref = strfind(line, [[<A [Nn][Aa][Mm][Ee]=['"]([^'"]*)['"]>.*</A>]], endRef + 1)
		if ref ~= nil then
			prevRef = ref
		end
	until startRef == nil
	-- Remove the anchors
	line = gsub(line, "<A [^>]+>", "")
	line = gsub(line, "</A>", "")

	-- Search if the line has a title (<Hn> tag, up to level 4)
	_, _, level, title = strfind(line, [[<[Hh]([1234])>([^<]*)</[Hh][1234]>]], 1)
	if level ~= nil then
		-- No need to loop to search another title in the line,
		-- unless the HTML is very sloppy...
		refNumber = refNumber + 1
		tocData =
		{
			['level'] = level,
			['ref']   = prevRef,
			['title'] = title
		}
		tocList[refNumber] = tocData
	end
	return 0
end

-- Output an element of table of content.
function OutputTOC(i, v)
	local level, nLevel
	level = v['level']
	nLevel = tonumber(level)
	while nLevel > prevLevel do
		write"<UL>\n"
		nLevel = nLevel - 1
	end
	while nLevel < prevLevel do
		write"</UL>\n"
		nLevel = nLevel + 1
	end
	prevLevel = tonumber(level)
	write([[<LI><A HREF="#]] .. v['ref'] .. [[">]] .. v['title'] .. [[</A>]] .. "\n")
end

-- Process the input file
function ProcessFile()
	if filenameIn ~= "-" then
		if not readfrom(filenameIn) then
			return nil, filenameIn
		end
	end
	if filenameOut ~= "-" then
		if not writeto(filenameOut) then
			return nil, filenameOut
		end
	end

	local result
	-- Loop on the lines and add any <Hn> tag to the tocList table
	-- Note that these titles must be on the same line, no <Hn> on one line and the title on the next...
	repeat
		result = ProcessLine(read())
	until result == nil

	-- Build the table of content
	write([[
<P>
<A NAME="contents"></A>
<H1>0 - Contents</H1>
]])
	foreach(tocList, OutputTOC)
	while prevLevel > 0 do
		write"</UL>\n"
		prevLevel = prevLevel - 1
	end
	write"\n<HR>\n"

	readfrom()	-- Restore default reading
	writeto()		-- Restore default writing
	return 0, nil
end

result, fn = ProcessFile()
if not result then
	print("Error in parameter: " .. (fn or 'nil'))
else
	print("Done.")
end