summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/conv-xkv/conv-xkv.dtx
blob: 478fdad7135893aed6572fe1be2c3fa424d949e3 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
% \iffalse
%<*copyright>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% conv-xkv package                                     %%
%% Copyright (C) 2016  D. P. Story                      %%
%%   dpstory@uakron.edu                                 %%
%%                                                      %%
%% This program can redistributed and/or modified under %%
%% the terms of the LaTeX Project Public License        %%
%% Distributed from CTAN archives in directory          %%
%% macros/latex/base/lppl.txt; either version 1 of the  %%
%% License, or (at your option) any later version.      %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%</copyright>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{conv-xkv}
%<package> [2016/12/20 v1.0 convert xkeyval format (dps)]
%<*driver>
\documentclass{ltxdoc}
\usepackage[colorlinks,hyperindex=false]{hyperref}
\OnlyDescription  % comment out for implementation details
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\InputIfFileExists{aebdocfmt.def}{\PackageInfo{web}{Inputting aebdocfmt.def}}
    {\def\IndexOpt{\DescribeMacro}\def\IndexKey{\DescribeMacro}\let\setupFullwidth\relax
     \PackageInfo{web}{aebdocfmt.def cannot be found}}
\begin{document}
\def\ameta#1{\ensuremath{\langle\textit{\texttt{#1}}\rangle}}
\def\meta#1{\textsl{\texttt{#1}}}
\def\darg#1{{\ttfamily\char123\relax#1\char125\relax}}
\def\CMD#1{\textbackslash#1}
\let\pkg\textsf
\let\opt\texttt
  \GetFileInfo{conv-xkv.sty}
  \title{\textsf{conv-xkv}: Convert \textsf{xkeyval} format style}
  \author{D. P. Story\\
    Email: \texttt{dpstory@uakron.edu}}
  \date{processed \today}
  \maketitle
  \tableofcontents
  \DocInput{conv-xkv.dtx}
\IfFileExists{\jobname.ind}{\newpage\setupFullwidth\par\PrintIndex}{\paragraph*{Index} The index goes here.\\Execute
    \texttt{makeindex -s gind.ist -o conv-xkv.ind conv-xkv.idx} on the command line and recompile
    \texttt{conv-xkv.dtx}.}
\IfFileExists{\jobname.gls}{\PrintChanges}{\paragraph*{Change History} The list of changes goes here.\\Execute
    \texttt{makeindex -s gglo.ist -o conv-xkv.gls conv-xkv.glo} on the command line and recompile
    \texttt{conv-xkv.dtx}.}
\end{document}
%</driver>
% \fi
%
% \MakeShortVerb{|}
% \InputIfFileExists{aebdonotindex.def}{\PackageInfo{web}{Inputting aebdonotindex.def}}
%    {\PackageInfo{web}{cannot find aebdonotindex.def}}
%
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%    \section{Introduction}
%    This is a intellectual exercise for creating \emph{alternate key-value} notation. The standard {\LaTeX}
%    notation is \texttt{\ameta{key}=\ameta{value}}. To change to the JavaScript object style of
%    key-values (\texttt{\ameta{key}:\ameta{value}}), use \cs{cxkvsetkeys} as you would \cs{setkeys}:
%\begin{quote}
%    \cs{cxkvsetkeys}\darg{\ameta{family}}\darg{\ameta{KV-pairs}}
%\end{quote}
%    to convert \texttt{\ameta{key}:\ameta{value}} to \texttt{\ameta{key}=\ameta{value}} and \pkg{xkeyval} processes the keys as it
%    normally does. The comma (,) separates sets of key-value pairs and must not, therefore, be used as the delimiter
%    that separates the \ameta{key} from the \ameta{value}.
%
%    The package is more general than what is described above. You can define several key-value delimiters, for whatever reason,
%    in your document or package. Declare a \textit{named} delimiter:
%\begin{quote}\ttfamily
%\cs{DeclareDelimiter}\darg{\ameta{name}}\darg{\ameta{delimiter}}
%\end{quote}
%Use the newly declared delimited as follows:
%\begin{quote}
%\cs{cxkvsetkeys[\ameta{name}]}\darg{\ameta{family}}\darg{\ameta{KV-pairs}}
%\end{quote}
%The case of using a colon (:) for the delimiter is already defined, its name is `\texttt{colon}' and need not be declared.
%
%    The example file is \texttt{convert2xkeyval.tex}, use it to explore the possibilities and is found
%    in the \texttt{examples} folder of this distribution.
%
%    \section{Preliminaries}
%    We require the \pkg{xkeyval} package.
%    \changes{v1.0}{2016/12/20}{Date of first upload to CTAN}
%     \begin{macrocode}
\RequirePackage{xkeyval}
%    \end{macrocode}
%    The code below is taken from \pkg{hyperref}, and set and restore commands are renamed. This hopefully makes
%    a number of special characters available to act as a delimiter.
%    \begin{macrocode}
\begingroup
  \@makeother\`%
  \@makeother\=%
  \edef\x{%
    \edef\noexpand\x{%
      \endgroup
      \noexpand\toks@{%
        \catcode 96=\noexpand\the\catcode`\noexpand\`\relax
        \catcode 61=\noexpand\the\catcode`\noexpand\=\relax
      }%
    }%
    \noexpand\x
  }%
