1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18\startluacode
19
20local patterns = lpeg.patterns
21
22local variables = interfaces.variables
23local v_random = variables.random
24
25local lowercase = characters.lower
26
27local ipsum = { }
28moduledata.ipsum = ipsum
29
30local data = { }
31
32local function getfiledata(settings)
33 local filename = settings.filename or ""
34 local filedata = data[filename]
35 if not filedata then
36 local text = resolvers.loadtexfile(filename) or ""
37 local paragraphs = lpeg.match(patterns.paragraphs,text) or { }
38 local sentences = lpeg.match(patterns.sentences, text) or { }
39 local words = lpeg.match(patterns.words, text) or { }
40 for i=1,#words do
41 words[i] = lowercase(words[i])
42 end
43 filedata = {
44
45 [variables.paragraph] = paragraphs,
46 [variables.lines] = sentences,
47 [variables.line] = sentences,
48 [variables.words] = words,
49 [variables.word] = words,
50 }
51
52 data[filename] = filedata
53 end
54 local d = filedata[settings.alternative or v_paragraph] or filedata[v_paragraph] or { }
55 local nd = #d
56 local n = settings.n
57 if n ~= v_random then
58 n = tonumber(n) or 0
59 if n == 0 then
60 n = nd
61 end
62 end
63 return d, n, nd
64end
65
66function moduledata.ipsum.typeset(settings)
67 local d, n, nd = getfiledata(settings)
68 if nd > 0 then
69 context(settings.before)
70 if n == v_random then
71 context(settings.left)
72 context(d[math.random(1,nd)])
73 context(settings.right)
74 else
75 for i=1,n do
76 context(settings.left)
77 context(d[i])
78 context(settings.right)
79 if i < n then
80 context(settings.inbetween)
81 end
82 end
83 end
84 context(settings.after)
85 end
86end
87
88function moduledata.ipsum.direct(settings)
89 local d, n, nd = getfiledata(settings)
90 if nd == 0 then
91
92 elseif n == v_random then
93 context(d[math.random(1,nd)])
94 else
95 for i=1,n do
96 context(d[i])
97 if i < n then
98 context(settings.separator)
99 end
100 end
101 end
102end
103
104interfaces.implement {
105 name = "ipsum",
106 protected = true,
107 actions = moduledata.ipsum.typeset,
108 arguments = { {
109 { "alternative" },
110 { "filename" },
111 { "n" },
112 { "left" },
113 { "right" },
114 { "before" },
115 { "after" },
116 { "inbetween" },
117 { "separator" },
118 } },
119}
120
121\stopluacode
122
123\unprotect
124
125\installnamespace {ipsum}
126
127\installcommandhandler \????ipsum {ipsum} \????ipsum
128
129\setupipsum
130 [\c!file=lorem,
131 \c!alternative=\v!paragraph,
132
133
134
135 \c!n=0,
136 \c!left=,
137 \c!right=,
138 \c!before=,
139 \c!after=,
140 \c!separator=,
141 \c!inbetween=]
142
143\installactionhandler{ipsum}
144
145\startsetups[handler:action:ipsum]
146 \useipsumstyleandcolor\c!style\c!color
147
148 \uselanguageparameter\ipsumparameter
149 \clf_ipsum
150 alternative {\ipsumparameter\c!alternative}
151 filename {\ipsumparameter\c!file}
152 n {\ipsumparameter\c!n}
153 left {\ipsumparameter\c!left}
154 right {\ipsumparameter\c!right}
155 before {\ipsumparameter\c!before}
156 after {\ipsumparameter\c!after}
157 inbetween {\ipsumparameter\c!inbetween}
158 \relax
159\stopsetups
160
161\def\directipsum#1
162 {\clf_ipsum
163 alternative {\namedipsumparameter{#1}\c!alternative}
164 filename {\namedipsumparameter{#1}\c!file}
165 n {\namedipsumparameter{#1}\c!n}
166 separator {\ipsumparameter\c!separator}
167 \relax}
168
169\protect
170
171\continueifinputfile{mipsum.mkiv}
172
173\setupbodyfont[dejavu,11pt]
174
175\starttext
176
177 \ipsum[alternative=paragraph,before=\blank,after=\blank,language=la]
178
179 \ipsum[alternative=lines,n=2,right=\par,before=\blank,after=\blank,language=la]
180
181 \ipsum[alternative=lines,n=random,before=\blank,after=\blank,language=la]
182
183 \ipsum[alternative=lines,before=\startitemize,after=\stopitemize,left=\startitem,right=\stopitem,language=la]
184
185 \ipsum[alternative=words,left=(,right=),inbetween=\space,language=la]
186
187 \page
188
189 \defineipsum
190 [ward]
191 [file=ward,
192 before=\blank,
193 after=\blank]
194
195 \defineipsum
196 [ward:itemize]
197 [ward]
198 [alternative=lines,
199 before={\startitemize[packed]},
200 after=\stopitemize,
201 left=\startitem,
202 right=\stopitem]
203
204 \defineipsum
205 [ward:title]
206 [ward]
207 [alternative=lines,
208 n=random]
209
210 \subject{\directipsum{ward:title}}
211
212 \ipsum[ward]
213 \ipsum[ward:itemize]
214
215\stoptext
216 |