mtx-context-compare.tex /size: 2389 b    last modification: 2023-12-21 09:44
1%D \module
2%D   [       file=mtx-context-compare,
3%D        version=2015.07.14,
4%D          title=\CONTEXT\ Extra Trickry,
5%D       subtitle=Compare Files,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14% begin help
15%
16% usage: context --extra=compare [options] file-1 file-2
17%
18% example: context --extra=compare file1.pdf file-2.pdf
19%
20% end help
21
22\input mtx-context-common.tex
23
24\starttext
25
26\starttexdefinition unexpanded ShowBoth #1#2#3
27    \startTEXpage[width=21cm]
28        \startoverlay
29            {\externalfigure[#1][page=#3,width=21cm]}
30            {\externalfigure[#2][page=#3,width=21cm]}
31        \stopoverlay
32    \stopTEXpage
33\stoptexdefinition
34
35\starttexdefinition unexpanded ShowPage #1#2
36    \startTEXpage[width=21cm]
37        \externalfigure[#1][page=#2,width=21cm]
38    \stopTEXpage
39\stoptexdefinition
40
41\startluacode
42
43local report = logs.reporter("compare")
44
45local fileone = document.files[1] or ""
46local filetwo = document.files[2] or ""
47
48if fileone == "" or filetwo == "" then
49    report("provide two filenames")
50    os.exit()
51end
52
53if not lfs.isfile(fileone) then
54    report("unknown file %a",fileone)
55    os.exit()
56end
57
58if not lfs.isfile(filetwo) then
59    report("unknown file %a",filetwo)
60    os.exit()
61end
62
63local function check(name)
64    local fig = figures.push { name = name }
65    figures.identify()
66    figures.check()
67    local used = fig.used
68    figures.pop()
69    return used
70end
71
72local one = check(fileone) -- can crash
73local two = check(filetwo) -- can crash
74
75if not one then
76    report("invalid file %a",fileone)
77    os.exit()
78end
79
80if not two then
81    report("invalid file %a",filetwo)
82    os.exit()
83end
84
85local n_one = tonumber(one.pages)
86local n_two = tonumber(two.pages)
87
88if not n_one or n_one ~= n_two then
89    report("files have different nofpages (%s vs %s)",n_one or "?",n_two or "?")
90end
91
92if n_one > n_two then
93    for i=1,n_two do
94        context.ShowBoth(fileone,filetwo,i)
95    end
96    for i=n_two+1,n_one do
97        context.ShowPage(fileone,i)
98    end
99else
100    for i=1,n_one do
101        context.ShowBoth(fileone,filetwo,i)
102    end
103    for i=n_one+1,n_two do
104        context.ShowPage(filetwo,i)
105    end
106end
107
108\stopluacode
109
110\stoptext
111
112\endinput
113