With attributes it is difficult to know when marked contend is really finished because when attribute removed (set to -"7FFFFFFF) it just stops adding new attributes. If latest written node with some current attribute would get some meaningful value that this is a final node with that attribute that would be perfect. For example \setbox0=\hbox {very long} \attribute2=5 Section \unhbox0 title \attribute2=-"7FFFFFFF And if I say that everything with attributes (2=5) is semantic section in this document than I'll get two "sections" in this case because when I'll be scanning nodes I will not know that this box without attributes is part of the section and I need to seek for other node without attribute which should mean ending. On 5/30/2024 11:25 AM, Max Chernoff wrote:
Hi Linas,
On Thu, 2024-05-30 at 10:39 +0300, Linas Stonys wrote:
For example if there would be few primitives which could add an attribute or/and properties to next (yet to be created) non discardable item like \luaAddFeaturesNodeNext {{attr=1, properties={"<section>}}} \section{foo} \luaAddFeaturesNodePrev{{attr=1, properties={"</section>}}} The primitive \attribute is pretty close. You just need a little bit of Lua to make it behave exactly like you want:
\attributedef\myattribute=1234 % or use \newattribute \newcount\mycount \def\addattribute#1{% \advance\mycount by 1 \global\myattribute=\mycount \global\expandafter\def\csname myattribute\the\mycount\endcsname{#1}% }
\catcode`\~=12 \directlua{% attribute_number = token.create("myattribute").index local function recurse(head) for n in node.traverse(head) do if n.list then recurse(n.list) end
local attr = node.get_attribute(n, attribute_number) local value if attr and n.id ~= node.id("glue") and n.id ~= node.id("local_par") then value = token.get_macro("myattribute" .. attr) token.set_macro("myattribute" .. attr, nil, "global") end
if value and value ~= "" then local colour = node.new("whatsit", "pdf_literal") node.unset_attribute(colour, attribute_number) colour.mode = 2 colour.data = value node.insert_before(head, n, colour) end end return head end callback.register("pre_output_filter", recurse) }
\addattribute{1 0 0 rg} abc \addattribute{0 1 0 rg} def \addattribute{0 0 1 rg} ghi
\bye
-- Max