[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[no subject]
From: |
Patrice Dumas |
Date: |
Thu, 11 Jan 2024 16:01:03 -0500 (EST) |
branch: master
commit 1defcc46621ae9df2acac69301f3adf9e52ab310
Author: Patrice Dumas <pertusus@free.fr>
AuthorDate: Thu Jan 11 22:00:57 2024 +0100
* tp/Texinfo/Convert/Text.pm: move functions around.
* tp/Texinfo/XS/main/convert_to_text.c (text_brace_no_arg_command):
rename brace_no_arg_command as text_brace_no_arg_command.
* tp/Texinfo/XS/main/convert_to_text.c: move code around, make
functions static or declare them in .h.
---
ChangeLog | 10 ++
tp/Texinfo/Convert/Text.pm | 249 ++++++++++++++++++-----------------
tp/Texinfo/XS/main/convert_to_text.c | 148 +++++++++++----------
tp/Texinfo/XS/main/convert_to_text.h | 5 +
4 files changed, 219 insertions(+), 193 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 00232223dc..23f7fe9358 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2024-01-11 Patrice Dumas <pertusus@free.fr>
+
+ * tp/Texinfo/Convert/Text.pm: move functions around.
+
+ * tp/Texinfo/XS/main/convert_to_text.c (text_brace_no_arg_command):
+ rename brace_no_arg_command as text_brace_no_arg_command.
+
+ * tp/Texinfo/XS/main/convert_to_text.c: move code around, make
+ functions static or declare them in .h.
+
2024-01-11 Patrice Dumas <pertusus@free.fr>
* tp/Texinfo/XS/convert/indices_in_conversion.c
diff --git a/tp/Texinfo/Convert/Text.pm b/tp/Texinfo/Convert/Text.pm
index 86aaff6eed..0118fad2e6 100644
--- a/tp/Texinfo/Convert/Text.pm
+++ b/tp/Texinfo/Convert/Text.pm
@@ -208,6 +208,100 @@ foreach my $type ('ignorable_spaces_after_command',
}
+my @text_indicator_converter_options = ('NUMBER_SECTIONS', 'ASCII_GLYPH',
'TEST');
+
+# TODO not documented. Document?
+# $SELF is an object implementing get_conf, in general a converter.
+# Setup options as used by Texinfo::Convert::Text::convert_to_text
+# based on the converter information.
+# This is relevant for file names, for instance.
+# $OPTIONS_IN can be used to pass additional options.
+sub copy_options_for_convert_text($;$)
+{
+ my $self = shift;
+ my $options_in = shift;
+ my %options;
+ if ($self->get_conf('ENABLE_ENCODING')
+ and $self->get_conf('OUTPUT_ENCODING_NAME')) {
+ $options{'enabled_encoding'} = $self->get_conf('OUTPUT_ENCODING_NAME');
+ }
+
+ foreach my $option (@text_indicator_converter_options) {
+ my $conf = $self->get_conf($option);
+ if ($conf) {
+ $options{$option} = 1;
+ } elsif (defined($conf)) {
+ $options{$option} = 0;
+ }
+ }
+ $options{'expanded_formats'} = $self->{'expanded_formats'};
+ # for locate_include_file
+ $options{'INCLUDE_DIRECTORIES'} = $self->get_conf('INCLUDE_DIRECTORIES');
+
+ $options{'converter'} = $self;
+
+ if ($options_in) {
+ foreach my $option (keys(%$options_in)) {
+ $options{$option} = $options_in->{$option};
+ }
+ }
+ return \%options;
+}
+
+sub set_options_code($)
+{
+ my $options = shift;
+ $options->{'_code_state'}++;
+}
+
+sub reset_options_code($)
+{
+ my $options = shift;
+ $options->{'_code_state'}--;
+}
+
+# set enabled_encoding unless the encoding is ascii, even if
+# ENABLE_ENCODING is not set.
+sub set_options_encoding_if_not_ascii($$)
+{
+ my $self = shift;
+ my $options = shift;
+ my $output_encoding_name = $self->get_conf('OUTPUT_ENCODING_NAME');
+ if (defined($output_encoding_name) and $output_encoding_name ne 'us-ascii') {
+ if (defined($options->{'_saved_enabled_encoding'})) {
+ print STDERR "BUG: if_not_ascii _saved_enabled_encoding set: "
+ .$options->{'_saved_enabled_encoding'}." / ".
+ $output_encoding_name ."\n";
+ #cluck();
+ }
+ $options->{'_saved_enabled_encoding'} = $options->{'enabled_encoding'};
+ $options->{'enabled_encoding'} = $output_encoding_name;
+ }
+}
+
+sub set_options_encoding($$)
+{
+ my $options = shift;
+ my $encoding = shift;
+ if (defined($options->{'_saved_enabled_encoding'})) {
+ print STDERR "BUG: _saved_enabled_encoding set: "
+ .$options->{'_saved_enabled_encoding'}." / ".
+ $encoding."\n";
+ }
+ $options->{'_saved_enabled_encoding'} = $options->{'enabled_encoding'};
+ $options->{'enabled_encoding'} = $encoding;
+}
+
+sub reset_options_encoding($)
+{
+ my $options = shift;
+ if (defined($options->{'_saved_enabled_encoding'})) {
+ $options->{'enabled_encoding'} = $options->{'_saved_enabled_encoding'};
+ delete $options->{'_saved_enabled_encoding'};
+ }
+}
+
+
sub _ascii_accent($$)
{
my $text = shift;
@@ -282,7 +376,7 @@ sub text_accents($;$$)
}
}
-# TODO docuent? Used in other converters.
+# TODO document? Used in other converters.
sub brace_no_arg_command($;$)
{
my $element = shift;
@@ -380,134 +474,12 @@ sub _text_heading($$$;$$)
return $result;
}
-my @text_indicator_converter_options = ('NUMBER_SECTIONS', 'ASCII_GLYPH',
'TEST');
-
-# TODO not documented. Document?
-# $SELF is an object implementing get_conf, in general a converter.
-# Setup options as used by Texinfo::Convert::Text::convert_to_text
-# based on the converter information.
-# This is relevant for file names, for instance.
-# $OPTIONS_IN can be used to pass additional options.
-sub copy_options_for_convert_text($;$)
-{
- my $self = shift;
- my $options_in = shift;
- my %options;
- if ($self->get_conf('ENABLE_ENCODING')
- and $self->get_conf('OUTPUT_ENCODING_NAME')) {
- $options{'enabled_encoding'} = $self->get_conf('OUTPUT_ENCODING_NAME');
- }
-
- foreach my $option (@text_indicator_converter_options) {
- my $conf = $self->get_conf($option);
- if ($conf) {
- $options{$option} = 1;
- } elsif (defined($conf)) {
- $options{$option} = 0;
- }
- }
- $options{'expanded_formats'} = $self->{'expanded_formats'};
- # for locate_include_file
- $options{'INCLUDE_DIRECTORIES'} = $self->get_conf('INCLUDE_DIRECTORIES');
-
- $options{'converter'} = $self;
-
- if ($options_in) {
- foreach my $option (keys(%$options_in)) {
- $options{$option} = $options_in->{$option};
- }
- }
- return \%options;
-}
-
-sub set_options_code($)
-{
- my $options = shift;
- $options->{'_code_state'}++;
-}
-
-sub reset_options_code($)
-{
- my $options = shift;
- $options->{'_code_state'}--;
-}
-
-# set enabled_encoding unless the encoding is ascii, even if
-# ENABLE_ENCODING is not set.
-sub set_options_encoding_if_not_ascii($$)
-{
- my $self = shift;
- my $options = shift;
- my $output_encoding_name = $self->get_conf('OUTPUT_ENCODING_NAME');
- if (defined($output_encoding_name) and $output_encoding_name ne 'us-ascii') {
- if (defined($options->{'_saved_enabled_encoding'})) {
- print STDERR "BUG: if_not_ascii _saved_enabled_encoding set: "
- .$options->{'_saved_enabled_encoding'}." / ".
- $output_encoding_name ."\n";
- #cluck();
- }
- $options->{'_saved_enabled_encoding'} = $options->{'enabled_encoding'};
- $options->{'enabled_encoding'} = $output_encoding_name;
- }
-}
-
-sub set_options_encoding($$)
-{
- my $options = shift;
- my $encoding = shift;
- if (defined($options->{'_saved_enabled_encoding'})) {
- print STDERR "BUG: _saved_enabled_encoding set: "
- .$options->{'_saved_enabled_encoding'}." / ".
- $encoding."\n";
- }
- $options->{'_saved_enabled_encoding'} = $options->{'enabled_encoding'};
- $options->{'enabled_encoding'} = $encoding;
-}
-
-sub reset_options_encoding($)
-{
- my $options = shift;
- if (defined($options->{'_saved_enabled_encoding'})) {
- $options->{'enabled_encoding'} = $options->{'_saved_enabled_encoding'};
- delete $options->{'_saved_enabled_encoding'};
- }
-}
-
# Will never be called, used for the override.
sub _convert_tree_with_XS($$)
{
}
-sub convert_to_text($;$)
-{
- my $root = shift;
- my $options = shift;
-
- if (ref($root) ne 'HASH') {
- confess "root not a hash";
- }
-
- #print STDERR "CONVERT\n";
- # this is needed for locate_include_file which uses
- # $configurations_information->get_conf() and thus requires a blessed
- # reference.
- $options = {} if (!defined($options));
- if (defined($options)) {
- if (!ref($options)) {
- confess("convert_to_text options not a ref\n");
- }
- bless $options;
- }
-
- # Interface with XS converter.
- if ($XS_convert and defined($root->{'tree_document_descriptor'})) {
- return _convert_tree_with_XS($options, $root);
- }
-
- return _convert($root, $options);
-}
-
sub _convert($;$);
sub _convert($;$)
@@ -838,6 +810,35 @@ sub _convert($;$)
return $result;
}
+# the main entry point
+sub convert_to_text($;$)
+{
+ my $root = shift;
+ my $options = shift;
+
+ if (ref($root) ne 'HASH') {
+ confess "root not a hash";
+ }
+
+ #print STDERR "CONVERT\n";
+ # this is needed for locate_include_file which uses
+ # $configurations_information->get_conf() and thus requires a blessed
+ # reference.
+ $options = {} if (!defined($options));
+ if (defined($options)) {
+ if (!ref($options)) {
+ confess("convert_to_text options not a ref\n");
+ }
+ bless $options;
+ }
+
+ # Interface with XS converter.
+ if ($XS_convert and defined($root->{'tree_document_descriptor'})) {
+ return _convert_tree_with_XS($options, $root);
+ }
+
+ return _convert($root, $options);
+}
# Implement the converters API, but as simply as possible
@@ -890,10 +891,10 @@ sub convert_tree($$)
return _convert($element, $options);
}
-# TODO set options with $self if defined?
# This function is not called in anywhere in Texinfo code, it is implemented
# to be in line with Texinfo::Convert::Converter documentation on functions
# defined for a converter.
+# TODO set options with $self if defined?
sub convert($$)
{
my $self = shift;
diff --git a/tp/Texinfo/XS/main/convert_to_text.c
b/tp/Texinfo/XS/main/convert_to_text.c
index 1178fac039..fa8ac41689 100644
--- a/tp/Texinfo/XS/main/convert_to_text.c
+++ b/tp/Texinfo/XS/main/convert_to_text.c
@@ -44,71 +44,6 @@
#include "cmd_text.c"
-/* the CONVERTER argument is not used, it is there solely to match the
- calling prototype in accent formatting commands */
-char *
-ascii_accent (CONVERTER *self, const char *text,
- const ELEMENT *command, int set_case)
-{
- const enum command_id cmd = command->cmd;
- TEXT accent_text;
-
- text_init (&accent_text);
-
- if (cmd == CM_udotaccent)
- {
- text_append (&accent_text, ".");
- text_append (&accent_text, text);
- }
- else
- {
- text_append (&accent_text, text);
- if (cmd == CM_H)
- text_append (&accent_text, "''");
- else if (cmd == CM_dotaccent)
- text_append (&accent_text, ".");
- else if (cmd == CM_ringaccent)
- text_append (&accent_text, "*");
- else if (cmd == CM_tieaccent)
- text_append (&accent_text, "[");
- else if (cmd == CM_u)
- text_append (&accent_text, "(");
- else if (cmd == CM_ubaraccent)
- text_append (&accent_text, "_");
- else if (cmd == CM_v)
- text_append (&accent_text, "<");
- else if (cmd == CM_ogonek)
- text_append (&accent_text, ";");
- else if (cmd != CM_dotless)
- text_append (&accent_text, builtin_command_name (cmd));
- }
- return accent_text.text;
-}
-
-char *
-ascii_accents_internal (const char *text, const ELEMENT_STACK *stack,
- int set_case)
-{
- char *result;
- int i;
-
- if (set_case)
- result = to_upper_or_lower_multibyte (text, set_case);
- else
- result = strdup (text);
-
- for (i = stack->top - 1; i >= 0; i--)
- {
- const ELEMENT *accent_command = stack->stack[i];
- char *formatted_accent = ascii_accent (0, result, accent_command,
- set_case);
- free (result);
- result = formatted_accent;
- }
-
- return result;
-}
-
TEXT_OPTIONS *
new_text_options (void)
{
@@ -126,6 +61,9 @@ destroy_text_options (TEXT_OPTIONS *text_options)
free (text_options->encoding);
free (text_options->expanded_formats);
free_strings_list (&text_options->include_directories);
+ /* if the customization options come from a converter or are another
+ structure options, in practice a document, options should not be
+ freed here, but by their respective structures */
if (text_options->other_converter_options
&& !text_options->converter
&& !text_options->other_options)
@@ -172,6 +110,7 @@ copy_options_for_convert_text (OPTIONS *options)
copy_strings (&text_options->include_directories,
options->INCLUDE_DIRECTORIES.strlist);
+ /* not a copy , but a reference to the options */
text_options->other_converter_options = options;
return text_options;
@@ -218,6 +157,9 @@ setup_converter_index_entry_keys_formatting (CONVERTER
*self)
return text_options;
}
+
+/* following functions to be used to modify TEXT_OPTIONS encoding */
+
void
text_set_options_encoding_if_not_ascii (CONVERTER *self,
TEXT_OPTIONS *text_options)
@@ -261,6 +203,74 @@ text_reset_options_encoding (TEXT_OPTIONS *text_options)
text_options->_saved_enabled_encoding = 0;
}
+
+/* the CONVERTER argument is not used, it is there solely to match the
+ calling prototype in accent formatting commands */
+char *
+ascii_accent (CONVERTER *self, const char *text,
+ const ELEMENT *command, int set_case)
+{
+ const enum command_id cmd = command->cmd;
+ TEXT accent_text;
+
+ text_init (&accent_text);
+
+ if (cmd == CM_udotaccent)
+ {
+ text_append (&accent_text, ".");
+ text_append (&accent_text, text);
+ }
+ else
+ {
+ text_append (&accent_text, text);
+ if (cmd == CM_H)
+ text_append (&accent_text, "''");
+ else if (cmd == CM_dotaccent)
+ text_append (&accent_text, ".");
+ else if (cmd == CM_ringaccent)
+ text_append (&accent_text, "*");
+ else if (cmd == CM_tieaccent)
+ text_append (&accent_text, "[");
+ else if (cmd == CM_u)
+ text_append (&accent_text, "(");
+ else if (cmd == CM_ubaraccent)
+ text_append (&accent_text, "_");
+ else if (cmd == CM_v)
+ text_append (&accent_text, "<");
+ else if (cmd == CM_ogonek)
+ text_append (&accent_text, ";");
+ else if (cmd != CM_dotless)
+ text_append (&accent_text, builtin_command_name (cmd));
+ }
+ return accent_text.text;
+}
+
+char *
+ascii_accents_internal (const char *text, const ELEMENT_STACK *stack,
+ int set_case)
+{
+ char *result;
+ int i;
+
+ if (set_case)
+ result = to_upper_or_lower_multibyte (text, set_case);
+ else
+ result = strdup (text);
+
+ for (i = stack->top - 1; i >= 0; i--)
+ {
+ const ELEMENT *accent_command = stack->stack[i];
+ char *formatted_accent = ascii_accent (0, result, accent_command,
+ set_case);
+ free (result);
+ result = formatted_accent;
+ }
+
+ return result;
+}
+
+/* local restricted set of TEXT_OPTIONS relevant for accent formatting
+ set from function arguments */
static TEXT_OPTIONS text_accents_options;
/* format an accent command and nested accents within as Text. */
@@ -292,7 +302,7 @@ text_accents (const ELEMENT *accent, char *encoding, int
set_case)
/* result to be freed by caller */
char *
-brace_no_arg_command (const ELEMENT *e, TEXT_OPTIONS *options)
+text_brace_no_arg_command (const ELEMENT *e, TEXT_OPTIONS *options)
{
char *result = 0;
enum command_id cmd = e->cmd;
@@ -356,7 +366,7 @@ static const char *underline_symbol[5] = {"*", "*", "=",
"-", "."};
/* Return the text of an underlined heading, possibly indented. */
/* return to be freed by caller */
-char *
+static char *
text_heading (const ELEMENT *current, const char *text, OPTIONS *options,
int numbered, int indent_length)
{
@@ -423,7 +433,7 @@ text_heading (const ELEMENT *current, const char *text,
OPTIONS *options,
#define ADD(x) text_append (result, x)
-void
+static void
convert_to_text_internal (const ELEMENT *element, TEXT_OPTIONS *text_options,
TEXT *result);
@@ -644,7 +654,7 @@ convert_to_text_internal (const ELEMENT *element,
TEXT_OPTIONS *text_options,
else if (text_brace_no_arg_commands[data_cmd])
{
char *brace_no_args_text
- = brace_no_arg_command (element, text_options);
+ = text_brace_no_arg_command (element, text_options);
ADD(brace_no_args_text);
free (brace_no_args_text);
return;
diff --git a/tp/Texinfo/XS/main/convert_to_text.h
b/tp/Texinfo/XS/main/convert_to_text.h
index 92d1d01d13..da096f1413 100644
--- a/tp/Texinfo/XS/main/convert_to_text.h
+++ b/tp/Texinfo/XS/main/convert_to_text.h
@@ -43,4 +43,9 @@ void text_set_options_encoding_if_not_ascii (CONVERTER *self,
TEXT_OPTIONS *text_options);
void text_set_options_encoding (TEXT_OPTIONS *text_options, char *encoding);
void text_reset_options_encoding (TEXT_OPTIONS *text_options);
+
+/* used in converters in perl */
+char *text_accents (const ELEMENT *accent, char *encoding, int set_case);
+char *text_brace_no_arg_command (const ELEMENT *e, TEXT_OPTIONS *options);
+
#endif