/* SUBJECT_TO_HTML.CMD -- Generate HTML directory listings with descriptions taken from the extended attributes */
/*
 * This is a REXX script that reads the .SUBJECT extended attributes of files
 * and directories in the current directory and from them generates an HTML
 * directory listing, that includes the descriptions, to its standard output,
 * suitable for inclusion in a web page.
 *
 * (c) Copyright 2004,2007 Jonathan de Boyne Pollard.  "Moral" rights asserted.
 *
 * Permission is hereby granted to copy, modify, and redistribute this script
 * as long as the author is not held responsible for any consequences of its
 * possession or use, and as long as you agree that it comes with no guarantee.
 *
 * NOTE: This script requires Object REXX.
 */

call RxFuncAdd 'SysFileTree','RexxUtil','SysFileTree'
call RxFuncAdd 'SysGetEA','RexxUtil','SysGetEA'
call RxFuncAdd 'SysGetMessage','RexxUtil','SysGetMessage'

error = SysFileTree('*', 'names', 'BT')
if error \= 0 then do
	say '<STRONG>'||escape(SysGetMessage(error,,'*'))||' "*"</STRONG>'
	exit
end

say '<DL>'

do i = 1 to names.0
	attr = names.i~subword(3,1)
	name = names.i~subword(4)

	isdir = attr~substr(2,1)
	basename = filespec("Name",name)

	if isdir == 'D' then
		image = 'internal-gopher-menu'
	else
		image = 'internal-gopher-unknown'

	say '<DT><IMG BORDER=0 SRC="'||image||'"> '||basename||'</DT>'

	error = SysGetEA(name, ".SUBJECT", "subject")
	select
		when error \= 0 then
			say '<DD><STRONG>'||escape(SysGetMessage(error,,name))||'</STRONG></DD>'
		when subject \= '' then do
			len = subject~substr(3,2)~reverse()~c2d()
			description = subject~substr(5,len)
			say '<DD>'||escape(description)||'</DD>'
		end
		otherwise nop
	end
end

say '</DL>'

exit

escape: procedure
	parse arg s

	r = ''
	do while s \= ''
		c = left(s,1)
		s = substr(s,2)
		select
			when c = '<' then r = r||'&lt;'
			when c = '>' then r = r||'&gt;'
			when c = '&' then r = r||'&amp;'
			otherwise		  r = r||c
		end
	end

	return r
