summaryrefslogtreecommitdiff
path: root/tex/context/third/enigma/t-enigma.mkvi
blob: 3165e43c48274347d6116ed896f9bc5177ee28dc (plain)
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
%D \module
%D   [     file=t-enigma,
%D      version=2012-02-21 10:59:43+0100,
%D        title=\CONTEXT\ User Module,
%D     subtitle=Enigma,
%D       author=Philipp Gesang,
%D         date=\currentdate,
%D    copyright=Philipp Gesang,
%D      license=2-clause BSD,
%D        email={gesang at stud dot uni-heidelberg dot de}]
%D This module is licensed under the conditions of the BSD license with
%D two clauses. There is a copy in a file named "COPYING" in the
%D t-enigma source tree.

\unprotect

\writestatus{loading} {ConTeXt module / Enigma Document Encryption}

\startinterface all
  \setinterfacevariable {enigma} {enigma}
\stopinterface

\definenamespace [\v!enigma] [
 \v!command=\v!no,
    comment=Enigma Document Encryption,
    \s!name=\v!enigma,
  \s!parent=\v!enigma,
      % setup=\v!list,
      setup=\v!no,
      style=\v!no,
       type=module,
    version=hg-tip,
]

%D Loading the \LUA\ conversion routines.

\startluacode
thirddata = thirddata or { }
\stopluacode
\registerctxluafile{enigma}

%D \startdocsection[title=setups]
%D \startparagraph
%D The main setup. The \texmacro{defineenigma} macro does not adhere to
%D the reommended practis of automatical macro derivation. Rather, we
%D have our own parser do the job of setting globals. This is a
%D consequence of the intention to offer the same behavior in any of the
%D three main formats, \PLAIN, \CONTEXT and \LATEX. Hence, we don’t rely
%D on the internal mechanisms but implement our own macro generator.
%D \stopparagraph

\def\do_define_enigma#id{%
  \expandafter\gdef\csname start\enigmaid\endcsname{%
    \bgroup
    \ctxlua{
      if thirddata.enigma.machines["#id"] then
        local callback = thirddata.enigma.callbacks["#id"],
        nodes.tasks.enableaction("processors", "thirddata.enigma.callbacks.#id")
      else
        print([[ENIGMA: No machine of that name: #id!]])
      end
    }%
  }%
  %
  \expandafter\gdef\csname stop\enigmaid\endcsname{%
    \ctxlua{
      nodes.tasks.disableaction("processors", "thirddata.enigma.callbacks.#id")
      thirddata.enigma.machines["#id"]:processed_chars()
    }%
    \egroup%
  }%
}

\def\defineenigma#id{%
  \begingroup
  \edef\enigmaid{#id}%
  \expandafter\do_define_enigma\expandafter{\enigmaid}%
  \endgroup%
}

%D \stopdocsection

%D \startdocsection[title=Setup]
\def\do_setup_enigma#args{%
    \ctxlua{
      local enigma = thirddata.enigma
      local current_args = enigma.parse_args([====[\detokenize{#args}]====])
      enigma.new_callback(
        enigma.new_machine(current_args,
                           [====[\currentenigmaid]====]),
        [====[\currentenigmaid]====])
    }%
  \popcatcodetable
  \egroup%
}

%D \startparagraph
%D The module setup \texmacro{setupenigma} expects key=value, notation.
%D All the logic is at the Lua end, not much to see here …
%D \stopparagraph

\def\setupenigma#id{%
  \bgroup
    \edef\currentenigmaid{#id}
    \pushcatcodetable
    \catcodetable \txtcatcodes
    \do_setup_enigma%
}

%D \stopdocsection

\protect
% vim:ft=context:sw=2:ts=2:tw=72