Skip to content

Commit 8129c68

Browse files
committed
working on environments
1 parent eb655f7 commit 8129c68

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

luaxml-sty.lua

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function luaxml_sty.parse_snippet(current, xml_string)
6666
else
6767
dom = domobject.html_parse(xml_string)
6868
end
69-
print(dom:serialize())
69+
luaxml_sty.debug(dom:serialize())
7070
local result = transform:process_dom(dom)
7171
luaxml_sty.packages.transform.print_tex(result)
7272
end
@@ -82,4 +82,29 @@ function luaxml_sty.parse_file(current, filename)
8282
luaxml_sty.parse_snippet(current, content)
8383
end
8484

85+
--- parse environment contents using Lua
86+
-- idea from https://tex.stackexchange.com/a/574323/2891
87+
function luaxml_sty.store_lines(env_name, callback_name)
88+
return function(str)
89+
print("str", str)
90+
local env_str = [[\end{]] .. env_name .. "}"
91+
if string.find (str , env_str:gsub("%*", "%%*")) then
92+
print("end of environment")
93+
luatexbase.remove_from_callback ( "process_input_buffer" , callback_name)
94+
return env_str -- str
95+
else
96+
table.insert(luaxml_sty.verb_table, str)
97+
end
98+
return ""
99+
end
100+
end
101+
102+
function luaxml_sty.register_verbatim(env_name)
103+
luaxml_sty.verb_table = {}
104+
local callback_name = "luaxml_store_lines_".. env_name
105+
local fn = luaxml_sty.store_lines(env_name, callback_name)
106+
luatexbase.add_to_callback(
107+
"process_input_buffer" , fn , callback_name)
108+
end
109+
85110
return luaxml_sty

luaxml.sty

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,33 @@ luaxml_sty.current.transformation = "#1",
8080
}
8181
}
8282

83+
\NewDocumentEnvironment{LXMLCode}{O{} }{
84+
\directlua{
85+
luaxml_sty.set_xml()
86+
luaxml_sty.register_verbatim("LXMLCode")
87+
%luaxml_sty.parse_snippet("\luaescapestring{#1}", [[\detokenize{#2}]])
88+
}
89+
}{}
90+
91+
%\NewDocumentEnvironment{LXMLCode*}{}{
92+
% I couldn't process the first line in the environment using the Lua callback with \NewDocumentEnvironment,
93+
% so \newenvironment is used instead.
94+
\newenvironment{LXMLCode*}{
95+
\directlua{
96+
luaxml_sty.set_html()
97+
luaxml_sty.register_verbatim("LXMLCode*")
98+
%luaxml_sty.parse_snippet("\luaescapestring{#1}", [[\detokenize{#2}]])
99+
}
100+
%\tl_set:Nn\l_luaxml_transformer{#1}
101+
}{
102+
\directlua{
103+
print(table.concat(luaxml_sty.verb_table, ":"))
104+
% workaround for \n, which cannot be used here
105+
print(table.concat(luaxml_sty.verb_table, string.char(13)))
106+
luaxml_sty.parse_snippet("\l_luaxml_transformer", table.concat(luaxml_sty.verb_table, string.char(13)))
107+
}
108+
}
109+
110+
83111
\ExplSyntaxOff
84112
\endinput

0 commit comments

Comments
 (0)