%D \module
%D   [       file=mtx-context-xml,
%D        version=2013.05.30,
%D          title=\CONTEXT\ Extra Trickry,
%D       subtitle=Analyzing XML files,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

% This module replaces mkii analyzers.

% begin help
%
% usage: context --extra=xml [options] list-of-files
%
% --analyze             : show elements and characters
% --template            : also export template
% --topspace=dimension  : distance above first line
% --backspace=dimension : distance before left margin
% --bodyfont=list       : additional bodyfont settings
% --paperformat=spec    : paper*print or paperxprint
%
% context --extra=xml --analyze path::i-context.xml
% context --extra=xml --analyze --template path::i-context.xml
% context --extra=xml --analyze selfautoparent:texmf-context/tex/context/interface/mkiv/i-*.xml
% end help

\input mtx-context-common.tex

\setupbodyfont
  [dejavu,11pt,tt,\getdocumentargument{bodyfont}]

\setuptyping
  [lines=yes]

\setuplayout
  [header=0cm,
   footer=1.5cm,
   topspace=\getdocumentargumentdefault{topspace}{1.5cm},
   backspace=\getdocumentargumentdefault{backspace}{1.5cm},
   width=middle,
   height=middle]

\setuppapersize
  [\getdocumentargument{paperformat_paper}]
  [\getdocumentargument{paperformat_print}]

\usemodule[xml-analyzers]

\starttext

\startluacode
    local files    = document.files
    local pattern  = document.arguments.pattern or (#files == 1 and files[1])
    local analyze  = document.arguments.analyze
    local template = document.arguments.template

    if pattern then
        files = dir.glob(pattern)
        context.setupfootertexts( { pattern }, { "pagenumber" })
    else
        context.setupfootertexts( { table.concat(files," ") }, { "pagenumber" })
    end

    if #files > 0 then
        if analyze then
            moduledata.xml.analyzers.structure(files)
            context.page()
            moduledata.xml.analyzers.characters(files)
            context.page()
            moduledata.xml.analyzers.entities(files)
            if template then
                moduledata.xml.analyzers.allsetups(files,type(template) == "string" and template or nil)
            end
            context.page()
            for i=1,#files do
                context.type(files[i])
                context.par()
            end
        else
            context("no action given")
        end
    else
        context("no files given")
    end
\stopluacode

\stoptext
