%context --ctx=t-openoffice BoldAndItalicContent.xml

\startenvironment t-openoffice.tex

\checknotes 
\initializeMPgraphics 
\MPLIBregister 
\the \everysetupxml
\ctxlua {input.stoptiming(ctx)}
\checkpreprocessor


  \startluacode
    document.commands = document.commands or { }
    function document.commands.somehexcolor(str)
      local r, g, b = str:match("#(..)(..)(..)")
      r, g, b = tonumber(r,16) or 0, tonumber(g,16) or 0, tonumber(b,16) or 0
      tex.sprint(string.format("\\colored[r=%s,g=%s,b=%s]",r/255,g/255,b/255))
    end
  \stopluacode

  \startxmlsetups office
    \xmlsetsetup{main}{text:p|text:span}{*}
  \stopxmlsetups
  \xmlregistersetup{office}

  \startxmlsetups text:p
    \xmlflush{#1}\endgraf
  \stopxmlsetups

  \startxmlsetups text:span
    \bgroup
      \let\leftofspan \relax
      \let\rightofspan\relax
      \xmlfilter{main}{office:automatic-styles/style:style[@style:name='\xmlatt{#1}{style-name}']/style:text-properties/command(do:style:text-properties)}
      \leftofspan
      \xmlflush{#1}
      \rightofspan
    \egroup
  \stopxmlsetups

  \startxmlsetups do:style:text-properties
    \xmlval{fo:font-weight}            {\xmlatt{#1}{font-weight}}{}% todo: namespaces in attributes
    \xmlval{fo:font-style}             {\xmlatt{#1}{font-style}}{}
    %\xmlval{fo:color}                  {\xmlatt{#1}{color}}{}
    \getvalue{fo:color}{\xmlatt{#1}{color}}
    \xmlval{style:text-position}       {\xmlatt{#1}{text-position}}{}
    \xmlval{style:text-underline-style}{\xmlatt{#1}{text-underline-style}}{}
  \stopxmlsetups

  \xmlmapvalue{fo:font-weight}{bold}{\bf}
  \xmlmapvalue{fo:font-style}{italic}{\em}

  \xmlmapvalue{fo:color}{#ff0000}{\colored[h=ff0000]}
  \setvalue{fo:color}#1{\expandafter\colored\expandafter[h=#1]}
  %\setvalue{fo:color}#1{\edef\temp{\noexpand\ctxlua{document.commands.somehexcolor("\letterhash\uppercase{#1}")}}\temp}

  \catcode`\%=11
  \xmlmapvalue{style:text-position}{sub 58%}  {\def\leftofspan##1\rightofspan{\low {##1}}}
  \xmlmapvalue{style:text-position}{super 58%}{\def\leftofspan##1\rightofspan{\high{##1}}}
  \catcode`\%=14

  \definetextbackground
    [underline]
    [location=text,
     alternative=1,
     background=,
     framecolor=black,
     frame=off]

  %\xmlmapvalue{style:text-underline-style}{solid}{\def\leftofspan##1\rightofspan{\startunderline##1\stopunderline}}
  \xmlmapvalue{style:text-underline-style}{solid}{\let\leftofspan\startunderline\let\rightofspan\stopunderline}

  \setupcolors[state=start]
  
  \starttext
    %\xmlprocess{main}{zip:///\inputfilename.odt?name=content.xml}{}
    \usezipfile[\inputfilename.odt]
    \xmlprocess{main}{*/content.xml}{}
  \stoptext
  
\stopenvironment



