summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/censor/censor.sty
blob: 49e11906198d68bd7499f1081e8d5ccce835746b (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
% censor.sty
\def\censorversionnumber{3.22}
\ProvidesPackage{censor}
[2018/11/15 \censorversionnumber
 Provides capability for redaction of sensitive information]

%
% This work may be distributed and/or modified under the 
% conditions of the LaTeX Project Public License, either version 1.3 
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX 
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Steven B. Segletes.

% VERSION:
% 1.00 - Initial release
% 2.00 - Added \blackout
% 2.10 - Allowed \blackout to cross paragraph boundaries with use of
%        \bpar. Stopped censoring periods, in order to preserve
%        end-of-sentence spacing, which differs from inter-word spacing.
% 3.00 - \censorbox introduced to handle figures, tables, etc.
% 3.10 - Made \blackout work with \par in argument.  Introduced 
%        \xblackout
% 3.20 - Specify depth/height of censor rule.  Introduced \def\censordot{}
% 3.21 - Fixed bug regarding \xblackout rules remaining after a
%        \StopCensoring
% 3.22 - changed `\if to \ifx in definition of \bl@t, to handle macros like \%
\usepackage{pbox}
\usepackage{ifnextok}

\newlength\censorruledepth
\newlength\censorruleheight

\censorruledepth=-0.3ex% -0.3ex DEFAULT
\censorruleheight=2.1ex%  2.1ex DEFAULT
\def\censordot{.}% versus \def\censordot{ }%

\newcommand\censorrule[1]{\protect\rule[\censorruledepth]{#1}{\censorruleheight}}

\newcommand\censor{\@ifstar{\@cenlen}{\@cenword}}
  \newcommand\@cenlen[1]{\censorrule{#1 ex}}
  \newcommand\@cenword[1]{\censorrule{\widthofpbox{#1}}}

\newcommand\un@censor{\@ifstar{\un@cenlen}{\un@cenword}}
  \newcommand\un@cenlen[1]{\protect\underline{\hspace{#1 ex}}}
  \newcommand\un@cenword[1]{#1}

\newcommand\StopCensoring{%
           \let\censor\un@censor%
           \let\censorbox\un@censorbox%
           \let\xblackout\blackout%
}
\newcommand\RestartCensoring{%
           \renewcommand\censor{\@ifstar{\@cenlen}{\@cenword}}%
           \renewcommand\censorbox{\@ifstar{\censor@dim}{\censor@box}}%
           \let\xblackout\sv@xblackout%
}

\let\sv@tilde~

\def\stringend{$}

\long\def\blackout#1{\def~{-}\censor@Block#1\stringend\let~\sv@tilde}
\long\def\censor@Block{\IfNextToken\stringend{\@gobble}%
  {\IfNextToken\@sptoken{ \bl@t{\censor@Block}}%
  {\bl@t{\censor@Block}}}}

% V2.00 DEFINITION:
% \def\bl@t#1#2{\censor{#2}#1}

% V2.10 DEFINITION (MADE \long IN V3.1):
%\long\def\bl@t#1#2{\if\bpar#2\par\else\if.#2\censordot\else\censor{#2}\fi\fi#1}

% V3.22 DEFINITION
\long\def\bl@t#1#2{\ifx\bpar#2\par\else\if.#2\censordot\else\censor{#2}\fi\fi#1}

%\def\bpar{_} %V3.00 DEFINITION
\let\bpar\par %AS OF V3.1, CAN HANDLE \par

\long\def\xblackout#1{\rule{0ex}{0ex}%
  \def~{-}%
  \def\@justpar{F}%
  \def\@justperiod{F}%
  \def\@justspace{F}%
  \xcensor@Block#1\stringend%
  \let~\sv@tilde%
 }

\let\sv@xblackout\xblackout

\long\def\xcensor@Block{\IfNextToken\stringend{\@gobble}%
  {\IfNextToken\@sptoken{ \def\@justspace{T}\xbl@t{\xcensor@Block}}%
  {\xbl@t{\xcensor@Block}}}}

\newlength\periodrlap\setlength\periodrlap{1.6ex}
\newlength\afterperiodlap\setlength\afterperiodlap{1.2ex}
\newlength\lletterlap\setlength\lletterlap{0.55ex}
\newlength\rletterlap\setlength\rletterlap{0.55ex}
\newlength\afterspacelap\setlength\afterspacelap{0.0ex}

\def\@periodrlap{\rlap{\censorrule{\periodrlap}}}
\def\@afterperiodlap{\llap{\censorrule{\afterperiodlap}}}
\def\@lletterlap{\llap{\censorrule{\lletterlap}}}
\def\@rletterlap{\rlap{\censorrule{\rletterlap}}}
\def\@afterspacelap{\llap{\censorrule{\afterspacelap}}}

\long\def\xbl@t#1#2{%
  \ifx\par#2%
    \par\def\@justpar{T}%
  \else%
    \if T\@justspace%
      \rule{0ex}{1ex}\@afterspacelap%
    \fi%
    \if.#2%
      \def\@justperiod{T}%
      \@periodrlap%
      \censordot%
    \else%
      \if F\@justpar%
        \if T\@justperiod%
          \@afterperiodlap%
        \else%
          \@lletterlap%
        \fi%
      \fi%
      \censor{#2}%
      \@rletterlap%
      \def\@justpar{F}%
      \def\@justperiod{F}%
      \def\@justspace{F}%
    \fi%
  \fi%
  #1%
}

\newcommand\censorbox{\@ifstar{\censor@dim}{\censor@box}}
  \newcommand\censor@dim[4][]{{#1%
                      \rule[-#4\baselineskip]{#2ex}{#3\baselineskip}}}
  \newcommand\censor@box[2][]{#1\setbox0\hbox{#2}%
                      \rule[-\the\dp0]{\the\wd0}{\the\ht0+\the\dp0}}

\newcommand\un@censorbox{\@ifstar{\un@censor@dim}{\un@censor@box}}
  \newcommand\un@censor@dim[4][]{{#1%
                      \fbox{\rule[-#4\baselineskip]{0ex}{#3\baselineskip}
                      \rule{#2ex}{0ex}}}}
  \newcommand\un@censor@box[2][]{#1#2}

% NOTE: A \protect\censorbox{} MAY BE REQUIRED INSIDE SOME ENVIRONMENTS
\endinput