In addition to posting the right answer for the specific example in my question, please feel free to leave short snippets of code that display capabilities of node library
Am new to LuaTeX node library and learning how it can be utilized to post-process lines using post_linebreak_filter. So far I am able to do very basic maneuvering, like in the answer to my own question here. Next I tried to traverse over hlist nodes with the hope to change their leftskip/rightskip and to convert raggedright text to raggedleft. My trail-and-error didn't work, am posting my incorrect attempts below (Caution: Attempt-1 seems to enter infinite loop, so don't try it in your main terminal). Can someone please explain what is wrong with these, and show how the correct code would look like (with some explanation)?
% Attempt-1: Set leftskip/rightskip outside, and rebox the contents using hpack?
\directlua{
function my_post_lb_filter(head,groupcode)
local HLIST = node.id("hlist") % node.id for a line of text in vertical list
for n in node.traverse(head) do % For every subnode within paragraph
if n.id==HLIST then % If its a line of text
tex.setglue("rightskip",0,0,0,2,2)
tex.setglue("leftskip",0,65536,0,2,2)
local b = node.copy(n)
b = node.hpack(b.head)
node.write(b)
end
end
return head
end
luatexbase.add_to_callback('post_linebreak_filter', my_post_lb_filter, 'Play with luatex node library')
}
% Attempt-2 Traverse glue nodes within lines, and surgically update them
\directlua{
function my_post_lb_filter(head,groupcode)
local HLIST = node.id("hlist") % node.id for a line of text in vertical list
local GLUE = node.id("glue")
local RSKIP = node.subtype("rightskip")
local LSKIP = node.subtype("leftskip")
for n in node.traverse(head) do % For every subnode within paragraph
if n.id==HLIST then % If its a line of text
for g in node.traverse(n) do % For every subnode within line
if g.id==GLUE then % If its a glue
if g.subtype == RSKIP then
node.setglue(g,0,0,0,2,2)
end
if g.subtype == LSKIP then
node.setglue(g,0,65536,0,2,2)
end
end
end
end
end
return head
end
luatexbase.add_to_callback('post_linebreak_filter', my_post_lb_filter, 'Play with luatex node library')
}
A comment about Attempt-2: In my observation from internals of hlist nodes, raggedright lines lack 'leftskip' subnode inside them. So how could one add leftskip subnode to such a line? While raggedleft lines have both 'leftskip' & 'rightskip' subnodes within them, so it is just a matter of updating them I guess.
post_linebreak_filter
the glue has already been set. You have to remove it from the end of the hlist and put it in the beginning.node.write
in your first example will result in an infinite loop. Never usenode.write
unless you know exactly that the generated node is going to end up in the correct place.