From 2ddd33f9dce3a1254f97dc5e560f9576ba63f3ef Mon Sep 17 00:00:00 2001 From: Norbert Preining Date: Mon, 27 May 2013 01:55:10 +0000 Subject: luaxml update 5/27 git-svn-id: svn://tug.org/texlive/trunk@30712 c570f23f-e606-0410-a88d-b1316a301751 --- Master/texmf-dist/doc/luatex/luaxml/luaxml.pdf | Bin 111453 -> 114519 bytes Master/texmf-dist/doc/luatex/luaxml/luaxml.tex | 68 ++++++++++++++++++--- .../tex/luatex/luaxml/luaxml-mod-handler.lua | 25 +++++--- .../tex/luatex/luaxml/luaxml-mod-xml.lua | 20 +++--- .../tex/luatex/luaxml/luaxml-selectors.lua | 62 ++++++++++++++----- .../texmf-dist/tex/luatex/luaxml/luaxml-stack.lua | 10 ++- 6 files changed, 143 insertions(+), 42 deletions(-) diff --git a/Master/texmf-dist/doc/luatex/luaxml/luaxml.pdf b/Master/texmf-dist/doc/luatex/luaxml/luaxml.pdf index cddfdcdf5d9..3a6967c9c66 100644 Binary files a/Master/texmf-dist/doc/luatex/luaxml/luaxml.pdf and b/Master/texmf-dist/doc/luatex/luaxml/luaxml.pdf differ diff --git a/Master/texmf-dist/doc/luatex/luaxml/luaxml.tex b/Master/texmf-dist/doc/luatex/luaxml/luaxml.tex index 34363fbcf04..4dcff6a51a1 100644 --- a/Master/texmf-dist/doc/luatex/luaxml/luaxml.tex +++ b/Master/texmf-dist/doc/luatex/luaxml/luaxml.tex @@ -4,6 +4,7 @@ \begin{document} \title{The \textsc{LuaXML} library} \author{Paul Chakravarti \and Michal Hoftich} + \date{Version 0.0.2\\May 25, 2013} \maketitle \tableofcontents \section*{Introduction} @@ -87,6 +88,56 @@ Note that |simpleTreeHandler| creates tables that can be easily accessed using s \end{verbatim} it produces wrong results. It is useful mostly for data |xml| files, not for text formats like |xhtml|. +For complex xml documents with mixed content, |domHandler| is capable of representing any valid XML document: + +\begin{verbatim} +-- dom-sample.lua +-- next line enables scripts called with texlua to use luatex libraries +kpse.set_program_name("luatex") +function traverseDom(parser, current,level) + local level = level or 0 + local spaces = string.rep(" ",level) + local root= current or parser._handler.root + local name = root._name or "unnamed" + local xtype = root._type or "untyped" + local attributes = root._attr or {} + if xtype == "TEXT" then + print(spaces .."TEXT : " .. root._text) + else + print(spaces .. xtype .. " : " .. name) + end + for k, v in pairs(attributes) do + print(spaces .. " ".. k.."="..v) + end + local children = root._children or {} + for _, child in ipairs(children) do + traverseDom(parser,child, level + 1) + end +end + +local xml = require('luaxml-mod-xml') +local handler = require('luaxml-mod-handler') +local x = '

hello world, how are you?

