DebugTree.pm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # DebugTexinfo::DebugTree.pm: debug a Texinfo::Parser tree.
  2. #
  3. # Copyright 2011, 2012, 2013 Free Software Foundation, Inc.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 3 of the License,
  8. # or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. # Original author: Patrice Dumas <pertusus@free.fr>
  19. # Example of calls
  20. # with creation of elements corresponding to sections:
  21. # ./texi2any.pl --set TEXINFO_OUTPUT_FORMAT=debugtree --set USE_NODES=0 file.texi
  22. # with creation of elements corresponding to nodes:
  23. # ./texi2any.pl --set TEXINFO_OUTPUT_FORMAT=debugtree --set USE_NODES=1 file.texi
  24. # no elements
  25. # ./texi2any.pl --set TEXINFO_OUTPUT_FORMAT=debugtree file.texi
  26. #
  27. # Some unofficial info about the --debug command line option ... with
  28. # --debug=1, the tree is not printed,
  29. # --debug=10 (or more), the tree is printed at the end of the run,
  30. # --debug=100 (or more), the tree is printed at each newline.
  31. use Texinfo::Convert::Converter;
  32. package DebugTexinfo::DebugTree;
  33. @ISA = qw(Texinfo::Convert::Converter);
  34. my %defaults = (
  35. 'EXTENSION' => 'debugtree',
  36. 'OUTFILE' => '-',
  37. );
  38. sub converter_defaults($$)
  39. {
  40. return %defaults;
  41. }
  42. sub output($$)
  43. {
  44. my $self = shift;
  45. my $root = shift;
  46. $self->_set_outfile();
  47. return undef unless $self->_create_destination_directory();
  48. my $fh;
  49. if (! $self->{'output_file'} eq '') {
  50. $fh = $self->Texinfo::Common::open_out ($self->{'output_file'});
  51. if (!$fh) {
  52. $self->document_error(sprintf($self->__("could not open %s for writing: %s"),
  53. $self->{'output_file'}, $!));
  54. return undef;
  55. }
  56. }
  57. my $elements;
  58. if ($self) {
  59. if ($self->get_conf('USE_NODES')) {
  60. $elements = Texinfo::Structuring::split_by_node($root);
  61. } elsif (defined($self->get_conf('USE_NODES'))) {
  62. #print STDERR "U sections\n";
  63. $elements = Texinfo::Structuring::split_by_section($root);
  64. }
  65. # Currently the information added is not used further.
  66. if ($elements and ($self->get_conf('SPLIT')
  67. or !$self->get_conf('MONOLITHIC'))) {
  68. #print STDERR "S ".$self->get_conf('SPLIT')."\n";
  69. Texinfo::Structuring::split_pages($elements,
  70. $self->get_conf('SPLIT'));
  71. }
  72. }
  73. if ($elements) {
  74. $root = {'type' => 'elements_root',
  75. 'contents' => $elements };
  76. }
  77. return $self->_output_text (_print_tree($self, $root), $fh);
  78. }
  79. sub convert($$)
  80. {
  81. my $self = shift;
  82. my $root = shift;
  83. return _print_tree($self, $root);
  84. }
  85. sub convert_tree($$)
  86. {
  87. my $self = shift;
  88. my $root = shift;
  89. return _print_tree($self, $root);
  90. }
  91. sub _print_tree($$;$$);
  92. sub _print_tree($$;$$)
  93. {
  94. my $self = shift;
  95. my $root = shift;
  96. my $level = shift;
  97. my $argument = shift;
  98. $level = 0 if (!defined($level));
  99. my $result = ' ' x $level;
  100. if ($argument) {
  101. $result .= '%';
  102. $level++;
  103. }
  104. if ($root->{'cmdname'}) {
  105. $result .= "\@$root->{'cmdname'} ";
  106. }
  107. if (defined($root->{'type'})) {
  108. $result .= "$root->{'type'} ";
  109. }
  110. if (defined($root->{'text'})) {
  111. my $text = $root->{'text'};
  112. $text =~ s/\n/\\n/g;
  113. $text =~ s/\f/\\f/g;
  114. $text =~ s/\r/\\r/g;
  115. $result .= "|$text|";
  116. }
  117. $result .= "\n";
  118. if ($root->{'args'}) {
  119. foreach my $arg (@{$root->{'args'}}) {
  120. $result .= _print_tree ($self, $arg, $level +1, 1);
  121. }
  122. }
  123. if ($root->{'contents'}) {
  124. foreach my $content (@{$root->{'contents'}}) {
  125. $result .= _print_tree ($self, $content, $level+1);
  126. }
  127. }
  128. return $result;
  129. }