latex2html.pm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. #+##############################################################################
  2. #
  3. # latex2html.pm: interface to LaTeX2HTML
  4. #
  5. # Copyright (C) 1999, 2000, 2003, 2005, 2006, 2009, 2011, 2013
  6. # Free Software Foundation, Inc.
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 3 of the License,
  11. # or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. # This code was taken from the main texi2html file in 2006.
  22. # Certainly originally written by Olaf Bachmann.
  23. # Adapted from texi2html T2h_l2h.pm in 2011.
  24. #
  25. #-##############################################################################
  26. require 5.0;
  27. use strict;
  28. use Cwd;
  29. use File::Copy;
  30. use File::Spec;
  31. my $global_cmds = get_conf('GLOBAL_COMMANDS');
  32. if (!defined($global_cmds)) {
  33. set_from_init_file('GLOBAL_COMMANDS', []);
  34. $global_cmds = get_conf('GLOBAL_COMMANDS');
  35. }
  36. push @$global_cmds, ('math', 'tex');
  37. texinfo_register_handler('structure', \&l2h_process);
  38. texinfo_register_handler('finish', \&l2h_finish);
  39. texinfo_register_command_formatting('math', \&l2h_do_tex);
  40. texinfo_register_command_formatting('tex', \&l2h_do_tex);
  41. # name/location of latex2html program
  42. set_from_init_file('L2H_L2H', 'latex2html');
  43. # If this is set the actual call to latex2html is skipped. The previously
  44. # generated content is reused, instead.
  45. # If set to 0, the cache is not used.
  46. # If undef the cache is used for as many tex fragments as possible
  47. # and for the remaining the command is run.
  48. set_from_init_file('L2H_SKIP', undef);
  49. # If this is set l2h uses the specified directory for temporary files. The path
  50. # leading to this directory may not contain a dot (i.e., a ".");
  51. # otherwise, l2h will fail.
  52. set_from_init_file('L2H_TMP', '');
  53. # If set, l2h uses the file as latex2html init file
  54. set_from_init_file('L2H_FILE', undef);
  55. # if this is set the intermediate files generated by texi2html in relation with
  56. # latex2html are cleaned (they all have the prefix <document name>_l2h_).
  57. set_from_init_file('L2H_CLEAN', 1);
  58. # latex2html conversions consist of 2 stages:
  59. # 1) l2h_process
  60. # to latex: Put "latex" code into a latex file
  61. # (l2h_to_latex, l2h_finish_to_latex)
  62. # to html: Use latex2html to generate corresponding html code and images
  63. # (l2h_to_html)
  64. # from html: Extract generated code and images from latex2html run
  65. # (l2h_init_from_html)
  66. # 2) l2h_do_tex called each time a @tex or @math command is encountered
  67. # in the output tree.
  68. # init l2h defaults for files and names
  69. my ($l2h_name, $l2h_latex_file, $l2h_cache_file, $l2h_html_file, $l2h_prefix);
  70. # holds the status of latex2html operations. If 0 it means that there was
  71. # an error
  72. my $status = 0;
  73. my $debug;
  74. my $verbose;
  75. my $docu_rdir;
  76. my $docu_volume;
  77. my $docu_directories;
  78. my $docu_name;
  79. my %commands_counters;
  80. # init_from_html
  81. my $extract_error_count;
  82. my $invalid_counter_count;
  83. # change_image_file_names
  84. my %l2h_img; # associate src file to destination file
  85. # such that files are not copied twice
  86. my $image_count;
  87. # do_tex
  88. my $html_output_count = 0; # html text outputed in html result file
  89. ##########################
  90. #
  91. # First stage: Generation of Latex file
  92. # Initialize with: init
  93. # Add content with: l2h_to_latex ($text) --> HTML placeholder comment
  94. # Finish with: finish_to_latex
  95. #
  96. my $l2h_latex_preamble = <<EOT;
  97. % This document was automatically generated by the l2h extenstion of texi2html
  98. % DO NOT EDIT !!!
  99. \\documentclass{article}
  100. \\usepackage{html}
  101. \\begin{document}
  102. EOT
  103. my $l2h_latex_closing = <<EOT;
  104. \\end{document}
  105. EOT
  106. my %l2h_to_latex = (); # associate a latex text with the index in the
  107. # html result array.
  108. my @l2h_to_latex = (); # array used to associate the index with
  109. # the original latex text.
  110. my $latex_count = 0; # number of latex texts really stored
  111. my $latex_converted_count = 0; # number of latex texts passed through latex2html
  112. my $to_latex_count = 0; # total number of latex texts processed
  113. my $cached_count = 0; # number of cached latex texts
  114. my %l2h_cache = (); # the cache hash. Associate latex text with
  115. # html from the previous run
  116. my @l2h_from_html; # array of resulting html
  117. my %global_count = (); # associate a command name and the
  118. # corresponding counter to the index in the
  119. # html result array
  120. # set $status to 1, if l2h could be initalized properly, to 0 otherwise
  121. sub l2h_process($)
  122. {
  123. my $self = shift;
  124. %l2h_to_latex = (); # associate a latex text with the index in the
  125. # html result array.
  126. @l2h_to_latex = (); # array used to associate the index with
  127. # the original latex text.
  128. $latex_count = 0; # number of latex texts really stored
  129. $latex_converted_count = 0; # number of latex texts passed through latex2html
  130. $to_latex_count = 0; # total number of latex texts processed
  131. $cached_count = 0; # number of cached latex texts
  132. %l2h_cache = (); # the cache hash. Associate latex text with
  133. # html from the previous run
  134. @l2h_from_html = (); # array of resulting html
  135. %global_count = (); # associate a command name and the
  136. # corresponding counter to the index in the
  137. # html result array
  138. %commands_counters = ();
  139. $extract_error_count = 0;
  140. $invalid_counter_count = 0;
  141. %l2h_img = (); # associate src file to destination file
  142. # such that files are not copied twice
  143. $image_count = 1;
  144. $html_output_count = 0; # html text outputed in html result file
  145. $status = 0;
  146. return if (defined($self->get_conf('OUTFILE'))
  147. and $Texinfo::Common::null_device_file{$self->get_conf('OUTFILE')});
  148. $docu_name = $self->{'document_name'};
  149. $docu_rdir = $self->{'destination_directory'};
  150. $docu_rdir = '' if (!defined($docu_rdir));
  151. my $no_file;
  152. ($docu_volume, $docu_directories, $no_file)
  153. = File::Spec->splitpath($docu_rdir, 1);
  154. $l2h_name = "${docu_name}_l2h";
  155. $l2h_latex_file = File::Spec->catpath($docu_volume, $docu_directories,
  156. "${l2h_name}.tex");
  157. $l2h_cache_file = File::Spec->catpath($docu_volume, $docu_directories,
  158. "${docu_name}-l2h_cache.pm");
  159. # destination dir -- generated images are put there, should be the same
  160. # as dir of enclosing html document --
  161. $l2h_html_file = File::Spec->catpath($docu_volume, $docu_directories,
  162. "${l2h_name}.html");
  163. $l2h_prefix = "${l2h_name}_";
  164. $debug = $self->get_conf('DEBUG');
  165. $verbose = $self->get_conf('VERBOSE');
  166. unless ($self->get_conf('L2H_SKIP')) {
  167. unless (open(L2H_LATEX, ">$l2h_latex_file")) {
  168. $self->document_error(sprintf($self->__(
  169. "l2h: could not open latex file %s for writing: %s"),
  170. $l2h_latex_file, $!));
  171. $status = 0;
  172. return;
  173. }
  174. warn "# l2h: use ${l2h_latex_file} as latex file\n" if ($verbose);
  175. print L2H_LATEX $l2h_latex_preamble;
  176. }
  177. # open the database that holds cached text
  178. l2h_init_cache($self) if (!defined($self->get_conf('L2H_SKIP'))
  179. or $self->get_conf('L2H_SKIP'));
  180. foreach my $command ('tex', 'math') {
  181. if ($self->{'extra'}->{$command}) {
  182. my $counter = 0;
  183. foreach my $root (@{$self->{'extra'}->{$command}}) {
  184. $counter++;
  185. my $tree;
  186. if ($command eq 'math') {
  187. $tree = $root->{'args'}->[0];
  188. } else {
  189. $tree = {'contents' => [@{$root->{'contents'}}]};
  190. if ($tree->{'contents'}->[0]
  191. and $tree->{'contents'}->[0]->{'type'}
  192. and $tree->{'contents'}->[0]->{'type'} eq 'empty_line_after_command') {
  193. shift @{$tree->{'contents'}};
  194. }
  195. if ($tree->{'contents'}->[-1]->{'cmdname'}
  196. and $tree->{'contents'}->[-1]->{'cmdname'} eq 'end') {
  197. pop @{$tree->{'contents'}};
  198. }
  199. }
  200. my $text = Texinfo::Convert::Texinfo::convert($tree);
  201. #$text .= "\n" if ($command eq 'tex');
  202. l2h_to_latex($self, $command, $text, $counter);
  203. $commands_counters{$root} = $counter;
  204. }
  205. }
  206. }
  207. $status = l2h_finish_to_latex($self);
  208. if ($status) {
  209. $status = l2h_to_html($self);
  210. }
  211. if ($status) {
  212. $status = l2h_init_from_html($self);
  213. }
  214. # FIXME use $status? That is abort when something goes wrong on the
  215. # latex2html front?
  216. return 1;
  217. }
  218. # print text (2nd arg) into latex file (if not already there nor in cache)
  219. # which can be later on replaced by the latex2html generated text.
  220. #
  221. sub l2h_to_latex($$$$)
  222. {
  223. my $self = shift;
  224. my $command = shift;
  225. my $text = shift;
  226. my $counter = shift;
  227. if ($command eq 'tex') {
  228. $text .= ' ';
  229. } elsif ($command eq 'math') {
  230. $text = "\$".$text."\$";
  231. }
  232. $to_latex_count++;
  233. $text =~ s/(\s*)$//;
  234. # try whether we have text already on things to do
  235. my $count = $l2h_to_latex{$text};
  236. unless ($count) {
  237. $latex_count++;
  238. $count = $latex_count;
  239. # try whether we can get it from cache
  240. my $cached_text = l2h_from_cache($text);
  241. if (defined($cached_text)) {
  242. $cached_count++;
  243. # put the cached result in the html result array
  244. $l2h_from_html[$count] = $cached_text;
  245. } else {
  246. $latex_converted_count++;
  247. unless ($self->get_conf('L2H_SKIP')) {
  248. print L2H_LATEX "\\begin{rawhtml}\n\n";
  249. print L2H_LATEX "<!-- l2h_begin $l2h_name $count -->\n";
  250. print L2H_LATEX "\\end{rawhtml}\n";
  251. print L2H_LATEX "$text\n";
  252. print L2H_LATEX "\\begin{rawhtml}\n";
  253. print L2H_LATEX "<!-- l2h_end $l2h_name $count -->\n\n";
  254. print L2H_LATEX "\\end{rawhtml}\n";
  255. }
  256. }
  257. $l2h_to_latex[$count] = $text;
  258. $l2h_to_latex{$text} = $count;
  259. }
  260. $global_count{"${command}_$counter"} = $count;
  261. return 1;
  262. }
  263. # print closing into latex file and close it
  264. sub l2h_finish_to_latex($)
  265. {
  266. my $self = shift;
  267. my $reused = $to_latex_count - $latex_converted_count - $cached_count;
  268. unless ($self->get_conf('L2H_SKIP')) {
  269. print L2H_LATEX $l2h_latex_closing;
  270. close (L2H_LATEX);
  271. }
  272. warn "# l2h: finished to latex ($cached_count cached, $reused reused, $latex_converted_count to process)\n" if ($verbose);
  273. unless ($latex_count) {
  274. # no @tex nor @math
  275. l2h_finish($self);
  276. return 0;
  277. }
  278. return 1;
  279. }
  280. ###################################
  281. # Use latex2html to generate corresponding html code and images
  282. #
  283. # to_html([$l2h_latex_file, [$l2h_html_dir]]):
  284. # Call latex2html on $l2h_latex_file
  285. # Put images (prefixed with $l2h_name."_") and html file(s) in $l2h_html_dir
  286. # Return 1, on success
  287. # 0, otherwise
  288. #
  289. sub l2h_to_html($)
  290. {
  291. my $self = shift;
  292. my ($call, $dotbug);
  293. # when there are no tex constructs to convert (happens in case everything
  294. # comes from the cache), there is no latex2html run
  295. if ($self->get_conf('L2H_SKIP') or ($latex_converted_count == 0)) {
  296. warn "# l2h: skipping latex2html run\n" if ($verbose);
  297. return 1;
  298. }
  299. # Check for dot in directory where dvips will work
  300. if ($self->get_conf('L2H_TMP')) {
  301. if ($self->get_conf('L2H_TMP') =~ /\./) {
  302. $self->document_warn($self->__("l2h: L2H_TMP directory contains a dot"));
  303. $dotbug = 1;
  304. }
  305. } else {
  306. if (cwd() =~ /\./) {
  307. $self->document_warn($self->__("l2h: current directory contains a dot"));
  308. $dotbug = 1;
  309. }
  310. }
  311. return 0 if ($dotbug);
  312. $call = $self->get_conf('L2H_L2H');
  313. # use init file, if specified
  314. my $init_file = $self->get_conf('L2H_FILE');
  315. $call = $call . " -init_file " . $init_file
  316. if (defined($init_file) and $init_file ne ''
  317. and -f $init_file and -r $init_file);
  318. # set output dir
  319. $call .= (($docu_rdir ne '') ? " -dir $docu_rdir" : " -no_subdir");
  320. # use l2h_tmp, if specified
  321. $call .= " -tmp ".$self->get_conf('L2H_TMP')
  322. if (defined($self->get_conf('L2H_TMP'))
  323. and $self->get_conf('L2H_TMP') ne '');
  324. # use a given html version if specified
  325. $call .= " -html_version ".$self->get_conf('L2H_HTML_VERSION')
  326. if (defined($self->get_conf('L2H_HTML_VERSION'))
  327. and $self->get_conf('L2H_HTML_VERSION') ne '');
  328. # options we want to be sure of
  329. $call .= " -address 0 -info 0 -split 0 -no_navigation -no_auto_link";
  330. $call .= " -prefix $l2h_prefix $l2h_latex_file";
  331. warn "# l2h: executing '$call'\n" if ($verbose);
  332. if (system($call)) {
  333. $self->document_error(sprintf($self->__("l2h: command did not succeed: %s"),
  334. $call));
  335. return 0;
  336. } else {
  337. warn "# l2h: latex2html finished successfully\n" if ($verbose);
  338. return 1;
  339. }
  340. }
  341. ##########################
  342. # Third stage: Extract generated contents from latex2html run
  343. # Initialize with: init_from_html
  344. # open $l2h_html_file for reading
  345. # reads in contents into array indexed by numbers
  346. # return 1, on success -- 0, otherwise
  347. # Finish with: finish
  348. # closes $l2h_html_dir/$l2h_name.".$docu_ext"
  349. # the images generated by latex2html have names like ${docu_name}_l2h_img?.png
  350. # they are copied to ${docu_name}_?.png, and html is changed accordingly.
  351. # FIXME is it really necessary to bother doing that? Looks like an unneeded
  352. # complication to me (pertusus, 2009), and it could go bad if there is some
  353. # SRC="(.*?)" in the text (though the regexp could be made more specific).
  354. # %l2h_img; # associate src file to destination file
  355. # such that files are not copied twice
  356. sub l2h_change_image_file_names($$)
  357. {
  358. my $self = shift;
  359. my $content = shift;
  360. my @images = ($content =~ /SRC="(.*?)"/g);
  361. my ($src, $dest);
  362. for $src (@images) {
  363. $dest = $l2h_img{$src};
  364. unless ($dest) {
  365. my $ext = '';
  366. if ($src =~ /.*\.(.*)$/ and (!defined($self->get_conf('EXTENSION'))
  367. or $1 ne $self->get_conf('EXTENSION'))) {
  368. $ext = ".$1";
  369. } else {
  370. # A warning when the image extension is the same than the
  371. # document extension. copying the file could result in
  372. # overwriting an output file (almost surely if the default
  373. # texi2html file names are used).
  374. $self->document_warn(sprintf($self->__(
  375. "l2h: image has invalid extension: %s"), $src));
  376. next;
  377. }
  378. while (-e File::Spec->catpath($docu_volume, $docu_directories,
  379. "${docu_name}_${image_count}$ext")) {
  380. $image_count++;
  381. }
  382. my $file_src
  383. = File::Spec->catpath($docu_volume, $docu_directories, $src);
  384. $dest = "${docu_name}_${image_count}$ext";
  385. my $file_dest
  386. = File::Spec->catpath($docu_volume, $docu_directories, $dest);
  387. if ($debug) {
  388. copy($file_src, $file_dest);
  389. } else {
  390. if (!rename($file_src, $file_dest)) {
  391. $self->document_warn(sprintf($self->__("l2h: rename %s as %s failed: %s"),
  392. $file_src, $file_dest, $!));
  393. }
  394. }
  395. $l2h_img{$src} = $dest;
  396. }
  397. $content =~ s/SRC="$src"/SRC="$dest"/g;
  398. }
  399. return $content;
  400. }
  401. sub l2h_init_from_html($)
  402. {
  403. my $self = shift;
  404. # when there are no tex constructs to convert (happens in case everything
  405. # comes from the cache), the html file that was generated by previous
  406. # latex2html runs isn't reused.
  407. if ($latex_converted_count == 0) {
  408. return 1;
  409. }
  410. if (! open(L2H_HTML, "<$l2h_html_file")) {
  411. $self->document_warn(sprintf($self->__("l2h: could not open %s: %s"),
  412. $l2h_html_file, $!));
  413. return 0;
  414. }
  415. warn "# l2h: use $l2h_html_file as html file\n" if ($verbose);
  416. my $html_converted_count = 0; # number of html resulting texts
  417. # retrieved in the file
  418. my ($count, $h_line);
  419. while ($h_line = <L2H_HTML>) {
  420. if ($h_line =~ /!-- l2h_begin $l2h_name ([0-9]+) --/) {
  421. $count = $1;
  422. my $h_content = '';
  423. my $h_end_found = 0;
  424. while ($h_line = <L2H_HTML>) {
  425. if ($h_line =~ /!-- l2h_end $l2h_name $count --/) {
  426. $h_end_found = 1;
  427. chomp $h_content;
  428. chomp $h_content;
  429. $html_converted_count++;
  430. # transform image file names and copy image files
  431. $h_content = l2h_change_image_file_names($self, $h_content);
  432. # store result in the html result array
  433. $l2h_from_html[$count] = $h_content;
  434. # also add the result in cache hash
  435. $l2h_cache{$l2h_to_latex[$count]} = $h_content;
  436. last;
  437. }
  438. $h_content = $h_content.$h_line;
  439. }
  440. unless ($h_end_found) {
  441. # couldn't found the closing comment. Should be a bug.
  442. $self->document_warn(sprintf(__("latex2html.pm: end of \@%s item %d not found"),
  443. $l2h_name, $count));
  444. close(L2H_HTML);
  445. return 0;
  446. }
  447. }
  448. }
  449. # Not the same number of converted elements and retrieved elements
  450. if ($latex_converted_count != $html_converted_count) {
  451. $self->document_warn(sprintf($self->__(
  452. "latex2html.pm: processing produced %d items in HTML; expected %d, the number of items found in the document"),
  453. $html_converted_count, $latex_converted_count));
  454. }
  455. warn "# l2h: Got $html_converted_count of $latex_count html contents\n"
  456. if ($verbose);
  457. close(L2H_HTML);
  458. return 1;
  459. }
  460. # $html_output_count = 0; # html text outputed in html result file
  461. # called each time a construct handled by latex2html is encountered, should
  462. # output the corresponding html
  463. sub l2h_do_tex($$)
  464. {
  465. my $self = shift;
  466. my $cmdname = shift;;
  467. my $command = shift;
  468. my $content = shift;
  469. my $counter = $commands_counters{$command};
  470. return '' unless ($status);
  471. my $count = $global_count{"${cmdname}_$counter"};
  472. ################################## begin debug section (incorrect counts)
  473. if (!defined($count)) {
  474. # counter is undefined
  475. $invalid_counter_count++;
  476. $self->document_warn(
  477. sprintf($self->__("l2h: could not determine the fragment %d for \@%s",
  478. $counter, $cmdname)));
  479. return ("<!-- l2h: ". __LINE__ . " undef count for ${cmdname}_$counter -->")
  480. if ($debug);
  481. return '';
  482. } elsif(($count <= 0) or ($count > $latex_count)) {
  483. # counter out of range
  484. $invalid_counter_count++;
  485. $self->_bug_message("l2h: request of $count out of range [0,$latex_count]");
  486. return ("<!-- l2h: ". __LINE__ . " out of range count $count -->")
  487. if ($debug);
  488. return '';
  489. }
  490. ################################## end debug section (incorrect counts)
  491. # this seems to be a valid counter
  492. my $result = '';
  493. $result = "<!-- l2h_begin $l2h_name $count -->" if ($debug);
  494. if (defined($l2h_from_html[$count])) {
  495. $html_output_count++;
  496. $result .= $l2h_from_html[$count];
  497. $result .= "\n" if ($cmdname eq 'tex');
  498. } else {
  499. # if the result is not in @l2h_from_html, there is an error somewhere.
  500. $extract_error_count++;
  501. $self->document_warn(sprintf($self->__(
  502. "l2h: could not extract the fragment %d for \@%s with output counter %d from HTML"),
  503. $counter, $cmdname, $count));
  504. # try simple (ordinary) substitution (without l2h)
  505. $result .= "<!-- l2h: ". __LINE__ . " use default -->" if ($debug);
  506. $result .= &{$self->default_commands_conversion($cmdname)}($self,
  507. $cmdname, $command, $content);
  508. }
  509. $result .= "<!-- l2h_end $l2h_name $count -->" if ($debug);
  510. return $result;
  511. }
  512. # store results in the cache and remove temporary files.
  513. sub l2h_finish($)
  514. {
  515. my $self = shift;
  516. return 1 unless($status);
  517. if ($verbose) {
  518. if ($extract_error_count + $invalid_counter_count) {
  519. warn "# l2h: finished from html ($extract_error_count extract and $invalid_counter_count invalid counter errors)\n";
  520. } else {
  521. warn "# l2h: finished from html (no error)\n";
  522. }
  523. if ($html_output_count != $latex_converted_count) {
  524. # this may happen if @-commands are collected at some places
  525. # but @-command at those places are not expanded later. For
  526. # example @math on @multitable lines.
  527. warn "# l2h: $html_output_count html outputed for $latex_converted_count converted\n";
  528. }
  529. }
  530. l2h_store_cache($self);
  531. if ($self->get_conf('L2H_CLEAN')) {
  532. warn "# l2h: removing temporary files generated by l2h extension\n"
  533. if ($verbose);
  534. my $quoted_l2h_name = quotemeta($l2h_name);
  535. my $dir = $docu_rdir;
  536. $dir = File::Spec->curdir() if ($dir eq '');
  537. if (opendir (DIR, $dir)) {
  538. foreach my $file (grep { /^$quoted_l2h_name/ } readdir(DIR)) {
  539. # FIXME error condition not checked
  540. unlink File::Spec->catpath($docu_volume, $docu_directories, $file);
  541. }
  542. }
  543. }
  544. warn "# l2h: Finished\n" if $verbose;
  545. return 1;
  546. }
  547. ##############################
  548. # stuff for l2h caching
  549. #
  550. # FIXME it is clear that l2h stuff takes very long compared with texi2any
  551. # which is already quite long. However this also adds some complexity
  552. # I tried doing this with a dbm data base, but it did not store all
  553. # keys/values. Hence, I did as latex2html does it
  554. sub l2h_init_cache($)
  555. {
  556. my $self = shift;
  557. if (-r $l2h_cache_file) {
  558. my $rdo = do "$l2h_cache_file";
  559. $self->document_error(sprintf($self->__("l2h: could not load %s: %s"),
  560. $l2h_cache_file, $@))
  561. unless ($rdo);
  562. }
  563. }
  564. # store all the text obtained through latex2html
  565. sub l2h_store_cache($)
  566. {
  567. my $self = shift;
  568. return unless $latex_count;
  569. my ($key, $value);
  570. unless (open(FH, ">$l2h_cache_file")) {
  571. $self->document_error(sprintf($self->__("l2h: could not open %s for writing: %s"),
  572. $l2h_cache_file, $!));
  573. return;
  574. }
  575. foreach my $key(sort(keys(%l2h_cache))) {
  576. #while (($key, $value) = each %l2h_cache) {
  577. my $value = $l2h_cache{$key};
  578. # escape stuff
  579. $key =~ s|/|\\/|g;
  580. $key =~ s|\\\\/|\\/|g;
  581. # weird, a \ at the end of the key results in an error
  582. # maybe this also broke the dbm database stuff
  583. $key =~ s|\\$|\\\\|;
  584. $value =~ s/\|/\\\|/go;
  585. $value =~ s/\\\\\|/\\\|/go;
  586. $value =~ s|\\\\|\\\\\\\\|g;
  587. print FH "\n\$l2h_cache_key = q/$key/;\n";
  588. print FH "\$l2h_cache{\$l2h_cache_key} = q|$value|;\n";
  589. }
  590. print FH "1;";
  591. close (FH);
  592. }
  593. # return cached html, if it exists for text, and if all pictures
  594. # are there, as well
  595. sub l2h_from_cache($)
  596. {
  597. my $text = shift;
  598. my $cached = $l2h_cache{$text};
  599. if (defined($cached)) {
  600. while ($cached =~ m/SRC="(.*?)"/g) {
  601. unless (-e File::Spec->catpath($docu_volume, $docu_directories, $1)) {
  602. return undef;
  603. }
  604. }
  605. return $cached;
  606. }
  607. return undef;
  608. }
  609. 1;