Debian Codegraph @

texmf-dist/tex/luatex/luaxml/luaxml-entities.lua

raw
1 local M = {}
2 local char = unicode and unicode.utf8.char or utf8.char
3 local named_entities
4 if kpse then
5 named_entities = require "luaxml-namedentities"
6 else
7 named_entities = require "luaxml.namedentities"
8 end
9 local hexchartable = {}
10 local decchartable = {}
11
12
13 local function get_named_entity(name)
14 return named_entities[name]
15 end
16
17 local function test_invalid_unicode(charnumber)
18 return charnumber > 127 and charnumber < 256
19 end
20
21 local function get_entity(charcode, chartable, base)
22 local character = chartable[charcode]
23 if not character then
24 local charnumber = tonumber(charcode,base)
25 -- if test_invalid_unicode(charnumber) then
26 -- return nil
27 -- end
28 character = char(charnumber)
29 chartable[charcode] = character
30 end
31 return character
32 end
33
34
35 function M.decode(s)
36 return s:gsub("&([#a-zA-Z0-9%_%:%-]-);", function(m)
37 -- check if this is named entity first
38 local named = get_named_entity(m)
39 local original_entity = "&" .. m .. ";"
40 if named then return named end
41 -- check if it is numeric entity
42 local hex, charcode = m:match("#([xX]?)([a-fA-F0-9]+)")
43 -- if the entity is not numeric
44 if not charcode then return
45 original_entity
46 end
47 local character
48 if hex~="" then
49 character = get_entity(charcode, hexchartable, 16) or original_entity
50 else
51 character = get_entity(charcode, decchartable, 10)
52 end
53 return character
54 end)
55 end
56
57 return M
58
59