Skip to content

Commit

Permalink
asciidoc: add cleanspaces option to remove extra spaces
Browse files Browse the repository at this point in the history
Extra spaces are removed in no-wrap segments, so that the output
conforms to English typography rules.

This fixes GitHub #464
  • Loading branch information
jnavila authored and mquinson committed May 9, 2024
1 parent ad50cf7 commit 645a033
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 10 deletions.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ Status of the documentation translation:
* 1 language >= 33%: ca (42%).
* 4 starting languages: eo (3%), hr (0%), hu (0%), nb (4%).

Asciidoc:
* Support a cleanspaces option to remove extra spaces in wrapped lines
(GitHub's #464). Thanks Jean-Noël Avila for the pull request.

=======================================================================
___ _____ ___
__ __/ _ \|___ / _ \
Expand Down
33 changes: 23 additions & 10 deletions lib/Locale/Po4a/AsciiDoc.pm
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ equality of length of opening and closing block fences.
Disable linting messages. When the source code cannot be fixed for clearer document structure, these messages are useless.
=item B<cleanspaces>
Remove extra spaces from the source segments in no-wrap mode. This is useful when the
translation tools are sensitive to the number of spaces.
=item B<yfm_keys>
Comma-separated list of keys to process for translation in the YAML Front Matter
Expand Down Expand Up @@ -221,6 +226,7 @@ sub initialize {
$self->{options}{'yfm_skip_array'} = 0;
$self->{options}{'yfm_paths'} = '';
$self->{options}{'nolinting'} = 0;
$self->{options}{'cleanspaces'} = 0;

foreach my $opt ( keys %options ) {
die wrap_mod( "po4a::asciidoc", dgettext( "po4a", "Unknown option: %s" ), $opt )
Expand Down Expand Up @@ -395,13 +401,20 @@ BEGIN {
sub translate {
my ( $self, $str, $ref, $type ) = @_;
my (%options) = @_;
if ( ($options{'wrap'}==1) && ($str =~ / \+\n/) ) {
$options{'wrap'} = 0;
$str =~ s/([^+])\n/$1 /g;
$str =~ s/ \+\n/\n/g;
$str = $self->SUPER::translate( $str, $ref, $type, %options);
$str =~ s/\n/ +\n/g;
$options{'wrap'} = 1;
if ( $options{'wrap'} == 1 ) {
if ($str =~ / \+\n/) {
$options{'wrap'} = 0;
$str =~ s/([^+])\n/$1 /g;
$str =~ s/ \+\n/\n/g;
$str = $self->SUPER::translate( $str, $ref, $type, %options);
$str =~ s/\n/ +\n/g;
$options{'wrap'} = 1;
} else {
if ( $self->{options}{'cleanspaces'} == 1 ) {
$str =~ s/[ \n]+/ /g;
}
$str = $self->SUPER::translate( $str, $ref, $type, %options);
}
} else {
$str = $self->SUPER::translate( $str, $ref, $type, %options );
}
Expand Down Expand Up @@ -1167,8 +1180,8 @@ sub do_paragraph {

my $end = "";
if ($wrap) {
$paragraph =~ s/^(.*?)(\n*)$/$1/s;
$end = $2 || "";
$paragraph =~ s/(\n*)$//s;
$end = $1 || "";
}
if ( defined $self->{bullet} ) {
my $bullet = $self->{bullet};
Expand All @@ -1189,7 +1202,7 @@ sub do_paragraph {

my $unwrap_result = !$self->{options}{'forcewrap'} && $wrap && (! ($t =~ /\+\n/) ) ;
if ($unwrap_result) {
$t =~ s/(\n| )+/ /g;
$t =~ s/[\n ]+/ /g;
}

@comments = ();
Expand Down
5 changes: 5 additions & 0 deletions t/fmt-asciidoc.t
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ push @tests,
'options' => '-o nolinting=1',
'input' => "fmt/asciidoc/LineBreak.adoc",
},
{
'format' => 'asciidoc',
'options' => '-o cleanspaces=1',
'input' => "fmt/asciidoc/CleanSpaces.adoc",
},
{
'format' => 'asciidoc',
'input' => "fmt/asciidoc/YamlFrontMatter.adoc",
Expand Down
20 changes: 20 additions & 0 deletions t/fmt/asciidoc/CleanSpaces.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Test Clean Spaces
=================

**************************
This is a Sidebar block.
This is a Sidebar block.
This is a Sidebar block.
This is a Sidebar block.
- This is a Sidebar block.
This is a Sidebar block.
**************************

<pathspec>...::
Files to add content from. Fileglobs (e.g. `*.c`) can
be given to add all matching files.

Some paragraph text. Some paragraph text. Some paragraph text.
Some paragraph text. Some paragraph text. Some paragraph text.
16 changes: 16 additions & 0 deletions t/fmt/asciidoc/CleanSpaces.norm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Test Clean Spaces
=================

**************************
This is a Sidebar block. This is a Sidebar block.

This is a Sidebar block.
This is a Sidebar block.

- This is a Sidebar block. This is a Sidebar block.
**************************

<pathspec>...::
Files to add content from. Fileglobs (e.g. `*.c`) can be given to add all matching files.

Some paragraph text. Some paragraph text. Some paragraph text. Some paragraph text. Some paragraph text. Some paragraph text.
61 changes: 61 additions & 0 deletions t/fmt/asciidoc/CleanSpaces.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# SOME DESCRIPTIVE TITLE
# Copyright (C) YEAR Free Software Foundation, Inc.
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2024-02-28 21:46+0100\n"
"PO-Revision-Date: 2024-02-28 21:47+0100\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#. type: Title =
#: CleanSpaces.adoc:2
#, no-wrap
msgid "Test Clean Spaces"
msgstr "TEST CLEAN SPACES"

#. type: delimited block *
#: CleanSpaces.adoc:7 CleanSpaces.adoc:13
msgid "This is a Sidebar block. This is a Sidebar block."
msgstr "THIS IS A SIDEBAR BLOCK. THIS IS A SIDEBAR BLOCK."

#. type: delimited block *
#: CleanSpaces.adoc:10
#, no-wrap
msgid ""
" This is a Sidebar block.\n"
" This is a Sidebar block.\n"
msgstr ""
" THIS IS A SIDEBAR BLOCK.\n"
" THIS IS A SIDEBAR BLOCK.\n"

#. type: Labeled list
#: CleanSpaces.adoc:15
#, no-wrap
msgid "<pathspec>..."
msgstr "<PATHSPEC>..."

#. type: Plain text
#: CleanSpaces.adoc:18
msgid ""
"Files to add content from. Fileglobs (e.g. `*.c`) can be given to add all "
"matching files."
msgstr ""
"FILES TO ADD CONTENT FROM. FILEGLOBS (E.G. `*.C`) CAN BE GIVEN TO ADD ALL "
"MATCHING FILES."

#. type: Plain text
#: CleanSpaces.adoc:20
msgid ""
"Some paragraph text. Some paragraph text. Some paragraph text. Some "
"paragraph text. Some paragraph text. Some paragraph text."
msgstr ""
"SOME PARAGRAPH TEXT. SOME PARAGRAPH TEXT. SOME PARAGRAPH TEXT. SOME "
"PARAGRAPH TEXT. SOME PARAGRAPH TEXT. SOME PARAGRAPH TEXT."
56 changes: 56 additions & 0 deletions t/fmt/asciidoc/CleanSpaces.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# SOME DESCRIPTIVE TITLE
# Copyright (C) YEAR Free Software Foundation, Inc.
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2024-02-28 21:46+0100\n"
"PO-Revision-Date: 2024-02-28 21:48+0100\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#. type: Title =
#: CleanSpaces.adoc:2
#, no-wrap
msgid "Test Clean Spaces"
msgstr ""

#. type: delimited block *
#: CleanSpaces.adoc:7 CleanSpaces.adoc:13
msgid "This is a Sidebar block. This is a Sidebar block."
msgstr ""

#. type: delimited block *
#: CleanSpaces.adoc:10
#, no-wrap
msgid ""
" This is a Sidebar block.\n"
" This is a Sidebar block.\n"
msgstr ""

#. type: Labeled list
#: CleanSpaces.adoc:15
#, no-wrap
msgid "<pathspec>..."
msgstr ""

#. type: Plain text
#: CleanSpaces.adoc:18
msgid ""
"Files to add content from. Fileglobs (e.g. `*.c`) can be given to add all "
"matching files."
msgstr ""

#. type: Plain text
#: CleanSpaces.adoc:20
msgid ""
"Some paragraph text. Some paragraph text. Some paragraph text. Some "
"paragraph text. Some paragraph text. Some paragraph text."
msgstr ""
16 changes: 16 additions & 0 deletions t/fmt/asciidoc/CleanSpaces.trans
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
TEST CLEAN SPACES
=================

**************************
THIS IS A SIDEBAR BLOCK. THIS IS A SIDEBAR BLOCK.

THIS IS A SIDEBAR BLOCK.
THIS IS A SIDEBAR BLOCK.

- THIS IS A SIDEBAR BLOCK. THIS IS A SIDEBAR BLOCK.
**************************

<PATHSPEC>...::
FILES TO ADD CONTENT FROM. FILEGLOBS (E.G. `*.C`) CAN BE GIVEN TO ADD ALL MATCHING FILES.

SOME PARAGRAPH TEXT. SOME PARAGRAPH TEXT. SOME PARAGRAPH TEXT. SOME PARAGRAPH TEXT. SOME PARAGRAPH TEXT. SOME PARAGRAPH TEXT.

0 comments on commit 645a033

Please sign in to comment.