| 1 |
%D \module |
| 2 |
%D [ file=t-enigma.mkvi, |
| 3 |
%D version=2013-03-28 02:15:37+0100, |
| 4 |
%D title=\CONTEXT\ User Module, |
| 5 |
%D subtitle=Enigma, |
| 6 |
%D author=Philipp Gesang, |
| 7 |
%D date=\currentdate, |
| 8 |
%D copyright=Philipp Gesang, |
| 9 |
%D license=2-clause BSD, |
| 10 |
%D email={phg42 dot 2a at gmail dot com}] |
| 11 |
%C This module is licensed under the conditions of the BSD license with |
| 12 |
%C two clauses. There is a copy in a file named "COPYING" in the |
| 13 |
%C t-enigma source tree. |
| 14 |
|
| 15 |
\unprotect |
| 16 |
|
| 17 |
\writestatus{loading} {ConTeXt module / Enigma Document Encryption} |
| 18 |
|
| 19 |
\startinterface all |
| 20 |
\setinterfacevariable {enigma} {enigma} |
| 21 |
\stopinterface |
| 22 |
|
| 23 |
\definenamespace [\v!enigma] [ |
| 24 |
\v!command=\v!no, |
| 25 |
comment=Enigma Document Encryption, |
| 26 |
\s!name=\v!enigma, |
| 27 |
\s!parent=\v!enigma, |
| 28 |
% setup=\v!list, |
| 29 |
setup=\v!no, |
| 30 |
style=\v!no, |
| 31 |
type=module, |
| 32 |
version=hg-tip, |
| 33 |
] |
| 34 |
|
| 35 |
%D Loading the \LUA\ conversion routines. |
| 36 |
|
| 37 |
\startluacode |
| 38 |
thirddata = thirddata or { } |
| 39 |
\stopluacode |
| 40 |
\registerctxluafile{enigma} |
| 41 |
|
| 42 |
%D \startdocsection[title=Macro Generator] |
| 43 |
%D \startparagraph |
| 44 |
%D The main setup.\reference[listing:define]{} The |
| 45 |
%D \texmacro{defineenigma} macro does not adhere to the reommended |
| 46 |
%D practis of automatical macro derivation. Rather, we have our own |
| 47 |
%D parser do the job of setting globals. This is a consequence of the |
| 48 |
%D intention to offer the same behavior in any of the three main |
| 49 |
%D formats, \PLAIN, \CONTEXT and \LATEX. Hence, we don’t rely on the |
| 50 |
%D internal mechanisms but implement our own macro generator. |
| 51 |
%D \stopparagraph |
| 52 |
|
| 53 |
\def\enigma_define_indeed#id{% |
| 54 |
\edef\enigmaid{#id}% |
| 55 |
\expandafter\gdef\csname \e!start\enigmaid\endcsname{% |
| 56 |
\endgraf |
| 57 |
\bgroup |
| 58 |
\ctxlua{ |
| 59 |
if thirddata.enigma.machines["#id"] then |
| 60 |
nodes.tasks.enableaction("processors", |
| 61 |
"thirddata.enigma.callbacks.#id") |
| 62 |
else |
| 63 |
print([[ENIGMA: No machine of that name: #id!]]) |
| 64 |
end |
| 65 |
}% |
| 66 |
}% |
| 67 |
% |
| 68 |
\expandafter\gdef\csname \e!stop\enigmaid\endcsname{% |
| 69 |
\endgraf%% We need to force a paragraph here for the callback to be |
| 70 |
%% applied. |
| 71 |
\ctxlua{ |
| 72 |
nodes.tasks.disableaction("processors", |
| 73 |
"thirddata.enigma.callbacks.#id") |
| 74 |
thirddata.enigma.machines["#id"]:processed_chars() |
| 75 |
}% |
| 76 |
\egroup% |
| 77 |
}% |
| 78 |
} |
| 79 |
|
| 80 |
%D \startparagraph |
| 81 |
%D The \texmacro{enigma_inherit}\reference[listing:inherit]{} is called |
| 82 |
%D as an intermediate step when deriving one machine from an already |
| 83 |
%D existing one. It gets the stored configuration of its ancestor, |
| 84 |
%D relying on the \luafunction{retrieve_raw_args} function (see |
| 85 |
%D \at{page}[listing:retrieve]. |
| 86 |
%D \stopparagraph |
| 87 |
\def\enigma_inherit#to#from{% |
| 88 |
\ctxlua{% |
| 89 |
local enigma = thirddata.enigma |
| 90 |
local current_args = enigma.retrieve_raw_args(\!!bs#from\!!es) |
| 91 |
enigma.save_raw_args(current_args, \!!bs#to\!!es) |
| 92 |
enigma.new_callback(enigma.new_machine(\!!bs#to\!!es), |
| 93 |
\!!bs#to\!!es) |
| 94 |
}% |
| 95 |
\enigma_define_indeed{#to}% |
| 96 |
} |
| 97 |
|
| 98 |
\def\enigma_define[#id][#secondid]{% |
| 99 |
\ifsecondargument %% Copy an existing machine and callback. |
| 100 |
\enigma_inherit{#id}{#secondid}% |
| 101 |
\else %% Create a new machine. |
| 102 |
\iffirstargument |
| 103 |
\enigma_define_indeed{#id}% |
| 104 |
\else |
| 105 |
\donothing |
| 106 |
\fi |
| 107 |
\fi |
| 108 |
\endgroup% |
| 109 |
} |
| 110 |
|
| 111 |
\def\defineenigma{% |
| 112 |
\begingroup |
| 113 |
\dodoubleempty\enigma_define |
| 114 |
} |
| 115 |
|
| 116 |
%D \stopdocsection |
| 117 |
|
| 118 |
%D \startdocsection[title=Setup] |
| 119 |
\def\enigma_setup_indeed#args{% |
| 120 |
\ctxlua{ |
| 121 |
local enigma = thirddata.enigma |
| 122 |
local current_args = |
| 123 |
enigma.parse_args(\!!bs\detokenize{#args}\!!es) |
| 124 |
enigma.save_raw_args(current_args, \!!bs\currentenigmaid\!!es) |
| 125 |
enigma.new_callback( |
| 126 |
enigma.new_machine(\!!bs\currentenigmaid\!!es), |
| 127 |
\!!bs\currentenigmaid\!!es) |
| 128 |
}% |
| 129 |
} |
| 130 |
|
| 131 |
%D \startparagraph |
| 132 |
%D The module setup \texmacro{setupenigma} expects key=value, notation. |
| 133 |
%D All the logic is at the Lua end, not much to see here … |
| 134 |
%D \stopparagraph |
| 135 |
|
| 136 |
\def\enigma_setup[#id][#args]{% |
| 137 |
\ifsecondargument |
| 138 |
\edef\currentenigmaid{#id} |
| 139 |
\pushcatcodetable |
| 140 |
\catcodetable \txtcatcodes |
| 141 |
\enigma_setup_indeed{#args}% |
| 142 |
\else |
| 143 |
\donothing |
| 144 |
\fi |
| 145 |
\popcatcodetable |
| 146 |
\egroup% |
| 147 |
} |
| 148 |
|
| 149 |
\def\setupenigma{% |
| 150 |
\bgroup |
| 151 |
\dodoubleempty\enigma_setup% |
| 152 |
} |
| 153 |
|
| 154 |
%D \stopdocsection |
| 155 |
|
| 156 |
\protect |
| 157 |
% vim:ft=context:sw=2:ts=2:tw=71 |