mtx-context-copy.tex /size: 7493 b    last modification: 2023-12-21 09:44
1%D \module
2%D   [       file=mtx-context-copy,
3%D        version=2008.11.10, % about that time i started playing with this
4%D          title=\CONTEXT\ Extra Trickry,
5%D       subtitle=Copying 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%D This is a \TEXEXEC\ features that has been moved to \MKIV.
15
16% begin help
17%
18% usage: context --extra=copy [options] list-of-files
19%
20% --interaction         : add hyperlinks
21%
22% end help
23
24\continuewhenlmtxmode
25
26\input mtx-context-common.tex
27
28\setupexternalfigures
29  [directory=]
30
31\definepapersize
32  [fit]
33  [width=\figurewidth,
34   height=\figureheight]
35
36\setuplayout
37  [page]
38
39\doif {\getdocumentargument{interaction}} {yes} {
40    \setupinteraction
41      [state=start]
42    \setupexternalfigures
43      [interaction=yes]
44}
45
46\doif {\getdocumentargument{tracelinks}} {yes} {
47    \enabletrackers[figures.links]
48%     \enabletrackers[refer*]
49%     \enabletrackers[dest*]
50}
51
52\starttexdefinition CopyPages #1
53
54    \filterpages[#1][all][alternative=page]
55
56\stoptexdefinition
57
58\starttexdefinition CopyEntry #1#2#3#4
59
60    \doifelsesomething {#4} {
61        \setupinteraction
62          [state=start]
63        \setupexternalfigures
64          [interaction={#4}]
65        \writestatus
66            {filter pages}
67            {file #1, first page #2, last page #3, interaction '#4'}
68
69    } {
70        \setupinteraction
71          [state=stop]
72        \setupexternalfigures
73          [interaction=]
74        \writestatus
75            {filter pages}
76            {file #1, first page #2, last page #3}
77    }
78
79    \filterpages[#1][#2:#3][alternative=page]
80
81\stoptexdefinition
82
83\starttext
84
85% return {
86%     comment = "tugboat 137",
87%     list    = {
88%         {
89%             filename    = "tb137complete",
90%             first       = 1,
91%             last        = 1,
92%             interaction = "all"
93%             pageoffset  = -1,
94%
95%         },
96%         {
97%             filename    = "issue",
98%             first       = 2,
99%             last        = 123,
100%             interaction = "all"
101%         },
102%         {
103%             filename    = "issue",
104%             first       = 124,
105%             last        = 125,
106%             interaction = "all"
107%             pageoffset  = -1,
108%         },
109%     },
110% }
111
112\startluacode
113
114    local find = string.find
115
116    if #document.files == 0 then
117        context("no files given")
118    elseif document.arguments.template then
119        local filename = file.addsuffix(document.files[1],"lua")
120        local template = table.load(filename)
121        if template then
122            local list = template.list
123            if type(list) == "table" then
124                for i=1,#list do
125                    local entry       = list[i]
126                    local filename    = entry.filename or ""
127                    local first       = tonumber(entry.first) or 1
128                    local last        = tonumber(entry.last)  or "*"
129                    local interaction = entry.interaction or ""
130                    local pageoffset  = tonumber(entry.pageoffset)
131                    local minheight   = tonumber(entry.minheight)
132                    if interaction ~= "" then
133                        if pageoffset then
134                            interaction = interaction .. ",pageoffset:" .. pageoffset
135                        end
136                        if minheight then
137                            interaction = interaction .. ",minheight:" .. minheight
138                        end
139                    end
140                    context.CopyEntry(filename,first,last,interaction)
141                end
142            end
143        else
144            logs.report("copy","invalid or missing template file")
145        end
146    else
147        if document.arguments.pattern then
148            document.files = dir.glob(document.arguments.pattern)
149        end
150        for i=1,#document.files do
151            local filename = document.files[i]
152            if not find(filename,"^mtx%-context%-") and not find(filename,"^context%-extra") then
153                logs.report("copy",filename)
154                context.CopyPages(filename)
155            end
156        end
157    end
158
159\stopluacode
160
161\stoptext
162
163% def copyoutput
164%     copyortrim(false,'copy')
165% end
166
167% def trimoutput
168%     copyortrim(true,'trim')
169% end
170
171% def copyortrim(trim=false,what='unknown')
172%     if job = TEX.new(logger) then
173%         prepare(job)
174%         job.cleanuptemprunfiles
175%         files =  if @commandline.option('sort') then @commandline.arguments.sort else @commandline.arguments end
176%         if files.length > 0 then
177%             if f = File.open(job.tempfilename('tex'),'w') then
178%                 scale = @commandline.checkedoption('scale')
179%                 begin
180%                     scale = (scale.to_f * 1000.0).to_i if scale.to_i < 10
181%                 rescue
182%                     scale = 1000
183%                 end
184%                 scale = scale.to_i
185%                 paperoffset = @commandline.checkedoption('paperoffset', '0cm')
186%                 f << "\\starttext\n"
187%                 files.each do |filename|
188%                     result = @commandline.checkedoption('result','texexec')
189%                     begin
190%                         if (filename !~ /^texexec/io) && (filename !~ /^#{result}/) then
191%                             report("copying file: #{filename}")
192%                             f <<  "\\getfiguredimensions\n"
193%                             f <<  "  [#{filename}]\n"
194%                             f <<  "  [scale=#{scale},\n"
195%                             f <<  "   page=1,\n"
196%                             f <<  "   size=trimbox\n" if trim
197%                             f <<  "]\n"
198%                             f <<  "\\definepapersize\n"
199%                             f <<  "  [copy]\n"
200%                             f <<  "  [width=\\figurewidth,\n"
201%                             f <<  "   height=\\figureheight]\n"
202%                             f <<  "\\setuppapersize\n"
203%                             f <<  "  [copy][copy]\n"
204%                             f <<  "\\setuplayout\n"
205%                             f <<  "  [page]\n"
206%                             f <<  "\\setupexternalfigures\n"
207%                             f <<  "  [directory=]\n"
208%                             f <<  "\\copypages\n"
209%                             f <<  "  [#{filename}]\n"
210%                             f <<  "  [scale=#{scale},\n"
211%                             f <<  "   marking=on,\n" if @commandline.option('markings')
212%                             f <<  "   size=trimbox,\n" if trim
213%                             f <<  "   offset=#{paperoffset}]\n"
214%                         end
215%                     rescue
216%                         report("wrong specification")
217%                     end
218%                 end
219%                 f << "\\stoptext\n"
220%                 f.close
221%                 job.setvariable('interface','english')
222%                 job.setvariable('simplerun',true)
223%                 # job.setvariable('nooptionfile',true)
224%                 job.setvariable('files',[job.tempfilename])
225%                 job.processtex
226%             else
227%                 report("no files to #{what}")
228%             end
229%         else
230%             report("no files to #{what}")
231%         end
232%         job.cleanuptemprunfiles
233%     end
234% end
235