| 1 |
--- XML transformation module for LuaXML |
| 2 |
-- @module luaxml-transform |
| 3 |
-- @author Michal Hoftich <[email protected] |
| 4 |
|
| 5 |
-- code originally comes from from https://github.com/michal-h21/luaxml-mathml |
| 6 |
-- |
| 7 |
local domobject |
| 8 |
local cssquery |
| 9 |
if kpse then |
| 10 |
domobject = require "luaxml-domobject" |
| 11 |
cssquery = require "luaxml-cssquery" |
| 12 |
else |
| 13 |
domobject = require "luaxml.domobject" |
| 14 |
cssquery = require "luaxml.cssquery" |
| 15 |
end |
| 16 |
-- initialize CSS selector object |
| 17 |
local css = cssquery() |
| 18 |
|
| 19 |
|
| 20 |
-- convert Unicode characters to TeX sequences |
| 21 |
local unicodes = { |
| 22 |
[35] = "\\#", |
| 23 |
[36] = "\\$", |
| 24 |
[37] = "\\%", |
| 25 |
[38] = "\\&", |
| 26 |
[60] = "\\textless{}", |
| 27 |
[62] = "\\textgreater{}", |
| 28 |
[92] = "\\textbackslash{}", |
| 29 |
[94] = "\\^", |
| 30 |
[95] = "\\_", |
| 31 |
[123] = "\\{", |
| 32 |
[125] = "\\}" |
| 33 |
} |
| 34 |
|
| 35 |
local function match_css(element,csspar) |
| 36 |
local css = csspar or css |
| 37 |
local selectors = css:match_querylist(element) |
| 38 |
if #selectors == 0 then return nil end |
| 39 |
-- return function with the highest specificity |
| 40 |
local last_specificity = selectors[1].specificity |
| 41 |
-- if multiple selectors have the same specificity, return the last one |
| 42 |
for i, x in ipairs(selectors) do |
| 43 |
if x.specificity < last_specificity then |
| 44 |
return selectors[i-1].func |
| 45 |
end |
| 46 |
end |
| 47 |
return selectors[1].func |
| 48 |
end |
| 49 |
|
| 50 |
|
| 51 |
local function process_text(text, parameters) |
| 52 |
local parameters = parameters or {} |
| 53 |
-- spaces are collapsed by default. set verbatim=true to disable it. |
| 54 |
local verbatim = parameters.verbatim |
| 55 |
local t = {} |
| 56 |
-- process all Unicode characters and find if they should be replaced |
| 57 |
for _, char in utf8.codes(text) do |
| 58 |
-- construct new string with replacements or original char |
| 59 |
if verbatim then |
| 60 |
t[#t+1] = utf8.char(char) |
| 61 |
else |
| 62 |
t[#t+1] = unicodes[char] or utf8.char(char) |
| 63 |
end |
| 64 |
end |
| 65 |
local text = table.concat(t) |
| 66 |
if parameters.collapse_newlines==true then |
| 67 |
text = text:gsub("\n", " ") |
| 68 |
end |
| 69 |
-- verbatim can be set in parameters table. it prevent collapsing of spaces. |
| 70 |
if not verbatim then |
| 71 |
text = text:gsub("(%s%s+)", function(a) return a:sub(1,1) end) |
| 72 |
end |
| 73 |
return text |
| 74 |
end |
| 75 |
|
| 76 |
-- this function is initialized later, I need the declaration here |
| 77 |
-- to prevent Lua run-time error |
| 78 |
local process_tree |
| 79 |
|
| 80 |
--- Transform DOM element and it's children |
| 81 |
-- @param element DOM element |
| 82 |
-- @param parameters Table with settings |
| 83 |
-- @return Transformed string |
| 84 |
local function process_children(element, parameters) |
| 85 |
-- accumulate text from children elements |
| 86 |
local t = {} |
| 87 |
-- sometimes we may get text node |
| 88 |
if type(element) ~= "table" then return element end |
| 89 |
for i, elem in ipairs(element:get_children()) do |
| 90 |
if elem:is_text() then |
| 91 |
-- concat text |
| 92 |
t[#t+1] = process_text(elem:get_text(), parameters) |
| 93 |
elseif elem:is_element() then |
| 94 |
-- recursively process child elements |
| 95 |
t[#t+1] = process_tree(elem) |
| 96 |
end |
| 97 |
end |
| 98 |
return table.concat(t) |
| 99 |
end |
| 100 |
|
| 101 |
-- we need to define different actions for XML elements. The default action is |
| 102 |
-- to just process child elements and return the result |
| 103 |
local function default_action(element) |
| 104 |
return process_children(element) |
| 105 |
end |
| 106 |
|
| 107 |
function process_tree(element) |
| 108 |
-- find specific action for the element, or use the default action |
| 109 |
local element_name = element:get_element_name() |
| 110 |
local action = match_css(element) or default_action |
| 111 |
return action(element) |
| 112 |
end |
| 113 |
|
| 114 |
|
| 115 |
--- return nth child element |
| 116 |
-- @param element DOM element to be processed |
| 117 |
-- @param count Number of child element that should be returned |
| 118 |
-- @return DOM object, or nil if it cannot be found |
| 119 |
local function get_child_element(element, count) |
| 120 |
-- return specified child element |
| 121 |
local i = 0 |
| 122 |
for _, el in ipairs(element:get_children()) do |
| 123 |
-- count elements |
| 124 |
if el:is_element() then |
| 125 |
-- return the desired numbered element |
| 126 |
i = i + 1 |
| 127 |
if i == count then return el end |
| 128 |
end |
| 129 |
end |
| 130 |
end |
| 131 |
|
| 132 |
|
| 133 |
--- Default transforming function. It replaces %s by text content of the |
| 134 |
-- element, and @ {attrname} by value of an attribute |
| 135 |
-- @param s Template string |
| 136 |
-- @param parameters Table with settings |
| 137 |
-- @return transforming function |
| 138 |
local function simple_content(s,parameters) |
| 139 |
return function(element) |
| 140 |
local content = process_children(element,parameters) |
| 141 |
local parameters = parameters or {} |
| 142 |
-- process attributes |
| 143 |
-- attribute should be marked as @{name} |
| 144 |
local expanded = s:gsub("@{(.-)}", function(name) |
| 145 |
return process_text(element:get_attribute(name) or "", parameters) |
| 146 |
end) |
| 147 |
-- process child elements |
| 148 |
expanded = expanded:gsub("@<(.-)>",function(name) |
| 149 |
-- @<.> return element's content |
| 150 |
if name == "." then |
| 151 |
return content |
| 152 |
-- @<1> returns first child element's content |
| 153 |
elseif name:match("^[0-9]+$") then |
| 154 |
local child = get_child_element(element, tonumber(name)) |
| 155 |
if child then |
| 156 |
return process_tree(child) |
| 157 |
end |
| 158 |
-- @<CSS selector> returns contents of matched selectors |
| 159 |
else |
| 160 |
local t = {} |
| 161 |
for _, child in ipairs(element:query_selector(name)) do |
| 162 |
t[#t+1] = process_children(child, parameters) |
| 163 |
end |
| 164 |
-- concetanate results, separated by parameters.separator |
| 165 |
local separator = parameters.separator or "" |
| 166 |
return table.concat(t, separator) |
| 167 |
end |
| 168 |
end) |
| 169 |
|
| 170 |
return expanded:gsub("%%s", function(a) return content end) |
| 171 |
end |
| 172 |
end |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
-- actions for particular elements |
| 177 |
local actions = { |
| 178 |
|
| 179 |
} |
| 180 |
|
| 181 |
--- Use function to transform selected element |
| 182 |
-- @param selector CSS selector for the matching element |
| 183 |
-- @param fn Function that transforms the selected DOM element. |
| 184 |
-- @param csspar cssquery object. Default is set by the library, so it is not necessary to use. |
| 185 |
local function add_custom_action(selector, fn, csspar) |
| 186 |
local css = csspar or css |
| 187 |
css:add_selector(selector,fn) |
| 188 |
end |
| 189 |
|
| 190 |
--- Use template to transform selected template |
| 191 |
-- @param selector CSS selector for the matching element |
| 192 |
-- @param template String template |
| 193 |
-- @param parameters Table with extra parameters. Use "verbatim=true" to keep spacing in the processed text. |
| 194 |
-- @param csspar cssquery object. Default is set by the library, so it is not necessary to use. |
| 195 |
local function add_action(selector, template, parameters, csspar) |
| 196 |
local css = csspar or css |
| 197 |
css:add_selector(selector, simple_content(template, parameters)) |
| 198 |
end |
| 199 |
|
| 200 |
|
| 201 |
--- Remove actions for a given selector |
| 202 |
-- @param selector CSS selector for the matching element |
| 203 |
-- @param csspar cssquery object. Default is set by the library, so it is not necessary to use. |
| 204 |
local function reset_actions(selector, csspar) |
| 205 |
local css = csspar or css |
| 206 |
css:remove_selector(selector) |
| 207 |
end |
| 208 |
|
| 209 |
|
| 210 |
--- Transform XML string |
| 211 |
-- @param content String with XML content |
| 212 |
-- @return transformed string |
| 213 |
local function parse_xml(content) |
| 214 |
-- parse XML string and process it |
| 215 |
local dom = domobject.parse(content) |
| 216 |
-- start processing of DOM from the root element |
| 217 |
-- return string with TeX content |
| 218 |
return process_tree(dom:root_node()) |
| 219 |
end |
| 220 |
|
| 221 |
|
| 222 |
--- Transform XML file |
| 223 |
-- @param filename XML file name |
| 224 |
-- @return transformed string |
| 225 |
local function load_file(filename) |
| 226 |
local f = io.open(filename, "r") |
| 227 |
local content = f:read("*all") |
| 228 |
f:close() |
| 229 |
return parse_xml(content) |
| 230 |
end |
| 231 |
|
| 232 |
--- Transform XML DOM object |
| 233 |
-- @param dom DOM object |
| 234 |
-- @return transformed string |
| 235 |
local function process_dom(dom) |
| 236 |
return process_tree(dom:root_node()) |
| 237 |
end |
| 238 |
|
| 239 |
--- print transformed file to PDF using LuaTeX functions |
| 240 |
-- @param content String to be printed |
| 241 |
local function print_tex(content) |
| 242 |
-- we need to replace "\n" characters with calls to tex.sprint |
| 243 |
for s in content:gmatch("([^\n]*)") do |
| 244 |
tex.print(s) |
| 245 |
end |
| 246 |
end |
| 247 |
|
| 248 |
|
| 249 |
-- make Transformer object |
| 250 |
local Transformer |
| 251 |
|
| 252 |
--- Make new Transformer object |
| 253 |
-- @return Transformer object |
| 254 |
local function new() |
| 255 |
local self = setmetatable({}, Transformer) |
| 256 |
self.css = cssquery() |
| 257 |
-- use the default unicodes table that escapes special LaTeX characters |
| 258 |
self.unicodes = unicodes |
| 259 |
return self |
| 260 |
end |
| 261 |
|
| 262 |
--- @type Transformer |
| 263 |
Transformer = {} |
| 264 |
Transformer.__index = Transformer |
| 265 |
-- the library uses shared css variable. in order to support multiple transformers, |
| 266 |
-- we need to save the original state, set the self.css variable as the global variable |
| 267 |
-- execute library function and then set the original function back |
| 268 |
-- |
| 269 |
function Transformer:save_css() |
| 270 |
self.old_css = css |
| 271 |
css = self.css |
| 272 |
-- we must also save unicodes table |
| 273 |
self.old_unicodes = unicodes |
| 274 |
unicodes = self.unicodes |
| 275 |
end |
| 276 |
|
| 277 |
function Transformer:restore_css() |
| 278 |
css = self.old_css |
| 279 |
unicodes = self.old_unicodes |
| 280 |
end |
| 281 |
|
| 282 |
--- add a new template |
| 283 |
-- @param selector CSS selector that should be matched |
| 284 |
-- @param template use %s for element's text, and @ {name} to access attribute "name" |
| 285 |
-- @param parameters table with extra parameters |
| 286 |
-- @see add_action |
| 287 |
function Transformer:add_action(selector, template, parameters ) |
| 288 |
add_action(selector, template, parameters, self.css) |
| 289 |
end |
| 290 |
|
| 291 |
--- Use function for transformation |
| 292 |
-- @param selector CSS selector that should be matched |
| 293 |
-- @param fn DOM transforming function |
| 294 |
-- @see add_custom_action |
| 295 |
function Transformer:add_custom_action(selector, fn ) |
| 296 |
add_custom_action(selector, fn, self.css) |
| 297 |
end |
| 298 |
|
| 299 |
--- Remove all actions that match the given selector |
| 300 |
---@param selector string |
| 301 |
function Transformer:reset_actions(selector) |
| 302 |
reset_actions(selector, self.css) |
| 303 |
end |
| 304 |
|
| 305 |
-- all methods that use transformation functions must |
| 306 |
-- correctly handle the cssquery object that this library uses |
| 307 |
|
| 308 |
--- Parse XML string |
| 309 |
-- @param content String with XML content |
| 310 |
-- @return transformed string |
| 311 |
-- @see parse_xml |
| 312 |
function Transformer:parse_xml(content) |
| 313 |
self:save_css() |
| 314 |
local result = parse_xml(content) |
| 315 |
self:restore_css() |
| 316 |
return result |
| 317 |
end |
| 318 |
|
| 319 |
-- make method for load_file function |
| 320 |
|
| 321 |
--- Transform XML file |
| 322 |
-- @param filename XML file name |
| 323 |
-- @return transformed string |
| 324 |
-- @see load_file |
| 325 |
function Transformer:load_file(filename) |
| 326 |
self:save_css() |
| 327 |
local result = load_file(filename) |
| 328 |
self:restore_css() |
| 329 |
return result |
| 330 |
end |
| 331 |
|
| 332 |
-- make method for process_dom function |
| 333 |
|
| 334 |
--- Transform XML DOM object |
| 335 |
-- @param dom DOM object |
| 336 |
-- @return transformed string |
| 337 |
-- @see process_dom |
| 338 |
function Transformer:process_dom(dom) |
| 339 |
self:save_css() |
| 340 |
local result = process_dom(dom) |
| 341 |
self:restore_css() |
| 342 |
return result |
| 343 |
end |
| 344 |
|
| 345 |
|
| 346 |
--- @export |
| 347 |
local M = { |
| 348 |
parse_xml = parse_xml, |
| 349 |
process_children = process_children, |
| 350 |
get_child_element= get_child_element, |
| 351 |
print_tex = print_tex, |
| 352 |
add_action = add_action, |
| 353 |
add_custom_action = add_custom_action, |
| 354 |
reset_actions = reset_actions, |
| 355 |
simple_content = simple_content, |
| 356 |
load_file = load_file, |
| 357 |
process_dom = process_dom, |
| 358 |
new = new |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
return M |