summaryrefslogtreecommitdiff
path: root/support/dtxgen/dtxgen
blob: 9d9e91103e17589b72261e8159d0886659b38fe1 (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/bin/bash

Version=1.08
Myname="${0##*/}"

:<<'DOC'
= dtxgen - generate template for LaTeX self-extracting .dtx file

= Synopsis
dtxgen [options] basename.[sty,cls]	

== Options
-h,--help	print short help and exit
-H,--Help	print full documentation via less and exit
-V,--version	print version and exit
-s,--short	short, one-liner, package description
-d,--date	initial version's date. Default: current date 
-m,--mail	your email address. Default: |$EMAIL|
-n,--name	your name. Default: |$NAME|
-c,--class	(class packs only) class to be preloaded. Default: article
-q,--quiet	run quietly

= Description
dtxgen creates a template for a self-extracting .dtx file, based on the model described by
[Joseph Wright](www.texdev.net/2009/10/06/a-model-dtx-file/). It is useful for
those who plan to create a new Documented LaTeX Source (.dtx) file.

Usage example:

  dtxgen -n 'your name' -m 'your@email.ad' myclass.cls

The script takes some variables such as:
- name and email address of the author,
- a short description of the class or package generated from the .dtx file,
- a date
from environment variables, or from command line options and generates,
among more, a template for the .dtx file with some minimal examples. Of
course, the user will have to replace those examples with the real work,
but the dates, basename, author's name and email address are already in
place and, depending on whether you use used a .cls or a .sty extension
in the argument, it is formatted to be either a class or a package
source file.

If you have an environment with your name and email address defined in
NAME and EMAIL, you could simply type:

  dtxgen myclass.cls

and you would end up with five files: |myclass.dtx|, |myclass.cls|,
|myclass.pdf|, |README|, and |Makefile|.

= Options
dtxgen recognizes the following options:

-s,--short=...	
	A short, one-liner, description for the class or package.
	By default, the string /A new LaTeX class/ or /A new LaTeX package/
	will be used.
-n,--name=...	
	Your name (first name, followed by surname). Alternatively,
	you can set a default value in the environment variable |NAME|;
	if you do so and still use this option, the option's value wills
	have priority.
-m,--mail=...	
	Your email address. Alternatively, you can set a default value ins
	the environment variable |EMAIL|; if you do so and still use this
	option, the option's value will have priority.
-c,--class=...	
	For class templates only: inserts a |\LoadClass{...}|, so that the
	new class will start with the properties of the |...| class.
	The default is |article|.
-d,--date=...	
	Set the initial version's date. By default, the current date will
	be used.  The date should be entered in the |yyyymmdd| format,
	although it will be stored the LaTeX way: |yyyy/mm/dd|.
-q,--quiet	
	Run quietly
-V,--version	
	Prints the script's version and exits.
-h,--help	
	Prints help information and exits.
-H,--Help	
	print full documentation via less and exit

= Makefile
The |Makefile| can be used to compile new versions of your work; it contains the following targets:

all		(the default) generate the style or class file, the pdf-documentation,
		and a README file.
distclean	remove all files that can be regenerated,
clean		same, except the style or class file, the pdf-documentation,
		and a README file.
inst		install in the user's TeX tree,
install		install in the local TeX tree (uses sudo)
zip		produce a zip file ready for upload to CTAN

= Author
[Wybo Dekker](wybo@dekkerdocumenten.nl)

= Copyright
Released under the [GNU General Public License](www.gnu.org/copyleft/gpl.html)
DOC

# check executables and versions of bash and getopt
((BASH_VERSINFO>=4)) || die "Need bash version >= 4"
for i in getopt make; do
   command -v "$i" &> /dev/null || die "$i: command not found"
done
i=$(getopt -T)
{ (($?==4)) && [[ -z $i ]]; } || die "Your getopt is not GNU"

# using the label in arg 2, read template from my self, filter it
# through the script in arg 1, and write the output to the file in arg 3.
# The default for arg 3 is arg 2
function createfile {
   file=$3
   test "$file" = "" && file=$2
   sed -n "/^:<<'$2'/,/^$2/p" "$0" |tail -n +2 |head -n -1 |sed "$1" >"$file"
}

REd='\e[38;5;9m'
    die() { local i; for i; do echo -e "$Myname: $REd$i"; done 1>&2; exit 1; }
helpsrt() { sed -n '/^= Synopsis/,/^= /p' "$0"|sed '1d;$d'; exit; }
instscr() { instscript --zip --pdf "$Myname"; exit; }

# shellcheck disable=SC2154
helpall() { sed -n "/^:<<'DOC'$/,/^DOC/p" "$0"|sed -n '1d;$d;p'|
            less -Ps"$Myname-${Version/./·} documentation - type h for help, q to quit."
		exit
	  }

if ! options=$(getopt \
   -n "$Myname" \
   -o s:n:m:c:d:hHVqI \
   -l short:,name:,mail:,class:,date:,help,Help,version,quiet -- "$@"
); then exit 1; fi
eval set -- "$options"

while [ $# -gt 0 ]; do
   case $1 in
   (-h|--help)    # print short help and exit
                  helpsrt
                  ;;
   (-H|--Help)    # print full documentation via less and exit
                  helpall
                  ;;      
   (-V|--version) # print version and exit
                  echo $Version
		  exit
                  ;;
   (-s|--short)   # short, one-liner, package description
                  short=$2
                  shift 2
                  ;;
   (-d|--date)    # initial version's date. Default: current date 
                  date=$2
                  shift 2
                  ;;
   (-m|--mail)    # your email address. Default: $EMAIL
                  mail=$2
                  shift 2
                  ;;
   (-n|--name)    # your name. Default: $NAME
                  name=$2
                  shift 2
                  ;;
   (-c|--class)   # (class packs only) class to be preloaded. Default: article
                  loadclass=$2
                  shift 2
                  ;;
   (-q|--quiet)   # run quietly
                  quiet=--quiet
                  shift
                  ;;
   (-I)           instscr
                  ;;
   (--)           shift
                  break
                  ;;
   (*)            break
                  ;;
   esac
done

export year typ Typ lcl use # used in templates - shellcheck se them as unused

name=${name:-${NAME:-(not set)}}
mail=${mail:-${EMAIL:-(not set)}}
date=${date:-$(date +%Y%m%d)}
year=${date:0:4}
loadclass=${loadclass:-article}

# date must be 8 digits; insert the two /'s:
if [[ ! $date =~ ^[[:digit:]]{8}$ ]]; then die "illegal date"; fi
date=$(sed 's/\(....\)\(..\)\(..\)/\1\\\/\2\\\/\3/' <<<"$date")

# argument must have .cls or .sty extension:
base=${1%.*}
ext=${1#*.}

case "$ext" in
(cls) typ=class; Typ=Class;
      short=${short:-A new LaTeX class}
      lcl='\\LoadClass[a4paper,fleqn]{'"$loadclass}"
     ;;
(sty) typ=package; Typ=Package;
      short=${short:-A new LaTeX package}
      use='\\usepackage{\\jobname}'
     ;;
(*)   die "The argument must have .cls or .sty extension
         (try the --help option)"
esac

# The short description may contain TeX commands, but then we need
# a version without them at some places; note that this removes only
# simple contructs:
shrt=$(sed 's/\\[[:alpha:]]\\+{\\([^}]*\\)}/\\1/g;s/\\//g' <<<"$short")

# Any \'s in the short description need to be duplicated for the script:
short="${short//\\/\\\\}"

test -z "$mail" && die "author's email not set - use option or environment variable EMAIL"
test -z "$name" && die "author's name not set - use option or environment variable NAME"

colw=${#base}
(( colw = colw < 7 ? 7 : colw ))
kopl="$(eval "printf '%.0s-' {1..$colw}")"
kopr="$(eval "printf '%.0s-' {1..$((72-colw))}")"
declare -A rm=(
    [$kopl]=$kopr
    [$base]=$shrt
   [Author]=$name
   [E-mail]=$mail
  [License]='Released under the LaTeX Project Public License v1.3c or later'
      [See]='http://www.latex-project.org/lppl.txt'
)

for i in $kopl $base Author E-mail License See; do
  readme+="$(printf "%*s:| %s\\\\n"  $colw "$i" "${rm[$i]}")"
done

# make sed script replacing the variables in the templates:
sedscript=''
for i in base year ext mail use typ Typ name date short shrt lcl readme; do
  test $quiet || eval "echo -e \"$i	\$$i\""|sed 's/\\\\/\\/g'
  eval "sedscript+=\"s=%$i%=\$$i=g;\""
done

createfile "$sedscript" Makefile
createfile "$sedscript" DTX "$base.dtx"
make $quiet
exit 0

# ---- TEMPLATES: ----
:<<'Makefile'
NAME  = %base%
SHELL = bash
PWD   = $(shell pwd)
VERS  = $(shell ltxfileinfo -v $(NAME).dtx|sed -e 's/^v//')
LOCAL = $(shell kpsewhich --var-value TEXMFLOCAL)
UTREE = $(shell kpsewhich --var-value TEXMFHOME)
all:	$(NAME).pdf
	test -e README.txt && mv README.txt README || exit 0
$(NAME).pdf: $(NAME).dtx
	pdflatex -shell-escape -recorder -interaction=batchmode $(NAME).dtx >/dev/null
	if [ -f $(NAME).glo ]; then makeindex -q -s gglo.ist -o $(NAME).gls $(NAME).glo; fi
	if [ -f $(NAME).idx ]; then makeindex -q -s gind.ist -o $(NAME).ind $(NAME).idx; fi
	pdflatex --recorder --interaction=nonstopmode $(NAME).dtx > /dev/null
	pdflatex --recorder --interaction=nonstopmode $(NAME).dtx > /dev/null
clean:
	rm -f $(NAME).{aux,fls,glo,gls,hd,idx,ilg,ind,ins,log,out}
distclean: clean
	rm -f $(NAME).{pdf,%ext%} README
inst: all
	mkdir -p $(UTREE)/{tex,source,doc}/latex/$(NAME)
	cp $(NAME).dtx $(UTREE)/source/latex/$(NAME)
	cp $(NAME).%ext% $(UTREE)/tex/latex/$(NAME)
	cp $(NAME).pdf $(UTREE)/doc/latex/$(NAME)
install: all
	sudo mkdir -p $(LOCAL)/{tex,source,doc}/latex/$(NAME)
	sudo cp $(NAME).dtx $(LOCAL)/source/latex/$(NAME)
	sudo cp $(NAME).%ext% $(LOCAL)/tex/latex/$(NAME)
	sudo cp $(NAME).pdf $(LOCAL)/doc/latex/$(NAME)
zip: all
	ln -sf . $(NAME)
	zip -Drq $(PWD)/$(NAME)-$(VERS).zip $(NAME)/{README,$(NAME).{pdf,dtx}}
	rm $(NAME)
Makefile

:<<'DTX'
% \iffalse meta-comment
% vim: textwidth=75
%<*internal>
\iffalse
%</internal>
%<*readme>
|
%readme%

Short description:
Some text about the %typ%: probably the same as the abstract.
%</readme>
%<*internal>
\fi
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
  \expandafter\begingroup
\fi
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\preamble
%readme%
\endpreamble
\postamble

Copyright (C) %year% by %name% <%mail%>

This work may be distributed and/or modified under the
conditions of the LaTeX Project Public License (LPPL), either
version 1.3c of this license or (at your option) any later
version.  The latest version of this license is in the file:

http://www.latex-project.org/lppl.txt

This work is "maintained" (as per LPPL maintenance status) by
%name%.

This work consists of the file %base%.dtx and a Makefile.
Running "make" generates the derived files README, %base%.pdf and %base%.%ext%.
Running "make inst" installs the files in the user's TeX tree.
Running "make install" installs the files in the local TeX tree.

\endpostamble

\usedir{tex/latex/%base%}
\generate{
  \file{\jobname.%ext%}{\from{\jobname.dtx}{%typ%}}
}
%</install>
%<install>\endbatchfile
%<*internal>
\usedir{source/latex/%base%}
\generate{
  \file{\jobname.ins}{\from{\jobname.dtx}{install}}
}
\nopreamble\nopostamble
\usedir{doc/latex/%base%}
\generate{
  \file{README.txt}{\from{\jobname.dtx}{readme}}
}
\ifx\fmtname\nameofplainTeX
  \expandafter\endbatchfile
\else
  \expandafter\endgroup
\fi
%</internal>
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{%base%.dtx}
%</driver>
%<%typ%>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<%typ%>\Provides%Typ%{%base%}
%<*%typ%>
    [%date% v1.00 %shrt%]
%</%typ%>
%<*driver>
\documentclass{ltxdoc}
\usepackage[a4paper,margin=25mm,left=50mm,nohead]{geometry}
\usepackage[numbered]{hypdoc}
%use%
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \GetFileInfo{\jobname.dtx}
% \DoNotIndex{\newcommand,\newenvironment}
%
%\title{\textsf{%base%} --- %short%\thanks{This file
%   describes version \fileversion, last revised \filedate.}
%}
%\author{%name%\thanks{E-mail: %mail%}}
%\date{Released \filedate}
%
%\maketitle
%
%\changes{v1.00}{%date%}{First public release}
%
% \begin{abstract}
% ==== Put abstract text here. ====
% \end{abstract}
%
% \section{Usage}
%
% ==== Put descriptive text here. ====
%
% \DescribeMacro{\dummyMacro}
% This macro does nothing.\index{doing nothing|usage} It is merely an
% example.  If this were a real macro, you would put a paragraph here
% describing what the macro is supposed to do, what its mandatory and
% optional arguments are, and so forth.
%
% \DescribeEnv{dummyEnv}
% This environment does nothing.  It is merely an example.
% If this were a real environment, you would put a paragraph here
% describing what the environment is supposed to do, what its
% mandatory and optional arguments are, and so forth.
%
%\StopEventually{^^A
%  \PrintChanges
%  \PrintIndex
%}
%
% \section{Implementation}
%
%    \begin{macrocode}
%<*%typ%>
%lcl%
%    \end{macrocode}
% \begin{macro}{\dummyMacro}
% This is a dummy macro.  If it did anything, we'd describe its
% implementation here.
%    \begin{macrocode}
\newcommand{\dummyMacro}{}
%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{dummyEnv}
% This is a dummy environment.  If it did anything, we'd describe its
% implementation here.
%    \begin{macrocode}
\newenvironment{dummyEnv}{%
}{%
%    \end{macrocode}
% \changes{v1.00a}{%date%}{Added a spurious change log entry to
%   show what a change \emph{within} an environment definition looks
%   like.}
% Don't use |%| to introduce a code comment within a |macrocode|
% environment.  Instead, you should typeset all of your comments with
% LaTeX---doing so gives much prettier results.  For comments within a
% macro/environment body, just do an |\end{macrocode}|, include some
% commentary, and do another |\begin{macrocode}|.  It's that simple.
%    \begin{macrocode}
}
%    \end{macrocode}
% \end{environment}
%
%    \begin{macrocode}
\endinput
%</%typ%>
%    \end{macrocode}
%\Finale
DTX