NB. J-WEB Some tools for generating HTML pages
NB. Last revision 2009.10.27
NB. Accomodate legacy args
9!:49 [ 1
NB. When using CGI scripts, the form "get" method sets the environment variable
NB. QUERY_STRING to indicate arguments passed to the CGI script.
NB. & separates field-value pairs; = separates each field and value.
NB. query
NB. Build a database of field-value pairs from the CGI environment variable QUERY_STRING
query =: 3 : 0
if. 0 -: r =. getenv y.
do. ''
else. (<;. _2 @ (, & '=') &. >) @ ( < ;. _2 @ (, & '&')) r
end.
)
NB. locate (a conjunction)
NB. key locate key_field_number
NB. Return the database of all records which match key in key_field_number
NB. Note that locate is a conjunction which produces a monad
NB. usage: key locate key_field_number db
locate =: 2 : '#~ > @ (u&-: &. >) @ ((v&{) @ >)'
NB. get_field
NB. Return the value of a named field from a CGI QUERY_STRING
NB. get_field_helper
NB. db get_field_helper field_name
get_field_helper =: 4 : '> 1{ , > ( y. locate 0 ) x.'
NB. field-value pairs are passed to get_field by Currying get_field_helper.
get_field =: (query 'QUERY_STRING') & get_field_helper
NB. system
NB. Perform Unix system call.
NB. system will provide all sorts of security issues if its
NB. arguments are not carefully checked.
system =: 2 !: 0
NB. do
NB. execute a J expression or return an error message
do =: 3 : 'try. ". y. catch. ''invalid J expresson'' end.'
NB. unmungle
NB. Remove hex-coded chars which are inserted by the Apache
NB. CGI interface
unmungle =: 3 : 0
if. 0 = # y.
do. ''
elseif. (# y.) = pos =. y. i. '%'
do. y.
elseif. 1
do. (pos {. y.) , ((16 16 #. '0123456789ABCDEF' i. (pos + 1 2) { y.) { a.) , unmungle (pos + 3) }. y.
end.
)
NB. An HTML page is defined to consist of components header, y. (body) , trailer and footer.
NB. page
NB. Output html page
page_helper =: 4 : 0
stdout > 0 { x.
stdout y.
stdout > 1 { x.
stdout > 2 { x.
NB.exit 0
)
NB. J db of HTML page components
NB. These components are provided in the style of the CS
NB. Department web site.
header =: 0 : 0
Content-type: text/html
The Computer Science Department
)
footer =: 0 : 0
)
trailer =: 0 : 0
)
NB. page components (header, footer, trailer) are passed to page by Currying page_helper.
page =: (header ; footer ; trailer) & page_helper
NB. An alternate page generation function page1
NB. Usage ('page-title';'page-options') page1 page-content
NB. The page content type
ct =: 0 : 0
Content-type: text/html
)
page1 =:dyad define
stdout ct , 'html' ht 'head' ht ('title' ht >0 { x.),('body';> 1 { x.) ht y.
exit 0
)
NB. HTML generation functions
NB. ht
NB. wrap HTML tag (x.) around y.
NB. usage 'tag' ht 'foo' ==> foo
NB. ('tag';'options') ht 'foo' ==> foo
ht =: dyad define
if. 32 = 3!:0 x.
do. '<',(> 0{ x.),' ',(> 1{ x.),'>',y.,'', (> 0{ x.),'>',10{a.
else. '<',x.,'>',y.,'',x.,'>',10{a.
end.
)
NB. Examples:
NB. 'html' ht 'some text'
NB. par and others
NB. by Currying ht
par =: 'p' & ht
NB. par 'some text'
pre =: 'pre' & ht
NB. pre 0 : 0
li =: 'li' & ht
NB. li 'some text'
href =: 'href=http://' & , &. >
NB. href <'www.apple.com'
aref =: 'a'&; @ href @ <
NB. aref 'www.apple.com'
NB. (aref 'www.apple.com')ht 'Apple'
hl =: monad def '(aref > 0{y.) ht > 1 { y.'
NB. hl 'www.apple.com';'Apple'
NB. Note: (ht&'') 'a'
NB. img 'options' is the same as ('img';'options') ht ''
img =: (ht & '') @ ('img'&;) @ <
NB. img 'src=/Pictures/cs.gif'
NB. When single tags are needed (i.e.
) to end lines
NB. A function producing function which appends a single tag
tag =: adverb define
, & ('<', m. ,'>')
)
NB. This is tricky. Since tag is an adverb, its arg must be written on the left!
br =: 'br' tag
hr =: 'hr' tag
NB. br ''
NB. br 'foo'
NB. HTML comment
co =: monad def ''''''
NB. HTML comment (tacit form)
co =: '')
NB. More tags (feel free to add your own as this list is not exhaustive)
bold =: 'b'&ht
italic =: 'i'&ht
center =: 'center'&ht
font =: 4 : '(''font''&; @ < x.) ht y.'
font =: 13 : '(''font''&; @ < x.) ht y.'
NB. (center&bold&italic) 'Hi there'
NB. 'face=Times' font 'Jack'
NB. Tables
table =: 'table'&ht
row =: 'tr'&ht
td =: 'td'&ht
NB. table (row (td 'one') , td 'two') , row (td 'three') , td 'four'
NB. Some tests
NB. ref =: ('www.cs.trinity.edu/About';'About');<('www.cs.trinity.edu/Faculty';'Faculty')
NB. ('Test Page' ; 'bgcolor=f00000') page1 'ol' ht ('center' ht 'Useful links'), , > (li & hl) &. > ref