m-ipsum.mkiv /size: 5291 b    last modification: 2020-07-01 14:35
1
%D \module
2
%D [ file=m-ipsum,
3
%D version=2012.07.19,
4
%D title=\CONTEXT\ Extra Modules,
5
%D subtitle=Ipsum,
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
%D After some discussing on the mailing list I made this example of
15
%D an implementation. Of course there can be alternatives as it's a
16
%D nice exercise in module writing.
17 18
\startluacode
19 20
local
patterns
=
lpeg
.
patterns
21 22
local
variables
=
interfaces
.
variables
23
local
v_random
=
variables
.
random
24 25
local
lowercase
=
characters
.
lower
26 27
local
ipsum
=
{
}
28
moduledata
.
ipsum
=
ipsum
29 30
local
data
=
{
}
31 32
local
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
-- [variables.paragraphs] = paragraphs,
45
[
variables
.
paragraph
]
=
paragraphs
,
46
[
variables
.
lines
]
=
sentences
,
47
[
variables
.
line
]
=
sentences
,
48
[
variables
.
words
]
=
words
,
49
[
variables
.
word
]
=
words
,
50
}
51
-- inspect(filedata)
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
64
end
65 66
function
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
86
end
87 88
function
moduledata
.
ipsum
.
direct
(
settings
)
89
local
d
,
n
,
nd
=
getfiledata
(
settings
)
90
if
nd
=
=
0
then
91
-- nothing
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
102
end
103 104
\stopluacode
105 106
\unprotect
107 108
\installnamespace
{
ipsum
}
109 110
\installcommandhandler
\????ipsum
{
ipsum
}
\????ipsum
111 112
\setupipsum
113
[
\c!file
=
lorem
,
114
\c!alternative
=
\v!paragraph
,
115
%\c!language=,
116
%\c!styl=,
117
%\c!color=,
118
\c!n
=
0
,
119
\c!left
=
,
120
\c!right
=
,
121
\c!before
=
,
122
\c!after
=
,
123
\c!separator
=
,
124
\c!inbetween
=]
125 126
\installactionhandler
{
ipsum
}
% grouped
127 128
\startsetups
[
handler
:
action
:
ipsum
]
129
\useipsumstyleandcolor
\c!style\c!color
130
% hm, also changes dates
131
\uselanguageparameter
\ipsumparameter
132
\ctxlua{
moduledata
.
ipsum
.
typeset
{
133
alternative
=
"
\ipsumparameter\c!alternative
"
,
134
filename
=
"
\ipsumparameter\c!file
"
,
135
n
=
"
\ipsumparameter\c!n
"
,
136
left
=
"
\luaescapestring{\ipsumparameter\c!left}
"
,
137
right
=
"
\luaescapestring{\ipsumparameter\c!right}
"
,
138
before
=
"
\luaescapestring{\ipsumparameter\c!before}
"
,
139
after
=
"
\luaescapestring{\ipsumparameter\c!after}
"
,
140
inbetween
=
"
\luaescapestring{\ipsumparameter\c!inbetween}
"
,
141
}
}
142
\stopsetups
143 144
\def
\directipsum
#
1
% only one argument, expanded
145
{
\ctxlua{
moduledata
.
ipsum
.
typeset
{
146
alternative
=
"
\namedipsumparameter{#1}\c!alternative
"
,
147
filename
=
"
\namedipsumparameter{#1}\c!file
"
,
148
n
=
"
\namedipsumparameter{#1}\c!n
"
,
149
separator
=
"
\luaescapestring{\ipsumparameter\c!separator}
"
,
150
}
}
151
}
152 153
\protect
154 155
\continueifinputfile
{
m
-
ipsum
.
mkiv
}
156 157
\setupbodyfont
[
dejavu
,
1
1
pt
]
158 159
\starttext
160 161
\ipsum
[
alternative
=
paragraph
,
before
=
\blank
,
after
=
\blank
,
language
=
la
]
162 163
\ipsum
[
alternative
=
lines
,
n
=
2
,
right
=
\par
,
before
=
\blank
,
after
=
\blank
,
language
=
la
]
164 165
\ipsum
[
alternative
=
lines
,
n
=
random
,
before
=
\blank
,
after
=
\blank
,
language
=
la
]
166 167
\ipsum
[
alternative
=
lines
,
before
=
\startitemize
,
after
=
\stopitemize
,
left
=
\startitem
,
right
=
\stopitem
,
language
=
la
]
168 169
\ipsum
[
alternative
=
words
,
left
=(
,
right
=)
,
inbetween
=
\space
,
language
=
la
]
170 171
\page
172 173
\defineipsum
174
[
ward
]
175
[
file
=
ward
,
176
before
=
\blank
,
177
after
=
\blank
]
178 179
\defineipsum
180
[
ward
:
itemize
]
181
[
ward
]
182
[
alternative
=
lines
,
183
before
=
{
\startitemize
[
packed
]
}
,
184
after
=
\stopitemize
,
185
left
=
\startitem
,
186
right
=
\stopitem
]
187 188
\defineipsum
189
[
ward
:
title
]
190
[
ward
]
191
[
alternative
=
lines
,
192
n
=
random
]
193 194
\subject
{
\directipsum
{
ward
:
title
}}
195 196
\ipsum
[
ward
]
197
\ipsum
[
ward
:
itemize
]
198 199
\stoptext
200