' +local domHandler = handler.domHandler() +local parser = xml.xmlParser(domHandler) +parser:parse(x) +traverseDom(parser) +\end{verbatim} + +This produces after running with |texlua dom-sample.lua|: + +\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|. + Because at the moment it is intended mainly as support for the odsfile package, there is little documentation, what follows is the original documentation of |LuaXML|, which may be little bit obsolete now. @@ -159,8 +210,8 @@ differently. The following events are generated by the tokeniser \begin{verbatim} -handler:start - Start Tag -handler:end - End Tag +handler:starttag - Start Tag +handler:endtag - End Tag handler:text - Text handler:decl - XML Declaration handler:pi - Processing Instruction @@ -179,8 +230,8 @@ callbacks - ie. \begin{tabular}{llp{5cm}} Callback & val & attrs (table)\\ \hline -start & name & |{ attributes (name=val).. }|\\ -end & name & nil\\ +starttag & name & |{ attributes (name=val).. }|\\ +endtag & name & nil\\ text & || & nil\\ cdata & | | & nil\\ decl & "xml" & |{ attributes (name=val).. }|\\ @@ -196,11 +247,11 @@ dtd & root element & \begin{verbatim}{ _root = , }\end{verbatim}\\ \end{tabular} -(start \& end provide the character positions of the start/end of the +(starttag \& endtag provide the character positions of the start/end of the element) XML data is passed to the parser instance through the `parse' method -(Nore: must be passed a single string currently) +(Note: must be passed as single string currently) \section{Options} @@ -232,8 +283,8 @@ NOTE: Boolean options must be set to `nil' not `0' Create a handler instance - \begin{verbatim} -h = { start = function(t,a,s,e) .... end, - end = function(t,a,s,e) .... end, +h = { starttag = function(t,a,s,e) .... end, + endtag = function(t,a,s,e) .... end, text = function(t,a,s,e) .... end, cdata = text } \end{verbatim} @@ -292,7 +343,6 @@ node = { _name = , } \end{verbatim} -The dom structure is capable of representing any valid XML document \subsubsection{simpleTreeHandler} simpleTreeHandler is a simplified handler which attempts to generate a diff --git a/Master/texmf-dist/tex/luatex/luaxml/luaxml-mod-handler.lua b/Master/texmf-dist/tex/luatex/luaxml/luaxml-mod-handler.lua index bc4d8ea9120..19e4ac11358 100644 --- a/Master/texmf-dist/tex/luatex/luaxml/luaxml-mod-handler.lua +++ b/Master/texmf-dist/tex/luatex/luaxml/luaxml-mod-handler.lua @@ -1,4 +1,4 @@ -module(...,package.seeall) +--..module(...,package.seeall) -- -- Overview: -- ========= @@ -111,9 +111,10 @@ module(...,package.seeall) --@param t Table to be parsed --@returns Returns a string representation of table +local M = {} local stack = require("luaxml-stack") -function showTable(t) +local function showTable(t) local sep = '' local res = '' if type(t) ~= 'table' then @@ -129,9 +130,12 @@ function showTable(t) res = '{'..res..'}' return res end - + + +M.showTable = showTable + ---Handler to generate a simple event trace -printHandler = function() +local printHandler = function() local obj = {} obj.starttag = function(self,t,a,s,e) io.write("Start : "..t.."\n") @@ -179,9 +183,9 @@ printHandler = function() end return obj end - +M.printHandler = printHandler ---Handler to generate a lua table from a XML content string -function simpleTreeHandler() +local function simpleTreeHandler() local obj = {} obj.root = {} @@ -242,10 +246,10 @@ function simpleTreeHandler() return obj end - +M.simpleTreeHandler = simpleTreeHandler --- domHandler -function domHandler() +local function domHandler() local obj = {} obj.options = {commentNode=1,piNode=1,dtdNode=1,declNode=1} obj.root = { _children = {n=0}, _type = "ROOT" } @@ -309,9 +313,10 @@ function domHandler() obj.cdata = obj.text return obj end +M.domHandler = domHandler -- -simpleTeXhandler=function() +local simpleTeXhandler=function() local obj={} local _stack=stack.Stack:Create() obj.starttag = function(self,t,a,s,e) @@ -336,3 +341,5 @@ simpleTeXhandler=function() end return obj end +M.simpleTeXhandler = simpleTeXhandler +return M diff --git a/Master/texmf-dist/tex/luatex/luaxml/luaxml-mod-xml.lua b/Master/texmf-dist/tex/luatex/luaxml/luaxml-mod-xml.lua index f3ebb0445e8..4da0edc43bb 100644 --- a/Master/texmf-dist/tex/luatex/luaxml/luaxml-mod-xml.lua +++ b/Master/texmf-dist/tex/luatex/luaxml/luaxml-mod-xml.lua @@ -1,4 +1,4 @@ -module(...,package.seeall) +-- module(...,package.seeall) --- -- Overview: -- ========= @@ -162,7 +162,8 @@ local format= string.format ---Parses a XML string --@param handler Handler object to be used to convert the XML string --to another formats. @see handler.lua -xmlParser = function(handler) +local M={} +local xmlParser = function(handler) local obj = {} -- Public attributes @@ -192,7 +193,7 @@ xmlParser = function(handler) if not match then if string.find(str,self._WS,pos) then -- No more text - check document complete - if table.getn(self._stack) ~= 0 then + if #self._stack ~= 0 then self:_err(self._errstr.incompleteXmlErr,pos) else break @@ -477,15 +478,17 @@ xmlParser = function(handler) return obj end +M.xmlParser = xmlParser -function xmlEscape(s) +local function xmlEscape(s) local t = {['"']=""",["'"]="'",["&"]="&",["<"]="<",[">"]=">"} return string.gsub(s,"([\"'<>&])",t) end +M.xmlEscape = xmlEscape -function serialize(tb) +local function serialize(tb) local function getAttributes(k,v) local i = "" if(type(v["_attr"])=="table") then @@ -496,8 +499,9 @@ local function getAttributes(k,v) --table.remove(v,"_attr") end return i -end - local function printable(tb, level,currTag) + end + + local function printable(tb, level,currTag) local r ={} local currTag = currTag or "" level = level or 0 @@ -545,3 +549,5 @@ end end return table.concat({'',printable(tb)},"\n") end +M.serialize = serialize +return M diff --git a/Master/texmf-dist/tex/luatex/luaxml/luaxml-selectors.lua b/Master/texmf-dist/tex/luatex/luaxml/luaxml-selectors.lua index 921f971eb2e..1e65d3b8058 100644 --- a/Master/texmf-dist/tex/luatex/luaxml/luaxml-selectors.lua +++ b/Master/texmf-dist/tex/luatex/luaxml/luaxml-selectors.lua @@ -1,29 +1,63 @@ -module(...,package.seeall) +--module(...,package.seeall) - -function makeTag(s) - return "<"..s.."[^>]*>" +local M = {} +local inside = 0 +local function makeTag(s) + local function makeTag(fuf) + return fuf .. "[^>]*" + end + local print = texio.write_nl + if inside > 0 then print ("inside "..inside) else print("outside") end + --[[if inside then + return s .. "[^>]*" + else + inside = true--]] + inside = inside + 1 + local f = "<"..s.."[^>]*>" + inside = inside - 1 + return f + --end end -function matchTag(tg) + +M.makeTag = makeTag +local function matchTag(tg) return makeTag(tg) end - -function matchDescendand(a,b) +M.matchTag=matchTag +local function matchDescendand(a,b) return makeTag(a)..makeTag(b) end +M.matchDescendand = matchDescendand -function matchChild(a,b) +local function matchChild(a,b) return makeTag(a)..".*"..makeTag(b) end +M.matchChild = matchChild + +local function matchSibling(a,b) + return a .. "[^>]*".."@%("..b.."[^>]*%)" +end +M.matchSibling = matchSibling -function matchSibling(a,b) - return makeTag(a .. "[^>]*".."@%("..b.."[^>]*%)") +local function matchClass(tg,class) + return tg.."[^>]*class=[|]*[^>]*|"..class.."[^>]*|" end -function matchClass(tg,class) - return makeTag(tg.."[^>]*class=[|]*[^>]*|"..class.."[^>]*|") +M.matchClass = matchClass +local function matchId(tg,id) + return tg.."[^>]*id="..id end -matcher = {} +M.matchId = matchId +local matcher = {} +M.matcher= matcher +local function makeElement(s) + local function makeTag(fuf) + return fuf .. "[^>]*" + end + return "<"..s .. "[^>]*>" +end + +M.makeElement = makeElement function matcher.new() local self = {} local selectors={} @@ -39,4 +73,4 @@ function matcher.new() end return self end - +return M diff --git a/Master/texmf-dist/tex/luatex/luaxml/luaxml-stack.lua b/Master/texmf-dist/tex/luatex/luaxml/luaxml-stack.lua index 54386ff84d3..6c2d83610fd 100644 --- a/Master/texmf-dist/tex/luatex/luaxml/luaxml-stack.lua +++ b/Master/texmf-dist/tex/luatex/luaxml/luaxml-stack.lua @@ -1,7 +1,9 @@ -- Code from http://lua-users.org/wiki/SimpleStack -module(...,package.seeall) -Stack = {} - +--module(...,package.seeall) +local M={} +local Stack = {} +M.Stack = Stack +local unpack = table.unpack -- Create a Table with stack functions function Stack:Create() @@ -61,3 +63,5 @@ function Stack:Create() end return t end + +return M -- cgit v1.2.3