summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mtx-context-arrange.tex
blob: 9d0bb901bee65c9dfa4bbf4526ae5cf1cd030858 (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
% engine=luatex

%D \module
%D   [       file=mtx-context-arrange,
%D        version=2009.03.21,
%D          title=\CONTEXT\ Extra Trickry,
%D       subtitle=Arrange 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=arrange [options] list-of-files
%
% --sort                  : sort filenames first
% --paperoffset=dimension : left-top-offset
% --noduplex              : singlesided (doublesided is default)
% --backspace=dimension   : extra left offset
% --topspace=dimension    : extra top offset
% --marking               : add cutmarks
% --addempty=list         : add empty pages at/after (comma separated list)
% --printformat           : 2UP, etc
%
% end help

\doifdocumentargument {paperoffset} {
    \definepapersize
      [offset=\getdocumentargument{paperoffset}]
}

\doifdocumentargumentelse {noduplex} {yes} {
    \setuppagenumbering
        [alternative=doublesided]
    \setdocumentargument{sided}{doublesided}
} {
    \setdocumentargument{sided}{singlesided}
}

\setdefaultdocumentargument {textwidth} {0cm}
\setdefaultdocumentargument {backspace} {0cm}
\setdefaultdocumentargument {topspace}  {0cm}

\setuplayout
    [backspace=\getdocumentargument{backspace},
     topspace=\getdocumentargument{topspace},
     width=middle,
     height=middle,
     location=middle,
     header=0pt,
     footer=0pt]

\doifdocumentargument {marking} {yes} {
    \setuplayout
        [marking=on]
}

\startluacode
    local printformat = document.arguments.printformat or ""
    if printformat == "" then
        printformat = "normal"
    elseif string.find(printformat,".*up") then
        printformat = "2UP,\\v!rotated"
    elseif string.find(printformat,".*down") then
        printformat = "2DOWN,\\v!rotated"
    elseif string.find(printformat,".*side") then
        printformat = "2SIDE,\\v!rotated"
    end
    document.setargument("printformat",printformat)
\stopluacode

\setuparranging
  [\getdocumentargument{sided},
   \getdocumentargument{printformat}]

\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 emptypages = document.arguments.addempty or ""
        local textwidth = document.arguments.textwidth or "0cm"
        for _, filename in ipairs(document.files) do
            if not string.find(filename,"^mtx%-context%-") then
                fprint("\\insertpages[%s][%s][width=%s]",filename,emptypages,textwidth)
            end
        end
    else
        fprint("no files given")
    end
\stopluacode

\stoptext