texinfo-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

branch master updated: * tp/ext/highlight_syntax.pm (highlight_setup, hi


From: Patrice Dumas
Subject: branch master updated: * tp/ext/highlight_syntax.pm (highlight_setup, highlight_process): determine list of languages highlighted in an handler, highlight_setup in order to be able to use the customization variables while doing this initialization.
Date: Sun, 19 Feb 2023 11:44:10 -0500

This is an automated email from the git hooks/post-receive script.

pertusus pushed a commit to branch master
in repository texinfo.

The following commit(s) were added to refs/heads/master by this push:
     new a2b9148b6a * tp/ext/highlight_syntax.pm (highlight_setup, 
highlight_process): determine list of languages highlighted in an handler, 
highlight_setup in order to be able to use the customization variables while 
doing this initialization.
a2b9148b6a is described below

commit a2b9148b6a2ddc455bab4fcb3a857b765c7fc37d
Author: Patrice Dumas <pertusus@free.fr>
AuthorDate: Sun Feb 19 17:43:14 2023 +0100

    * tp/ext/highlight_syntax.pm (highlight_setup, highlight_process):
    determine list of languages highlighted in an handler, highlight_setup
    in order to be able to use the customization variables while doing
    this initialization.
    
    * tp/ext/highlight_syntax.pm (highlight_setup): silence the redundant
    warning message associated to a failure to run the command through
    open.
    
    * tp/ext/highlight_syntax.pm (_get_language): handle not defined language.
    
    * tp/tests/other/highlight_example.texi: add more @example blocks,
    including without language set.
---
 ChangeLog                                          |  18 +++-
 tp/ext/highlight_syntax.pm                         | 113 ++++++++++++++-------
 tp/tests/other/highlight_example.texi              |  17 ++++
 .../highlight_syntax_example/chapter.html          |  19 +++-
 .../highlight_example_highlight_perl_input.pl      |  10 ++
 .../highlight_example_highlight_perl_output.html   |   9 ++
 .../highlight_syntax_example_latin9/chapter.html   |  19 +++-
 .../highlight_example_highlight_perl_input.pl      |  10 ++
 .../highlight_example_highlight_perl_output.html   |   9 ++
 9 files changed, 186 insertions(+), 38 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 7f70a3017b..b80576a71f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -18,6 +18,22 @@
        (\safewhatsit): Propagate \lastpenalty in horizontal mode.
        (\newindex): Remove obsolete comment about vms filenames.
 
+2023-02-19  Patrice Dumas  <pertusus@free.fr>
+
+       * tp/ext/highlight_syntax.pm (highlight_setup, highlight_process):
+       determine list of languages highlighted in an handler, highlight_setup
+       in order to be able to use the customization variables while doing
+       this initialization.
+
+       * tp/ext/highlight_syntax.pm (highlight_setup): silence the redundant
+       warning message associated to a failure to run the command through
+       open.
+
+       * tp/ext/highlight_syntax.pm (_get_language): handle not defined 
language.
+
+       * tp/tests/other/highlight_example.texi: add more @example blocks,
+       including without language set.
+
 2023-02-19  Patrice Dumas  <pertusus@free.fr>
 
        * tp/Texinfo/Common.pm (@variable_string_settables),
@@ -31,7 +47,7 @@
        (Syntax Highlighting): document syntax highlighting support in
        texi2any for @example for HTML.
 
-       * tp/tests/other/list-of-tests: use the HIGHLIGHT_SYNTAX to load
+       * tp/tests/other/list-of-tests: use HIGHLIGHT_SYNTAX to load
        syntax highlighting for one test case.
 
 2023-02-18  Patrice Dumas  <pertusus@free.fr>
diff --git a/tp/ext/highlight_syntax.pm b/tp/ext/highlight_syntax.pm
index a8bcf728aa..5db2489a1e 100644
--- a/tp/ext/highlight_syntax.pm
+++ b/tp/ext/highlight_syntax.pm
@@ -39,51 +39,80 @@ my %languages_extensions = (
   'perl' => 'pl',
 );
 
-# reference on a hash
-my $highlighted_languages_list;
-
-# FIXME open shows an error message if the file is not found
-# which is a duplicate with the texinfo_register_init_loading_error
-# registered message, which is better
-# Can't exec "source-highlight": No such file or directory at 
./init/highlight_syntax.pm line 74
-my $cmd = 'source-highlight --lang-list';
-if (not(open(HIGHLIGHT_LANG_LIST, '-|', $cmd))) {
-  texinfo_register_init_loading_error(
-        sprintf(__('%s: %s'), $cmd, $!));
-} else {
-  $highlighted_languages_list = {};
+my %highlighted_languages_list;
+
+texinfo_register_handler('setup', \&highlight_setup);
+texinfo_register_handler('structure', \&highlight_process);
+texinfo_register_command_formatting('example', 
\&highlight_preformatted_command);
+# normally this is done in preformatted type, but preformatted
+# types conversion output in example is discarded in
+# highlight_preformatted_command, so register a replacement.
+# Register inline pending content when opening an example block.
+texinfo_register_command_opening('example',
+                                 \&highlight_open_inline_container_type);
+
+sub highlight_setup($$)
+{
+  my $self = shift;
+  my $document_root = shift;
+
+  %highlighted_languages_list = ();
+
+  my $cmd = 'source-highlight --lang-list';
+
+  # NOTE open failure triggers a warning message if run with -w if the
+  # file is not found.  This message can be catched with $SIG{__WARN__}.
+  # This message is along:
+  # Can't exec "source-highlight": No such file or directory at 
./init/highlight_syntax.pm line XX
+  # This message is redundant with the message registered below with
+  # document_error, using $!.  $! is set to: No such file or directory
+
+  # Tried to show both messages, but through the $self->document_*()
+  # facility, either by getting the warning message in the main context or by
+  # register the warning message, but failed.  So simply silence the redundant
+  # message.
+
+  # does not store the message from within the sub but syntactically
+  # needed.
+  my $message;
+  local $SIG{__WARN__} = sub {
+        $message = shift;
+        # this shows the message
+        #warn "$message";
+        # not sure why, but this does not work, the warning is not actually
+        # registered, as if it was done in a scope that is later destroyed.
+        #$self->document_warn($self, sprintf(__('%s: %s'), $cmd, $message));
+      };
+
+  my $status = open(HIGHLIGHT_LANG_LIST, '-|', $cmd);
+  $SIG{__WARN__} = undef;
+  if (not($status)) {
+    $self->document_error($self, sprintf(__('%s: %s'), $cmd, $!));
+    return 1;
+  }
+
   my $line;
   while (defined($line = <HIGHLIGHT_LANG_LIST>)) {
     chomp($line);
     if ($line =~ /^([A-Za-z0-9_\-]+) =/) {
-       my $language = $1;
-       $highlighted_languages_list->{$language} = 1;
+      my $language = $1;
+      $highlighted_languages_list{$language} = 1;
     } else {
-      texinfo_register_init_loading_warning(sprintf(__(
-                        '%s: %s: cannot parse language line'), $cmd, $line))
+      $self->document_warn($self, sprintf(__(
+                      '%s: %s: cannot parse language line'), $cmd, $line))
     }
   }
+  # FIXME check error status
   close(HIGHLIGHT_LANG_LIST);
-}
-
-if (defined($highlighted_languages_list)) {
-  if (scalar(keys(%$highlighted_languages_list)) > 0) {
-    texinfo_register_handler('structure', \&highlight_process);
 
-    texinfo_register_command_formatting('example', 
\&highlight_preformatted_command);
-
-    # normally this is done in preformatted type, but preformatted
-    # types conversion output in example is discarded in
-    # highlight_preformatted_command, so register a replacement.
-    # Register inline pending content when opening an example block.
-    texinfo_register_command_opening('example',
-                                     \&highlight_open_inline_container_type);
-  } else {
+  if (scalar(keys(%highlighted_languages_list)) == 0) {
     # important if $cmd returns no output to have a message.  If there
     # is some output, there will already be some line parse error messages.
-    texinfo_register_init_loading_warning(sprintf(__(
-                              '%s: no highlighted language found'), $cmd));
+    $self->document_warn($self, sprintf(__(
+                            '%s: no highlighted language found'), $cmd));
+    # the remaining will be skipped, but no error is returned
   }
+  return 0;
 }
 
 sub _get_language($$$)
@@ -121,7 +150,7 @@ sub _get_language($$$)
     $language = $converted_language;
   }
 
-  if ($highlighted_languages_list->{$language}) {
+  if (defined($language) and $highlighted_languages_list{$language}) {
     return $language;
   } else {
     return undef;
@@ -139,7 +168,7 @@ sub highlight_process($$)
   my $self = shift;
   my $document_root = shift;
 
-  # initialization, important in case of multiple manuals processed
+  # initialization, important in case multiple manuals are processed
   %commands = ();              # associates a command name and element to the 
resulting
                                # highlighted text.
                                # Also holds per language counters.
@@ -147,11 +176,15 @@ sub highlight_process($$)
   return 0 if (defined($self->get_conf('OUTFILE'))
         and $Texinfo::Common::null_device_file{$self->get_conf('OUTFILE')});
 
+  return 0 if (!scalar(keys(%highlighted_languages_list)));
+
   my $document_name = $self->get_info('document_name');
   my $highlight_basename = "${document_name}_highlight";
 
   my $highlight_out_dir = $self->get_info('destination_directory');
 
+  my $verbose = $self->get_conf('VERBOSE');
+
   my @highlighted_commands = ('example');
 
   my $collected_commands
@@ -266,6 +299,8 @@ sub highlight_process($$)
        ."-i '$input_language_path_name' -o '$html_result_path_name' "
    ."--line-range=$option_line_range_str --range-separator='$range_separator'";
 
+    warn "# highlight_syntax: exec ($language): $cmd\n" if ($verbose);
+
     my $encoding = $self->get_conf('MESSAGE_ENCODING');
     my $encoded_cmd;
     if (defined($encoding)) {
@@ -353,6 +388,14 @@ sub highlight_open_inline_container_type($$$)
   my $cmdname = shift;
   my $command = shift;
 
+  if (!scalar(keys(%highlighted_languages_list))) {
+    my $default_open = $self->default_command_open($cmdname);
+    if (defined($default_open)) {
+      return &{$default_open}($self, $cmdname, $command);
+    } else {
+      return '';
+    }
+  }
   my $pending_formatted = $self->get_pending_formatted_inline_content();
 
   if (defined($pending_formatted)) {
diff --git a/tp/tests/other/highlight_example.texi 
b/tp/tests/other/highlight_example.texi
index 970438a023..06d24d1605 100644
--- a/tp/tests/other/highlight_example.texi
+++ b/tp/tests/other/highlight_example.texi
@@ -33,6 +33,23 @@ sub do ($) @{
 @}
 @end example
 
+@example perl
+my @@SECTION_BUTTONS =
+  (
+   \&singular_banner,
+   'Back', 'Forward',   'FastBack', 'FastForward',
+   'Up', 'Top', 'Contents', 'Index', 'About'
+  );
+
+texinfo_set_from_init_file ('SECTION_BUTTONS', \@@SECTION_BUTTONS);
+@end example
+
+No argument
+@example
+texinfo_set_from_init_file('NO_CSS', 1);
+@end example
+
+Unknown language
 @example unknown
 unknown language
 @end example
diff --git a/tp/tests/other/res_parser/highlight_syntax_example/chapter.html 
b/tp/tests/other/res_parser/highlight_syntax_example/chapter.html
index 374e8ea3e7..94701e1f01 100644
--- a/tp/tests/other/res_parser/highlight_syntax_example/chapter.html
+++ b/tp/tests/other/res_parser/highlight_syntax_example/chapter.html
@@ -62,7 +62,24 @@ Previous: <a href="index.html" accesskey="p" 
rel="prev">top</a>, Up: <a href="in
 <span style="color:#FF0000">}</span>
 </pre></div>
 
-<div class="example user-unknown">
+<div class="example user-perl">
+<pre class="example-preformatted"><b><span style="color:#0000FF">my</span></b> 
<span style="color:#009900">@SECTION_BUTTONS</span> <span 
style="color:#990000">=</span>
+  <span style="color:#990000">(</span>
+   <span style="color:#990000">\&amp;</span>singular_banner<span 
style="color:#990000">,</span>
+   <span style="color:#FF0000">'Back'</span><span 
style="color:#990000">,</span> <span 
style="color:#FF0000">'Forward'</span><span style="color:#990000">,</span>   
<span style="color:#FF0000">'FastBack'</span><span 
style="color:#990000">,</span> <span 
style="color:#FF0000">'FastForward'</span><span style="color:#990000">,</span>
+   <span style="color:#FF0000">'Up'</span><span style="color:#990000">,</span> 
<span style="color:#FF0000">'Top'</span><span style="color:#990000">,</span> 
<span style="color:#FF0000">'Contents'</span><span 
style="color:#990000">,</span> <span style="color:#FF0000">'Index'</span><span 
style="color:#990000">,</span> <span style="color:#FF0000">'About'</span>
+  <span style="color:#990000">);</span>
+
+<b><span style="color:#000000">texinfo_set_from_init_file</span></b> <span 
style="color:#990000">(</span><span 
style="color:#FF0000">'SECTION_BUTTONS'</span><span 
style="color:#990000">,</span> <span style="color:#990000">\</span><span 
style="color:#009900">@SECTION_BUTTONS</span><span 
style="color:#990000">);</span>
+</pre></div>
+
+<p>No argument
+</p><div class="example">
+<pre class="example-preformatted">texinfo_set_from_init_file('NO_CSS', 1);
+</pre></div>
+
+<p>Unknown language
+</p><div class="example user-unknown">
 <pre class="example-preformatted">unknown language
 </pre></div>
 
diff --git 
a/tp/tests/other/res_parser/highlight_syntax_example/highlight_example_highlight_perl_input.pl
 
b/tp/tests/other/res_parser/highlight_syntax_example/highlight_example_highlight_perl_input.pl
index 0620bafb0d..f391c07c8f 100644
--- 
a/tp/tests/other/res_parser/highlight_syntax_example/highlight_example_highlight_perl_input.pl
+++ 
b/tp/tests/other/res_parser/highlight_syntax_example/highlight_example_highlight_perl_input.pl
@@ -7,3 +7,13 @@ sub do ($) {
   return $arg + 4;
 }
 _______________________ 0
+_______________________ 1
+my @SECTION_BUTTONS =
+  (
+   \&singular_banner,
+   'Back', 'Forward',   'FastBack', 'FastForward',
+   'Up', 'Top', 'Contents', 'Index', 'About'
+  );
+
+texinfo_set_from_init_file ('SECTION_BUTTONS', \@SECTION_BUTTONS);
+_______________________ 1
diff --git 
a/tp/tests/other/res_parser/highlight_syntax_example/highlight_example_highlight_perl_output.html
 
b/tp/tests/other/res_parser/highlight_syntax_example/highlight_example_highlight_perl_output.html
index db61f90348..0cf3c15e61 100644
--- 
a/tp/tests/other/res_parser/highlight_syntax_example/highlight_example_highlight_perl_output.html
+++ 
b/tp/tests/other/res_parser/highlight_syntax_example/highlight_example_highlight_perl_output.html
@@ -9,4 +9,13 @@ http://www.gnu.org/software/src-highlite -->
   <b><span style="color:#0000FF">return</span></b> <span 
style="color:#009900">$arg</span> <span style="color:#990000">+</span> <span 
style="color:#993399">4</span><span style="color:#990000">;</span>
 <span style="color:#FF0000">}</span>
 _______________________________________ highlight texinfo _GT Haib0aik zei4YieH
+<b><span style="color:#0000FF">my</span></b> <span 
style="color:#009900">@SECTION_BUTTONS</span> <span 
style="color:#990000">=</span>
+  <span style="color:#990000">(</span>
+   <span style="color:#990000">\&amp;</span>singular_banner<span 
style="color:#990000">,</span>
+   <span style="color:#FF0000">'Back'</span><span 
style="color:#990000">,</span> <span 
style="color:#FF0000">'Forward'</span><span style="color:#990000">,</span>   
<span style="color:#FF0000">'FastBack'</span><span 
style="color:#990000">,</span> <span 
style="color:#FF0000">'FastForward'</span><span style="color:#990000">,</span>
+   <span style="color:#FF0000">'Up'</span><span style="color:#990000">,</span> 
<span style="color:#FF0000">'Top'</span><span style="color:#990000">,</span> 
<span style="color:#FF0000">'Contents'</span><span 
style="color:#990000">,</span> <span style="color:#FF0000">'Index'</span><span 
style="color:#990000">,</span> <span style="color:#FF0000">'About'</span>
+  <span style="color:#990000">);</span>
+
+<b><span style="color:#000000">texinfo_set_from_init_file</span></b> <span 
style="color:#990000">(</span><span 
style="color:#FF0000">'SECTION_BUTTONS'</span><span 
style="color:#990000">,</span> <span style="color:#990000">\</span><span 
style="color:#009900">@SECTION_BUTTONS</span><span 
style="color:#990000">);</span>
+_______________________________________ highlight texinfo _GT Haib0aik zei4YieH
 </pre>
diff --git 
a/tp/tests/other/res_parser/highlight_syntax_example_latin9/chapter.html 
b/tp/tests/other/res_parser/highlight_syntax_example_latin9/chapter.html
index c957430558..a93cb14445 100644
--- a/tp/tests/other/res_parser/highlight_syntax_example_latin9/chapter.html
+++ b/tp/tests/other/res_parser/highlight_syntax_example_latin9/chapter.html
@@ -62,7 +62,24 @@ Previous: <a href="index.html" accesskey="p" 
rel="prev">top</a>, Up: <a href="in
 <span style="color:#FF0000">}</span>
 </pre></div>
 
-<div class="example user-unknown">
+<div class="example user-perl">
+<pre class="example-preformatted"><b><span style="color:#0000FF">my</span></b> 
<span style="color:#009900">@SECTION_BUTTONS</span> <span 
style="color:#990000">=</span>
+  <span style="color:#990000">(</span>
+   <span style="color:#990000">\&amp;</span>singular_banner<span 
style="color:#990000">,</span>
+   <span style="color:#FF0000">'Back'</span><span 
style="color:#990000">,</span> <span 
style="color:#FF0000">'Forward'</span><span style="color:#990000">,</span>   
<span style="color:#FF0000">'FastBack'</span><span 
style="color:#990000">,</span> <span 
style="color:#FF0000">'FastForward'</span><span style="color:#990000">,</span>
+   <span style="color:#FF0000">'Up'</span><span style="color:#990000">,</span> 
<span style="color:#FF0000">'Top'</span><span style="color:#990000">,</span> 
<span style="color:#FF0000">'Contents'</span><span 
style="color:#990000">,</span> <span style="color:#FF0000">'Index'</span><span 
style="color:#990000">,</span> <span style="color:#FF0000">'About'</span>
+  <span style="color:#990000">);</span>
+
+<b><span style="color:#000000">texinfo_set_from_init_file</span></b> <span 
style="color:#990000">(</span><span 
style="color:#FF0000">'SECTION_BUTTONS'</span><span 
style="color:#990000">,</span> <span style="color:#990000">\</span><span 
style="color:#009900">@SECTION_BUTTONS</span><span 
style="color:#990000">);</span>
+</pre></div>
+
+<p>No argument
+</p><div class="example">
+<pre class="example-preformatted">texinfo_set_from_init_file('NO_CSS', 1);
+</pre></div>
+
+<p>Unknown language
+</p><div class="example user-unknown">
 <pre class="example-preformatted">unknown language
 </pre></div>
 
diff --git 
a/tp/tests/other/res_parser/highlight_syntax_example_latin9/highlight_example_highlight_perl_input.pl
 
b/tp/tests/other/res_parser/highlight_syntax_example_latin9/highlight_example_highlight_perl_input.pl
index 4a2e4aac36..b361ed0964 100644
--- 
a/tp/tests/other/res_parser/highlight_syntax_example_latin9/highlight_example_highlight_perl_input.pl
+++ 
b/tp/tests/other/res_parser/highlight_syntax_example_latin9/highlight_example_highlight_perl_input.pl
@@ -7,3 +7,13 @@ sub do ($) {
   return $arg + 4;
 }
 _______________________ 0
+_______________________ 1
+my @SECTION_BUTTONS =
+  (
+   \&singular_banner,
+   'Back', 'Forward',   'FastBack', 'FastForward',
+   'Up', 'Top', 'Contents', 'Index', 'About'
+  );
+
+texinfo_set_from_init_file ('SECTION_BUTTONS', \@SECTION_BUTTONS);
+_______________________ 1
diff --git 
a/tp/tests/other/res_parser/highlight_syntax_example_latin9/highlight_example_highlight_perl_output.html
 
b/tp/tests/other/res_parser/highlight_syntax_example_latin9/highlight_example_highlight_perl_output.html
index fb541100fb..5eedee8e07 100644
--- 
a/tp/tests/other/res_parser/highlight_syntax_example_latin9/highlight_example_highlight_perl_output.html
+++ 
b/tp/tests/other/res_parser/highlight_syntax_example_latin9/highlight_example_highlight_perl_output.html
@@ -9,4 +9,13 @@ http://www.gnu.org/software/src-highlite -->
   <b><span style="color:#0000FF">return</span></b> <span 
style="color:#009900">$arg</span> <span style="color:#990000">+</span> <span 
style="color:#993399">4</span><span style="color:#990000">;</span>
 <span style="color:#FF0000">}</span>
 _______________________________________ highlight texinfo _GT Haib0aik zei4YieH
+<b><span style="color:#0000FF">my</span></b> <span 
style="color:#009900">@SECTION_BUTTONS</span> <span 
style="color:#990000">=</span>
+  <span style="color:#990000">(</span>
+   <span style="color:#990000">\&amp;</span>singular_banner<span 
style="color:#990000">,</span>
+   <span style="color:#FF0000">'Back'</span><span 
style="color:#990000">,</span> <span 
style="color:#FF0000">'Forward'</span><span style="color:#990000">,</span>   
<span style="color:#FF0000">'FastBack'</span><span 
style="color:#990000">,</span> <span 
style="color:#FF0000">'FastForward'</span><span style="color:#990000">,</span>
+   <span style="color:#FF0000">'Up'</span><span style="color:#990000">,</span> 
<span style="color:#FF0000">'Top'</span><span style="color:#990000">,</span> 
<span style="color:#FF0000">'Contents'</span><span 
style="color:#990000">,</span> <span style="color:#FF0000">'Index'</span><span 
style="color:#990000">,</span> <span style="color:#FF0000">'About'</span>
+  <span style="color:#990000">);</span>
+
+<b><span style="color:#000000">texinfo_set_from_init_file</span></b> <span 
style="color:#990000">(</span><span 
style="color:#FF0000">'SECTION_BUTTONS'</span><span 
style="color:#990000">,</span> <span style="color:#990000">\</span><span 
style="color:#009900">@SECTION_BUTTONS</span><span 
style="color:#990000">);</span>
+_______________________________________ highlight texinfo _GT Haib0aik zei4YieH
 </pre>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]