summaryrefslogtreecommitdiff
path: root/tex/context/base/mtx-context-combine.tex
blob: 991e974aed8b38d400ba390e99f30029fdafa03d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
% engine=luatex

%D \module
%D   [       file=mtx-context-combine,
%D        version=2009.03.21,
%D          title=\CONTEXT\ Extra Trickry,
%D       subtitle=Combine Files,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright=\PRAGMA]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D This is a \TEXEXEC\ features that has been moved to \MKIV.

% begin help
%
% usage: context --extra=combine [options] list-of-files
%
% --sort                  : sort filenames first
% --paperoffset=dimension : left-top-offset
% --nobanner              : no footer etc
% --combination           : h*v or hxv
% --paperformat           : paper*print or paperxprint
% --nobanner              : no footerlines
% --bannerheight          : height of banner
% --bannerstring          : height of bannerstring
%
% end help

\doifdocumentargumentelse {paperoffset} {

    \setuplayout
      [topspace=\getdocumentargument{paperoffset},
       backspace=\getdocumentargument{paperoffset}]

} {

    \setuplayout
      [topspace=0pt,
       backspace=0pt]

}

\setuplayout
  [header=0pt,
   footer=0pt,
   width=middle,
   height=middle]

\startluacode
    local combination = document.arguments['combination'] or '2*2'
    local nx, ny = string.match(combination,"^(%d+)%s*[%*x]%s*(%d+)$")
    if not nx then
        nx, ny = 2, 2
    elseif not ny then
        nx = tonumber(combination) or 2
        ny = nx
    else
        nx = tonumber(nx) or 2
        ny = tonumber(ny) or nx or 2
    end
    document.setargument("nx",nx)
    document.setargument("ny",ny)
\stopluacode

\startluacode
    local paperformat = document.arguments['paperformat'] or 'A4*A4'
    paperformat = string.upper(paperformat)
    local f, t = string.match(paperformat,"^(.-)%s*[%*xX]%s*(.-)$")
    if not f then
        f, t = "A4", "A4"
    elseif not t then
        t = f
    end
    document.setargument("from",f)
    document.setargument("to",t)
\stopluacode

\setuppapersize
  [\getdocumentargument{from}]
  [\getdocumentargument{to}]

\doifnotdocumentargument {bannerheight} {
    \setuplayout
        [footer=1cm]
}

\doifdocumentargumentelse {nobanner} {yes} {
    \setuplayout
      [footer=0cm]
    \setupbackgrounds
      [page]
      [background=]
} {
    \definelayer
      [page]
      [width=\paperwidth,
       height=\paperheight]

    \setupbackgrounds
      [page]
      [background=page]
}

\setupexternalfigures
  [directory=]

\starttext

\startluacode
    local format = string.format
    local fprint = function(...) tex.sprint(tex.ctxcatcodes,format(...)) end

    if #document.files > 0 then
        if document.arguments["sort"] then
            table.sort(document.files)
        end
        local dobanner = not document.arguments["nobanner"]
        local bannerheight = document.arguments["bannerheight"]
        local nx = document.arguments.nx or 2
        local ny = document.arguments.ny or 2
        for _, filename in ipairs(document.files) do
            if not string.find(filename,"^mtx%-context%-") then
                -- could be a macro
                local bannerstring = format("\\tttf\\detokenize{%s}\\quad\\quad\\currentdate\\quad\\quad\\pagenumber",file.basename(filename))
                if dobanner then
                    if bannerheight then
                        fprint("\\setuptexttexts[{\\setlayerframed[page][preset=middlebottom][frame=off,height=%s]{%s}}]",bannerheight,bannerstring)
                    else
                        fprint("\\setupfootertexts[{%s}]",bannerstring)
                    end
                end
                fprint("\\combinepages[%s][nx=%s,ny=%s]",filename,nx,ny)
                fprint("\\page")
            end
        end
    else
        fprint("no files given")
    end
\stopluacode

\stoptext