Murga-Projects Forums
CGI work in progress - Printable Version

+- Murga-Projects Forums (http://www.murga-projects.com/forum)
+-- Forum: Project Forums (/forumdisplay.php?fid=1)
+--- Forum: MurgaLua - General (/forumdisplay.php?fid=2)
+--- Thread: CGI work in progress (/showthread.php?tid=39)


CGI work in progress - mikshaw - 06-06-2007 02:54 AM

It was really cold and yucky yesterday, so I stayed in and messed around with some mrgaLua CGI. This script uses a specific file structure to generate pages containing a jpeg image followed by one or two blocks of text. It is a work in progress, as stated in the thread title. The code claims to generate strict XHTML 1.0, but this hasn't been tested.
It is called with "script_name?directory_name", where directory_name is a subdirectory within the path listed in the datadir variable. When it gets to the point of being "finished", I'll upload a sample directory structure with the script.
As always, feedback is welcome.

Code:
--[[
    A very basic html "book" for photos and plain text, using
    murgaLua as a CGI interpreter. It simply reads in a few files
    stored in a given directory and places them on a web page
    along with a menu of other directories found in the same place.
    This script was designed with a specific task in mind, but it
    should be fairly simple to apply it to other purposes, such as
    a family photo album or a digital art gallery.

    Here's what you need in the "sitedir" directory:
    1. A subdirectory for each page. If you want spaces in the
       displayed names they should be underscores in the filename.
      
    Here's what is wanted in each subdirectory:
    1. A file named info.txt, which will display text on the page
       (html tags will be removed). If there is no info.txt file,
       a "no information" message will be displayed on the page.
    2. A file named credits.txt, which is essentially the same.
       The difference here is that omitting the file will show
       no message to the viewer.
    3. One or more images, named image1.jpg, image2.jpg, etc.,
       limited by the "max_images" variable. These will all display
       in the typical left->right up->down fashion, without any
       special arrangement, so it might be best to stick with pics
       of similar dimensions. I'll squeeze in png and gif at some
       later date, as well as making the layout more pleasant.

       mikshaw 2007
--]]

title=os.getenv("QUERY_STRING")
if not title then
print("no page specified")
exit()
end

-- things to make customizing a little easier
this_script="/cgi-bin/plantinfo.lua" -- relative or absolute URL path
sitedir="/opt/lighttpd/lansite/www/plantinfo/" --where's the data
datadir="/plantinfo/"..title --URL path
realdatadir=sitedir..title --absolute path
max_images=4 -- max number of pics to display on a page
fgcolor='#d7e395' -- text/borders
bgcolor='#315f08' -- body background
linkcolor='#a4ce68' -- link
alinkcolor='#fdfbbd' -- active link
boxcolor='#527533'


-- open a paragraph (using the CSS)
function par_open()
print('<div id="xsnazzy">\n'..
'<b class="xtop"><b class="xb1"></b><b class="xb2"></b><b class="xb3"></b><b class="xb4"></b></b>\n'..
'<div class="xboxcontent"><p>')
end
-- close paragraph
function par_close()
print('</p></div>\n'..
'<b class="xbottom"><b class="xb4"></b><b class="xb3"></b><b class="xb2"></b><b class="xb1"></b></b>\n'..
'</div>\n')
end

function build_menu()
par_open()
for dir in io.popen("ls -1 "..sitedir):lines() do
if murgaLua.isDirectory(sitedir..dir) then
print('&#183 <a href="'..this_script..'?'..dir..'">'..string.gsub(dir,"_"," ")..'</a> &#183')
end
end
par_close()
print('</body></html>')
end

function print_file(filename)
for line in io.lines(realdatadir.."/"..filename)
do
-- removes html tags
print(string.gsub(line,"%b<>","")..'<br/>')
end
end

display_title=string.gsub(title,"_"," ") --removes underscores for page display

-- start writing the page
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=ISO-8859-1" />\n'..
'<title>'..title..'</title>\n</head>\n\n<body>\n')

-- CSS based on the work of
-- Alessandro Fulciniti, http://pro.html.it/esempio/nifty/
-- and Stu Nicholls, http://www.cssplay.co.uk/
print('<style type="text/css">\n'..
'* { color: '..fgcolor..';}\n'..
'body { margin: 20px; background-color: '..bgcolor..'; }\n'..
'img { border: 0 }\n'..
'a:hover, a:focus { color: '..alinkcolor..'; }\n'..
'a { color: '..linkcolor..'; }\n'..
'a { text-decoration: none; }\n'..
'h2 {margin:0 10px; letter-spacing:1px; font-size:2em; border:0; font-variant: small-caps; }\n'..
'#xsnazzy {background: transparent; margin:1em;}\n'..
'.xtop, .xbottom {display:block; background:transparent; font-size:1px;}\n'..
'.xb1, .xb2, .xb3, .xb4 {display:block; overflow:hidden;}\n'..
'.xb1, .xb2, .xb3 {height:1px;}\n'..
'.xb2, .xb3, .xb4 {background: '..boxcolor..'; border-left:1px solid '..fgcolor..'; border-right:1px solid '..fgcolor..';}\n'..
'.xb1 {margin:0 5px; background: '..fgcolor..';}\n'..
'.xb2 {margin:0 3px; border-width:0 2px;}\n'..
'.xb3 {margin:0 2px;}\n'..
'.xb4 {height:2px; margin:0 1px;}\n'..
'.xboxcontent {display:block; background: '..boxcolor..'; border:0 solid '..fgcolor..'; border-width:0 1px; padding: 10px }\n'..
'</style>')


-- check if specified directory exists
if not murgaLua.isDirectory(realdatadir) then
par_open()
print(display_title.." does not exist")
par_close()
build_menu()
exit()
end

-- load any images we can find
par_open()
print('<h2>'..display_title..'</h2>\n<br/>')
for i=1,max_images do
if murgaLua.isFile(realdatadir.."/image"..i..".jpg") then
print('<img src="'..datadir..'/image'..i..'.jpg"/>') end
end
par_close()

par_open()
if murgaLua.isFile(realdatadir.."/info.txt") then
print_file("info.txt")
else print("No information available for "..display_title)
end
par_close()

if murgaLua.isFile(realdatadir.."/credits.txt") then
par_open()
print_file("credits.txt")
par_close()
end

build_menu()




RE: CGI work in progress - mikshaw - 06-13-2007 02:15 PM

I'm worn out on this particular script for now, so I'll post it in its current state (which should be a little bit more useful than the above)

The attached archive includes an example page