summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/luaxml/luaxml-entities.lua
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
committerNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
commite0c6872cf40896c7be36b11dcc744620f10adf1d (patch)
tree60335e10d2f4354b0674ec22d7b53f0f8abee672 /macros/luatex/generic/luaxml/luaxml-entities.lua
Initial commit
Diffstat (limited to 'macros/luatex/generic/luaxml/luaxml-entities.lua')
-rw-r--r--macros/luatex/generic/luaxml/luaxml-entities.lua37
1 files changed, 37 insertions, 0 deletions
diff --git a/macros/luatex/generic/luaxml/luaxml-entities.lua b/macros/luatex/generic/luaxml/luaxml-entities.lua
new file mode 100644
index 0000000000..dd7e6f7156
--- /dev/null
+++ b/macros/luatex/generic/luaxml/luaxml-entities.lua
@@ -0,0 +1,37 @@
+local M = {}
+local char = unicode and unicode.utf8.char or utf8.char
+local named_entities = require "luaxml-namedentities"
+local hexchartable = {}
+local decchartable = {}
+
+
+local function get_named_entity(name)
+ return named_entities[name]
+end
+
+function M.decode(s)
+ return s:gsub("&([#a-zA-Z0-9]+);?", function(m)
+ -- check if this is named entity first
+ local named = get_named_entity(m)
+ if named then return named end
+ -- check if it is numeric entity
+ local hex, charcode = m:match("#([xX]?)([a-fA-F0-9]+)")
+ -- if the entity is not numeric
+ if not charcode then return
+ "&" .. m .. ";"
+ end
+ local character
+ if hex~="" then
+ character = hexchartable[charcode] or char(tonumber(charcode,16))
+ hexchartable[charcode] = character
+ else
+ character = decchartable[charcode] or char(tonumber(charcode))
+ decchartable[charcode] = character
+ end
+ return character
+ end)
+end
+
+return M
+
+