summaryrefslogtreecommitdiffstats
path: root/lib/Locale/Po4a/Gemtext.pm
blob: 7be810f8244d40222e4b3d1cc72e0fe76f8b5b54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env perl -w

# Po4a::Gemtext.pm
#
# extract and translate translatable strings from a Gemtext documents
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
########################################################################

require Exporter;

package Locale::Po4a::Gemtext;

use 5.006;
use strict;
use warnings;

use vars qw(@ISA @EXPORT @AUTOLOAD);
@ISA    = qw(Locale::Po4a::TransTractor);
@EXPORT = qw();

use Locale::Po4a::TransTractor qw(process new);
use Locale::Po4a::Common;

=encoding UTF-8

=head1 NAME

Locale::Po4a::Gemtext - convert Gettext documents from/to PO files.

=head1 DESCRIPTION

The po4a (PO for anything) project goal is to ease translations (and more
interestingly, the maintenance of translations) using gettext tools on
areas where they were not expected like documentation.

Locale::Po4a::Gemtext is a module to help the translation of Gemtext documents into
other [human] languages.

=cut

sub initialize { }

sub parse {
    my $self = shift;

    my ( $line, $ref ) = $self->shiftline();

    while ( defined($line) ) {
        chomp($line);

             $self->parse_heading( $line, $ref )
          or $self->parse_preformatted_text( $line, $ref )
          or $self->parse_list( $line, $ref )
          or $self->parse_quote( $line, $ref )
          or $self->parse_link( $line, $ref )
          or $self->pushline( $self->translate( $line, $ref, "paragraph" ) . "\n" );

        ( $line, $ref ) = $self->shiftline();
    }
}

sub parse_heading() {
    my $self = shift;
    my $line = shift;
    my $ref  = shift;

    $line =~ m/^(#{1,3}) *(.+)/ or return;
    my $level   = $1;
    my $content = $2;

    $self->pushline( "$level " . $self->translate( $content, $ref, "heading $level" ) . "\n" );

    return 1;
}

sub parse_preformatted_text() {
    my $self = shift;
    my $line = shift;
    my $ref  = shift;

    $line =~ m/^(``` *)(.*)/ or return;
    my $prefix  = $1;
    my $content = $2;

    my $toggle_line = $prefix;
    $toggle_line .= $self->translate( $content, $ref, "alt text" ) if $content;
    $self->pushline("$toggle_line\n");

    my $paragraph;
    ( $line, $ref ) = $self->shiftline();

    while ( defined($line) ) {
        chomp($line);

        if ( $line =~ m/^```/ ) {
            $self->pushline( $self->translate( $paragraph, $ref, "preformatted text" ) . "\n" );
            $self->pushline("$line\n");

            return 1;
        }

        if ($paragraph) {
            $paragraph .= "\n$line";
        } else {
            $paragraph = $line;
        }

        ( $line, $ref ) = $self->shiftline();
    }
}

sub parse_list() {
    my $self = shift;
    my $line = shift;
    my $ref  = shift;

    $line =~ m/^\* (.+)/ or return;
    my $content = $1;

    $self->pushline( "* " . $self->translate( $content, $ref, "list" ) . "\n" );

    return 1;
}

sub parse_quote() {
    my $self = shift;
    my $line = shift;
    my $ref  = shift;

    $line =~ m/^(> *)(.+)/ or return;
    my $prefix  = $1;
    my $content = $2;

    $self->pushline( $prefix . $self->translate( $content, $ref, "quote" ) . "\n" );

    return 1;
}

sub parse_link() {
    my $self = shift;
    my $line = shift;
    my $ref  = shift;

    $line =~ m/^(=>[ \t]+[^ \t]+)(?:([ \t]+)(.*))?/ or return;
    my $prefix    = $1;
    my $separator = $2;
    my $content   = $3;

    my $result = $prefix;
    $result .= $separator . $self->translate( $content, $ref, "link" )
      if $content;
    $self->pushline( $result . "\n" );

    return 1;
}

1;

=head1 STATUS OF THIS MODULE

Tested successfully on simple Gemtext files, such as the official Gemtext documentation.

=head1 SEE ALSO

L<Locale::Po4a::TransTractor(3pm)>, L<po4a(7)|po4a.7>

=head1 AUTHORS

 gemmaro <gemmaro.dev@gmail.com>

=head1 COPYRIGHT AND LICENSE

 Copyright © 2024 gemmaro <gemmaro.dev@gmail.com>.

This program is free software; you may redistribute it and/or modify it
under the terms of GPL v2.0 or later (see the COPYING file).

=cut