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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
%D \module
%D [ file=m-ipsum,
%D version=2012.07.19,
%D title=\CONTEXT\ Extra Modules,
%D subtitle=Ipsum,
%D author=Hans Hagen,
%D date=\currentdate,
%D copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%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 After some discussing on the mailing list I made this example of
%D an implementation. Of course there can be alternatives as it's a
%D nice exercise in module writing.
\startluacode
local patterns = lpeg.patterns
local variables = interfaces.variables
local v_random = variables.random
local lowercase = characters.lower
local ipsum = { }
moduledata.ipsum = ipsum
local data = { }
local function getfiledata(settings)
local filename = settings.filename or ""
local filedata = data[filename]
if not filedata then
local text = resolvers.loadtexfile(filename) or ""
local paragraphs = lpeg.match(patterns.paragraphs,text) or { }
local sentences = lpeg.match(patterns.sentences, text) or { }
local words = lpeg.match(patterns.words, text) or { }
for i=1,#words do
words[i] = lowercase(words[i])
end
filedata = {
-- [variables.paragraphs] = paragraphs,
[variables.paragraph] = paragraphs,
[variables.lines] = sentences,
[variables.line] = sentences,
[variables.words] = words,
[variables.word] = words,
}
-- inspect(filedata)
data[filename] = filedata
end
local d = filedata[settings.alternative or v_paragraph] or filedata[v_paragraph] or { }
local nd = #d
local n = settings.n
if n ~= v_random then
n = tonumber(n) or 0
if n == 0 then
n = nd
end
end
return d, n, nd
end
function moduledata.ipsum.typeset(settings)
local d, n, nd = getfiledata(settings)
if nd > 0 then
context(settings.before)
if n == v_random then
context(settings.left)
context(d[math.random(1,nd)])
context(settings.right)
else
for i=1,n do
context(settings.left)
context(d[i])
context(settings.right)
if i < n then
context(settings.inbetween)
end
end
end
context(settings.after)
end
end
function moduledata.ipsum.direct(settings)
local d, n, nd = getfiledata(settings)
if nd == 0 then
-- nothing
elseif n == v_random then
context(d[math.random(1,nd)])
else
for i=1,n do
context(d[i])
if i < n then
context(settings.separator)
end
end
end
end
\stopluacode
\unprotect
\installnamespace {ipsum}
\installcommandhandler \????ipsum {ipsum} \????ipsum
\setupipsum
[\c!file=lorem,
\c!alternative=\v!paragraph,
%\c!language=,
%\c!styl=,
%\c!color=,
\c!n=0,
\c!left=,
\c!right=,
\c!before=,
\c!after=,
\c!separator=,
\c!inbetween=]
\installactionhandler{ipsum} % grouped
\startsetups[handler:action:ipsum]
\useipsumstyleandcolor\c!style\c!color
% hm, also changes dates
\uselanguageparameter\ipsumparameter
\ctxlua{moduledata.ipsum.typeset {
alternative = "\ipsumparameter\c!alternative",
filename = "\ipsumparameter\c!file",
n = "\ipsumparameter\c!n",
left = "\luaescapestring{\ipsumparameter\c!left}",
right = "\luaescapestring{\ipsumparameter\c!right}",
before = "\luaescapestring{\ipsumparameter\c!before}",
after = "\luaescapestring{\ipsumparameter\c!after}",
inbetween = "\luaescapestring{\ipsumparameter\c!inbetween}",
}}
\stopsetups
\def\directipsum#1% only one argument, expanded
{\ctxlua{moduledata.ipsum.typeset {
alternative = "\namedipsumparameter{#1}\c!alternative",
filename = "\namedipsumparameter{#1}\c!file",
n = "\namedipsumparameter{#1}\c!n",
separator = "\luaescapestring{\ipsumparameter\c!separator}",
}}
}
\protect
\continueifinputfile{m-ipsum.mkiv}
\setupbodyfont[dejavu,11pt]
\starttext
\ipsum[alternative=paragraph,before=\blank,after=\blank,language=la]
\ipsum[alternative=lines,n=2,right=\par,before=\blank,after=\blank,language=la]
\ipsum[alternative=lines,n=random,before=\blank,after=\blank,language=la]
\ipsum[alternative=lines,before=\startitemize,after=\stopitemize,left=\startitem,right=\stopitem,language=la]
\ipsum[alternative=words,left=(,right=),inbetween=\space,language=la]
\page
\defineipsum
[ward]
[file=ward,
before=\blank,
after=\blank]
\defineipsum
[ward:itemize]
[ward]
[alternative=lines,
before={\startitemize[packed]},
after=\stopitemize,
left=\startitem,
right=\stopitem]
\defineipsum
[ward:title]
[ward]
[alternative=lines,
n=random]
\subject{\directipsum{ward:title}}
\ipsum[ward]
\ipsum[ward:itemize]
\stoptext
|