/* %How2Compile% azpc -p puttxt.pl /e puttxt.exe /no */

top_level:-
	prompt(_,''),

	get_parameter([X|Else]),
	(Else=[Charset] -> true;Charset='utf-8'),
	atom_append('Content-Type: text/html; charset=',Charset,MyCharset),
	write(MyCharset),nl,nl,
	write('<html><pre>'),nl,   
	see(X),
	 repeat,
		get0(C),read_line(C,Line,End),check_line("TMPL_",Line),End == end,
	seen.

read_line(26,[],end):-!.    % Windows 
read_line(4,[],end):-!.     % Linux
read_line(31,[31],cont):-!.
read_line(X,[X|L],EndCont):-get0(C),read_line(C,L,EndCont).

check_line(_,[]):-!.
check_line([T,M,P,L,V],[Q,H,T,M,P,L,V|ELSE]):-
	!,([Q] = "<" -> [Q,H,T,M,P,L,V|ELSE]=ALL;puta([Q]),[H,T,M,P,L,V|ELSE]=ALL),
	red_line(ALL,Out),check_line("TMPL_",Out).
check_line([T,M,P,L,V],[H,T,M,P,L,V|ELSE]):-
	!,red_line([H,T,M,P,L,V|ELSE],Out),check_line("TMPL_",Out).
check_line(X,[A|L]):- puta([A]),check_line(X,L).

red_line(ALL,Out):-write('<B><font color=#ff0000>'),put_line(">",ALL,Out),write('</font></B>').

put_line([A],[A|L],L):- !,puta([A]).
put_line(X,[C|L],Out):-  puta([C]),put_line(X,L,Out).

puta([26]):-!.
puta("<"):-write('&lt;'),!.
puta(">"):-write('&gt;'),!.
puta("&"):-write('&amp;'),!.
puta(""""):-write('&quot;'),!.
puta([X]):- put(X).

