page-blk.mkxl /size: 4650 b    last modification: 2024-01-16 10:22
1%D \module
2%D   [       file=page-blk,
3%D        version=2022.06.04,
4%D          title=\CONTEXT\ Page Macros,
5%D       subtitle=Shuffling Pages,
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\writestatus{loading}{ConTeXt Page Macros / Shuffling pages}
15
16%D This is a somewhat tricky module and although we have a somewhat abstract
17%D interface we are pretty much bound to \PDF\ output here. When large amounts of
18%D documents are to be generated and|/|or when saving a run makes sense the table
19%D of contents can be typeset at the end and moved to the beginning. However, it
20%D assumes a rather stable and predictable rendering. Don't use this without
21%D checking it really works out well.
22%D
23%D The low level backend code has been around for quite a while but we never had
24%D a real (need and therefore) interface for it. There actually is not that much
25%D code involved anyway. There is no need to backport it to \MKIV.
26
27\registerctxluafile{page-blk}{autosuffix}
28
29% Musical timestamp: end May 2022 xpropaganda (in loop mode to keep the pace).
30
31% pitfalls: we need to keep the toc in order (or generate it 'manually' based on
32% deltas but even then we get the numbers wrong)
33
34\unprotect
35
36\newinteger\c_pages_prerolled
37
38\permanent\protected\def\startpageblock[#1]%
39  {\page
40   \clf_startpageblock{#1}}
41
42\permanent\protected\def\stoppageblock
43  {\page
44   \clf_stoppageblock}
45
46\permanent\protected\def\flushpageblocks[#1]%
47  {\page
48   \clf_flushpageblocks{#1}}
49
50\permanent\def\pageblockrealpage#1%
51  {\clf_pageblockrealpage{#1}}
52
53\tolerant\permanent\protected\def\startprerollpageblock[#1]%
54  {\page
55   \clf_startprerollpageblock{#1}%
56   \global\c_pages_prerolled\realpageno % hack
57   \setuppaper[\c!method=\v!none]}
58
59\permanent\protected\def\stopprerollpageblock
60  {\page
61   \clf_stopprerollpageblock
62   \setcounter[realpage][\c_pages_prerolled]% hack
63   \setuppaper[\c!method=\v!normal]}
64
65\permanent\def\prerolledpages#1%
66  {\clf_prerolledpages{#1}}
67
68\protect \endinput
69
70\usemodule[article-basic]
71
72\setupinteraction[state=start]
73
74% \enabletrackers[*reference*]
75% \enabletrackers[*destination*]
76
77\useMPlibrary[dum]
78
79\starttext
80
81\def\AlphaOffset{0}
82\def\BetaOffset {0}
83
84% the flexible ones (can come from files)
85
86\startbuffer[alpha]
87    \startchapter[title=Alpha]
88        \dorecurse{20}{\samplefile{tufte}\par}
89    \stopchapter
90\stopbuffer
91
92\startbuffer[beta]
93    \startchapter[title=Beta]
94        \dorecurse{6}{\samplefile{tufte}\par}
95    \stopchapter
96\stopbuffer
97
98% the fixed ones
99
100\startbuffer[gamma]
101    \startchapter[title=Gamma]
102        \externalfigure[whatever-dummy-3][width=\textwidth]
103    \stopchapter
104\stopbuffer
105
106\startbuffer[rest]
107    \startchapter[title=Rest A]
108        \samplefile{tufte}
109    \stopchapter
110    \startchapter[title=Rest B]
111        \samplefile{tufte}
112        \blank
113        Alpha : \goto{page \AlphaPages}[realpage(\AlphaPages)]\par
114        Beta  : \goto{page \BetaPages }[realpage(\BetaPages )]
115    \stopchapter
116\stopbuffer
117
118\startbuffer[contents]
119    \starttitle[title=Contents]
120        \placelist[chapter][criterium=text]
121    \stoptitle
122\stopbuffer
123
124% Preroll.
125
126\startprerollpageblock[alpha]
127    \start
128        \setuphead[chapter][incrementnumber=no]
129        \getbuffer[alpha]
130    \stop
131\stopprerollpageblock
132
133% \writestatus{!!!!!}{nofalphapages: \prerolledpages{alpha}}
134
135\startprerollpageblock[beta]
136    \start
137        \setuphead[chapter][incrementnumber=no]
138        \getbuffer[beta]
139    \stop
140\stopprerollpageblock
141
142% \writestatus{!!!!!}{nofbetapages: \prerolledpages{beta}}
143
144% Some titling.
145
146\startpageblock[titlepage]
147    \startstandardmakeup
148        \externalfigure[whatever-dummy-1][height=\textheight,width=\textwidth]
149    \stopstandardmakeup
150\stoppageblock
151
152\startpageblock[boguspage]
153    \startstandardmakeup
154        \externalfigure[whatever-dummy-2][height=\textheight,width=\textwidth]
155    \stopstandardmakeup
156\stoppageblock
157
158% The contents will end up here.
159
160% page 1
161
162% The main document.
163
164\setuppagenumber[number=2]
165
166\startpageblock[chapters]
167    % we have a predictable rest:
168
169    \edef\AlphaPages{\the\numexpr 4 \relax}
170    \edef\BetaPages {\the\numexpr 4 + \prerolledpages{alpha} \relax}
171
172    \getbuffer[rest]
173    \getbuffer[alpha]
174    \getbuffer[beta]
175    \getbuffer[gamma]
176\stoppageblock
177
178% This one will move:
179
180\setuppagenumber[number=1]
181
182\startpageblock[contents]
183    \getbuffer[contents]
184\stoppageblock
185
186\flushpageblocks
187  [titlepage,
188   boguspage,
189   contents,
190   chapters]
191
192\stoptext
193