\x
\@makeother\`
\@makeother\=
\def\ckv@SetCatcodes{%
  \@makeother\`%
  \@makeother\=%
  \@makeother\~%
  \catcode`\$=3 %
  \catcode`\&=4 %
  \catcode`\^=7 %
  \catcode`\_=8 %
  \@makeother\|%
  \@makeother\:%
  \@makeother\(%
  \@makeother\)%
  \@makeother\[%
  \@makeother\]%
  \@makeother\/%
  \@makeother\!%
  \@makeother\<%
  \@makeother\>%
  \@makeother\.%
  \@makeother\;%
  \@makeother\+%
  \@makeother\-%
  \@makeother\"%
  \@makeother\'%
}
\begingroup
  \def\x#1{\catcode`\noexpand#1=\the\catcode`#1\relax}%
  \xdef\ckv@RestoreCatcodes{%
    \the\toks@
    \x\~%
    \x\$%
    \x\&%
    \x\^%
    \x\_%
    \x\|%
    \x\:%
    \x\(%
    \x\)%
    \x\[%
    \x\]%
    \x\/%
    \x\!%
    \x\<%
    \x\>%
    \x\.%
    \x\;%
    \x\+%
    \x\-%
    \x\"%
    \x\'%
  }%
\endgroup
\ckv@SetCatcodes
%    \end{macrocode}
%    \section{Core commands for this package}
%    The default delimiter is the colon (:).
%    \begin{macrocode}
\def\csarg#1#2{\expandafter#1\csname#2\endcsname}
\csarg\def{kvdelim-colon}{:}
%    \end{macrocode}
%    Use \DescribeMacro{\usekvdelim}\cs{usekvdelim} to display delimiter, as associated with
%    the argument \texttt{\#1}.
%    \begin{macrocode}
\def\usekvdelim#1{\@nameuse{kvdelim-#1}}
%    \end{macrocode}
%    \begin{macro}{\DeclareDelimiter}
%    In the preamble, we declare the delimiter to be used. The command takes on argument, which
%    is the delimiter to be used, for example `\texttt{:}' or `\texttt{->}'. If this declaration
%    does not appear in the preamble, the delimited is taken to be `\texttt{:}'.
%    \begin{macrocode}
\def\DeclareDelimiter{\ckv@SetCatcodes\DeclareDelimiter@i}
\def\DeclareDelimiter@i#1#2{\@ifundefined{kvdelim-#1}
    {\csarg\def{kvdelim-#1}{#2}\ckv@RestoreCatcodes\cxkvSetup{#1}}
    {\ckv@RestoreCatcodes}%
}
\@onlypreamble\DeclareDelimiter
%    \end{macrocode}
%    \end{macro}
%    \cs{cxkv@tmptoks} is used to hold the converted key-values, the contents of
%    this token register is passed to \cs{setkeys} in \cs{cxkv@convertColoniiEquali}
%    \begin{macrocode}
\newtoks\cxkv@tmptoks \cxkv@tmptoks={}
\def\cxkv@dummy{dummy}
\def\cxkv@dummyc{dummy,}
\bgroup
    \catcode`\#=12\relax\gdef\cxkvarg{#}
    \obeyspaces\gdef\cxkv@TAB{    }
\egroup
%    \end{macrocode}
%    \begin{macro}{\cxkvsetkeys}
%    This is the default definition, setup for using the colon (:) as the key-value
%    delimiter. But these next two commands are redefined by the
%    \cs{DeclareDelimiter} command in the preamble. The syntax is
%\begin{quote}
%\cs{cxkvsetkeys[\ameta{name}]}\darg{\ameta{family}}\darg{\ameta{KV-pairs}}
%\end{quote}
%where \ameta{KV-pairs} are the key-value pairs using the declared delimiter.
%\begin{quote}\ttfamily
%\cs{cxkvsetkeys}\darg{myfam}\{fname:\,Fred,lname:\,Flintstone\}
%\end{quote}
%The family \texttt{myfam} and keys \texttt{fname} and \texttt{lname} must have been defined
%earlier: If the optional argument is not specified, then it is assumed the \ameta{name} argument
%is \texttt{colon}, a reserved word for this package for this argument.
%\begin{verbatim}
%   \define@key{myfam}{\def\fname{#1}}
%   \define@key{myfam}{\def\lname{#1}}
%\end{verbatim}
%    \begin{macrocode}
\def\cxkv@colon{colon}
\newcommand\cxkvsetkeys[1][colon]{%
    \def\@rgi{#1}\ifx\@rgi\cxkv@colon\else
        \InputIfFileExists{xkv-#1.cut}
        {\PackageInfo{conv-xkv}{Inputting xkv-#1.cut}}
        {\PackageInfo{conv-xkv}{Cannot find xkv-#1.cut}}\fi
    \@nameuse{cxkvsetkeys-#1}}
%    \end{macrocode}
%    \begin{macrocode}
\csarg\def{cxkvsetkeys-colon}#1#2{%
    \def\thisxkvF@mily{#1}\def\cxkv@scratch{}\cxkv@tmptoks={}%
    \@nameuse{cxkv@convertColoniiEqual-colon}#2,dummy:dummy,\@nil}
\csarg\def{cxkv@convertColoniiEqual-colon}#1:#2,#3\@nil{%
    \cxkv@convertColoniiEquali{colon}{#1}{#2}{#3}}
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\cxkvSetup}
%    Write the definitions of \cs{cxkvsetkeys} and \cs{cxkv@convertColoniiEqual} to
%    the file \texttt{conv-xkv.cut} then input this file back in.
%    \begin{macrocode}
\def\cxkvSetup#1{\bgroup
\IfFileExists{xkv-#1.cut}{\PackageInfo{conv-xkv}{xkv-#1.cut
    already exists,\MessageBreak will not create another one}}{%
    \PackageInfo{conv-xkv}{Creating the file xkv-#1.cut
        containing\MessageBreak required definitions}%
    \newwrite \cxkv@write
    \uccode`c=`\%
    \def\w{#1}\def\x{cxkvsetkeys-#1}%
    \def\y{cxkv@convertColoniiEqual-#1}%
    \def\z{kvdelim-#1}%
    \immediate\openout \cxkv@write xkv-#1.cut
    \immediate\write\cxkv@write{\string\makeatletter}%
    \uppercase{\immediate\write\cxkv@write{\string
        \csarg\string\def{\y}\cxkvarg1\@nameuse{\z}%
        \cxkvarg2,\cxkvarg3\string\@nil{c^^J\cxkv@TAB
        \string\cxkv@convertColoniiEquali{\w}{\cxkvarg1}%
        {\cxkvarg2}{\cxkvarg3}}}}
    \uppercase{\immediate\write\cxkv@write{\string\csarg\string\def
        {\x}\cxkvarg1\cxkvarg2{c^^J\cxkv@TAB
        \string\def\string\thisxkvF@mily{\cxkvarg1}\string
        \let\string\cxkv@scratch\string\@empty\string
        \cxkv@tmptoks={}c^^J\cxkv@TAB
        \string\@nameuse{\y}\cxkvarg2,%
        \cxkv@dummy\@nameuse{\z}\cxkv@dummy,\string\@nil}}}
    \immediate\write\cxkv@write{\string\makeatother}%
    \immediate\closeout \cxkv@write
}%
\egroup}
%    \end{macrocode}
%    \end{macro}
%    \cs{cxkv@convertColoniiEquali} continues \cs{cxkv@convertColoniiEqual}. It is
%    the part that does not need to be redefined.
%    \begin{macrocode}
\newif\ifcxkv@keyonly \cxkv@keyonlyfalse
\def\cxkv@comma{,}
\def\cxkv@removecomma#1,\@nil{\def\cxkv@key{#1}}
\def\cxkv@parsecomma#1,#2\@nil{\def\@rgi{#1}\def\@rgii{#2}%
    \ifx\@rgii\@empty\cxkv@keyonlyfalse\else
        \cxkv@keyonlytrue\cxkv@removecomma#2\@nil\fi}
\def\cxkv@convertColoniiEquali#1#2#3#4{\def\cxkv@rgiii{#3}%
    \ifx\cxkv@rgiii\cxkv@dummy
        \cxkv@parsecomma#2,\@nil
        \ifcxkv@keyonly
            \edef\cxkv@tmp{\the\cxkv@tmptoks,\@rgi}%
            \cxkv@tmptoks=\expandafter{\cxkv@tmp}%
            \edef\cxkv@scratch{\the\cxkv@tmptoks}%
            \edef\cxkv@next{\noexpand
                \setkeys{\thisxkvF@mily}{\the\cxkv@tmptoks}}%
        \else
            \edef\cxkv@next{\noexpand
                \setkeys{\thisxkvF@mily}{\the\cxkv@tmptoks}}%
        \fi
    \else
        \cxkv@parsecomma#2,\@nil
        \ifcxkv@keyonly
            \edef\cxkv@tmp{\the\cxkv@tmptoks,\@rgi}%
            \cxkv@tmptoks=\expandafter{\cxkv@tmp}%
            \edef\cxkv@scratch{\the\cxkv@tmptoks}%
            \edef\cxkv@next{\noexpand
                \@nameuse{cxkv@convertColoniiEqual-#1}\cxkv@key
                \@nameuse{kvdelim-#1}#3,#4\noexpand\@nil}
        \else
            \cxkv@tmptoks=\expandafter{\cxkv@scratch,#2=#3}%
            \edef\cxkv@scratch{\the\cxkv@tmptoks}%
            \def\cxkv@next{%
                \@nameuse{cxkv@convertColoniiEqual-#1}#4\@nil}\fi
    \fi\cxkv@next
}
\ckv@RestoreCatcodes
%    \end{macrocode}
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%  \Finale
\endinput