More reliable pdfTeX annotations

This commit is contained in:
Marcel Fabian Krüger 2021-06-27 05:39:42 +02:00
parent d63bed65a1
commit e1b8debcf6
3 changed files with 52 additions and 36 deletions

View File

@ -313,14 +313,17 @@
\cs_new_protected:Npn \luamml_annotate:nen #1#2#3 {
\int_gincr:N \g__luamml_annotation_id_int
\iow_shipout_x:Nx \l__luamml_pdf_stream {
\tl_to_str:e {
LUAMML_MARK_REF:
\int_use:N \g__luamml_annotation_id_int
:
}
\iow_now:Nx \l__luamml_pdf_stream {
LUAMML_MARK:
\int_use:N \g__luamml_annotation_id_int
:
count = \int_eval:n {#1},
#2
}
\exp_not:N \iow_newline:
\iow_newline:
LUAMML_MARK_END
}
#3
@ -328,27 +331,33 @@
\cs_new_protected:Npn \luamml_annotate:en #1#2 {
\int_gincr:N \g__luamml_annotation_id_int
\iow_shipout_x:Nx \l__luamml_pdf_stream {
\tl_to_str:e {
LUAMML_MARK_REF:
\int_use:N \g__luamml_annotation_id_int
:
}
\iow_now:Nx \l__luamml_pdf_stream {
LUAMML_MARK:
\int_use:N \g__luamml_annotation_id_int
:
count = data.count[\int_use:N \g__luamml_annotation_id_int],
#1
}
\exp_not:N \iow_newline:
\iow_newline:
LUAMML_MARK_END
}
\iow_now:Nx \l__luamml_pdf_stream {
\use:x {
\iow_now:Nn \l__luamml_pdf_stream {
LUAMML_COUNT:
\int_use:N \g__luamml_annotation_id_int
}
\__luamml_pdf_showlists:
#2
\iow_now:Nx \l__luamml_pdf_stream {
LUAMML_COUNT_END
\exp_not:n {#2}
\iow_now:Nn \l__luamml_pdf_stream {
LUAMML_COUNT_END:
\int_use:N \g__luamml_annotation_id_int
}
\__luamml_pdf_showlists:
}
}
% \end{macrocode}
% \end{macro}
%

View File

@ -18,13 +18,11 @@ local luamml_mark = l.Cg('LUAMML_MARK:' * id * ':' * l.Cs((1 - l.P'\n' + l.Cg('\
local function add(a, b) return a + b end
local count_block = '### ' * line * l.Cf(l.Cc(0) * (('\\' * l.Cc(1))^-1 * line - '### ')^0, add)
local luamml_count = l.Cg('LUAMML_COUNT:' * id * l.P'\n'^1
* count_block
* (line-'LUAMML_COUNT_END\n')^0
* 'LUAMML_COUNT_END' * l.P'\n'^1
* count_block / function(id, first, second)
return id, second - first
end * l.Cc'count')
local luamml_precount = l.Cg('LUAMML_COUNT:' * id * l.P'\n'^1
* count_block * l.Cc'precount')
local luamml_postcount = l.Cg('LUAMML_COUNT_END:' * id * l.P'\n'^1
* count_block * l.Cc'postcount')
local luamml_instruction = l.Cg('LUAMML_INSTRUCTION:' * l.Cc(nil) * l.C((1 - l.P'\n')^0) * '\n' * l.Cc'instructions')
@ -34,10 +32,11 @@ local function multi_table_set(t, key, value, table)
return t
end
local log_file = l.Cf(l.Ct(l.Cg(l.Ct'', 'groups')
* l.Cg(l.Ct'', 'count')
* l.Cg(l.Ct'', 'precount')
* l.Cg(l.Ct'', 'postcount')
* l.Cg(l.Ct'', 'marks')
* l.Cg(l.Ct'', 'instructions'))
* (luamml_block + luamml_mark + luamml_instruction + luamml_count + line)^0,
* (luamml_block + luamml_mark + luamml_instruction + luamml_precount + luamml_postcount + line)^0,
multi_table_set)
return function(filename)
@ -50,5 +49,13 @@ return function(filename)
if f then f:close() end
-- The following does *not* end with * -1 since we want to allow the last line to not end with \n.
-- In that case we ignore the last line, but that's safe since the last line never contains our markers.
return assert(log_file:match(content))
local parsed = assert(log_file:match(content))
local precount, postcount, count = parsed.precount, parsed.postcount, {}
for id, pre in next, precount do
local post = assert(postcount[id], 'Unbalanced count')
count[id], postcount[id] = post-pre, nil
end
assert(not next(postcount), 'Unbalanced count')
parsed.precount, parsed.postcount, parsed.count = nil, nil, count
return parsed
end

View File

@ -66,7 +66,7 @@ local fraction_noad = l.Ct('\\fraction, thickness '
local mathchoice_noad = l.Ct('\\mathchoice' * l.Cg(l.Cc'choice', 'id') * -1)
local mark_whatsit = '\\write' * ('-' + l.R'09'^1) * '{LUAMML_MARK:' * (l.R'09'^1/tonumber) * ':'
local mark_whatsit = '\\write' * ('-' + l.R'09'^1) * '{LUAMML_MARK_REF:' * (l.R'09'^1/tonumber) * ':'
local parse_list
local function parse_kernel(lines, i, prefix, parsed)