%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