Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1591 lines
43 KiB

  1. package Pod::LaTeX;
  2. # Copyright (C) 2000 by Tim Jenness <[email protected]>
  3. # All Rights Reserved.
  4. =head1 NAME
  5. Pod::LaTeX - Convert Pod data to formatted Latex
  6. =head1 SYNOPSIS
  7. use Pod::LaTeX;
  8. my $parser = Pod::LaTeX->new ( );
  9. $parser->parse_from_filehandle;
  10. $parser->parse_from_file ('file.pod', 'file.tex');
  11. =head1 DESCRIPTION
  12. C<Pod::LaTeX> is a module to convert documentation in the Pod format
  13. into Latex. The L<B<pod2latex>|pod2latex> X<pod2latex> command uses
  14. this module for translation.
  15. C<Pod::LaTeX> is a derived class from L<Pod::Select|Pod::Select>.
  16. =cut
  17. use strict;
  18. require Pod::ParseUtils;
  19. use base qw/ Pod::Select /;
  20. # use Data::Dumper; # for debugging
  21. use Carp;
  22. use vars qw/ $VERSION %HTML_Escapes @LatexSections /;
  23. $VERSION = '0.53';
  24. # Definitions of =headN -> latex mapping
  25. @LatexSections = (qw/
  26. chapter
  27. section
  28. subsection
  29. subsubsection
  30. paragraph
  31. subparagraph
  32. /);
  33. # Standard escape sequences converted to Latex
  34. # Up to "yuml" these are taken from the original pod2latex
  35. # command written by Taro Kawagish ([email protected])
  36. %HTML_Escapes = (
  37. 'amp' => '&', # ampersand
  38. 'lt' => '$<$', # ' left chevron, less-than
  39. 'gt' => '$>$', # ' right chevron, greater-than
  40. 'quot' => '"', # double quote
  41. 'sol' => '/',
  42. 'verbar' => '$|$',
  43. "Aacute" => "\\'{A}", # capital A, acute accent
  44. "aacute" => "\\'{a}", # small a, acute accent
  45. "Acirc" => "\\^{A}", # capital A, circumflex accent
  46. "acirc" => "\\^{a}", # small a, circumflex accent
  47. "AElig" => '\\AE', # capital AE diphthong (ligature)
  48. "aelig" => '\\ae', # small ae diphthong (ligature)
  49. "Agrave" => "\\`{A}", # capital A, grave accent
  50. "agrave" => "\\`{a}", # small a, grave accent
  51. "Aring" => '\\u{A}', # capital A, ring
  52. "aring" => '\\u{a}', # small a, ring
  53. "Atilde" => '\\~{A}', # capital A, tilde
  54. "atilde" => '\\~{a}', # small a, tilde
  55. "Auml" => '\\"{A}', # capital A, dieresis or umlaut mark
  56. "auml" => '\\"{a}', # small a, dieresis or umlaut mark
  57. "Ccedil" => '\\c{C}', # capital C, cedilla
  58. "ccedil" => '\\c{c}', # small c, cedilla
  59. "Eacute" => "\\'{E}", # capital E, acute accent
  60. "eacute" => "\\'{e}", # small e, acute accent
  61. "Ecirc" => "\\^{E}", # capital E, circumflex accent
  62. "ecirc" => "\\^{e}", # small e, circumflex accent
  63. "Egrave" => "\\`{E}", # capital E, grave accent
  64. "egrave" => "\\`{e}", # small e, grave accent
  65. "ETH" => '\\OE', # capital Eth, Icelandic
  66. "eth" => '\\oe', # small eth, Icelandic
  67. "Euml" => '\\"{E}', # capital E, dieresis or umlaut mark
  68. "euml" => '\\"{e}', # small e, dieresis or umlaut mark
  69. "Iacute" => "\\'{I}", # capital I, acute accent
  70. "iacute" => "\\'{i}", # small i, acute accent
  71. "Icirc" => "\\^{I}", # capital I, circumflex accent
  72. "icirc" => "\\^{i}", # small i, circumflex accent
  73. "Igrave" => "\\`{I}", # capital I, grave accent
  74. "igrave" => "\\`{i}", # small i, grave accent
  75. "Iuml" => '\\"{I}', # capital I, dieresis or umlaut mark
  76. "iuml" => '\\"{i}', # small i, dieresis or umlaut mark
  77. "Ntilde" => '\\~{N}', # capital N, tilde
  78. "ntilde" => '\\~{n}', # small n, tilde
  79. "Oacute" => "\\'{O}", # capital O, acute accent
  80. "oacute" => "\\'{o}", # small o, acute accent
  81. "Ocirc" => "\\^{O}", # capital O, circumflex accent
  82. "ocirc" => "\\^{o}", # small o, circumflex accent
  83. "Ograve" => "\\`{O}", # capital O, grave accent
  84. "ograve" => "\\`{o}", # small o, grave accent
  85. "Oslash" => "\\O", # capital O, slash
  86. "oslash" => "\\o", # small o, slash
  87. "Otilde" => "\\~{O}", # capital O, tilde
  88. "otilde" => "\\~{o}", # small o, tilde
  89. "Ouml" => '\\"{O}', # capital O, dieresis or umlaut mark
  90. "ouml" => '\\"{o}', # small o, dieresis or umlaut mark
  91. "szlig" => '\\ss{}', # small sharp s, German (sz ligature)
  92. "THORN" => '\\L', # capital THORN, Icelandic
  93. "thorn" => '\\l',, # small thorn, Icelandic
  94. "Uacute" => "\\'{U}", # capital U, acute accent
  95. "uacute" => "\\'{u}", # small u, acute accent
  96. "Ucirc" => "\\^{U}", # capital U, circumflex accent
  97. "ucirc" => "\\^{u}", # small u, circumflex accent
  98. "Ugrave" => "\\`{U}", # capital U, grave accent
  99. "ugrave" => "\\`{u}", # small u, grave accent
  100. "Uuml" => '\\"{U}', # capital U, dieresis or umlaut mark
  101. "uuml" => '\\"{u}', # small u, dieresis or umlaut mark
  102. "Yacute" => "\\'{Y}", # capital Y, acute accent
  103. "yacute" => "\\'{y}", # small y, acute accent
  104. "yuml" => '\\"{y}', # small y, dieresis or umlaut mark
  105. # Added by TimJ
  106. "iexcl" => '!`', # inverted exclamation mark
  107. # "cent" => ' ', # cent sign
  108. "pound" => '\pounds', # (UK) pound sign
  109. # "curren" => ' ', # currency sign
  110. # "yen" => ' ', # yen sign
  111. # "brvbar" => ' ', # broken vertical bar
  112. "sect" => '\S', # section sign
  113. "uml" => '\"{}', # diaresis
  114. "copy" => '\copyright', # Copyright symbol
  115. # "ordf" => ' ', # feminine ordinal indicator
  116. "laquo" => '$\ll$', # ' # left pointing double angle quotation mark
  117. "not" => '$\neg$', # ' # not sign
  118. "shy" => '-', # soft hyphen
  119. # "reg" => ' ', # registered trademark
  120. "macr" => '$^-$', # ' # macron, overline
  121. "deg" => '$^\circ$', # ' # degree sign
  122. "plusmn" => '$\pm$', # ' # plus-minus sign
  123. "sup2" => '$^2$', # ' # superscript 2
  124. "sup3" => '$^3$', # ' # superscript 3
  125. "acute" => "\\'{}", # acute accent
  126. "micro" => '$\mu$', # micro sign
  127. "para" => '\P', # pilcrow sign = paragraph sign
  128. "middot" => '$\cdot$', # middle dot = Georgian comma
  129. "cedil" => '\c{}', # cedilla
  130. "sup1" => '$^1$', # ' # superscript 1
  131. # "ordm" => ' ', # masculine ordinal indicator
  132. "raquo" => '$\gg$', # ' # right pointing double angle quotation mark
  133. "frac14" => '$\frac{1}{4}$', # ' # vulgar fraction one quarter
  134. "frac12" => '$\frac{1}{2}$', # ' # vulgar fraction one half
  135. "frac34" => '$\frac{3}{4}$', # ' # vulgar fraction three quarters
  136. "iquest" => "?'", # inverted question mark
  137. "times" => '$\times$', # ' # multiplication sign
  138. "divide" => '$\div$', # division sign
  139. # Greek letters using HTML codes
  140. "alpha" => '$\alpha$', # '
  141. "beta" => '$\beta$', # '
  142. "gamma" => '$\gamma$', # '
  143. "delta" => '$\delta$', # '
  144. "epsilon"=> '$\epsilon$', # '
  145. "zeta" => '$\zeta$', # '
  146. "eta" => '$\eta$', # '
  147. "theta" => '$\theta$', # '
  148. "iota" => '$\iota$', # '
  149. "kappa" => '$\kappa$', # '
  150. "lambda" => '$\lambda$', # '
  151. "mu" => '$\mu$', # '
  152. "nu" => '$\nu$', # '
  153. "xi" => '$\xi$', # '
  154. "omicron"=> '$o$', # '
  155. "pi" => '$\pi$', # '
  156. "rho" => '$\rho$', # '
  157. "sigma" => '$\sigma$', # '
  158. "tau" => '$\tau$', # '
  159. "upsilon"=> '$\upsilon$', # '
  160. "phi" => '$\phi$', # '
  161. "chi" => '$\chi$', # '
  162. "psi" => '$\psi$', # '
  163. "omega" => '$\omega$', # '
  164. "Alpha" => '$A$', # '
  165. "Beta" => '$B$', # '
  166. "Gamma" => '$\Gamma$', # '
  167. "Delta" => '$\Delta$', # '
  168. "Epsilon"=> '$E$', # '
  169. "Zeta" => '$Z$', # '
  170. "Eta" => '$H$', # '
  171. "Theta" => '$\Theta$', # '
  172. "Iota" => '$I$', # '
  173. "Kappa" => '$K$', # '
  174. "Lambda" => '$\Lambda$', # '
  175. "Mu" => '$M$', # '
  176. "Nu" => '$N$', # '
  177. "Xi" => '$\Xi$', # '
  178. "Omicron"=> '$O$', # '
  179. "Pi" => '$\Pi$', # '
  180. "Rho" => '$R$', # '
  181. "Sigma" => '$\Sigma$', # '
  182. "Tau" => '$T$', # '
  183. "Upsilon"=> '$\Upsilon$', # '
  184. "Phi" => '$\Phi$', # '
  185. "Chi" => '$X$', # '
  186. "Psi" => '$\Psi$', # '
  187. "Omega" => '$\Omega$', # '
  188. );
  189. =head1 OBJECT METHODS
  190. The following methods are provided in this module. Methods inherited
  191. from C<Pod::Select> are not described in the public interface.
  192. =over 4
  193. =begin __PRIVATE__
  194. =item C<initialize>
  195. Initialise the object. This method is subclassed from C<Pod::Parser>.
  196. The base class method is invoked. This method defines the default
  197. behaviour of the object unless overridden by supplying arguments to
  198. the constructor.
  199. Internal settings are defaulted as well as the public instance data.
  200. Internal hash values are accessed directly (rather than through
  201. a method) and start with an underscore.
  202. This method should not be invoked by the user directly.
  203. =end __PRIVATE__
  204. =cut
  205. # - An array for nested lists
  206. # Arguments have already been read by this point
  207. sub initialize {
  208. my $self = shift;
  209. # print Dumper($self);
  210. # Internals
  211. $self->{_Lists} = []; # For nested lists
  212. $self->{_suppress_all_para} = 0; # For =begin blocks
  213. $self->{_suppress_next_para} = 0; # For =for blocks
  214. $self->{_dont_modify_any_para}=0; # For =begin blocks
  215. $self->{_dont_modify_next_para}=0; # For =for blocks
  216. $self->{_CURRENT_HEAD1} = ''; # Name of current HEAD1 section
  217. # Options - only initialise if not already set
  218. # Cause the '=head1 NAME' field to be treated specially
  219. # The contents of the NAME paragraph will be converted
  220. # to a section title. All subsequent =head1 will be converted
  221. # to =head2 and down. Will not affect =head1's prior to NAME
  222. # Assumes: 'Module - purpose' format
  223. # Also creates a purpose field
  224. # The name is used for Labeling of the subsequent subsections
  225. $self->{ReplaceNAMEwithSection} = 0
  226. unless exists $self->{ReplaceNAMEwithSection};
  227. $self->{AddPreamble} = 1 # make full latex document
  228. unless exists $self->{AddPreamble};
  229. $self->{StartWithNewPage} = 0 # Start new page for pod section
  230. unless exists $self->{StartWithNewPage};
  231. $self->{TableOfContents} = 0 # Add table of contents
  232. unless exists $self->{TableOfContents}; # only relevent if AddPreamble=1
  233. $self->{AddPostamble} = 1 # Add closing latex code at end
  234. unless exists $self->{AddPostamble}; # effectively end{document} and index
  235. $self->{MakeIndex} = 1 # Add index (only relevant AddPostamble
  236. unless exists $self->{MakeIndex}; # and AddPreamble)
  237. $self->{UniqueLabels} = 1 # Use label unique for each pod
  238. unless exists $self->{UniqueLabels}; # either based on the filename
  239. # or supplied
  240. # Control the level of =head1. default is \section
  241. #
  242. $self->{Head1Level} = 1 # Offset in latex sections
  243. unless exists $self->{Head1Level}; # 0 is chapter, 2 is subsection
  244. # Control at which level numbering of sections is turned off
  245. # ie subsection becomes subsection*
  246. # The numbering is relative to the latex sectioning commands
  247. # and is independent of Pod heading level
  248. # default is to number \section but not \subsection
  249. $self->{LevelNoNum} = 2
  250. unless exists $self->{LevelNoNum};
  251. # Label to be used as prefix to all internal section names
  252. # If not defined will attempt to derive it from the filename
  253. # This can not happen when running parse_from_filehandle though
  254. # hence the ability to set the label externally
  255. # The label could then be Pod::Parser_DESCRIPTION or somesuch
  256. $self->{Label} = undef # label to be used as prefix
  257. unless exists $self->{Label}; # to all internal section names
  258. # These allow the caller to add arbritrary latex code to
  259. # start and end of document. AddPreamble and AddPostamble are ignored
  260. # if these are set.
  261. # Also MakeIndex and TableOfContents are also ignored.
  262. $self->{UserPreamble} = undef # User supplied start (AddPreamble =1)
  263. unless exists $self->{Label};
  264. $self->{UserPostamble} = undef # Use supplied end (AddPostamble=1)
  265. unless exists $self->{Label};
  266. # Run base initialize
  267. $self->SUPER::initialize;
  268. }
  269. =back
  270. =head2 Data Accessors
  271. The following methods are provided for accessing instance data. These
  272. methods should be used for accessing configuration parameters rather
  273. than assuming the object is a hash.
  274. Default values can be supplied by using these names as keys to a hash
  275. of arguments when using the C<new()> constructor.
  276. =over 4
  277. =item B<AddPreamble>
  278. Logical to control whether a C<latex> preamble is to be written.
  279. If true, a valid C<latex> preamble is written before the pod data is written.
  280. This is similar to:
  281. \documentclass{article}
  282. \begin{document}
  283. but will be more complicated if table of contents and indexing are required.
  284. Can be used to set or retrieve the current value.
  285. $add = $parser->AddPreamble();
  286. $parser->AddPreamble(1);
  287. If used in conjunction with C<AddPostamble> a full latex document will
  288. be written that could be immediately processed by C<latex>.
  289. =cut
  290. sub AddPreamble {
  291. my $self = shift;
  292. if (@_) {
  293. $self->{AddPreamble} = shift;
  294. }
  295. return $self->{AddPreamble};
  296. }
  297. =item B<AddPostamble>
  298. Logical to control whether a standard C<latex> ending is written to the output
  299. file after the document has been processed.
  300. In its simplest form this is simply:
  301. \end{document}
  302. but can be more complicated if a index is required.
  303. Can be used to set or retrieve the current value.
  304. $add = $parser->AddPostamble();
  305. $parser->AddPostamble(1);
  306. If used in conjunction with C<AddPreaamble> a full latex document will
  307. be written that could be immediately processed by C<latex>.
  308. =cut
  309. sub AddPostamble {
  310. my $self = shift;
  311. if (@_) {
  312. $self->{AddPostamble} = shift;
  313. }
  314. return $self->{AddPostamble};
  315. }
  316. =item B<Head1Level>
  317. The C<latex> sectioning level that should be used to correspond to
  318. a pod C<=head1> directive. This can be used, for example, to turn
  319. a C<=head1> into a C<latex> C<subsection>. This should hold a number
  320. corresponding to the required position in an array containing the
  321. following elements:
  322. [0] chapter
  323. [1] section
  324. [2] subsection
  325. [3] subsubsection
  326. [4] paragraph
  327. [5] subparagraph
  328. Can be used to set or retrieve the current value:
  329. $parser->Head1Level(2);
  330. $sect = $parser->Head1Level;
  331. Setting this number too high can result in sections that may not be reproducible
  332. in the expected way. For example, setting this to 4 would imply that C<=head3>
  333. do not have a corresponding C<latex> section (C<=head1> would correspond to
  334. a C<paragraph>).
  335. A check is made to ensure that the supplied value is an integer in the
  336. range 0 to 5.
  337. Default is for a value of 1 (i.e. a C<section>).
  338. =cut
  339. sub Head1Level {
  340. my $self = shift;
  341. if (@_) {
  342. my $arg = shift;
  343. if ($arg =~ /^\d$/ && $arg <= $#LatexSections) {
  344. $self->{Head1Level} = $arg;
  345. } else {
  346. carp "Head1Level supplied ($arg) must be integer in range 0 to ".$#LatexSections . "- Ignoring\n";
  347. }
  348. }
  349. return $self->{Head1Level};
  350. }
  351. =item B<Label>
  352. This is the label that is prefixed to all C<latex> label and index
  353. entries to make them unique. In general, pods have similarly titled
  354. sections (NAME, DESCRIPTION etc) and a C<latex> label will be multiply
  355. defined if more than one pod document is to be included in a single
  356. C<latex> file. To overcome this, this label is prefixed to a label
  357. whenever a label is required (joined with an underscore) or to an
  358. index entry (joined by an exclamation mark which is the normal index
  359. separator). For example, C<\label{text}> becomes C<\label{Label_text}>.
  360. Can be used to set or retrieve the current value:
  361. $label = $parser->Label;
  362. $parser->Label($label);
  363. This label is only used if C<UniqueLabels> is true.
  364. Its value is set automatically from the C<NAME> field
  365. if C<ReplaceNAMEwithSection> is true. If this is not the case
  366. it must be set manually before starting the parse.
  367. Default value is C<undef>.
  368. =cut
  369. sub Label {
  370. my $self = shift;
  371. if (@_) {
  372. $self->{Label} = shift;
  373. }
  374. return $self->{Label};
  375. }
  376. =item B<LevelNoNum>
  377. Control the point at which C<latex> section numbering is turned off.
  378. For example, this can be used to make sure that C<latex> sections
  379. are numbered but subsections are not.
  380. Can be used to set or retrieve the current value:
  381. $lev = $parser->LevelNoNum;
  382. $parser->LevelNoNum(2);
  383. The argument must be an integer between 0 and 5 and is the same as the
  384. number described in C<Head1Level> method description. The number has
  385. nothing to do with the pod heading number, only the C<latex> sectioning.
  386. Default is 2. (i.e. C<latex> subsections are written as C<subsection*>
  387. but sections are numbered).
  388. =cut
  389. sub LevelNoNum {
  390. my $self = shift;
  391. if (@_) {
  392. $self->{LevelNoNum} = shift;
  393. }
  394. return $self->{LevelNoNum};
  395. }
  396. =item B<MakeIndex>
  397. Controls whether C<latex> commands for creating an index are to be inserted
  398. into the preamble and postamble
  399. $makeindex = $parser->MakeIndex;
  400. $parser->MakeIndex(0);
  401. Irrelevant if both C<AddPreamble> and C<AddPostamble> are false (or equivalently,
  402. C<UserPreamble> and C<UserPostamble> are set).
  403. Default is for an index to be created.
  404. =cut
  405. sub MakeIndex {
  406. my $self = shift;
  407. if (@_) {
  408. $self->{MakeIndex} = shift;
  409. }
  410. return $self->{MakeIndex};
  411. }
  412. =item B<ReplaceNAMEwithSection>
  413. This controls whether the C<NAME> section in the pod is to be translated
  414. literally or converted to a slightly modified output where the section
  415. name is the pod name rather than "NAME".
  416. If true, the pod segment
  417. =head1 NAME
  418. pod::name - purpose
  419. =head1 SYNOPSIS
  420. is converted to the C<latex>
  421. \section{pod::name\label{pod_name}\index{pod::name}}
  422. Purpose
  423. \subsection*{SYNOPSIS\label{pod_name_SYNOPSIS}%
  424. \index{pod::name!SYNOPSIS}}
  425. (dependent on the value of C<Head1Level> and C<LevelNoNum>). Note that
  426. subsequent C<head1> directives translate to subsections rather than
  427. sections and that the labels and index now include the pod name (dependent
  428. on the value of C<UniqueLabels>).
  429. The C<Label> is set from the pod name regardless of any current value
  430. of C<Label>.
  431. $mod = $parser->ReplaceNAMEwithSection;
  432. $parser->ReplaceNAMEwithSection(0);
  433. Default is to translate the pod literally.
  434. =cut
  435. sub ReplaceNAMEwithSection {
  436. my $self = shift;
  437. if (@_) {
  438. $self->{ReplaceNAMEwithSection} = shift;
  439. }
  440. return $self->{ReplaceNAMEwithSection};
  441. }
  442. =item B<StartWithNewPage>
  443. If true, each pod translation will begin with a C<latex>
  444. C<\clearpage>.
  445. $parser->StartWithNewPage(1);
  446. $newpage = $parser->StartWithNewPage;
  447. Default is false.
  448. =cut
  449. sub StartWithNewPage {
  450. my $self = shift;
  451. if (@_) {
  452. $self->{StartWithNewPage} = shift;
  453. }
  454. return $self->{StartWithNewPage};
  455. }
  456. =item B<TableOfContents>
  457. If true, a table of contents will be created.
  458. Irrelevant if C<AddPreamble> is false or C<UserPreamble>
  459. is set.
  460. $toc = $parser->TableOfContents;
  461. $parser->TableOfContents(1);
  462. Default is false.
  463. =cut
  464. sub TableOfContents {
  465. my $self = shift;
  466. if (@_) {
  467. $self->{TableOfContents} = shift;
  468. }
  469. return $self->{TableOfContents};
  470. }
  471. =item B<UniqueLabels>
  472. If true, the translator will attempt to make sure that
  473. each C<latex> label or index entry will be uniquely identified
  474. by prefixing the contents of C<Label>. This allows
  475. multiple documents to be combined without clashing
  476. common labels such as C<DESCRIPTION> and C<SYNOPSIS>
  477. $parser->UniqueLabels(1);
  478. $unq = $parser->UniqueLabels;
  479. Default is true.
  480. =cut
  481. sub UniqueLabels {
  482. my $self = shift;
  483. if (@_) {
  484. $self->{UniqueLabels} = shift;
  485. }
  486. return $self->{UniqueLabels};
  487. }
  488. =item B<UserPreamble>
  489. User supplied C<latex> preamble. Added before the pod translation
  490. data.
  491. If set, the contents will be prepended to the output file before the translated
  492. data regardless of the value of C<AddPreamble>.
  493. C<MakeIndex> and C<TableOfContents> will also be ignored.
  494. =cut
  495. sub UserPreamble {
  496. my $self = shift;
  497. if (@_) {
  498. $self->{UserPreamble} = shift;
  499. }
  500. return $self->{UserPreamble};
  501. }
  502. =item B<UserPostamble>
  503. User supplied C<latex> postamble. Added after the pod translation
  504. data.
  505. If set, the contents will be prepended to the output file after the translated
  506. data regardless of the value of C<AddPostamble>.
  507. C<MakeIndex> will also be ignored.
  508. =cut
  509. sub UserPostamble {
  510. my $self = shift;
  511. if (@_) {
  512. $self->{UserPostamble} = shift;
  513. }
  514. return $self->{UserPostamble};
  515. }
  516. =begin __PRIVATE__
  517. =item B<Lists>
  518. Contains details of the currently active lists.
  519. The array contains C<Pod::List> objects. A new C<Pod::List>
  520. object is created each time a list is encountered and it is
  521. pushed onto this stack. When the list context ends, it
  522. is popped from the stack. The array will be empty if no
  523. lists are active.
  524. Returns array of list information in list context
  525. Returns array ref in scalar context
  526. =cut
  527. sub lists {
  528. my $self = shift;
  529. return @{ $self->{_Lists} } if wantarray();
  530. return $self->{_Lists};
  531. }
  532. =end __PRIVATE__
  533. =back
  534. =begin __PRIVATE__
  535. =head2 Subclassed methods
  536. The following methods override methods provided in the C<Pod::Select>
  537. base class. See C<Pod::Parser> and C<Pod::Select> for more information
  538. on what these methods require.
  539. =over 4
  540. =cut
  541. ######### END ACCESSORS ###################
  542. # Opening pod
  543. =item B<begin_pod>
  544. Writes the C<latex> preamble if requested.
  545. =cut
  546. sub begin_pod {
  547. my $self = shift;
  548. # Get the pod identification
  549. # This should really come from the '=head1 NAME' paragraph
  550. my $infile = $self->input_file;
  551. my $class = ref($self);
  552. my $date = gmtime(time);
  553. # Comment message to say where this came from
  554. my $comment = << "__TEX_COMMENT__";
  555. %% Latex generated from POD in document $infile
  556. %% Using the perl module $class
  557. %% Converted on $date
  558. __TEX_COMMENT__
  559. # Write the preamble
  560. # If the caller has supplied one then we just use that
  561. my $preamble = '';
  562. if (defined $self->UserPreamble) {
  563. $preamble = $self->UserPreamble;
  564. # Add the description of where this came from
  565. $preamble .= "\n$comment";
  566. } elsif ($self->AddPreamble) {
  567. # Write our own preamble
  568. # Code to initialise index making
  569. # Use an array so that we can prepend comment if required
  570. my @makeidx = (
  571. '\usepackage{makeidx}',
  572. '\makeindex',
  573. );
  574. unless ($self->MakeIndex) {
  575. foreach (@makeidx) {
  576. $_ = '%% ' . $_;
  577. }
  578. }
  579. my $makeindex = join("\n",@makeidx) . "\n";
  580. # Table of contents
  581. my $tableofcontents = '\tableofcontents';
  582. $tableofcontents = '%% ' . $tableofcontents
  583. unless $self->TableOfContents;
  584. # Roll our own
  585. $preamble = << "__TEX_HEADER__";
  586. \\documentclass{article}
  587. $comment
  588. $makeindex
  589. \\begin{document}
  590. $tableofcontents
  591. __TEX_HEADER__
  592. }
  593. # Write the header (blank if none)
  594. $self->_output($preamble);
  595. # Start on new page if requested
  596. $self->_output("\\clearpage\n") if $self->StartWithNewPage;
  597. }
  598. =item B<end_pod>
  599. Write the closing C<latex> code.
  600. =cut
  601. sub end_pod {
  602. my $self = shift;
  603. # End string
  604. my $end = '';
  605. # Use the user version of the postamble if deinfed
  606. if (defined $self->UserPostamble) {
  607. $end = $self->UserPostamble;
  608. $self->_output($end);
  609. } elsif ($self->AddPostamble) {
  610. # Check for index
  611. my $makeindex = '\printindex';
  612. $makeindex = '%% '. $makeindex unless $self->MakeIndex;
  613. $end = "$makeindex\n\n\\end{document}\n";
  614. }
  615. $self->_output($end);
  616. }
  617. =item B<command>
  618. Process basic pod commands.
  619. =cut
  620. sub command {
  621. my $self = shift;
  622. my ($command, $paragraph, $line_num, $parobj) = @_;
  623. # return if we dont care
  624. return if $command eq 'pod';
  625. $paragraph = $self->_replace_special_chars($paragraph);
  626. # Interpolate pod sequences in paragraph
  627. $paragraph = $self->interpolate($paragraph, $line_num);
  628. $paragraph =~ s/\s+$//;
  629. # Now run the command
  630. if ($command eq 'over') {
  631. $self->begin_list($paragraph, $line_num);
  632. } elsif ($command eq 'item') {
  633. $self->add_item($paragraph, $line_num);
  634. } elsif ($command eq 'back') {
  635. $self->end_list($line_num);
  636. } elsif ($command eq 'head1') {
  637. # Store the name of the section
  638. $self->{_CURRENT_HEAD1} = $paragraph;
  639. # Print it
  640. $self->head(1, $paragraph, $parobj);
  641. } elsif ($command eq 'head2') {
  642. $self->head(2, $paragraph, $parobj);
  643. } elsif ($command eq 'head3') {
  644. $self->head(3, $paragraph, $parobj);
  645. } elsif ($command eq 'head4') {
  646. $self->head(4, $paragraph, $parobj);
  647. } elsif ($command eq 'head5') {
  648. $self->head(5, $paragraph, $parobj);
  649. } elsif ($command eq 'head6') {
  650. $self->head(6, $paragraph, $parobj);
  651. } elsif ($command eq 'begin') {
  652. # pass through if latex
  653. if ($paragraph =~ /^latex/i) {
  654. # Make sure that subsequent paragraphs are not modfied before printing
  655. $self->{_dont_modify_any_para} = 1;
  656. } else {
  657. # Suppress all subsequent paragraphs unless
  658. # it is explcitly intended for latex
  659. $self->{_suppress_all_para} = 1;
  660. }
  661. } elsif ($command eq 'for') {
  662. # pass through if latex
  663. if ($paragraph =~ /^latex/i) {
  664. # Make sure that next paragraph is not modfied before printing
  665. $self->{_dont_modify_next_para} = 1;
  666. } else {
  667. # Suppress the next paragraph unless it is latex
  668. $self->{_suppress_next_para} = 1
  669. }
  670. } elsif ($command eq 'end') {
  671. # Reset suppression
  672. $self->{_suppress_all_para} = 0;
  673. $self->{_dont_modify_any_para} = 0;
  674. } elsif ($command eq 'pod') {
  675. # Do nothing
  676. } else {
  677. carp "Command $command not recognised at line $line_num\n";
  678. }
  679. }
  680. =item B<verbatim>
  681. Verbatim text
  682. =cut
  683. sub verbatim {
  684. my $self = shift;
  685. my ($paragraph, $line_num, $parobj) = @_;
  686. # Expand paragraph unless in =for or =begin block
  687. if ($self->{_dont_modify_any_para} || $self->{_dont_modify_next_para}) {
  688. # Just print as is
  689. $self->_output($paragraph);
  690. # Reset flag if in =for
  691. $self->{_dont_modify_next_para} = 0;
  692. } else {
  693. return if $paragraph =~ /^\s+$/;
  694. # Clean trailing space
  695. $paragraph =~ s/\s+$//;
  696. # Clean tabs
  697. $paragraph =~ s/\t/ /g;
  698. $self->_output('\begin{verbatim}' . "\n$paragraph\n". '\end{verbatim}'."\n");
  699. }
  700. }
  701. =item B<textblock>
  702. Plain text paragraph.
  703. =cut
  704. sub textblock {
  705. my $self = shift;
  706. my ($paragraph, $line_num, $parobj) = @_;
  707. # print Dumper($self);
  708. # Expand paragraph unless in =for or =begin block
  709. if ($self->{_dont_modify_any_para} || $self->{_dont_modify_next_para}) {
  710. # Just print as is
  711. $self->_output($paragraph);
  712. # Reset flag if in =for
  713. $self->{_dont_modify_next_para} = 0;
  714. return;
  715. }
  716. # Escape latex special characters
  717. $paragraph = $self->_replace_special_chars($paragraph);
  718. # Interpolate interior sequences
  719. my $expansion = $self->interpolate($paragraph, $line_num);
  720. $expansion =~ s/\s+$//;
  721. # If we are replacing 'head1 NAME' with a section
  722. # we need to look in the paragraph and rewrite things
  723. # Need to make sure this is called only on the first paragraph
  724. # following 'head1 NAME' and not on subsequent paragraphs that may be
  725. # present.
  726. if ($self->{_CURRENT_HEAD1} =~ /^NAME/i && $self->ReplaceNAMEwithSection()) {
  727. # Strip white space from start and end
  728. $paragraph =~ s/^\s+//;
  729. $paragraph =~ s/\s$//;
  730. # Split the string into 2 parts
  731. my ($name, $purpose) = split(/\s+-\s+/, $expansion,2);
  732. # Now prevent this from triggering until a new head1 NAME is set
  733. $self->{_CURRENT_HEAD1} = '_NAME';
  734. # Might want to clear the Label() before doing this (CHECK)
  735. # Print the heading
  736. $self->head(1, $name, $parobj);
  737. # Set the labeling in case we want unique names later
  738. $self->Label( $self->_create_label( $name, 1 ) );
  739. # Raise the Head1Level by one so that subsequent =head1 appear
  740. # as subsections of the main name section unless we are already
  741. # at maximum [Head1Level() could check this itself - CHECK]
  742. $self->Head1Level( $self->Head1Level() + 1)
  743. unless $self->Head1Level == $#LatexSections;
  744. # Now write out the new latex paragraph
  745. $purpose = ucfirst($purpose);
  746. $self->_output("\n\n$purpose\n\n");
  747. } else {
  748. # Just write the output
  749. $self->_output("\n\n$expansion\n\n");
  750. }
  751. }
  752. =item B<interior_sequence>
  753. Interior sequence expansion
  754. =cut
  755. sub interior_sequence {
  756. my $self = shift;
  757. my ($seq_command, $seq_argument, $pod_seq) = @_;
  758. if ($seq_command eq 'B') {
  759. return "\\textbf{$seq_argument}";
  760. } elsif ($seq_command eq 'I') {
  761. return "\\textit{$seq_argument}";
  762. } elsif ($seq_command eq 'E') {
  763. # If it is simply a number
  764. if ($seq_argument =~ /^\d+$/) {
  765. return chr($seq_argument);
  766. # Look up escape in hash table
  767. } elsif (exists $HTML_Escapes{$seq_argument}) {
  768. return $HTML_Escapes{$seq_argument};
  769. } else {
  770. my ($file, $line) = $pod_seq->file_line();
  771. warn "Escape sequence $seq_argument not recognised at line $line of file $file\n";
  772. return;
  773. }
  774. } elsif ($seq_command eq 'Z') {
  775. # Zero width space
  776. return '$\!$'; # '
  777. } elsif ($seq_command eq 'C') {
  778. return "\\texttt{$seq_argument}";
  779. } elsif ($seq_command eq 'F') {
  780. return "\\emph{$seq_argument}";
  781. } elsif ($seq_command eq 'S') {
  782. # non breakable spaces
  783. my $nbsp = '$\:$'; #'
  784. $seq_argument =~ s/\s/$nbsp/g;
  785. return $seq_argument;
  786. } elsif ($seq_command eq 'L') {
  787. my $link = new Pod::Hyperlink($seq_argument);
  788. # undef on failure
  789. unless (defined $link) {
  790. carp $@;
  791. return;
  792. }
  793. # Handle internal links differently
  794. my $type = $link->type;
  795. my $page = $link->page;
  796. if ($type eq 'section' && $page eq '') {
  797. # Use internal latex reference
  798. my $node = $link->node;
  799. # Convert to a label
  800. $node = $self->_create_label($node);
  801. return "\\S\\ref{$node}";
  802. } else {
  803. # Use default markup for external references
  804. # (although Starlink would use \xlabel)
  805. my $markup = $link->markup;
  806. my ($file, $line) = $pod_seq->file_line();
  807. return $self->interpolate($link->markup, $line);
  808. }
  809. } elsif ($seq_command eq 'P') {
  810. # Special markup for Pod::Hyperlink
  811. # Replace :: with /
  812. my $link = $seq_argument;
  813. $link =~ s/::/\//g;
  814. my $ref = "\\emph{$seq_argument}";
  815. return $ref;
  816. } elsif ($seq_command eq 'Q') {
  817. # Special markup for Pod::Hyperlink
  818. return "\\textsf{$seq_argument}\n";
  819. } elsif ($seq_command eq 'X') {
  820. # Index entries
  821. # use \index command
  822. # I will let '!' go through for now
  823. # not sure how sub categories are handled in X<>
  824. my $index = $self->_create_index($seq_argument);
  825. return "\\index{$index}\n";
  826. } else {
  827. carp "Unknown sequence $seq_command<$seq_argument>";
  828. }
  829. }
  830. =back
  831. =head2 List Methods
  832. Methods used to handle lists.
  833. =over 4
  834. =item B<begin_list>
  835. Called when a new list is found (via the C<over> directive).
  836. Creates a new C<Pod::List> object and stores it on the
  837. list stack.
  838. $parser->begin_list($indent, $line_num);
  839. =cut
  840. sub begin_list {
  841. my $self = shift;
  842. my $indent = shift;
  843. my $line_num = shift;
  844. # Indicate that a list should be started for the next item
  845. # need to do this to work out the type of list
  846. push ( @{$self->lists}, new Pod::List(-indent => $indent,
  847. -start => $line_num,
  848. -file => $self->input_file,
  849. )
  850. );
  851. }
  852. =item B<end_list>
  853. Called when the end of a list is found (the C<back> directive).
  854. Pops the C<Pod::List> object off the stack of lists and writes
  855. the C<latex> code required to close a list.
  856. $parser->end_list($line_num);
  857. =cut
  858. sub end_list {
  859. my $self = shift;
  860. my $line_num = shift;
  861. unless (defined $self->lists->[-1]) {
  862. my $file = $self->input_file;
  863. warn "No list is active at line $line_num (file=$file). Missing =over?\n";
  864. return;
  865. }
  866. # What to write depends on list type
  867. my $type = $self->lists->[-1]->type;
  868. # Dont write anything if the list type is not set
  869. # iomplying that a list was created but no entries were
  870. # placed in it (eg because of a =begin/=end combination)
  871. $self->_output("\\end{$type}\n")
  872. if (defined $type && length($type) > 0);
  873. # Clear list
  874. pop(@{ $self->lists});
  875. }
  876. =item B<add_item>
  877. Add items to the list. The first time an item is encountered
  878. (determined from the state of the current C<Pod::List> object)
  879. the type of list is determined (ordered, unnumbered or description)
  880. and the relevant latex code issued.
  881. $parser->add_item($paragraph, $line_num);
  882. =cut
  883. sub add_item {
  884. my $self = shift;
  885. my $paragraph = shift;
  886. my $line_num = shift;
  887. unless (defined $self->lists->[-1]) {
  888. my $file = $self->input_file;
  889. warn "List has already ended by line $line_num of file $file. Missing =over?\n";
  890. # Replace special chars
  891. # $paragraph = $self->_replace_special_chars($paragraph);
  892. $self->_output("$paragraph\n\n");
  893. return;
  894. }
  895. # If paragraphs printing is turned off via =begin/=end or whatver
  896. # simply return immediately
  897. return if ($self->{_suppress_all_para} || $self->{_suppress_next_para});
  898. # Check to see whether we are starting a new lists
  899. if (scalar($self->lists->[-1]->item) == 0) {
  900. # Examine the paragraph to determine what type of list
  901. # we have
  902. $paragraph =~ s/\s+$//;
  903. $paragraph =~ s/^\s+//;
  904. my $type;
  905. if (substr($paragraph, 0,1) eq '*') {
  906. $type = 'itemize';
  907. } elsif ($paragraph =~ /^\d/) {
  908. $type = 'enumerate';
  909. } else {
  910. $type = 'description';
  911. }
  912. $self->lists->[-1]->type($type);
  913. $self->_output("\\begin{$type}\n");
  914. }
  915. my $type = $self->lists->[-1]->type;
  916. if ($type eq 'description') {
  917. # Handle long items - long items do not wrap
  918. if (length($paragraph) < 40) {
  919. # A real description list item
  920. $self->_output("\\item[$paragraph] \\mbox{}");
  921. } else {
  922. # The item is now simply bold text
  923. $self->_output(qq{\\item \\textbf{$paragraph}});
  924. }
  925. } else {
  926. # If the item was '* Something' we still need to write
  927. # out the something
  928. my $extra_info = $paragraph;
  929. $extra_info =~ s/^\*\s*//;
  930. $self->_output("\\item $extra_info");
  931. }
  932. # Store the item name in the object. Required so that
  933. # we can tell if the list is new or not
  934. $self->lists->[-1]->item($paragraph);
  935. }
  936. =back
  937. =head2 Methods for headings
  938. =over 4
  939. =item B<head>
  940. Print a heading of the required level.
  941. $parser->head($level, $paragraph, $parobj);
  942. The first argument is the pod heading level. The second argument
  943. is the contents of the heading. The 3rd argument is a Pod::Paragraph
  944. object so that the line number can be extracted.
  945. =cut
  946. sub head {
  947. my $self = shift;
  948. my $num = shift;
  949. my $paragraph = shift;
  950. my $parobj = shift;
  951. # If we are replace 'head1 NAME' with a section
  952. # we return immediately if we get it
  953. return
  954. if ($self->{_CURRENT_HEAD1} =~ /^NAME/i && $self->ReplaceNAMEwithSection());
  955. # Create a label
  956. my $label = $self->_create_label($paragraph);
  957. # Create an index entry
  958. my $index = $self->_create_index($paragraph);
  959. # Work out position in the above array taking into account
  960. # that =head1 is equivalent to $self->Head1Level
  961. my $level = $self->Head1Level() - 1 + $num;
  962. # Warn if heading to large
  963. if ($num > $#LatexSections) {
  964. my $line = $parobj->file_line;
  965. my $file = $self->input_file;
  966. warn "Heading level too large ($level) for LaTeX at line $line of file $file\n";
  967. $level = $#LatexSections;
  968. }
  969. # Check to see whether section should be unnumbered
  970. my $star = ($level >= $self->LevelNoNum ? '*' : '');
  971. # Section
  972. $self->_output("\\" .$LatexSections[$level] .$star ."{$paragraph\\label{".$label ."}\\index{".$index."}}");
  973. }
  974. =back
  975. =end __PRIVATE__
  976. =begin __PRIVATE__
  977. =head2 Internal methods
  978. Internal routines are described in this section. They do not form part of the
  979. public interface. All private methods start with an underscore.
  980. =over 4
  981. =item B<_output>
  982. Output text to the output filehandle. This method must be always be called
  983. to output parsed text.
  984. $parser->_output($text);
  985. Does not write anything if a =begin or =for is active that should be
  986. ignored.
  987. =cut
  988. sub _output {
  989. my $self = shift;
  990. my $text = shift;
  991. print { $self->output_handle } $text
  992. unless $self->{_suppress_all_para} ||
  993. $self->{_suppress_next_para};
  994. # Reset pargraph stuff for =for
  995. $self->{_suppress_next_para} = 0
  996. if $self->{_suppress_next_para};
  997. }
  998. =item B<_replace_special_chars>
  999. Subroutine to replace characters that are special in C<latex>
  1000. with the escaped forms
  1001. $escaped = $parser->_replace_special_chars($paragraph);
  1002. Need to call this routine before interior_sequences are munged but
  1003. not if verbatim.
  1004. Special characters and the C<latex> equivalents are:
  1005. } \}
  1006. { \{
  1007. _ \_
  1008. $ \$
  1009. % \%
  1010. & \&
  1011. \ $\backslash$
  1012. ^ \^{}
  1013. ~ \~{}
  1014. | $|$
  1015. =cut
  1016. sub _replace_special_chars {
  1017. my $self = shift;
  1018. my $paragraph = shift;
  1019. # Replace a \ with $\backslash$
  1020. # This is made more complicated because the dollars will be escaped
  1021. # by the subsequent replacement. Easiest to add \backslash
  1022. # now and then add the dollars
  1023. $paragraph =~ s/\\/\\backslash/g;
  1024. # Must be done after escape of \ since this command adds latex escapes
  1025. # Replace characters that can be escaped
  1026. $paragraph =~ s/([\$\#&%_{}])/\\$1/g;
  1027. # Replace ^ characters with \^{} so that $^F works okay
  1028. $paragraph =~ s/(\^)/\\$1\{\}/g;
  1029. # Replace tilde (~) with \texttt{\~{}}
  1030. $paragraph =~ s/~/\\texttt\{\\~\{\}\}/g;
  1031. # Replace | with $|$
  1032. $paragraph =~ s'\|'$|$'g;
  1033. # Now add the dollars around each \backslash
  1034. $paragraph =~ s/(\\backslash)/\$$1\$/g;
  1035. return $paragraph;
  1036. }
  1037. =item B<_create_label>
  1038. Return a string that can be used as an internal reference
  1039. in a C<latex> document (i.e. accepted by the C<\label> command)
  1040. $label = $parser->_create_label($string)
  1041. If UniqueLabels is true returns a label prefixed by Label()
  1042. This can be suppressed with an optional second argument.
  1043. $label = $parser->_create_label($string, $suppress);
  1044. If a second argument is supplied (of any value including undef)
  1045. the Label() is never prefixed. This means that this routine can
  1046. be called to create a Label() without prefixing a previous setting.
  1047. =cut
  1048. sub _create_label {
  1049. my $self = shift;
  1050. my $paragraph = shift;
  1051. my $suppress = (@_ ? 1 : 0 );
  1052. # Remove latex commands
  1053. $paragraph = $self->_clean_latex_commands($paragraph);
  1054. # Remove non alphanumerics from the label and replace with underscores
  1055. # want to protect '-' though so use negated character classes
  1056. $paragraph =~ s/[^-:\w]/_/g;
  1057. # Multiple underscores will look unsightly so remove repeats
  1058. # This will also have the advantage of tidying up the end and
  1059. # start of string
  1060. $paragraph =~ s/_+/_/g;
  1061. # If required need to make sure that the label is unique
  1062. # since it is possible to have multiple pods in a single
  1063. # document
  1064. if (!$suppress && $self->UniqueLabels() && defined $self->Label) {
  1065. $paragraph = $self->Label() .'_'. $paragraph;
  1066. }
  1067. return $paragraph;
  1068. }
  1069. =item B<_create_index>
  1070. Similar to C<_create_label> except an index entry is created.
  1071. If C<UniqueLabels> is true, the index entry is prefixed by
  1072. the current C<Label> and an exclamation mark.
  1073. $ind = $parser->_create_index($paragraph);
  1074. An exclamation mark is used by C<makeindex> to generate
  1075. sub-entries in an index.
  1076. =cut
  1077. sub _create_index {
  1078. my $self = shift;
  1079. my $paragraph = shift;
  1080. my $suppress = (@_ ? 1 : 0 );
  1081. # Remove latex commands
  1082. $paragraph = $self->_clean_latex_commands($paragraph);
  1083. # If required need to make sure that the index entry is unique
  1084. # since it is possible to have multiple pods in a single
  1085. # document
  1086. if (!$suppress && $self->UniqueLabels() && defined $self->Label) {
  1087. $paragraph = $self->Label() .'!'. $paragraph;
  1088. }
  1089. # Need to replace _ with space
  1090. $paragraph =~ s/_/ /g;
  1091. return $paragraph;
  1092. }
  1093. =item B<_clean_latex_commands>
  1094. Removes latex commands from text. The latex command is assumed to be of the
  1095. form C<\command{ text }>. "C<text>" is retained
  1096. $clean = $parser->_clean_latex_commands($text);
  1097. =cut
  1098. sub _clean_latex_commands {
  1099. my $self = shift;
  1100. my $paragraph = shift;
  1101. # Remove latex commands of the form \text{ }
  1102. # and replace with the contents of the { }
  1103. # need to make this non-greedy so that it can handle
  1104. # "\text{a} and \text2{b}"
  1105. # without converting it to
  1106. # "a} and \text2{b"
  1107. # This match will still get into trouble if \} is present
  1108. # This is not vital since the subsequent replacement of non-alphanumeric
  1109. # characters will tidy it up anyway
  1110. $paragraph =~ s/\\\w+{(.*?)}/$1/g;
  1111. return $paragraph
  1112. }
  1113. =back
  1114. =end __PRIVATE__
  1115. =head1 NOTES
  1116. Compatible with C<latex2e> only. Can not be used with C<latex> v2.09
  1117. or earlier.
  1118. A subclass of C<Pod::Select> so that specific pod sections can be
  1119. converted to C<latex> by using the C<select> method.
  1120. Some HTML escapes are missing and many have not been tested.
  1121. =head1 SEE ALSO
  1122. L<Pod::Parser>, L<Pod::Select>, L<pod2latex>
  1123. =head1 AUTHORS
  1124. Tim Jenness E<lt>t.jenness@jach.hawaii.eduE<gt>
  1125. =head1 COPYRIGHT
  1126. Copyright (C) 2000 Tim Jenness. All Rights Reserved.
  1127. This program is free software; you can redistribute it and/or modify it
  1128. under the same terms as Perl itself.
  1129. =begin __PRIVATE__
  1130. =head1 REVISION
  1131. $Id: LaTeX.pm,v 1.6 2000/08/21 09:05:03 timj Exp $
  1132. =end __PRIVATE__
  1133. =cut