I'm coding an algorithm to detect and highlight homeoarchies.
Here my current almost working (but compiling at least) code:
\documentclass{article}
\usepackage[french]{babel}
\usepackage{kpfonts}
\usepackage{luatexbase}
\usepackage{luacode}
\begin{luacode}
compare_lines = function (line1,line2)
local head1 = line1.head
local head2 = line2.head
local char_count = 0
local word_count = 0
while (head1.id == 37 and head2.id == 37
and head1.char == head2.char) -- identical glyph
or (head1.id == 10 and head2.id == 10) do -- glue
if (head1.id == 37) then -- glyph
char_count = char_count + 1
elseif (head1.id == 10) then -- glue
word_count = word_count + 1
end
head1 = head1.next
head2 = head2.next
end
return char_count,word_count,head1,head2
end
homeoarchy = function ( head )
local cur_line = head
local prev_line -- initiate prev_head
local max_char = 3
local max_word = 1
while head do
if head.id == 0 then -- new line
print("new line")
prev_line = cur_line
cur_line = head
if prev_line.id == 0 then
char_count,word_count,prev_head,cur_head = compare_lines(prev_line,cur_line)
if char_count >= max_char or word_count >= max_word then
w1_prev = node.new("whatsit","pdf_literal")
w1_prev.data = "q 1 0 1 rg"
w1_prev.mode = 1
w2_prev = node.new("whatsit","pdf_literal")
w2_prev.data = "Q"
w2_prev.mode = 1
node.insert_before(prev_line,prev_line.head.next,w1_prev)
node.insert_before(prev_line,prev_head,w2_prev)
w1_cur = node.new("whatsit","pdf_literal")
w1_cur.data = "q 1 0 1 rg"
w1_prev.mode = 1
w2_cur = node.new("whatsit","pdf_literal")
w2_cur.data = "Q"
w2_cur.mode = 1
node.insert_before(cur_line,cur_line.head.next,w1_cur)
node.insert_before(cur_line,cur_head,w2_cur)
print("done placing markers")
end
end
end
head = head.next
end
return true
end
luatexbase.add_to_callback("post_linebreak_filter",homeoarchy,"homeoarchy")
\end{luacode}
\begin{document}
\begin{minipage}{1.7in}
Je suis venu non pour juger le monde, mais pour sauver le monde. Celui qui me rejette
et qui ne re\c coit pas mes
\end{minipage}
\bigskip
\begin{minipage}{1.7in}
Je suis venu non pour juger~le monde, mais pour sauver le monde. Celui qui me rejette
et qui ne re\c coit pas mes
\end{minipage}
\bigskip
\begin{minipage}{1.7in}
Je suis venu non pour juger le monde, mais pour sauver~le monde. Celui qui me rejette
et qui ne re\c coit pas mes
\end{minipage}
\bigskip
\begin{minipage}{1.6in}
Je suis venu non pour juger le monde, mais pour sauver le monde. Celui qui me rejette
et qui ne re\c coit pas mes
\end{minipage}
\bigskip
\begin{minipage}{1.8in}
Je suis venu non pour juger le monde, mais pour sauver le monde. Celui qui me rejette
et qui ne re\c coit pas mes
\end{minipage}
\end{document}
and the result:

It's easy to spot two bugs here:
- the color starts after the "l" instead of before it. I've tried all sorts of combinations, from "prev_line.head" to "prev_line.head.next.prev", and I always get an error saying I can't insert before a non-existing node. I'm pretty sure the "l" node does exist. How can I insert before it?
- the text stops after the second colored word. So I've colored the words alright, but I'd still like the rest of the text... How can I do that?
