summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/luatex/luaxml
diff options
context:
space:
mode:
authorKarl Berry <karl@freefriends.org>2014-01-20 23:19:52 +0000
committerKarl Berry <karl@freefriends.org>2014-01-20 23:19:52 +0000
commitc22509075f62a577b84ac0a4260769351460fd14 (patch)
tree404b9b458d848725bae68cc60b342c1c6d54c7dc /Master/texmf-dist/tex/luatex/luaxml
parent72cab7bce1d68c5d53ee82a50de80e3aa10950fb (diff)
luxaml (20jan14)
git-svn-id: svn://tug.org/texlive/trunk@32741 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/luatex/luaxml')
-rw-r--r--Master/texmf-dist/tex/luatex/luaxml/dom-sample.lua29
-rw-r--r--Master/texmf-dist/tex/luatex/luaxml/luaxml-mod-handler.lua10
-rw-r--r--Master/texmf-dist/tex/luatex/luaxml/luaxml-pretty.lua89
-rw-r--r--Master/texmf-dist/tex/luatex/luaxml/luaxml-testxml.lua147
4 files changed, 270 insertions, 5 deletions
diff --git a/Master/texmf-dist/tex/luatex/luaxml/dom-sample.lua b/Master/texmf-dist/tex/luatex/luaxml/dom-sample.lua
new file mode 100644
index 00000000000..d3fcde51ff0
--- /dev/null
+++ b/Master/texmf-dist/tex/luatex/luaxml/dom-sample.lua
@@ -0,0 +1,29 @@
+--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 = '<p>hello <a href="http://world.com/">world</a>, how are you?</p>'
+local domHandler = handler.domHandler()
+local parser = xml.xmlParser(domHandler)
+parser:parse(x)
+traverseDom(parser)
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 19e4ac11358..155ce28d9f3 100644
--- a/Master/texmf-dist/tex/luatex/luaxml/luaxml-mod-handler.lua
+++ b/Master/texmf-dist/tex/luatex/luaxml/luaxml-mod-handler.lua
@@ -200,7 +200,7 @@ local function simpleTreeHandler()
self:reduce(v,k,node)
end
end
- if table.getn(node) == 1 and not self.options.noreduce[key] and
+ if #node == 1 and not self.options.noreduce[key] and
node._attr == nil then
parent[key] = node[1]
else
@@ -214,7 +214,7 @@ local function simpleTreeHandler()
node._attr=a
end
- local current = self.stack[table.getn(self.stack)]
+ local current = self.stack[#self.stack]
if current[t] then
table.insert(current[t],node)
else
@@ -224,8 +224,8 @@ local function simpleTreeHandler()
end
obj.endtag = function(self,t,s)
- local current = self.stack[table.getn(self.stack)]
- local prev = self.stack[table.getn(self.stack)-1]
+ local current = self.stack[#self.stack]
+ local prev = self.stack[#self.stack-1]
if not prev[t] then
error("XML Error - Unmatched Tag ["..s..":"..t.."]\n")
end
@@ -237,7 +237,7 @@ local function simpleTreeHandler()
end
obj.text = function(self,t)
- local current = self.stack[table.getn(self.stack)]
+ local current = self.stack[#self.stack]
table.insert(current,t)
end
diff --git a/Master/texmf-dist/tex/luatex/luaxml/luaxml-pretty.lua b/Master/texmf-dist/tex/luatex/luaxml/luaxml-pretty.lua
new file mode 100644
index 00000000000..44f31839494
--- /dev/null
+++ b/Master/texmf-dist/tex/luatex/luaxml/luaxml-pretty.lua
@@ -0,0 +1,89 @@
+--module(...,package.seeall)
+
+--- Lua pretty printer from <a href="http://mini.net/cgi-bin/lua/44.html">http://mini.net/cgi-bin/lua/44.html</a><br/>
+-- This was extracted from utility code in "util.lua".<br/>
+-- 23/02/2001 jcw@equi4.com<br/>
+-- Pretty displays a value, properly dealing with tables and cycles
+
+local displayvalue=
+ function (s)
+ if not s or type(s)=='function' or type(s)=='userdata' then
+ s=tostring(s)
+ elseif type(s)~='number' then
+ s=string.gsub(string.format('%q',s),'^"([^"\']*)"$',"'%1'")
+ end
+ return s
+ end
+
+local askeystr=
+ function (u,s)
+ if type(u)=='string' and string.find(u,'^[%w_]+$') then return s..u end
+ return '['..displayvalue(u)..']'
+ end
+
+local horizvec=
+ function (x,n)
+ local o,e='',''
+ for i=1,#x do
+ if type(x[i])=='table' then return end
+ o=o..e..displayvalue(x[i])
+ if string.len(o)>n then return end
+ e=','
+ end
+ return '('..o..')'
+ end
+
+local horizmap=
+ function (x,n)
+ local o,e='',''
+ for k,v in pairs(x) do
+ if type(v)=='table' then return end
+ o=o..e..askeystr(k,'')..'='..displayvalue(v)
+ if string.len(o)>n then return end
+ e=','
+ end
+ return '{'..o..'}'
+ end
+local M = {}
+local function pretty(p,x,h,q)
+ if not p then p,x='globals',globals() end
+ if type(x)=='table' then
+ if not h then h={} end
+ if h[x] then
+ x=h[x]
+ else
+ if not q then q=p end
+ h[x]=q
+ local s={}
+ for k,v in pairs(x) do table.insert(s,k) end
+ if #s>0 then
+ local n=75-string.len(p)
+ local f=#s==#x and horizvec(x,n)
+ if not f then f=horizmap(x,n) end
+ if not f then
+ table.sort(s,function (a,b)
+ --if tag(a)~=tag(b) then a,b=tag(b),tag(a) end
+ if type(a)~=type(b) then a,b=type(b),type(a) end
+ return a<b
+ end)
+ for i=1,#s do
+ if s[i] then
+ local u=askeystr(s[i],'.')
+ pretty(p..u,x[s[i]],h,q..u)
+ p=string.rep(' ',string.len(p))
+ end
+ end
+ return
+ end
+ x=f
+ else
+ x='{}'
+ end
+ end
+ else
+ x=displayvalue(x)
+ end
+ print(p..' = '..x)
+end
+M.pretty = pretty
+return M
diff --git a/Master/texmf-dist/tex/luatex/luaxml/luaxml-testxml.lua b/Master/texmf-dist/tex/luatex/luaxml/luaxml-testxml.lua
new file mode 100644
index 00000000000..82316421162
--- /dev/null
+++ b/Master/texmf-dist/tex/luatex/luaxml/luaxml-testxml.lua
@@ -0,0 +1,147 @@
+#!/usr/bin/lua
+---Simple command line test parser - applies handler[s] specified
+-- to XML file (or STDIN) and dumps results<br/>
+--
+-- $Id: testxml.lua,v 1.1.1.1 2001/11/28 06:11:33 paulc Exp $<br/>
+--
+-- $Log: testxml.lua,v $<br/>
+-- Revision 1.1.1.1 2001/11/28 06:11:33 paulc<br/>
+-- Initial Import
+--
+
+modxml = require('luaxml-mod-xml')
+handler = require('luaxml-mod-handler')
+pretty = require('luaxml-pretty')
+
+
+-- Defaults
+_print = nil
+_simpletree = nil
+_dom = nil
+_file = nil
+_xmlrpc = nil
+_debug = nil
+_ws = nil
+_noentity = nil
+
+_usage = [[
+textxml.lua [-print] [-simpletree] [-dom] [-xmlrpc] [-debug]
+ [-ws] [-noentity] [-help] [file]
+]]
+
+_help = [[
+testxml.lua - Simple command line XML processor
+
+Options:
+
+ -print : Generate event dump (default)
+ -simpletree : Generate simple tree
+ -dom : Generate DOM-like tree
+ -debug : Print debug info (filename/text)
+ -ws : Do not strip whitespace
+ -noentity : Do not expand entities
+ -help : Print help
+ file : XML File (parse stdin in nil)
+]]
+
+index = 1
+local exit = os.exit
+
+function setOptions(x)
+ if _ws then
+ x.options.stripWS = nil
+ end
+ if _noentity then
+ x.options.expandEntities = nil
+ end
+end
+
+while arg[index] do
+ --print (arg[index])
+ if (string.sub(arg[index],1,1)=='-') then
+ if arg[index] == "-print" then
+ _print = 1
+ elseif arg[index] == "-simpletree" then
+ _simpletree= 1
+ elseif arg[index] == "-dom" then
+ _dom= 1
+ elseif arg[index] == "-xmlrpc" then
+ _xmlrpc= 1
+ elseif arg[index] == "-debug" then
+ _debug = 1
+ elseif arg[index] == "-ws" then
+ _ws = 1
+ elseif arg[index] == "-noentity" then
+ _noentity = 1
+ elseif arg[index] == "-help" then
+ print(_usage)
+ exit()
+ else
+ print(_usage)
+ exit()
+ end
+ else
+ -- Filename is last argument if present
+ if arg[index+1] then
+ print(_usage)
+ exit()
+ else
+ _file = arg[index]
+ end
+ end
+ index = index + 1
+end
+
+if _file then
+ print("File",_file)
+ if (_debug) then
+ io.write ( "File: ".._file.."\n" )
+ end
+ --xml = read(openfile(_file,"r"),"*a")
+
+ local f, e = io.open(_file, "r")
+ if f then
+ xml = f:read("*a")
+ else
+ error(e)
+ end
+
+else
+ xml = io.read("*a")
+end
+
+if _debug then
+ io.write ( "----------- XML\n" )
+ io.write (xml.."\n")
+end
+
+if _print or not (_print or _dom or _simpletree or _print or _xmlrpc) then
+ io.write ( "----------- Print\n" )
+ h = handler.printHandler()
+ x = modxml.xmlParser(h)
+ setOptions(x)
+ x:parse(xml)
+end
+
+if _simpletree then
+ io.write ( "----------- SimpleTree\n" )
+ h = handler.simpleTreeHandler()
+ x = modxml.xmlParser(h)
+ setOptions(x)
+ x:parse(xml)
+ pretty.pretty('root',h.root)
+end
+
+if _dom then
+ io.write ( "----------- Dom\n" )
+ h = handler.domHandler()
+ x = modxml.xmlParser(h)
+ setOptions(x)
+ x:parse(xml)
+ pretty.pretty('root',h.root)
+ io.write ( "-----------\n" )
+end
+
+
+
+