KoboldAI-Client/extern/lualibs/pl/xml.lua

7 lines
11 KiB
Lua
Raw Normal View History

-- Penlight 1.11.0-1 | /lua/pl/xml.lua | https://github.com/lunarmodules/Penlight | License: MIT | Minified using https://www.npmjs.com/package/luamin/v/1.0.4
-- Copyright (C) 2009-2016 Steve Donovan, David Manura.
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
local a=require'pl.utils'local b=a.split;local c=table.insert;local d=table.concat;local e=table.remove;local f=string.match;local tostring=tostring;local setmetatable=setmetatable;local getmetatable=getmetatable;local pairs=pairs;local ipairs=ipairs;local type=type;local next=next;local print=print;local g=a.unpack;local h=string.gsub;local i=string.find;local pcall,require,io=pcall,require,io;local j={}local k={__type="doc"}k.__index=k;function j.new(l,m)local n={tag=l,attr=m or{},last_add={}}return setmetatable(n,k)end;function j.parse(o,p,q)local r,s,t;if q then r=j.basic_parse else s,t=pcall(require,'lxp.lom')if not s then r=j.basic_parse else r=t.parse end end;if p then local u,v=io.open(o)if not u then return nil,v end;o=u:read'*a'u:close()end;local n,v=r(o)if not n then return nil,v end;if t then j.walk(n,false,function(w,x)setmetatable(x,k)end)end;return n end;function k:addtag(l,y)local z=j.new(l,y)(self.last_add[#self.last_add]or self):add_direct_child(z)c(self.last_add,z)return self end;function k:text(A)(self.last_add[#self.last_add]or self):add_direct_child(A)return self end;function k:up()e(self.last_add)return self end;function k:reset()local B=self.last_add;for C=1,#B do B[C]=nil end;return self end;function k:add_direct_child(D)c(self,D)end;function k:add_child(D)(self.last_add[#self.last_add]or self):add_direct_child(D)return self end;function k:set_attribs(E)for F,G in pairs(E)do self.attr[F]=G end end;function k:set_attrib(H,G)self.attr[H]=G end;function k:get_attribs()return self.attr end;local function I(z)return type(z)=='string'end;function j.elem(l,J)local z=j.new(l)if I(J)then J={J}end;if j.is_tag(J)then c(z,J)elseif type(J)=='table'then for F,G in pairs(J)do if I(F)then z.attr[F]=G;c(z.attr,F)else z[F]=G end end end;return z end;function j.tags(K)local L={}if I(K)then K=b(K,'%s*,%s*')end;for w,l in ipairs(K)do local M=function(J)return j.elem(l,J)end;c(L,M)end;return g(L)end;local N={}local function O(P)if I(P)then if N[P]then P=N[P]else local Q,v=P;P,v=j.parse(Q,false,true)if not P then return nil,v end;N[Q]=P end elseif not j.is_tag(P)then return nil,"template is not a document"end;return P end;local function R(S)return#S==0 or type(S[1])~='table'end;local function T(S)for C,G in ipairs(S)do S[tostring(C)]=G end end;function k.subst(P,S)local v;if type(S)~='table'or not next(S)then return nil,"data must be a non-empty table"end;if R(S)then T(S)end;P,v=O(P)if v then return nil,v end;local function U(V)return j.clone(P,function(z)return z:gsub('%$(%w+)',V)end)end;if R(S)then return U(S)end;local K={}for w,V in ipairs(S)do T(V)c(K,U(V))end;if S.tag then K=j.elem(S.tag,K)end;return K end;function k:child_with_name(l)for w,D in ipairs(self)do if D.tag==l then return D end end end;local W;function W(self,l,K,X)for w,D in ipairs(self)do if type(D)=='table'then if D.tag==l then c(K,D)end;if X then W(D,l,K,X)end end end end;function k:get_elements_with_name(l,Y)local Z={}W(self,l,Z,not Y)return Z end;function k:children()local C=0;return function(H)C=C+1;return H[C]end,self,C end;function k:first_childtag()if#self==0 then return end;for w,E in ipairs(self)do if type(E)=='table'then return E end end end;function k:matching_tags(l,_)_=_ or self.attr.xmlns;local a0=self;local a1,a2,G=1,#a0;return function()for C=a1,a2 do G=a0[C]if(not l or G.tag==l)and(not _ or _==G.attr.xmlns)then a1=C+1;return G end end end,a0,a1 end;function k:childtags()local C=0;return function(H)local G;repeat C=C+1;G=self[C]if G and type(G)=='table'then return G end until not G end,self[1],C end;function k:maptags(a3)local a4=j.is_tag;local C=1;while C<=#self do if a4(self[C])then local a5=a3(self[C])if a5==nil then e(self,C)else self[C]=a5;C=C+1 end end end;return self end;local a6;do local a7={["'"]="&apos;",["\""]="&quot;",["<"]="&lt;",[">"]="&gt;",["&"]="&amp;"}function a6(Q)return h(Q,"['&<>\"]",a7)end;j.xml_escape=a6 end;local function a8(E,a9,self,a6,aa,ab,ac,ad)local ae=0;local l=E.tag;local af,ag=""," "if ac then af='\n'..ab end;if ad then ag='\n'..ab..ad end;c(a9,af.."<"..l)local function ah(F,G)if i(F,"\1",1,true)then