Debian Codegraph @

texmf-dist/doc/generic/olsak-misc/cnv-word.tex

raw
1 % cnv-word.tex --- an example of the usage of cnv.tex: word to word converting
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % April 2005 Petr Olsak
4
5 \input cnv
6
7 % This file is a complex example of the usage of cnv.tex macro.
8 % The macros presented here give the possibility to declare word to word
9 % conversion a of given string. Each occurence of declared words will be
10 % converted to the declared outputs.
11
12 % Double percent: "%%" is a prefix of technical comments.
13 % Please, read the "user" comments (prefixed by one "%") first.
14
15 %% \wtest is an internal macro which tests if the given {pattern} matches
16 %% the begin of the input stream. If it is true then other \wtests
17 %% are ignored and a given {output} is stored to \cnvout. The input
18 %% stream is reduced by the pattern. Else input stream is restored
19 %% and the next \wtest is run. We suppose the following parameters:
20 %% \wtest{pattern}{output}\wtest{pattern}{output}...\endwtest{letter}input stream\END
21
22 %% The main idea of \wtest is to declare the macro \trywtest by:
23 %% \def\trywtest #1pattern#2\end and run it by:
24 %% \trywtest input-stream\END pattern\end. If #1 is empty then pattern
25 %% matches.
26
27 \gdef\wtest #1#2#3\endwtest#4#5\END {\def\wout{#2}\def\otherwtests{#3}%
28 \def\inputstream{#5}%
29 \def\trywtest ##1#1##2\end {\def\tmp{##1}%
30 \def\tmpa{#1}\ifx\tmpa\empty \def\tmp{}\fi % empty pattern
31 \ifx\tmp\empty % found!
32 \def\next{\cnvstoree{\expandafter \cnvout \wout}%
33 \expandafter \cnvcontinue \inputstream\END}%
34 \else % not found!
35 \def\next{\expandafter\otherwtests\expandafter\endwtest
36 \expandafter{\expandafter#4\expandafter}%
37 \inputstream \END}%
38 \fi \next}
39 \def\cnvcontinue #1{\cnvnext}%
40 \expandafter\trywtest \inputstream \END#1\end
41 }
42
43 %% \stringdef {<letter><pattern>} {<output>}
44 %% adds to the internal cnv macro \E:<letter>:<table> the next part of
45 %% the type "\wtest{pattern}{output}" The \E:<letter>:<table> looks like:
46 %% \cnvexec \wtest{pattern1}{output1}\wtest{pattern2}{output2}...\endwtest{<letter>}
47 %% The new "\wtest{pattern}{output}" is added at the end of \wtests
48 %% string before \endwtest{letter}.
49
50 \def\wstringdef #1#2 #3{%
51 \expandafter\ifx \csname E:\string#1:\cnvtable\endcsname \relax
52 \def\tmp{}%
53 \else
54 \expandafter\expandafter\expandafter
55 \wstoretmp \csname E:\string#1:\cnvtable\endcsname
56 \fi
57 \expandafter \def
58 \csname E:\string#1:\cnvtable \expandafter\endcsname \expandafter
59 {\expandafter \cnvexec \tmp \wtest{#2}{#3}\endwtest{#1}}
60 }
61 \def\wstoretmp \cnvexec #1\endwtest#2{\def\tmp{#1}}
62 \def\stringdef #1#2{\wstringdef #1 {#2}}
63
64 %% If all wtests fail then \endwtest{<letter>} is processed. It adds
65 %% the <letter> itself to the \cnvout.
66
67 \def\endwtest#1{\def\tmp{#1}%
68 \ifx\tmp\inSPACE \cnvstoree{\expandafter \cnvout \space}%
69 \else \cnvstore{\cnvout #1}
70 \fi\cnvnext}
71 \def\inSPACE{\SPACE}
72
73 %% The \wconvert{<input token list>} does the main conversion. It runs
74 %% \cnvin twice (with different tables). First conversion converts the
75 %% spaces to \SPACE tokens and braces to \BGROUP and \EGROUP tokens. The
76 %% reason is that now we can read \SPACE and converted braces to #1 parameter
77 %% without changes. This is needed in \wtest macro. No matter that
78 %% \SPACE is undefined. We never process it by normal \TeX{} expansion.
79
80 %% The second conversion (with table 2) does the real word to word conversion and
81 %% returns the \SPACE \BGROUP and \EGROUP to the desired output.
82
83 \def\wconvert #1{%
84 \def\cnvtable{1}\cnvin{#1}
85 \def\cnvtable{2}%
86 \expandafter \cnvin \expandafter {\cnvout \END}}
87
88 %% Table 1: only converts spaces and braces
89
90 \def\cnvtable{1}
91 \findef { } {\SPACE}
92
93 %% Table 2: the \END mark is ignored and \SPACE is converted back to space
94
95 \def\cnvtable{2}
96 \findef \END {}
97 \findef \SPACE { }
98
99 %% We have to keep the \BGROUP and \EGROUP unchanged. Unfortunately,
100 %% the declaration:
101 %%
102 %% \let\findefmethod=\edef
103 %% \findef \BGROUP {{\iffalse}\fi}
104 %% \findef \EGROUP {\iffalse{\fi}}
105 %%
106 %% does not work because the cnv macro fails: the \output macro needs
107 %% to be balanced text everytime during conversion process. It means
108 %% that there is impossible to return the input tokens of catcode 1
109 %% and 2 to their original maening. We can return them to their original
110 %% meaning only if the \cnvout can be normally expanded. In such a case,
111 %% we can write after \wconvert is done:
112 %%
113 %% \bgroup \def\BGROUP {{\iffalse}\fi} \def\EGROUP {\iffalse{\fi}}
114 %% \xdef \cnvout{\cnvout} \egroup
115
116 % Users can declare the "word to word" conversion here. Each string declared at
117 % the left hand side will be converted to the output at the right hand side.
118 % If there are more left side strings with the same initial letter then
119 % the table is read in this order during the conversion. It means
120 % that the last declaration in the following example is irrelevant:
121 % if the string begins by "th" then the result of conversion is "TH"
122 % and the last declaration of "the" is ignored.
123
124 \stringdef {input} {output}
125 \stringdef {this} {THIS}
126 \stringdef {test} {TEST}
127 \stringdef {th} {TH}
128 \stringdef {the} {THE}
129
130 % Now, we can try this declaration.
131
132 \wconvert {this is a test of the motherboard input}
133 \message {"\cnvout"}
134
135 % The result is: "THIS is a TEST of THe moTHerboard output".
136
137 % Users have to use the \SPACE or \BGROUP or \EGROUP instead of real tokens
138 % of catcode 10, 1 and 2 at the left hand side of the declaration:
139
140 \stringdef {\SPACE the} { !THE!}
141
142 \wconvert {this is a test of the motherboard input}
143 \message {output: "\meaning\cnvout"}
144
145 \end
146
147
148
149
150
151
152
153
154