Debian Codegraph @

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

raw
1 --- Helper functions for the luaxml.sty package
2 --- @module luaxml-sty
3 --- @author Michal Hoftich <[email protected]
4 -- provide global object with all variables we will use
5 luaxml_sty = {
6 current = {
7 transformation = "default",
8 parameters = {}, -- "parameters" argument for transform:add_action
9 },
10 packages = {},
11 -- we want to support multiple transformation objects, they will be stored here
12 transformations = {},
13 }
14 luaxml_sty.packages.transform = require "luaxml-transform"
15 luaxml_sty.packages.domobject = require "luaxml-domobject"
16
17 -- declare default transformer, used if no explicit transformer is used in LuaXML LaTeX commands
18 luaxml_sty.transformations.default = luaxml_sty.packages.transform.new()
19
20 -- debuggind functions
21 function luaxml_sty.error(...)
22 local arg = {...}
23 print("LuaXML error: " .. table.concat(arg, " "))
24 end
25
26 luaxml_sty.do_debug = false
27
28 function luaxml_sty.debug(...)
29 if luaxml_sty.do_debug then
30 local arg = {...}
31 print("LuaXML: " .. table.concat(arg, " "))
32 end
33 end
34
35 --- Declare new transformer
36 ---@param name string transformer name
37 ---@return table transformer object
38 function luaxml_sty.declare_transformer(name)
39 luaxml_sty.transformations[name] = luaxml_sty.packages.transform.new()
40 return luaxml_sty.transformations[name]
41 end
42
43
44 --- Add luaxml-transform rule
45 --- @param current string transformer name, empty for the default object
46 --- @param selector string CSS selector to be used
47 --- @param rule string luaxml-transform rule
48 function luaxml_sty.add_rule(current, selector, rule)
49 if current == "" then
50 current = luaxml_sty.current.transformation
51 end
52 -- the +v parameter type in LaTeX replaces newlines with \obeyedline. we need to replace it back to newlines
53 rule = rule:gsub("\\obeyedline", "\n")
54 luaxml_sty.debug("************* luaxml_sty rule: " .. selector, rule, current, (luaxml_sty.current.parameters.verbatim and "verbatim" or "not verbatim"))
55 local transform = luaxml_sty.transformations[current] or luaxml_sty.declare_transformer(current)
56 if not transform then
57 luaxml_sty.error("Cannot find LuaXML transform object: " .. (current or ""))
58 return nil, "Cannot find LuaXML transform object: " .. (current or "")
59 end
60 transform:add_action(selector, rule, luaxml_sty.current.parameters)
61 end
62
63 -- by default, we will use XML parser, so use_xml is set to true
64 luaxml_sty.use_xml = true
65
66 --- Use XML parser for parsing of next snippets
67 function luaxml_sty.set_xml()
68 luaxml_sty.use_xml = true
69 end
70
71
72 --- Use HTML parser for parsing of next snippets
73 function luaxml_sty.set_html()
74 luaxml_sty.use_xml = false
75 end
76
77 --- transform XML string and print it to the output
78 ---@param current string transformer name, empty for the default object
79 ---@param xml_string string to be transformed
80 function luaxml_sty.parse_snippet(current, xml_string)
81 local domobject = luaxml_sty.packages.domobject
82 -- get the current transformer object
83 if current == "" then
84 current = luaxml_sty.current.transformation
85 end
86 local transform = luaxml_sty.transformations[current]
87 if not transform then
88 luaxml_sty.error("Cannot load transformer: " .. current)
89 return nil, "Cannot load transformer: " .. current
90 end
91 local dom
92 -- decide if we should use XML or HTML parser
93 if luaxml_sty.use_xml then
94 dom = domobject.parse(xml_string)
95 else
96 dom = domobject.html_parse(xml_string)
97 end
98 luaxml_sty.debug(dom:serialize())
99 local result = transform:process_dom(dom)
100 luaxml_sty.debug(result)
101 luaxml_sty.packages.transform.print_tex(result)
102 end
103
104 --- Transform file
105 ---@param current string transformer name, empty for the default object
106 ---@param filename string file to be transformed
107 function luaxml_sty.parse_file(current, filename)
108 local f = io.open(filename, "r")
109 if not f then
110 luaxml_sty.packages.transform.print_tex("\\textbf{LuaXML error}: cannot find file " .. filename)
111 return nil, "Cannot find file " .. filename
112 end
113 local content = f:read("*a")
114 f:close()
115 luaxml_sty.parse_snippet(current, content)
116 end
117
118 --- parse environment contents using Lua
119 ---@param env_name string environment name
120 ---@param callback_name string name which will be used in the callback registration
121 ---@return function
122 -- idea from https://tex.stackexchange.com/a/574323/2891
123 function luaxml_sty.store_lines(env_name, callback_name)
124 return function(str)
125 luaxml_sty.debug("str", str)
126 local env_str = [[\end{]] .. env_name .. "}"
127 if string.find (str , env_str:gsub("%*", "%%*")) then
128 luaxml_sty.debug("end of environment")
129 luatexbase.remove_from_callback ( "process_input_buffer" , callback_name)
130 return env_str -- str
131 else
132 table.insert(luaxml_sty.verb_table, str)
133 end
134 return ""
135 end
136 end
137
138 --- require line grabbing for an environment
139 ---@param env_name string environment name
140 function luaxml_sty.register_verbatim(env_name)
141 luaxml_sty.verb_table = {}
142 local callback_name = "luaxml_store_lines_".. env_name
143 local fn = luaxml_sty.store_lines(env_name, callback_name)
144 luatexbase.add_to_callback(
145 "process_input_buffer" , fn , callback_name)
146 end
147
148 --- parse content of the previous environment registered using luaxml_sty.register_verbatim()
149 --- and print the transformed content
150 ---@param transformer string transformer name, empty for the default object
151 function luaxml_sty.print_verbatim(transformer)
152 luaxml_sty.parse_snippet(transformer, table.concat(luaxml_sty.verb_table, "\n"))
153 end
154
155 return luaxml_sty