]> git.donarmstrong.com Git - debian/debian-policy.git/blob - DebianDoc_SGML/Format/LaTeX.pm
Manually added patch 125
[debian/debian-policy.git] / DebianDoc_SGML / Format / LaTeX.pm
1 ## -*- perl -*-
2 ## ----------------------------------------------------------------------
3 ## DebianDoc_SGML/Format/LaTeX: SGML conversion specification for LaTeX output format
4 ## ----------------------------------------------------------------------
5 ## Copyright (C) 1998-2001 Ardo van Rangelrooij
6 ## Copyright (C) 1997 Christian Leutloff
7 ##
8 ## This is free software; see the GNU General Public Licence
9 ## version 2 or later for copying conditions.  There is NO warranty.
10 ## ----------------------------------------------------------------------
11
12 ## ----------------------------------------------------------------------
13 ## package interface definition
14 package DebianDoc_SGML::Format::LaTeX;
15 use strict;
16 use vars qw( @ISA @EXPORT );
17 use Exporter;
18 @ISA = ( 'Exporter' );
19 @EXPORT = qw ();
20
21 ## ----------------------------------------------------------------------
22 ## import packages
23 use SGMLS::Output;
24
25 ## ----------------------------------------------------------------------
26 ## paper size definitions
27 my @paper = split( /\s/, `2>/dev/null paperconf -N` );
28 my $pagespec = "A4";
29 if ( $#paper > -1 )
30 {
31     if ( $paper[0] =~ m/[ABC][0-9]/ )
32     {
33         $pagespec = $paper[0];
34     }
35     elsif ( $paper[0] =~ m/[letter|legal|executive]/ )
36     {
37         $paper[0] =~ tr/A-Z/a-z/;
38         $pagespec = "US" . $paper[0];
39     }
40 }
41
42 ## ----------------------------------------------------------------------
43 ## layout definitions
44 $DebianDoc_SGML::Format::Driver::indent_level = 1;
45
46 ## ----------------------------------------------------------------------
47 ## global variables
48 use vars qw( $set_appendix $set_arabic );
49
50 ## ----------------------------------------------------------------------
51 ## book output subroutines
52 ## ----------------------------------------------------------------------
53 sub _output_start_book
54 {
55     my $babel = $DebianDoc_SGML::Format::Driver::i18n{'babel'};
56     output( "\\documentclass[11pt,$babel]{book}\n" );
57     output( "\\usepackage{t1enc}\n" );
58     output( "\\usepackage[$DebianDoc_SGML::Format::Driver::i18n{'inputenc'}]{inputenc}\n" )
59         if length( $DebianDoc_SGML::Format::Driver::i18n{'inputenc'} );
60     output( "\\usepackage{babel}\n" );
61     output( "\n" );
62     output( "\\usepackage{helvet}\n" );
63     ## NOTE: below is a temporary solution !!!!!!!!!
64     output( "\\usepackage{times}\n" )
65         if $DebianDoc_SGML::Format::Driver::i18n{ 'inputenc' } ne 'latin2';
66     output( "\n" );
67     output( "\\usepackage{vmargin}\n" );
68     output( "\\setpapersize{$pagespec}\n" );
69     output( "\\setmarginsrb{25.4mm}{25.4mm}{25.4mm}{25.4mm}{15pt}{11mm}{0pt}{11mm}\n" );
70     output( "\n" );
71     output( "\\usepackage{fancyhdr}\n" );
72     output( "\\pagestyle{fancy}\n" );
73     output( "\\renewcommand{\\chaptermark}[1]{%\n" );
74     output( "\\markboth{\\chaptername\\ \\thechapter.\\ #1}{}}\n" );
75     output( "\\lhead{\\leftmark}\n" );
76     output( "\\chead{}\n" );
77     output( "\\rhead{\\thepage}\n" );
78     output( "\\lfoot{}\n" );
79     output( "\\cfoot{}\n" );
80     output( "\\rfoot{}\n" );
81     output( "\\renewcommand{\\headrulewidth}{0.4pt}\n" );
82     output( "\\renewcommand{\\footrulewidth}{0pt}\n" );
83     output( "\\fancypagestyle{plain}{%\n" );
84     output( "\\lhead{}\n" );
85     output( "\\chead{}\n" );
86     output( "\\rhead{\\thepage}\n" );
87     output( "\\lfoot{}\n" );
88     output( "\\cfoot{}\n" );
89     output( "\\rfoot{}\n" );
90     output( "\\renewcommand{\\headrulewidth}{0.4pt}\n" );
91     output( "\\renewcommand{\\footrulewidth}{0pt}}\n" );
92     output( "\n" );
93     output( "\\usepackage{paralist}" );
94     output( "\\usepackage{alltt}\n" );
95     output( "\n" );
96     output( "\\usepackage[multiple]{footmisc}\n" );
97     output( "\\usepackage{url}\n" );
98     output( "\\usepackage{varioref}\n" );
99     output( "\\vrefwarning\n" );
100     output( "\\usepackage{hyperref}\n" );
101     output( "\n" );
102     output( "\\parindent=0pt\n" );
103     output( "\\setlength{\\parskip}{%\n" );
104     output( "0.5\\baselineskip plus0.1\\baselineskip minus0.1\\baselineskip}\n" );
105     output( "\n" );
106     output( "\\begin{document}\n" );
107 }
108 sub _output_end_book
109 {
110     output( "\n" );
111     output( "\\end{document}\n");
112     output( "\n" );
113 }
114
115 ## ----------------------------------------------------------------------
116 ## title page output subroutines
117 ## ----------------------------------------------------------------------
118 sub _output_titlepag
119 {
120     output( "\n" );
121     output( "\\begin{titlepage}\n" );
122     output( "\n" );
123     output( "\\thispagestyle{empty}\n" );
124     output( "\n" );
125     output( "\\begin{center}\n" );
126     output( "{\\Huge \\vspace*{2ex} $DebianDoc_SGML::Format::Driver::title \\\\[2ex]}\n" );
127     grep( output( "{\\large $_ } \\\\\n" ), @DebianDoc_SGML::Format::Driver::authors );
128     output( "\\vspace*{1ex} $DebianDoc_SGML::Format::Driver::version \\\\\n" )
129         if length( $DebianDoc_SGML::Format::Driver::version );
130     output( "\\end{center}\n" ); 
131     if ( length( $DebianDoc_SGML::Format::Driver::abstract ) )
132     {
133         output( "\n" );
134         output( "\\vspace*{1ex}\n" );
135         output( "\\begin{center}\n" );
136         output( "\\section*{$DebianDoc_SGML::Format::Driver::i18n{ 'abstract' }}\n" );
137         output( "\\end{center}\n" ); 
138         output( "$DebianDoc_SGML::Format::Driver::abstract\n" );
139     }
140     if ( length($DebianDoc_SGML::Format::Driver::copyright ) )
141     {
142         output( "\n" );
143         output( "\\newpage\n" );
144         output( "\n" );
145         output( "\\thispagestyle{empty}\n" );
146         output( "\n" );
147         output( "\\vspace*{1ex}\n" );
148         output( "\\vfill\n" );
149         output( "\\section*{$DebianDoc_SGML::Format::Driver::i18n{ 'copyright notice' }}\n" );
150         output( "\n" );
151         output( "$DebianDoc_SGML::Format::Driver::copyright\n" );
152     }
153     output( "\n" );
154     output( "\\end{titlepage}\n" ); 
155 }
156 sub _output_copyrightsummaries
157 {
158     output( join( " \\\\\n", @_ ), "\n" );
159 }
160
161 ## ----------------------------------------------------------------------
162 ## table of contents output subroutines
163 ## ----------------------------------------------------------------------
164 sub _output_toc
165 {
166     output( "\n" );
167     output( "\\pagenumbering{roman}\n" );
168     output( "\\tableofcontents\n" );
169 }
170 sub _output_tocentry
171 {
172 }
173
174 ## ----------------------------------------------------------------------
175 ## section output subroutines
176 ## ----------------------------------------------------------------------
177 sub _output_chapter
178 {
179     output( $_[0] );
180 }
181 sub _output_appendix
182 {
183     output( $_[0] );
184 }
185 sub _output_heading
186 {
187     if ( $_[1] == -2 && ! $set_appendix )
188     {
189         $set_appendix = 1;
190         output( "\n" );
191         output( "\\appendix\n" );
192     }
193     output( "\n" );
194     if ( $_[1] < 0 )
195     {
196         output( "\\chapter" );
197     }
198     elsif ( $_[1] == 0 )
199     {
200         output( "\\section" );
201     }
202     elsif ( $_[1] == 1 )
203     {
204         output( "\\subsection" );
205     }
206     elsif ( $_[1] == 2 )
207     {
208         output( "\\subsubsection" );
209     }
210     elsif ( $_[1] == 3 )
211     {
212         output( "\\paragraph" );
213     }
214     else
215     {
216         output( "\\subparagraph" );
217     }
218     output( "{$_[0]}\n" );
219     output( "\\label{$_[3]}\n" ) if length( $_[3] );
220     if ( ! $set_arabic )
221     {
222         $set_arabic = 1;
223         output( "\\pagenumbering{arabic}\n" );
224     }
225 }
226
227 ## ----------------------------------------------------------------------
228 ## paragraph output subroutines
229 ## ----------------------------------------------------------------------
230 sub _output_p
231 {
232     if ( length( $_[0] ) )
233     {
234         # Before a new paragraph and after a non-compact example
235         output( "\n" )
236             if ! $DebianDoc_SGML::Format::Driver::is_compact
237                 && ! $DebianDoc_SGML::Format::Driver::was_compact;
238         $_[0] =~ s/^\s+//gm;      # remove leading spaces
239         $_[0] =~ s/\n\n+/\n/g;    # no blank lines in paragraphs
240         output( "$_[0]\n" );
241     }
242     else
243     {
244         # This puts a newline between adjacent specials, which doesn't
245         # do anything, but more importantly ensures that there is a
246         # newline before a paragraph which begins with a compact special
247         output( "\n" )
248             if ( $DebianDoc_SGML::Format::Driver::is_special
249                  && ! $DebianDoc_SGML::Format::Driver::is_compact
250                  && $DebianDoc_SGML::Format::Driver::will_be_compact );
251     }
252
253     # The logic here is a bit hairy.  Basically, we only change the
254     # \parskip setting if the following is the case:
255     #  set to >0 if we're not currently compact, but we're coming out
256     #               of a compact state, but we're not about to enter
257     #               one again
258     if ( ! $DebianDoc_SGML::Format::Driver::is_compact &&
259          $DebianDoc_SGML::Format::Driver::was_compact &&
260          ( ( $DebianDoc_SGML::Format::Driver::is_special &&
261              ! $DebianDoc_SGML::Format::Driver::will_be_compact) ||
262            ! $DebianDoc_SGML::Format::Driver::is_special ) )
263     {
264         output( "\\setlength{\\parskip}{%\n" );
265         output( "0.5\\baselineskip plus0.1\\baselineskip minus0.1\\baselineskip}\n" );
266     }
267     # and set to 0 if we're about to enter a compact state, but we aren't
268     #                 currently compact or leaving a compact state
269     if ( $DebianDoc_SGML::Format::Driver::is_special &&
270          $DebianDoc_SGML::Format::Driver::will_be_compact &&
271          ! $DebianDoc_SGML::Format::Driver::is_compact &&
272          ! $DebianDoc_SGML::Format::Driver::was_compact )
273     {
274         output( "\\setlength{\\parskip}{0ex}\n" );
275     }
276 }
277
278 ## ----------------------------------------------------------------------
279 ## example output subroutines
280 ## ----------------------------------------------------------------------
281 sub _output_example
282 {
283     my $space = $DebianDoc_SGML::Format::Driver::indent_level > 0 ?
284         "  " x $DebianDoc_SGML::Format::Driver::indent_level : "    ";
285     $_[0] = " $space" . $_[0];
286     $_[0] =~ s/\n/\n $space/g;
287     $_[0] =~ s/\s+$/\n/;
288     output( "\n" ) if ! $DebianDoc_SGML::Format::Driver::is_compact;
289     output( "\\begin{alltt}\n" );
290     output( $_[0] );
291     output( "\\end{alltt}\n" );
292 }
293
294 ## ----------------------------------------------------------------------
295 ## footnote output subroutines
296 ## ----------------------------------------------------------------------
297 sub _output_footnote
298 {
299     output( "\\footnote{$_[0]}" );
300 }
301
302 ## ----------------------------------------------------------------------
303 ## comment output subroutines
304 ## ----------------------------------------------------------------------
305 sub _output_comment
306 {
307 }
308
309 ## ----------------------------------------------------------------------
310 ## list output subroutines
311 ## ----------------------------------------------------------------------
312 sub _output_list
313 {
314     output( "\n" ) if ! $DebianDoc_SGML::Format::Driver::is_compact;
315     output( $DebianDoc_SGML::Format::Driver::is_compact
316             ? "\\begin{compactitem}\n" : "\\begin{itemize}\n" );
317     output( $_[0] );
318     output( "\n" ) if ! $DebianDoc_SGML::Format::Driver::is_compact;
319     output( $DebianDoc_SGML::Format::Driver::is_compact
320             ? "\\end{compactitem}\n" : "\\end{itemize}\n" );
321 }
322 sub _output_enumlist
323 {
324     output( "\n" ) if ! $DebianDoc_SGML::Format::Driver::is_compact;
325     output( $DebianDoc_SGML::Format::Driver::is_compact
326             ? "\\begin{compactenum}\n" : "\\begin{enumerate}\n" );
327     output( $_[0] );
328     output( "\n" ) if ! $DebianDoc_SGML::Format::Driver::is_compact;
329     output( $DebianDoc_SGML::Format::Driver::is_compact
330             ? "\\end{compactenum}\n" : "\\end{enumerate}\n" );
331 }
332 sub _output_taglist
333 {
334     output( "\n" ) if ! $DebianDoc_SGML::Format::Driver::is_compact;
335     output( $DebianDoc_SGML::Format::Driver::is_compact
336             ? "\\begin{compactdesc}\n" : "\\begin{description}\n" );
337     output( $_[0] );
338     output( "\n" ) if ! $DebianDoc_SGML::Format::Driver::is_compact;
339     output( $DebianDoc_SGML::Format::Driver::is_compact
340             ? "\\end{compactdesc}\n" : "\\end{description}\n" );
341 }
342 sub _output_list_tag
343 {
344 }
345 sub _output_enumlist_tag
346 {
347 }
348 sub _output_taglist_tag
349 {
350 }
351 sub _output_list_item
352 {
353     $_[0] =~ s/^\n//;
354     output( "\n" ) if ! $DebianDoc_SGML::Format::Driver::is_compact;
355     output( "\\item\n" );
356     output( $_[0] );
357 }
358 sub _output_enumlist_item
359 {
360     $_[0] =~ s/^\n//;
361     output( "\n" ) if ! $DebianDoc_SGML::Format::Driver::is_compact;
362     output( "\\item\n" );
363     output( $_[0] );
364 }
365 sub _output_taglist_item
366 {
367     $_[0] =~ s/^\n//;
368     output( "\n" ) if ! $DebianDoc_SGML::Format::Driver::is_compact;
369     foreach ( @{$_[1]} )
370     {
371         output( "\\item[$_]\n" );
372     }
373     output( "$_[0]" );
374 }
375
376 ## ----------------------------------------------------------------------
377 ## emph output subroutines
378 ## ----------------------------------------------------------------------
379 sub _output_em
380 {
381     output( "\\textit{$_[0]}" );
382 }
383 sub _output_strong
384 {
385     output( "\\textbf{$_[0]}" );
386 }
387 sub _output_var
388 {
389     output( "\\textit{$_[0]}" );
390 }
391 sub _output_package
392 {
393     output( "\\texttt{$_[0]}" );
394 }
395 sub _output_prgn
396 {
397     output( "\\texttt{$_[0]}" );
398 }
399 sub _output_file
400 {
401     output( "\\path{$_[0]}" );
402 }
403 sub _output_tt
404 {
405     # Double dashes should be written as --- to get a double dash in
406     # the font.  Strange or what?
407     $_[0] =~ s/--/---/g;
408     output( "\\texttt{$_[0]}" );
409 }
410 sub _output_qref
411 {
412     output( $_[0] );
413 }
414
415 ## ----------------------------------------------------------------------
416 ## xref output subroutines
417 ## ----------------------------------------------------------------------
418 sub _output_ref
419 {
420     output( "`$_[0]' \\vpageref{$_[3]}" );
421 }
422 sub _output_manref
423 {
424     output( "\\texttt{" );
425     output( _sani( $_[0] ) );
426     output( "($_[1])}" );
427 }
428 sub _output_email
429 {
430     output( ' ' ) if ( $DebianDoc_SGML::Format::Driver::in_author );
431     output( "\\texttt{<$_[0]>}" );
432 }
433 sub _output_ftpsite
434 {
435     output( "\\url{$_[0]}" );
436 }
437 sub _output_ftppath
438 {
439     output( "\\path{$_[1]}" );
440 }
441 sub _output_httpsite
442 {
443     output( "\\url{$_[0]}" );
444 }
445 sub _output_httppath
446 {
447     output( "\\path{$_[1]}" );
448 }
449 sub _output_url
450 {
451     output( _sani( $_[1] ) ) if $_[1] ne "";
452     output( " (" ) if $_[1] ne "";
453     output( "\\path{" );
454     output( $_[0] );
455     output( "}" );
456     output( ")" ) if $_[1] ne "";
457 }
458
459 ## ----------------------------------------------------------------------
460 ## data output subroutines
461 ## ----------------------------------------------------------------------
462 sub _cdata
463 {
464     output( _sani( $_[0] ) );
465 }
466 sub _sdata
467 {
468     output( $DebianDoc_SGML::Format::Driver::sdata_mapping{ $_[0] } )
469         if defined( $DebianDoc_SGML::Format::Driver::sdata_mapping{ $_[0] } );
470 }
471
472 my $in_quote=0;
473 sub _sani
474 {
475     ( $_ ) = @_;
476
477     # replace backslash character
478     s/\\/\\(\\backslash\\)/g;
479
480     # escape command characters
481     s/{/\\{/g;
482     s/}/\\}/g;
483     s/\$/\\\$/g;
484
485     # escape command characters
486     s/_/\\_/g;
487     s/&/\\&/g;
488     s/\#/\\\#/g;
489     s/\%/\\\%/g;
490     s/\^/\\^{}/g;
491     s/~/\\~{}/g;
492
493     # no further replacement in examples
494     return $_ if $DebianDoc_SGML::Format::Driver::is_example;
495
496 # Don't want this to happen; we can use &ndash; and &mdash if we
497 # really want dashes.
498 #    # hyphens
499 #    s/-/--/g;
500 #    s/----/---/g;
501
502     # quotes
503     if ($in_quote && /\"/) {
504         s/\"/\'\'/;
505         $in_quote=0;
506     }
507     s/\"(.*?)\"/\`\`$1\'\'/g;
508     if (/\"/) { # quotes left
509         s/\"/\`\`/;
510         $in_quote=1;
511     }
512
513     # dots should be ellipsis "..."
514     s/\.\.\./\\dots /g;
515
516     return $_;
517 }
518
519 ## ----------------------------------------------------------------------
520 ## don't forget this
521 1;
522
523 ## ----------------------------------------------------------------------