% engine=luatex

\startluacode
	xml.entities = { [' '] = '&nbsp;' }
	
	function lxml.flush(id)
		xml.sprint(xml.mystupidfunction(lxml.id(id)))
	end
	
	function xml.mystupidfunction(root)
		local d = root.dt
			for k=1,#d do
				local dk = d[k]
				if type(dk) == "string" then
					d[k] = dk:gsub("&nbsp;",' ')
					dk = d[k]
					d[k] = dk:gsub("&le;", '\\mathematics{\\le}')
				end
			end
		return d
	end
\stopluacode

\startxmlsetups all:html
    \xmlsetsetup{main}{head|h1|h2|p|pre|span|sub|table|b}{*}
\stopxmlsetups

% register this so that it's done for each load
\xmlregistersetup{all:html}

% title - almost OK, should be printed on every page, I will figure out (I already asked the same question once already)
\startxmlsetups head
	\setupheadertexts[\xmlfilter{main}{/head/title/text()}][pagenumber]
\stopxmlsetups

% subject - OK
\startxmlsetups h1
    \subject{\xmlflush{#1}}
\stopxmlsetups

% subsubject - OK, but should be treated different depending on class
\startxmlsetups h2
	% this doesn't work
	% \doifelse{\xmlatt{#1}{class}}{filename}{...}{...}
	\subsubject{\xmlflush{#1}}
\stopxmlsetups

% paragraphs - OK
\startxmlsetups p
	\xmlflush{#1}\par
\stopxmlsetups

% code samples should be typeset as they are (also: no entities substitution)
% currently no blank lines are printed, but I will try to figure out how to solve that
\startxmlsetups pre
	\bgroup\obeylines\tt
	\xmlflush{#1}
	\egroup
\stopxmlsetups

% tables - really ugly implementation, only colspan not working, but I don't need it right now
\startxmlsetups table
	\xmlsetsetup{main}{tr}{*}
	\bTABLE \xmlflush{#1} \eTABLE
\stopxmlsetups

\startxmlsetups tr
	\xmlsetsetup{main}{td}{*}
	\bTR \xmlflush{#1} \eTR
\stopxmlsetups

\startxmlsetups td
	\bTD \xmlflush{#1} \eTD
\stopxmlsetups

% should be only defined for class=simplemath
\startxmlsetups span
	\mathematics{\xmlflush{#1}}
\stopxmlsetups

% OK in my case
\startxmlsetups sub
	\low{\xmlflush{#1}}
\stopxmlsetups

% bold - OK, unless when nested with <i>
\startxmlsetups b
	{\bf\xmlflush{#1}}
\stopxmlsetups

\setupcolors
	[state=start]
\setuphead
	[subject]
	[style=\bfc,
	 color=blue,
	 page=yes,
	 after=]
\setuphead
	[subsubject]
	[style=\bfa,
	 color=blue]
\setupblank
	[big]
\setuppagenumbering
	[location=]

\starttext

\xmlprocess{main}{frogs.html}{}

\stoptext
