Programming and Scripting :: CGI scripting with Lua



Yeah, I know...my CGI tests with Bash didn't go very far yet.  This isn't because Bash isn't worthy, of course....I use Bash more than anything else.

But at this time I'm trying to learn more about Lua scripting beyond just making GUI frontends to commandline tools, and since I've also been experimenting with dynamic web pages I thought Lua CGI would be a good place to get involved.  My main goal is to create a minimalist wiki, but that will be after I learn more about it.

First thing was a function to print the headers of an html document. My current function is static, with the exception of the page title:
Code Sample
function html_begin(title)
print('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"\n'..
'"http://www.w3.org/TR/html4/strict.dtd">\n'..
'<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">\n'..
'<title>'..title..'</title>\n'..
'<link href="/style.css" rel="stylesheet" type="text/css"></head><body>')
end


I did this particularly because I can never remember what to use for the doctype.  But now I want to make it more dynamic, so you can pass a doctype and style sheet to the function.  The problem is that I'm not sure if generating the doctype is acceptable from a standards viewpoint.  Note this page:
http://www.w3.org/QA/2002/04/valid-dtd-list.html
At the bottom, where it says "Authoring tools MUST NOT use the following list". I wonder if CGI page generation is considered an authoring tool.

Any thoughts?

EDIT: Oh...I just noticed something. That list apparently pertains to specific dtd addresses.  I thought they meant that authoring tools should not set certain doc types (maybe because it couldn't guarantee the page conforms to the doctype or something).  Anyway, I was mistaken and apparently I can go ahead with my plan.

So I guess it's going to be something like this:
Code Sample
function html_begin(doctype,title,style)

I just need to contemplate the most efficient way to check for various doctypes, since there seems to be slight changes in syntax from version to version....

Just getting some basic HTML generated is more complicated than I thought...particularly when trying to adhere to w3c standards.  Anyway, I got a few things done that will hopefully make it a little easier....
Code Sample

-- create the document type declaration (dtd)
function add_doctype(major,minor)
-- major: 2.0 3.2 4.0 4.01 x1.0 x1.1 minor: strict transitional frameset
local def_doctype = "HTML 4.01"
local def_dtd = "\"http://www.w3.org/TR/html4/strict.dtd\""
local doctype,dtd
if major == "4.01" then
 if minor == "transitional" then doctype,dtd = "HTML 4.01 Transitional","\"http://www.w3.org/TR/html4/loose.dtd\""
 elseif minor == "frameset" then doctype,dtd = "HTML 4.01 Frameset","\"http://www.w3.org/TR/html4/frameset.dtd\""
 else doctype,dtd = "HTML 4.01","\"http://www.w3.org/TR/html4/strict.dtd\""
 end
elseif major == "x1.0" then
 if minor == "transitional" then doctype,dtd = "XHTML 1.0 Transitional","\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\""
 elseif minor == "frameset" then doctype,dtd = "XHTML 1.0 Frameset","\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd\""
 else doctype,dtd = "XHTML 1.0 Strict","\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\""
 end
elseif major == "x1.1" then doctype,dtd = "XHTML 1.1",""
elseif major == "2.0" then doctype,dtd = "HTML 2.0",""
elseif major == "3.2" then doctype,dtd = "HTML 3.2 Final",""
else doctype,dtd = def_doctype,def_dtd
end
print('<!DOCTYPE HTML PUBLIC "-//W3C//DTD '..doctype..'//EN"\n'..dtd)
end
-- EXAMPLES
-- add_doctype("4.01","transitional") -> HTML 4.01 Transitional
-- add_doctype() -> HTML 4.01 Strict (the default)
-- add_doctype("x1.0") -> XHTML 1.0 Strict
-- add doctype(nil,"transitional") -> if major is nil, it uses the default HTML 4.01 Strict
   
-- create the "head" part and open the body
function html_begin(title,style,charset)
if not title then title = "untitled" end
if not charset then charset = "ISO-8859-1" end
print('<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html; charset='..charset..'" />\n<title>'..title..'</title>')
if style then print('<link href="'..style..'" rel="stylesheet" type="text/css" />') end
print('</head>\n\n<body>\n')
end
-- EXAMPLES
-- html_begin("my page","/css/mystyle.css") -> uses iso-8859-1 charset and custom stylesheet
-- html_begin("my page") -> uses iso-8859-1 charset and no stylesheet
-- html_begin("my page",nil,"utf-8") -> uses utf-8 charset and no stylesheet
-- html_begin() -> uses iso-8859-1 charset, no stylesheet, and page title "untitled"

-- a simplified version that combines both of the above functions,
-- with a static doctype declaration (XHTML 1.0 Strict)
function my_html_begin(title,style,charset)
if not title then title = "untitled" end
if not charset then charset = "ISO-8859-1" end
print('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\n'..
'"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n'..
'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n'..
'<head>\n<meta http-equiv="Content-Type" content="text/html; charset='..charset..'" />')
if style then print('<link href="'..style..'" rel="stylesheet" type="text/css" />') end
print('<title>'..title..'</title>\n</head>\n\n<body>\n')
end

Just a misc suggestion...

If you want to easily check your doc for w3c standards, it might be nice to have an (optional) link to the w3c html validator.

That's a good idea...I think i'll do that. I was wondering about the issue of putting in a strict dtd but at the same time possibly having functions that create conflicting html.
I'm using the offline HTML Validator extension with Firefox, but this is not always going to be the case.

I'm also wondering if that first function is just too much code for too little result.  I'm thinking of just going with the third one, although I don't know how well strict XHTML works for old browsers.


original here.