Debian Codegraph @

texmf-dist/tex/lualatex/luacensor/luacensor.sty

raw
1 %luacensor.sty
2 \def\luacensorversionnumber{1.1.1}
3 \ProvidesPackage{luacensor}
4 [2024/04/06 \luacensorversionnumber\
5 Redact sensitive information using Lua]
6 % !TeX program = lualatex
7 % !TeX encoding = utf8
8 % This work may be distributed and/or modified under the
9 % conditions of the LaTeX Project Public License, either version 1.3
10 % of this license or (at your option) any later version.
11 % The latest version of this license is in
12 % http://www.latex-project.org/lppl.txt
13 % and version 1.3 or later is part of all distributions of LaTeX
14 % version 2005/12/01 or later.
15 %
16 % This work has the LPPL maintenance status `maintained'.
17 %
18 % The Current Maintainer of this work is Elijah Z Granet
19 %%%%%%%%%%%%%%%%%%%%%%%%%%%
20 %%%%%%%%%%%%%%%%%%%%%%%%%%%
21 % option (we'll come back
22 % to this later
23 %%%%%%%%%%%%%%%%%%%%%%%%%%%
24 %%%%%%%%%%%%%%%%%%%%%%%%%%%
25 \newif\ifwarning
26 \warningfalse
27 \DeclareOption{warning}{\warningtrue}
28 \ProcessOptions*
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%
30 %%%%%%%%%%%%%%%%%%%%%%%%%%%
31 % DEPENDENCIES
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%
34 \RequirePackage{luacode}
35 \RequirePackage{environ}% http://ctan.org/pkg/environ
36
37 \RequirePackage{verbatim}
38 % ^ for the censoring
39 \RequirePackage{accsupp}
40 %^for accessibility
41 \RequirePackage{fontspec}
42 %^for black lines
43 %in theory, you could do
44 %a lighter version of this
45 %package with just asterisks
46 %or `[TEXT-REDACTED]'
47 %And perhaps that would be better for
48 %the environment with printing
49 %BUT I MADE MY CHOICE!
50 \RequirePackage{xcolor}
51 \RequirePackage{graphicx}
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%
54 % FONTS
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%
56 %%%%%%%%%%%%%%%%%%%%%%%%%%%
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%
59 % redacted is prettier and free to download
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%
62 % Strongly recommended
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%
67 \IfFontExistsTF{Redacted}{%
68 \newfontface\cnsrfnt[%%%%%%
69 %the scale is arbitrary, but kind of works
70 %Scale=1.1,
71 %%the below declarations are to prevent warnings about shapes not being available
72 %WordSpace=0,
73 ItalicFont={Redacted-Regular.ttf},%
74 BoldItalicFont={Redacted-Regular.ttf},%
75 BoldFont={Redacted-Regular.ttf},%
76 SmallCapsFont={Redacted-Regular.ttf}]{Redacted-Regular.ttf}
77 \newcommand{\onething}{\cnsrfnt\ • }
78 \newcommand{\twothings}{\cnsrfnt\ • •}
79 \newcommand{\donothing}{\cnsrfnt\ }
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%
82 %The little spaces let justification happen
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%
85 %%%%%%%%%%%%%%%%%%%%%%%%%%%
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%
87 % • chosen as an arbitrary average width
88 %%%%%%%%%%%%%%%%%%%%%%%%%%%
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%
90 }{
91 %%%%%%%%%%%%%%%%%%%%%%%%%%%
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%
93 % This option works perfectly
94 %fine, it's just less pretty
95 %%but a good fallback because
96 % Source Sans is in TeX dists by default
97 %%%%%%%%%%%%%%%%%%%%%%%%%%%
98 %%%%%%%%%%%%%%%%%%%%%%%%%%%
99 \newfontface\cnsrfnt[Scale=1.01,%To allow for separate use of source sans in text
100 WordSpace=0,%To make it all one black line
101 %the below declarations are to prevent warnings about shapes not being available
102 ItalicFont={Source Sans Pro Black},BoldItalicFont={Source Sans Pro Black},BoldFont={Source Sans Pro Black},SmallCapsFont={Source Sans Pro Black}]{Source Sans Pro Black}
103 %%%%%%%%%%%%%%%%%%%%%%%%%%%
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%
105 % Bit of unicode magic below to make the black line effect
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%
108 \newcommand{\onething}{\cnsrfnt ▬ }
109 \newcommand{\twothings}{\cnsrfnt ▬ ▬ }
110 \newcommand{\donothing}{ }
111 }
112
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%
115 % A neat fallback for disappearing things…
116 %%%%%%%%%%%%%%%%%%%%%%%%%%%
117 %%%%%%%%%%%%%%%%%%%%%%%%%%%
118 %%%%%%%%%%%%%%%%%%%%%%%%%%%
119 %%%%%%%%%%%%%%%%%%%%%%%%%%%
120 % FULL CREDIT
121 % and FULSOME THANKS
122 % TO TEX.SE USER
123 % Werner for the code below
124 %%%%%%%%%%%%%%%%%%%%%%%%%%%
125 %%%%%%%%%%%%%%%%%%%%%%%%%%%
126 \makeatletter
127 \newcommand{\voidenvironment}[1]{%
128 \expandafter\providecommand\csname env@#1@save@env\endcsname{}%
129 \expandafter\providecommand\csname env@#1@process\endcsname{}%
130 \@ifundefined{#1}{}{\RenewEnviron{#1}{}}%
131 }
132 \makeatother
133 \newcommand{\hddn}[1]{%
134 \ifcnsr{}\else%
135 #1\fi}
136 \newenvironment*{hidden}{\begin{@empty}
137 }{\end{@empty}}
138 \voidenvironment{hidden}
139
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%
141 %%%%%%%%%%%%%%%%%%%%%%%%%%%
142 %%%%%%%%%%%%%%%%%%%%%%%%%%%
143 %%%%%%%%%%%%%%%%%%%%%%%%%%%
144 %%%%%%%%%%%%%%%%%%%%%%%%%%%
145 %%%%%%%%%%%%%%%%%%%%%%%%%%%
146 % the CENSOR COMMAND
147 %%%%%%%%%%%%%%%%%%%%%%%%%%%
148
149 \newif\ifcnsr\cnsrfalse
150 \newcommand{\cnsr}[1]{%
151 \ifcnsr{%
152 \voidenvironment{equation*}%
153 \voidenvironment{equation}%
154 \voidenvironment{table}%
155 \voidenvironment{table*}%
156 \voidenvironment{tabular}%
157 \voidenvironment{tabular*}%
158 \voidenvironment{}%
159 %%%%%%%%%%%%%%%%%%%%%%%%%%%
160 % I don't know how many
161 % people use TEX native accent commands
162 % in LuaTEX given that using Unicode is more
163 %people's style. But just in case, because these can lead to stray accent marks floating above censored letters.
164 %%%%%%%%%%%%%%%%%%%%%%%%%%%
165 %%%%%%%%%%%%%%%%%%%%%%%%%%%
166 %%%%%%%%%%%%%%%%%%%%%%%%%%%
167 \renewcommand{\`}[1]{}%
168 \renewcommand{\'}[1]{}%
169 \renewcommand{\^}[1]{}%
170 \renewcommand{\"}[1]{}%
171 \renewcommand{\H}[1]{}%
172 \renewcommand{\~}[1]{}%
173 \renewcommand{\c}[1]{}%
174 \renewcommand{\k}[1]{}%
175 \renewcommand{\l}[1]{}%
176 \renewcommand{\=}[1]{}%
177 \renewcommand{\b}[1]{}%
178 \renewcommand{\.}[1]{}%
179 \renewcommand{\d}[1]{}%
180 \renewcommand{\r}[1]{}%
181 \renewcommand{\u}[1]{}%
182 \renewcommand{\v}[1]{}%
183 \renewcommand{\t}[1]{}%
184 \renewcommand{\o}[1]{}%
185 \renewcommand{\i}[1]{}%
186 %%%%%%%%%%%%%%%%%%%%%%%%%%%d
187 %%%%%%%%%%%%%%%%%%%%%%%%%%%
188 %%%%%%%%%%%%%%%%%%%%%%%%%%%
189 %%%%%%%%%%%%%%%%%%%%%%%%%%%
190 % here we have the accsupp magic
191 % this operates by replacing the 'x's
192 % or unicode black squares as the case
193 % may be with an alt text
194 % this serves a dual purpose of both making
195 %pdftotext not break with huge strings of meaningless characters
196 %but more importantly
197 % it means screen readers don't subject
198 %. their users to the meaningless reading out of unicode black squares 50 times in a row!
199 %%%%%%%%%%%%%%%%%%%%%%%%%%%
200 %%%%%%%%%%%%%%%%%%%%%%%%%%%
201 %%%%%%%%%%%%%%%%%%%%%%%%%%%
202 %%%%%%%%%%%%%%%%%%%%%%%%%%%
203 \BeginAccSupp{method=plain,ActualText={TEXT REDACTED}}%
204 \rndstring{#1}%
205 \EndAccSupp{}}%
206 \else%
207 %%%%%%%%%%%%%%%%%%%%%%%%%%%
208 %%%%%%%%%%%%%%%%%%%%%%%%%%%
209 % if the conditional is off
210 % the command does absolutely nothing
211 %%%%%%%%%%%%%%%%%%%%%%%%%%%
212 %%%%%%%%%%%%%%%%%%%%%%%%%%%
213 #1%
214 \fi}
215 %%%%%%%%%%%%%%%%%%%%%%%%%%%
216 %%%%%%%%%%%%%%%%%%%%%%%%%%%
217 % %%%%%%%%%%%%%%%%%%%%%%%%%%%
218 %%%%%%%%%%%%%%%%%%%%%%%%%%%
219 % The LUA MAGIC PART
220 %%%%%%%%%%%%%%%%%%%%%%%%%%%
221 %%%%%%%%%%%%%%%%%%%%%%%%%%%
222 %%%%%%%%%%%%%%%%%%%%%%%%%%%
223 %%%%%%%%%%%%%%%%%%%%%%%%%%%
224 \begin{luacode}
225 --fulsome thanks to TeX.SE users Henri Menke and David Carlisle, without whom none of this would be possible
226 local function rndstring()
227 local toks = token.scan_toks(s)
228 local on = true
229 for n, t in ipairs(toks) do
230 if t.csname == "begin" or t.csname == "end" then
231 on = false
232 -- The below is necessary as TeX primitives can break the code otherwise because they do not use brackets
233 end
234
235 if not(on) and t.cmdname == "right_brace" then
236 on = true
237 -- This prevents needless errors about gibberish up commands
238 end
239 if on and t.csname == "&" then
240 local letter = token.create'donothing'
241 toks[n] = letter
242
243 elseif on and t.csname == "%" then
244 local letter = token.create'donothing'
245 toks[n] = letter
246
247 elseif on and t.csname == "$" then
248 local letter = token.create'donothing'
249 toks[n] = letter
250
251 elseif on and t.csname == "#" then
252 local letter = token.create'donothing'
253 toks[n] = letter
254
255 elseif on and t.csname == "_" then
256 local letter = token.create'donothing'
257 toks[n] = letter
258
259 elseif on and t.csname == "{" then
260 local letter = token.create'donothing'
261 toks[n] = letter
262
263 elseif on and t.csname == "}" then
264 local letter = token.create'donothing'
265 toks[n] = letter
266
267 elseif on and t.csname == "~" then
268 local letter = token.create'donothing'
269 toks[n] = letter
270
271 elseif on and t.csname == "^" then
272 local letter = token.create'donothing'
273 toks[n] = letter
274 elseif on and t.cmdname == "letter" then
275 -- The below is the randomness part of this, which I admit is fairly arbitrary, but will more often artificially shorten strings than lengthen them, as testing found if lengthening was too frequent, it led to really unsightly long strings.
276 local f = math.random (1,20)
277 if f == 1 then
278 local letter = token.create'donothing'
279 toks[n] = letter
280
281 elseif f == 2 then
282 local letter = token.create'donothing'
283 toks[n] = letter
284 elseif f == 3 then
285 local letter = token.create'donothing' toks[n] = letter
286 elseif f == 4 then
287 local letter = token.create'twothings'
288 toks[n] = letter
289 elseif f == 5 then
290 local letter = token.create'donothing' toks[n] = letter
291
292 else
293 local letter = token.create'onething'
294 toks[n] = letter
295 end
296 elseif
297 on and t.cmdname == "spacer" then
298 local f = math.random (1,20)
299 if f == 2 then
300 local letter = token.create'donothing'
301 toks[n] = letter
302 elseif f == 3 then
303
304 local letter = token.create'donothing' toks[n] = letter
305 elseif f == 4 then
306 local letter = token.create'donothing'
307 toks[n] = letter
308 elseif f == 5 then
309
310
311 local letter = token.create'twothings' toks[n] = letter
312 elseif f == 6 then
313
314
315 local letter = token.create'donothing' toks[n] = letter
316 elseif f == 7 then
317 local letter = token.create'donothing' toks[n] = letter
318
319
320 else
321 local letter = token.create'onething'
322 toks[n] = letter
323
324 end
325
326 elseif
327 on and t.cmdname == "other_char" then
328 local f = math.random (1,20)
329 if f == 2 then
330 local letter = token.create'donothing'
331 toks[n] = letter
332 elseif f == 3 then
333
334 local letter = token.create'donothing' toks[n] = letter
335 elseif f == 4 then
336 local letter = token.create'donothing'
337 toks[n] = letter
338 elseif f == 5 then
339
340
341 local letter = token.create'twothings' toks[n] = letter
342 elseif f == 6 then
343
344
345 local letter = token.create'donothing' toks[n] = letter
346 elseif f == 7 then
347 local letter = token.create'donothing' toks[n] = letter
348
349
350 else
351 local letter = token.create'onething'
352 toks[n] = letter
353
354 end
355 end
356 end
357 --Drop the token in and move on
358 token.put_next(toks)
359 end
360 local lft = lua.get_functions_table()
361 --make a global command
362 lft[#lft + 1] = rndstring
363 token.set_lua("rndstring", #lft, "global")
364 \end{luacode}
365 %%%%%%%%%%%%%%%%%%%%%%%%%%%
366 %%%%%%%%%%%%%%%%%%%%%%%%%%%
367 % WARNING FUN YAY
368 %%%%%%%%%%%%%%%%%%%%%%%%%%%
369 %%%%%%%%%%%%%%%%%%%%%%%%%%%
370 %%%%%%%%%%%%%%%%%%%%%%%%%%%
371 %%%%%%%%%%%%%%%%%%%%%%%%%%%
372 % Definitely this whole section
373 %is there to be user modified, because
374 % depending on language, jurisdiction
375 %type of document etc, everyone will need
376 %a specific warning style. So the important
377 % part of the code here is the
378 % conditional and global [warning]
379 % option, because that's the magic value added
380 %%%%%%%%%%%%%%%%%%%%%%%%%%%
381 %%%%%%%%%%%%%%%%%%%%%%%%%%%
382 %%%%%%%%%%%%%%%%%%%%%%%%%%%
383 %%%%%%%%%%%%%%%%%%%%%%%%%%%
384 %%%%%%%%%%%%%%%%%%%%%%%%%%%
385 %%%%%%%%%%%%%%%%%%%%%%%%%%%
386 % fonts for the warning:
387 %I chose default LaTeX fonts
388 % here to be changed as users wish
389 %%%%%%%%%%%%%%%%%%%%%%%%%%%
390 %%%%%%%%%%%%%%%%%%%%%%%%%%%
391 \newfontface\wrnstncl{QT Military}
392 \newcommand{\warnword}{WARNING}
393 \newfontface\smbl{DejaVuSans-Bold}
394 \newcommand{\danger}{\smbl ⚠︎\normalfont}
395 \newcommand{\warnformat}{\sffamily\bfseries \color{red}}
396 \newcommand{\textwarn}{This document is {\underline{NOT}} redacted. It contains private and confidential personal data, and may {\underline{NOT}} be distributed, published, or shown to those without the right to view such information. The publication of the information in this document may constitute a contempt of court, punishable by a term of imprisonment.}
397 \newcommand{\textsafe}{This document has been altered to remove sensitive personal data. It is cleared for publication and dissemination.
398 }
399 \definecolor{darkgreen}{rgb}{0.0, 0.2, 0.13}
400 \definecolor{darkspringgreen}{rgb}{0.09, 0.45, 0.27}
401 \definecolor{forestgreen}{rgb}{0.13, 0.55, 0.13}
402 \newcommand{\dquad}{\danger\danger\danger\danger}
403 \newcommand{\dangersign}[1]{\scalebox{2}{\huge\danger}}
404 \newcommand{\dangerblock}{\scalebox{2}{\huge\danger\quad\danger\quad\danger}}
405 \newcommand{\warnblock}{{\Large\wrnstncl\warnword\quad\warnword\quad\warnword}}
406 \newcommand{\tworules}{\hrule width \hsize height .7pt\vskip2pt\hrule width \hsize height .7pt}
407 \newcommand{\allwarning}{\dangerblock\\\warnblock\\\normalfont\smallskip\warnformat\textwarn }
408 \newcommand{\confwarning}{%
409 %%%%%%%%%%%%%%%%%%%%%%%%%%%
410 %%%%%%%%%%%%%%%%%%%%%%%%%%%
411 % The warning option
412 %%%%%%%%%%%%%%%%%%%%%%%%%%%
413 %%%%%%%%%%%%%%%%%%%%%%%%%%%
414 \ifwarning
415 \ifcnsr
416 %%%%%%%%%%%%%%%%%%%%%%%%%%%
417 %%%%%%%%%%%%%%%%%%%%%%%%%%%
418 % a note saying document is redacted
419 %%%%%%%%%%%%%%%%%%%%%%%%%%%
420 %%%%%%%%%%%%%%%%%%%%%%%%%%%
421 \begin{center}
422 \color{forestgreen}
423 \tworules\vskip5pt
424 \normalsize\normalfont\sffamily\bfseries\textsafe
425 \vskip5pt\tworules
426 \end{center}
427 \else
428 %%%%%%%%%%%%%%%%%%%%%%%%%%%
429 %%%%%%%%%%%%%%%%%%%%%%%%%%%
430 % The WARNING for un-redacted docs
431 %%%%%%%%%%%%%%%%%%%%%%%%%%%
432 %%%%%%%%%%%%%%%%%%%%%%%%%%%
433 \begin{center}\color{red}\tworules\vskip 5pt\allwarning
434 \vskip5pt\tworules%
435 \end{center}%
436 \fi%
437 \else\fi}
438 %%%%%%%%%%%%%%%%%%%%%%%%%%%
439 %%%%%%%%%%%%%%%%%%%%%%%%%%%
440 % Allow \maketitle
441 % on same page
442 % yay
443 %
444 %%%%%%%%%%%%%%%%%%%%%%%%%%%
445 %%%%%%%%%%%%%%%%%%%%%%%%%%%
446 \ifwarning\let\oldmaketitle\maketitle\renewcommand{\maketitle}{{\let\newpage\relax\maketitle}}\else\fi
447 %%%%%%%%%%%%%%%%%%%%%%%%%%%
448 %%%%%%%%%%%%%%%%%%%%%%%%%%%
449 % print the warning at the start of the document
450 %%%%%%%%%%%%%%%%%%%%%%%%%%%
451 %%%%%%%%%%%%%%%%%%%%%%%%%%%
452 \AtBeginDocument{\confwarning}