t2h.pm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. # makeinfo HTML output init file
  2. #
  3. # Copyright (c) 2011, 2012 Free Software Foundation, Inc.
  4. # Copyright (c) 2014 Andreas Cadhalpun
  5. # Copyright (c) 2014 Tiancheng "Timothy" Gu
  6. #
  7. # This file is part of FFmpeg.
  8. #
  9. # FFmpeg is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 3 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # FFmpeg is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. # General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public
  20. # License along with FFmpeg; if not, write to the Free Software
  21. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. # Texinfo 7.0 changed the syntax of various functions.
  23. # Provide a shim for older versions.
  24. sub ff_set_from_init_file($$) {
  25. my $key = shift;
  26. my $value = shift;
  27. if (exists &{'texinfo_set_from_init_file'}) {
  28. texinfo_set_from_init_file($key, $value);
  29. } else {
  30. set_from_init_file($key, $value);
  31. }
  32. }
  33. sub ff_get_conf($) {
  34. my $key = shift;
  35. if (exists &{'texinfo_get_conf'}) {
  36. texinfo_get_conf($key);
  37. } else {
  38. get_conf($key);
  39. }
  40. }
  41. sub get_formatting_function($$) {
  42. my $obj = shift;
  43. my $func = shift;
  44. my $sub = $obj->can('formatting_function');
  45. if ($sub) {
  46. return $obj->formatting_function($func);
  47. } else {
  48. return $obj->{$func};
  49. }
  50. }
  51. # determine texinfo version
  52. my $program_version_num = version->declare(ff_get_conf('PACKAGE_VERSION'))->numify;
  53. my $program_version_6_8 = $program_version_num >= 6.008000;
  54. # no navigation elements
  55. ff_set_from_init_file('HEADERS', 0);
  56. sub ffmpeg_heading_command($$$$$)
  57. {
  58. my $self = shift;
  59. my $cmdname = shift;
  60. my $command = shift;
  61. my $args = shift;
  62. my $content = shift;
  63. my $result = '';
  64. # not clear that it may really happen
  65. if ($self->in_string) {
  66. $result .= $self->command_string($command) ."\n" if ($cmdname ne 'node');
  67. $result .= $content if (defined($content));
  68. return $result;
  69. }
  70. my $element_id = $self->command_id($command);
  71. $result .= "<a name=\"$element_id\"></a>\n"
  72. if (defined($element_id) and $element_id ne '');
  73. print STDERR "Process $command "
  74. .Texinfo::Structuring::_print_root_command_texi($command)."\n"
  75. if ($self->get_conf('DEBUG'));
  76. my $element;
  77. if ($Texinfo::Common::root_commands{$command->{'cmdname'}}
  78. and $command->{'parent'}
  79. and $command->{'parent'}->{'type'}
  80. and $command->{'parent'}->{'type'} eq 'element') {
  81. $element = $command->{'parent'};
  82. }
  83. if ($element) {
  84. $result .= &{get_formatting_function($self, 'format_element_header')}($self, $cmdname,
  85. $command, $element);
  86. }
  87. my $heading_level;
  88. # node is used as heading if there is nothing else.
  89. if ($cmdname eq 'node') {
  90. if (!$element or (!$element->{'extra'}->{'section'}
  91. and $element->{'extra'}->{'node'}
  92. and $element->{'extra'}->{'node'} eq $command
  93. # bogus node may not have been normalized
  94. and defined($command->{'extra'}->{'normalized'}))) {
  95. if ($command->{'extra'}->{'normalized'} eq 'Top') {
  96. $heading_level = 0;
  97. } else {
  98. $heading_level = 3;
  99. }
  100. }
  101. } else {
  102. $heading_level = $command->{'level'};
  103. }
  104. my $heading = $self->command_text($command);
  105. # $heading not defined may happen if the command is a @node, for example
  106. # if there is an error in the node.
  107. if (defined($heading) and $heading ne '' and defined($heading_level)) {
  108. if ($Texinfo::Common::root_commands{$cmdname}
  109. and $Texinfo::Common::sectioning_commands{$cmdname}) {
  110. my $content_href = $self->command_contents_href($command, 'contents',
  111. $self->{'current_filename'});
  112. if ($content_href) {
  113. my $this_href = $content_href =~ s/^\#toc-/\#/r;
  114. $heading .= '<span class="pull-right">'.
  115. '<a class="anchor hidden-xs" '.
  116. "href=\"$this_href\" aria-hidden=\"true\">".
  117. ($ENV{"FA_ICONS"} ? '<i class="fa fa-link"></i>'
  118. : '#').
  119. '</a> '.
  120. '<a class="anchor hidden-xs"'.
  121. "href=\"$content_href\" aria-hidden=\"true\">".
  122. ($ENV{"FA_ICONS"} ? '<i class="fa fa-navicon"></i>'
  123. : 'TOC').
  124. '</a>'.
  125. '</span>';
  126. }
  127. }
  128. if ($self->in_preformatted()) {
  129. $result .= $heading."\n";
  130. } else {
  131. # if the level was changed, set the command name right
  132. if ($cmdname ne 'node'
  133. and $heading_level ne $Texinfo::Common::command_structuring_level{$cmdname}) {
  134. $cmdname
  135. = $Texinfo::Common::level_to_structuring_command{$cmdname}->[$heading_level];
  136. }
  137. # format_heading_text expects an array of headings for texinfo >= 7.0
  138. if ($program_version_num >= 7.000000) {
  139. $heading = [$heading];
  140. }
  141. $result .= &{get_formatting_function($self,'format_heading_text')}(
  142. $self, $cmdname, $heading,
  143. $heading_level +
  144. $self->get_conf('CHAPTER_HEADER_LEVEL') - 1, $command);
  145. }
  146. }
  147. $result .= $content if (defined($content));
  148. return $result;
  149. }
  150. foreach my $command (keys(%Texinfo::Common::sectioning_commands), 'node') {
  151. texinfo_register_command_formatting($command, \&ffmpeg_heading_command);
  152. }
  153. # print the TOC where @contents is used
  154. if ($program_version_6_8) {
  155. ff_set_from_init_file('CONTENTS_OUTPUT_LOCATION', 'inline');
  156. } else {
  157. ff_set_from_init_file('INLINE_CONTENTS', 1);
  158. }
  159. # make chapters <h2>
  160. ff_set_from_init_file('CHAPTER_HEADER_LEVEL', 2);
  161. # Do not add <hr>
  162. ff_set_from_init_file('DEFAULT_RULE', '');
  163. ff_set_from_init_file('BIG_RULE', '');
  164. # Customized file beginning
  165. sub ffmpeg_begin_file($$$)
  166. {
  167. my $self = shift;
  168. my $filename = shift;
  169. my $element = shift;
  170. my $command;
  171. if ($element and $self->get_conf('SPLIT')) {
  172. $command = $self->element_command($element);
  173. }
  174. my ($title, $description, $encoding, $date, $css_lines,
  175. $doctype, $bodytext, $copying_comment, $after_body_open,
  176. $extra_head, $program_and_version, $program_homepage,
  177. $program, $generator);
  178. if ($program_version_num >= 7.000000) {
  179. ($title, $description, $encoding, $date, $css_lines,
  180. $doctype, $bodytext, $copying_comment, $after_body_open,
  181. $extra_head, $program_and_version, $program_homepage,
  182. $program, $generator) = $self->_file_header_information($command);
  183. } else {
  184. ($title, $description, $encoding, $date, $css_lines,
  185. $doctype, $bodytext, $copying_comment, $after_body_open,
  186. $extra_head, $program_and_version, $program_homepage,
  187. $program, $generator) = $self->_file_header_informations($command);
  188. }
  189. my $links = $self->_get_links ($filename, $element);
  190. my $head1 = $ENV{"FFMPEG_HEADER1"} || <<EOT;
  191. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  192. <html>
  193. <!-- Created by $program_and_version, $program_homepage -->
  194. <head>
  195. <meta charset="utf-8">
  196. <title>
  197. EOT
  198. my $head_title = <<EOT;
  199. $title
  200. EOT
  201. my $head2 = $ENV{"FFMPEG_HEADER2"} || <<EOT;
  202. </title>
  203. <meta name="viewport" content="width=device-width,initial-scale=1.0">
  204. <link rel="stylesheet" type="text/css" href="bootstrap.min.css">
  205. <link rel="stylesheet" type="text/css" href="style.min.css">
  206. </head>
  207. <body>
  208. <div class="container">
  209. <h1>
  210. EOT
  211. my $head3 = $ENV{"FFMPEG_HEADER3"} || <<EOT;
  212. </h1>
  213. EOT
  214. return $head1 . $head_title . $head2 . $head_title . $head3;
  215. }
  216. if ($program_version_6_8) {
  217. texinfo_register_formatting_function('format_begin_file', \&ffmpeg_begin_file);
  218. } else {
  219. texinfo_register_formatting_function('begin_file', \&ffmpeg_begin_file);
  220. }
  221. sub ffmpeg_program_string($)
  222. {
  223. my $self = shift;
  224. if (defined($self->get_conf('PROGRAM'))
  225. and $self->get_conf('PROGRAM') ne ''
  226. and defined($self->get_conf('PACKAGE_URL'))) {
  227. return $self->convert_tree(
  228. $self->gdt('This document was generated using @uref{{program_homepage}, @emph{{program}}}.',
  229. { 'program_homepage' => $self->get_conf('PACKAGE_URL'),
  230. 'program' => $self->get_conf('PROGRAM') }));
  231. } else {
  232. return $self->convert_tree(
  233. $self->gdt('This document was generated automatically.'));
  234. }
  235. }
  236. if ($program_version_6_8) {
  237. texinfo_register_formatting_function('format_program_string', \&ffmpeg_program_string);
  238. } else {
  239. texinfo_register_formatting_function('program_string', \&ffmpeg_program_string);
  240. }
  241. # Customized file ending
  242. sub ffmpeg_end_file($)
  243. {
  244. my $self = shift;
  245. my $program_string = &{get_formatting_function($self,'format_program_string')}($self);
  246. my $program_text = <<EOT;
  247. <p style="font-size: small;">
  248. $program_string
  249. </p>
  250. EOT
  251. my $footer = $ENV{FFMPEG_FOOTER} || <<EOT;
  252. </div>
  253. </body>
  254. </html>
  255. EOT
  256. return $program_text . $footer;
  257. }
  258. if ($program_version_6_8) {
  259. texinfo_register_formatting_function('format_end_file', \&ffmpeg_end_file);
  260. } else {
  261. texinfo_register_formatting_function('end_file', \&ffmpeg_end_file);
  262. }
  263. # Dummy title command
  264. # Ignore title. Title is handled through ffmpeg_begin_file().
  265. ff_set_from_init_file('USE_TITLEPAGE_FOR_TITLE', 1);
  266. sub ffmpeg_title($$$$)
  267. {
  268. return '';
  269. }
  270. texinfo_register_command_formatting('titlefont',
  271. \&ffmpeg_title);
  272. # Customized float command. Part of code borrowed from GNU Texinfo.
  273. sub ffmpeg_float($$$$$)
  274. {
  275. my $self = shift;
  276. my $cmdname = shift;
  277. my $command = shift;
  278. my $args = shift;
  279. my $content = shift;
  280. my ($caption, $prepended);
  281. if ($program_version_num >= 7.000000) {
  282. ($caption, $prepended) = Texinfo::Convert::Converter::float_name_caption($self,
  283. $command);
  284. } else {
  285. ($caption, $prepended) = Texinfo::Common::float_name_caption($self,
  286. $command);
  287. }
  288. my $caption_text = '';
  289. my $prepended_text;
  290. my $prepended_save = '';
  291. if ($self->in_string()) {
  292. if ($prepended) {
  293. $prepended_text = $self->convert_tree_new_formatting_context(
  294. $prepended, 'float prepended');
  295. } else {
  296. $prepended_text = '';
  297. }
  298. if ($caption) {
  299. $caption_text = $self->convert_tree_new_formatting_context(
  300. {'contents' => $caption->{'args'}->[0]->{'contents'}},
  301. 'float caption');
  302. }
  303. return $prepended.$content.$caption_text;
  304. }
  305. my $id = $self->command_id($command);
  306. my $label;
  307. if (defined($id) and $id ne '') {
  308. $label = "<a name=\"$id\"></a>";
  309. } else {
  310. $label = '';
  311. }
  312. if ($prepended) {
  313. if ($caption) {
  314. # prepend the prepended tree to the first paragraph
  315. my @caption_original_contents = @{$caption->{'args'}->[0]->{'contents'}};
  316. my @caption_contents;
  317. my $new_paragraph;
  318. while (@caption_original_contents) {
  319. my $content = shift @caption_original_contents;
  320. if ($content->{'type'} and $content->{'type'} eq 'paragraph') {
  321. %{$new_paragraph} = %{$content};
  322. $new_paragraph->{'contents'} = [@{$content->{'contents'}}];
  323. unshift (@{$new_paragraph->{'contents'}}, {'cmdname' => 'strong',
  324. 'args' => [{'type' => 'brace_command_arg',
  325. 'contents' => [$prepended]}]});
  326. push @caption_contents, $new_paragraph;
  327. last;
  328. } else {
  329. push @caption_contents, $content;
  330. }
  331. }
  332. push @caption_contents, @caption_original_contents;
  333. if ($new_paragraph) {
  334. $caption_text = $self->convert_tree_new_formatting_context(
  335. {'contents' => \@caption_contents}, 'float caption');
  336. $prepended_text = '';
  337. }
  338. }
  339. if ($caption_text eq '') {
  340. $prepended_text = $self->convert_tree_new_formatting_context(
  341. $prepended, 'float prepended');
  342. if ($prepended_text ne '') {
  343. $prepended_save = $prepended_text;
  344. $prepended_text = '<p><strong>'.$prepended_text.'</strong></p>';
  345. }
  346. }
  347. } else {
  348. $prepended_text = '';
  349. }
  350. if ($caption and $caption_text eq '') {
  351. $caption_text = $self->convert_tree_new_formatting_context(
  352. $caption->{'args'}->[0], 'float caption');
  353. }
  354. if ($prepended_text.$caption_text ne '') {
  355. if ($program_version_num >= 7.000000) {
  356. $prepended_text = $self->html_attribute_class('div',['float-caption']). '>'
  357. . $prepended_text;
  358. } else {
  359. $prepended_text = $self->_attribute_class('div','float-caption'). '>'
  360. . $prepended_text;
  361. }
  362. $caption_text .= '</div>';
  363. }
  364. my $html_class = '';
  365. if ($prepended_save =~ /NOTE/) {
  366. $html_class = 'info';
  367. $prepended_text = '';
  368. $caption_text = '';
  369. } elsif ($prepended_save =~ /IMPORTANT/) {
  370. $html_class = 'warning';
  371. $prepended_text = '';
  372. $caption_text = '';
  373. }
  374. if ($program_version_num >= 7.000000) {
  375. return $self->html_attribute_class('div', [$html_class]). '>' . "\n" .
  376. $prepended_text . $caption_text . $content . '</div>';
  377. } else {
  378. return $self->_attribute_class('div', $html_class). '>' . "\n" .
  379. $prepended_text . $caption_text . $content . '</div>';
  380. }
  381. }
  382. texinfo_register_command_formatting('float',
  383. \&ffmpeg_float);
  384. 1;