| 1 | package Book::Bilingual::File; | ||
| 2 | # ABSTRACT: Construct a Bilingual book from file | ||
| 3 | use Mojo::Base -base; | ||
| 4 | use Book::Bilingual; | ||
| 5 | use Book::Bilingual::Dline; | ||
| 6 | use Book::Bilingual::Dlineset; | ||
| 7 | use Book::Bilingual::Chapter; | ||
| 8 | use Path::Tiny qw/path/; | ||
| 9 | use Carp; | ||
| 10 | |||
| 11 | has 'book' => sub { Book::Bilingual->new}; # Book::Bilingual | ||
| 12 | has 'file'; # Path to file | ||
| 13 | has 'chapters'; # Arrayref of chapter text | ||
| 14 | |||
| 15 | sub new { ## ($path :Path) | ||
| 16 | croak 'Need args ($path)' unless @_ > 1; | ||
| 17 | my $self = $_[0]->SUPER::new({ file => path($_[1]) }); | ||
| 18 | $self->_init(); | ||
| 19 | } | ||
| 20 | |||
| 21 | sub _init { | ||
| 22 | my ($self) = @_; | ||
| 23 | #say ref $self->book; | ||
| 24 | |||
| 25 | my $text = $self->file->slurp_utf8; | ||
| 26 | $self->chapters( _extract_chapters($text)); | ||
| 27 | |||
| 28 | foreach my $chapter (@{$self->chapters}) { | ||
| 29 | |||
| 30 | # Generate Dlinesets from extracted chapter text | ||
| 31 | my @Dsets = map { | ||
| 32 | Book::Bilingual::Dlineset->new->set(_extract_dlines($_)); | ||
| 33 | } @{_extract_dlineset($chapter)}; | ||
| 34 | |||
| 35 | my $book_chapter = Book::Bilingual::Chapter->new; | ||
| 36 | $book_chapter->number(shift @Dsets); | ||
| 37 | $book_chapter->title(shift @Dsets); | ||
| 38 | $book_chapter->body([@Dsets]); | ||
| 39 | $self->book->push($book_chapter); | ||
| 40 | }; | ||
| 41 | |||
| 42 | return $self; | ||
| 43 | } | ||
| 44 | sub _extract_chapters { ## ($file_text) :> [$chapter_text] | ||
| 45 | my ($text) = @_; | ||
| 46 | |||
| 47 | my @chapters = split /^----$/m, $text; | ||
| 48 | shift @chapters if scalar @chapters; # Ignore TOC and other front matter | ||
| 49 | |||
| 50 | return [@chapters]; | ||
| 51 | } | ||
| 52 | =head2 _extract_dlineset | ||
| 53 | |||
| 54 | Returns arrayref of DSET_TEXT. | ||
| 55 | |||
| 56 | Sample of DSET_TEXT: | ||
| 57 | |||
| 58 | Chapter One | ||
| 59 | #chapter-number <-- class definition | ||
| 60 | |||
| 61 | บทที่หนึ่ง / | ||
| 62 | บทที่ /One | ||
| 63 | /Chapter One | ||
| 64 | .. <-- last line | ||
| 65 | |||
| 66 | |||
| 67 | =cut | ||
| 68 | sub _extract_dlineset { ## ($chapter_text) :> [DSET_TEXT] | ||
| 69 | my ($chapter) = @_; | ||
| 70 | |||
| 71 | my @dsets = grep { | ||
| 72 | $_ !~ /^#/ # Ignore comments | ||
| 73 | } split /^__ /m, $chapter; | ||
| 74 | |||
| 75 | shift @dsets if scalar @dsets; # Ignore chapter front matter | ||
| 76 | |||
| 77 | return [@dsets]; | ||
| 78 | } | ||
| 79 | =head2 _extract_dlines | ||
| 80 | |||
| 81 | A set with class definition | ||
| 82 | |||
| 83 | Chapter One | ||
| 84 | #chapter-number <-- class definition | ||
| 85 | |||
| 86 | บทที่หนึ่ง / | ||
| 87 | บทที่ /One | ||
| 88 | /Chapter One | ||
| 89 | .. <-- last line | ||
| 90 | |||
| 91 | A set without the class definition | ||
| 92 | |||
| 93 | "Can we go to Polseath as usual?" | ||
| 94 | |||
| 95 | "เราไปชายหาดตามปกติได้ไหม" / | ||
| 96 | /"Can /เราไปชายหาดตามปกติ?" | ||
| 97 | /"Can /เราไป /beach as usual?" | ||
| 98 | /"Can /เรา /go to the beach as usual?" | ||
| 99 | /"Can we go to the beach as usual?" | ||
| 100 | .. <-- last line | ||
| 101 | |||
| 102 | =cut | ||
| 103 | sub _extract_dlines { ## ($dset_text) :> [Dline] | ||
| 104 | my ($dset) = @_; | ||
| 105 | my @lines = split "\n", $dset; | ||
| 106 | |||
| 107 | shift @lines; # Ignore first line | ||
| 108 | my $class = _extract_class(shift @lines); # Extract dline class | ||
| 109 | pop @lines; # Ignore last line | ||
| 110 | |||
| 111 | @lines = grep { | ||
| 112 | $_ !~ /^\s*$/; # Ignore empty lines | ||
| 113 | } @lines; | ||
| 114 | |||
| 115 | @lines = map { | ||
| 116 | $_ =~ s/^ //; # Trim prefix | ||
| 117 | Book::Bilingual::Dline->new({ class=>$class, str=>$_ }); | ||
| 118 | } @lines; | ||
| 119 | |||
| 120 | return [@lines]; | ||
| 121 | } | ||
| 122 | sub _extract_class { | ||
| 123 | my ($line) = @_; | ||
| 124 | |||
| 125 | $line =~ s/^\s+|\s+$//g; # Trim front and back | ||
| 126 | return '' unless $line; | ||
| 127 | |||
| 128 | $line =~ s/#//g; # Remove # markers | ||
| 129 | return join ' ',(split /\s+/, $line); # Split and recombine words | ||
| 130 | } | ||
| 131 | |||
| 132 | 1; |