\documentclass{ltxdoc} % \usepackage{tgschola,url} \usepackage{url} \usepackage[english]{babel} \usepackage{hyperref} \usepackage{luacode} \usepackage{framed} % Version is defined in the makefile, use default values when compiled directly \ifdefined\version\else \def\version{v0.1h} \let\gitdate\date \fi \newcommand\modulename[1]{\subsection{#1}\label{sec:#1}} \newcommand\modulesummary[1]{#1\\} \newcommand\moduleclass[1]{\subsubsection{Class: #1}} \newcommand\functionname[2]{\par\noindent\textbf{#1(#2)}\\} \newcommand\functionsummary[1]{#1\\\textbf{Parameters:}\\} \newcommand\functionparam[2]{\texttt{#1}: #2\\} \newcommand\functionreturn[1]{\textbf{Return: }\\#1\\} \begin{document} \title{The \textsc{LuaXML} library} \author{Paul Chakravarti \and Michal Hoftich} \date{Version \version\\\gitdate} \maketitle \tableofcontents \section{Introduction} |LuaXML| is pure lua library for processing and serializing of the |xml| files. The base code code has been written by Paul Chakravarti, with minor changes which brings Lua 5.3 or HTML 5 support. On top of that, new modules for accessing the |xml| files using |DOM| like methods or |CSS| selectors\footnote{Thanks to Leaf Corcoran for |CSS selector| parsing code.} have been added. The documentation is divided to three parts -- first part deals with the |DOM| library, second part describes the low-level libraries and the third part is original documentation by Paul Chakravarti. % Current release is aimed mainly as support for the odsfile package. % In first release it was included with the odsfile package, % but as it is general library which can be used also with other packages, % I decided to distribute it as separate library. \section{The \texttt{DOM\_Object} library} This library can process a |xml| sources using |DOM| like functions. To load it, you need to require |luaxml-domobject.lua| file. The |parse| function provided by the library creates \texttt{DOM\_Object} object, which provides several methods for processing the |xml| tree. \begin{verbatim} local dom = require "luaxml-domobject" local document = [[
hello
]] -- dom.parse returns the DOM_Object local obj = dom.parse(document) -- it is possible to call methods on the object local root_node = obj:root_node() for _, x in ipairs(root_node:get_children()) do print(x:get_element_name()) end \end{verbatim} The details about available methods can be found in the API docs, section \ref{sec:luaxml-domobject}. The above code will load a |xml| document, it will get the ROOT element and print all it's children element names. The \verb|DOM_Object:get_children| function returns Lua table, so it is possible to loop over it using standard table functions. \begin{framed} \begin{luacode*} dom = require "luaxml-domobject" local document = [[hello
]] -- dom.parse returns the DOM_Object obj = dom.parse(document) -- it is possible to call methods on the object local root_node = obj:root_node() for _, x in ipairs(root_node:get_children()) do tex.print(x:get_element_name().. "\\par") end \end{luacode*} \end{framed} \subsection{Node selection methods} There are some other methods for element retrieving. \subsubsection{The \texttt{DOM\_Object:get\_path} method} If you want to print text content of all child elements of the body element, you can use \verb|DOM_Object:get_path|: \begin{verbatim} local path = obj:get_path("html body") for _, el in ipairs(path[1]:get_children()) do print(el:get_text()) end \end{verbatim} The \verb|DOM_Object:get_path| function always return array with all elements which match the requested path, even it there is only one such element. In this case, it is possible to use standard Lua table indexing to get the first and only one matched element and get it's children using \verb|DOM_Object:get_children| method. It the children node is an element, it's text content is printed using \verb|DOM_Object:get_text|. \begin{framed} \begin{luacode*} local path = obj:get_path("html body") for _, el in ipairs(path[1]:get_children()) do if el:is_element() then tex.print(el:get_text().."\\par") end end \end{luacode*} \end{framed} \subsubsection{The \texttt{DOM\_Object:query\_selector} method} This method uses |CSS selector| syntax to select elements, similarly to JavaScript \textit{jQuery} library. \begin{verbatim} for _, el in ipairs(obj:query_selector("h1,p")) do print(el:get_text()) end \end{verbatim} \begin{framed} \begin{luacode*} for _, el in ipairs(obj:query_selector("h1,p")) do tex.print(el:get_text().."\\par") end \end{luacode*} \end{framed} It supports also |XML| namespaces, using \verb_namespace|element_ syntax. \subsection{Element traversing} \subsubsection{The \texttt{DOM\_Object:traverse\_elements} method} It may be useful to traverse over all elements and apply a function on all of them. \begin{verbatim} obj:traverse_elements(function(node) print(node:get_text()) end) \end{verbatim} \begin{framed} \begin{luacode*} obj:traverse_elements(function(node) tex.print(node:get_text().."\\par") end) \end{luacode*} \end{framed} The \verb|get_text| method gets text from all children elements, so the first line shows all text contained in the \verb|| element, the second one in \verb|| element and so on. \subsection{DOM modifications} It is possible to add new elements, text nodes, or to remove them. \begin{verbatim} local headers = obj:query_selector("h1") for _, header in ipairs(headers) do header:remove_node() end -- query selector returns array, we must retrieve the first element -- to get the actual body element local body = obj:query_selector("body")[1] local paragraph = body:create_element("p", {}) body:add_child_node(paragraph) paragraph:add_child_node(paragraph:create_text_node("This is a second paragraph")) for _, el in ipairs(body:get_children()) do if el:is_element() then print(el:get_element_name().. ": ".. el:get_text()) end end \end{verbatim} In this example, \verb|Some text, italics
]] local dom = domobj.parse(xmltext) local css = cssobj() css:add_selector("h1", function(obj) print("header found: " .. obj:get_text()) end) css:add_selector("p", function(obj) print("paragraph found: " .. obj:get_text()) end) css:add_selector("i", function(obj) print("found italics: " .. obj:get_text()) end) dom:traverse_elements(function(el) -- find selectors that match the current element local querylist = css:match_querylist(el) -- add templates to the element css:apply_querylist(el,querylist) end) \end{verbatim} \begin{framed} \begin{luacode*} local cssobj = require "luaxml-cssquery" local domobj = require "luaxml-domobject" local print = function(s) tex.print(s .. "\\par") end local xmltext = [[Some text, italics
]] local dom = domobj.parse(xmltext) local css = cssobj() css:add_selector("h1", function(obj) print("header found: " .. obj:get_text()) end) css:add_selector("p", function(obj) print("paragraph found: " .. obj:get_text()) end) css:add_selector("i", function(obj) print("found italics: " .. obj:get_text()) end) dom:traverse_elements(function(el) -- find selectors that match the current element local querylist = css:match_querylist(el) -- add templates to the element css:apply_querylist(el,querylist) end) \end{luacode*} \end{framed} More complete example may be found in the \texttt{examples} directory in the \texttt{LuaXML} source code repository\footnote{\url{https://github.com/michal-h21/LuaXML/blob/master/examples/xmltotex.lua}}. \section{The API documentation} \input{doc/api.tex} \section{Low-level functions usage} % The processing is done with several handlers, their usage will be shown in the % following section. Full description of handlers is given in the original % documentation in section \ref{sec:handlers}. % \subsection{Usage examples} The original |LuaXML| library provides some low-level functions for |XML| handling. First of all, we need to load the libraries: \begin{verbatim} xml = require('luaxml-mod-xml') handler = require('luaxml-mod-handler') \end{verbatim} The |luaxml-mod-xml| file contains the xml parser and also the serializer. In |luaxml-mod-handler|, various handlers for dealing with xml data are defined. Handlers transforms the |xml| file to data structures which can be handled from the Lua code. More information about handlers can be found in the original documentation, section \ref{sec:handlers}. \subsection{The simpleTreeHandler} \begin{verbatim} sample = [[hello world, how are you?
' local domHandler = handler.domHandler() local parser = xml.xmlParser(domHandler) parser:parse(x) traverseDom(domHandler.root) \end{verbatim} The ROOT element is stored in |domHandler.root| table, it's child nodes are stored in |_children| tables. Node type is saved in |_type| field, if the node type is |ELEMENT|, then |_name| field contains element name, |_attr| table contains element attributes. |TEXT| node contains text content in |_text| field. The previous code produces following output in the terminal: % after command % |texlua dom-sample.lua| running: \begin{verbatim} ROOT : unnamed ELEMENT : p TEXT : hello ELEMENT : a href=http://world.com/ TEXT : world TEXT : , how are you? \end{verbatim} % With \verb|domHandler|, you can process documents with mixed content, like % \verb|xhtml|, so it is a most powerful handler. \clearpage \part{Original \texttt{LuaXML} documentation by Paul Chakravarti} \medskip \noindent This document was created automatically from the original source code comments using Pandoc\footnote{\url{http://johnmacfarlane.net/pandoc/}} \section{Overview} This module provides a non-validating XML stream parser in Lua. \section{Features} \begin{itemize} \item Tokenises well-formed XML (relatively robustly) \item Flexible handler based event api (see below) \item Parses all XML Infoset elements - ie. \begin{itemize} \item Tags \item Text \item Comments \item CDATA \item XML Decl \item Processing Instructions \item DOCTYPE declarations \end{itemize} \item Provides limited well-formedness checking (checks for basic syntax \& balanced tags only) \item Flexible whitespace handling (selectable) \item Entity Handling (selectable) \end{itemize} \section{Limitations} \begin{itemize} \item Non-validating \item No charset handling \item No namespace support \item Shallow well-formedness checking only (fails to detect most semantic errors) \end{itemize} \section{API} The parser provides a partially object-oriented API with functionality split into tokeniser and hanlder components. The handler instance is passed to the tokeniser and receives callbacks for each XML element processed (if a suitable handler function is defined). The API is conceptually similar to the SAX API but implemented differently. The following events are generated by the tokeniser \begin{verbatim} handler:starttag - Start Tag handler:endtag - End Tag handler:text - Text handler:decl - XML Declaration handler:pi - Processing Instruction handler:comment - Comment handler:dtd - DOCTYPE definition handler:cdata - CDATA \end{verbatim} The function prototype for all the callback functions is \begin{verbatim} callback(val,attrs,start,end) \end{verbatim} where attrs is a table and val/attrs are overloaded for specific callbacks - ie. \begin{tabular}{llp{5cm}} Callback & val & attrs (table)\\ \hline starttag & name & |{ attributes (name=val).. }|\\ endtag & name & nil\\ text & |