summaryrefslogtreecommitdiff
path: root/lib/LangFile.pm
blob: e81f7f7aaa1cd4b164d6a4e8e7083e783d0cb3f2 (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


package LangFile;

use strict;
use warnings;
use Fcntl qw(LOCK_SH LOCK_EX SEEK_SET);


sub new {
  my($class, $action, $file) = @_;
  open my $F, $action eq 'read' ? '<:utf8' : '>:utf8', $file or die "Opening $file: $!";
  flock($F, $action eq 'read' ? LOCK_SH : LOCK_EX) or die "Locking $file: $!";
  seek($F, 0, SEEK_SET) or die "Seeking $file: $!";
  return bless {
    act => $action,
    FH => $F,
    # status vars for reading
    intro => 1,
    last => [],
  }, $class;
}


sub read {
  my $self = shift;
  my $FH = $self->{FH};
  my @lines;
  my $state = '';
  my($lang, $sync);

  while((my $l = shift(@{$self->{last}}) || <$FH>)) {
    $l =~ s/[\r\n\t\s]+$//;

    # header
    if($self->{intro}) {
      push @lines, $l;
      next if $l ne '/intro';
      $self->{intro} = 0;
      return [ 'space', @lines ];
    }

    # key
    if(!$state && $l =~ /^:(.+)$/) {
      return [ 'key', $1 ];
    }

    # space
    if((!$state || $state eq 'space') && ($l =~ /^#/ || $l eq '')) {
      $state = 'space';
      push @lines, $l;
    } elsif($state eq 'space') {
      push @{$self->{last}}, "$l\n";
      return [ 'space', @lines ];
    }

    # tl
    if(!$state && $l =~ /^([a-z_-]{2})([ *]):(?: (.+)|)$/) {
      $lang = $1;
      $sync = $2 eq '*' ? 0 : 1;
      push @lines, $3||'';
      $state = 'tl';
    } elsif($state eq 'tl' && $l =~ /^\s{5}(.+)$/) {
      push @lines, $1;
    } elsif($state eq 'tl' && $l eq '') {
      push @lines, $l;
    } elsif($state eq 'tl') {
      my $trans = join "\n", @lines;
      push @{$self->{last}}, "\n" while $trans =~ s/\n$//;
      push @{$self->{last}}, $l;
      return [ 'tl', $lang, $sync, $trans ];
    }

    die "Don\'t know what to do with \"$l\"" if !$state;
  }
  if($state eq 'space') {
    return [ 'space', @lines ];
  }
  if($state eq 'tl') {
    my $trans = join "\n", @lines;
    push @{$self->{last}}, "\n" while $trans =~ s/\n$//;
    return [ 'tl', $lang, $sync, $trans ];
  }
  return undef;
}


sub write {
  my($self, @line) = @_;
  my $FH = $self->{FH};

  my $t = shift @line;

  if($t eq 'space') {
    print $FH "$_\n" for @line;
  }

  if($t eq 'key') {
    print $FH ":$line[0]\n";
  }

  if($t eq 'tl') {
    my($lang, $sync, $text) = @line;
    $text =~ s/\n([^\n])/\n     $1/g;
    $text = " $text" if $text ne '';
    printf $FH "%s%s:%s\n", $lang, $sync ? ' ' : '*', $text;
  }
}


sub close {
  my $self = shift;
  close $self->{FH};
}

1;

__END__
=pod

=head1 NAME

LangFile - Simple object oriented interface for the parsing and creation of lang.txt

=head1 USAGE

  use LangFile;
  my $read = LangFile->new(read => "data/lang.txt");
  my $write = LangFile->new(write => "lang-copy.txt");

  while((my $line = $read->read())) {
    # $line is an arrayref in one of the following formats:
    # [ 'space', @lines ]
    #   unparsed lines, like the header, newlines and comments
    # [ 'key', $key ]
    #   key line, $key is key name
    # [ 'tl', $lang, $sync, $text ]
    #   translation line(s), $lang = language tag, $sync = 1/0, $text = translation (can include newlines)
    # $line is undef on EOF, $read->next() die()s on a parsing error

    # create an identical copy of $read in $write
    $write->write(@$line);
  }
  $write->close;