| 1 | package Book::Bilingual::File; | ||||||||||
| 2 | # ABSTRACT: Construct a Bilingual book from file | ||||||||||
| 3 | 2 | ▶ | use Mojo::Base -base; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use Book::Bilingual; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use Book::Bilingual::Dline; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use Book::Bilingual::Dlineset; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use Book::Bilingual::Chapter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use Path::Tiny qw/path/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 20 | ▶ | has 'book' => sub { Book::Bilingual->new}; # Book::Bilingual | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | has 'file'; # Path to file | |||||||||
| 13 | 2 | has 'chapters'; # Arrayref of chapter text | |||||||||
| 14 | |||||||||||
| 15 | sub new { ## ($path :Path) | ||||||||||
| 16 | 20 | ▶ | croak 'Need args ($path)' unless @_ > 1; | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
Branch: 1 branch
| |||||||||||
| 17 | 20 | my $self = $_[0]->SUPER::new({ file => path($_[1]) }); | |||||||||
| 18 | 20 | $self->_init(); | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | sub _init { | ||||||||||
| 22 | 20 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
_init: called CC=2 SCAR=6.9
| |||||||||||
| 23 | #say ref $self->book; | ||||||||||
| 24 | |||||||||||
| 25 | 20 | my $text = $self->file->slurp_utf8; | |||||||||
| 26 | 20 | $self->chapters( _extract_chapters($text)); | |||||||||
| 27 | |||||||||||
| 28 | 20 | foreach my $chapter (@{$self->chapters}) { | |||||||||
| 29 | |||||||||||
| 30 | # Generate Dlinesets from extracted chapter text | ||||||||||
| 31 | my @Dsets = map { | ||||||||||
| 32 | 180 | Book::Bilingual::Dlineset->new->set(_extract_dlines($_)); | |||||||||
| 33 | 40 | } @{_extract_dlineset($chapter)}; | |||||||||
| 34 | |||||||||||
| 35 | 40 | my $book_chapter = Book::Bilingual::Chapter->new; | |||||||||
| 36 | 40 | $book_chapter->number(shift @Dsets); | |||||||||
| 37 | 40 | $book_chapter->title(shift @Dsets); | |||||||||
| 38 | 40 | $book_chapter->body([@Dsets]); | |||||||||
| 39 | 40 | $self->book->push($book_chapter); | |||||||||
| 40 | }; | ||||||||||
| 41 | |||||||||||
| 42 | 20 | return $self; | |||||||||
| 43 | } | ||||||||||
| 44 | sub _extract_chapters { ## ($file_text) :> [$chapter_text] | ||||||||||
| 45 | 20 | ▶ | my ($text) = @_; | ||||||||
|
Subroutine
_extract_chapters: called CC=2 SCAR=7.0
| |||||||||||
| 46 | |||||||||||
| 47 | 20 | my @chapters = split /^----$/m, $text; | |||||||||
| 48 | 20 | ▶ | shift @chapters if scalar @chapters; # Ignore TOC and other front matter | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | |||||||||||
| 50 | 20 | 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 | 43 | ▶ | my ($chapter) = @_; | ||||||||
|
Subroutine
_extract_dlineset: called CC=2 SCAR=7.0
| |||||||||||
| 70 | |||||||||||
| 71 | my @dsets = grep { | ||||||||||
| 72 | 43 | $_ !~ /^#/ # Ignore comments | |||||||||
| 73 | } split /^__ /m, $chapter; | ||||||||||
| 74 | |||||||||||
| 75 | 43 | ▶ | shift @dsets if scalar @dsets; # Ignore chapter front matter | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | |||||||||||
| 77 | 43 | 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 | 182 | ▶ | my ($dset) = @_; | ||||||||
|
Subroutine
_extract_dlines: called CC=1 SCAR=0.0
| |||||||||||
| 105 | 182 | my @lines = split "\n", $dset; | |||||||||
| 106 | |||||||||||
| 107 | 182 | shift @lines; # Ignore first line | |||||||||
| 108 | 182 | my $class = _extract_class(shift @lines); # Extract dline class | |||||||||
| 109 | 182 | pop @lines; # Ignore last line | |||||||||
| 110 | |||||||||||
| 111 | @lines = grep { | ||||||||||
| 112 | 182 | $_ !~ /^\s*$/; # Ignore empty lines | |||||||||
| 113 | } @lines; | ||||||||||
| 114 | |||||||||||
| 115 | @lines = map { | ||||||||||
| 116 | 182 | $_ =~ s/^ //; # Trim prefix | |||||||||
| 117 | 1026 | Book::Bilingual::Dline->new({ class=>$class, str=>$_ }); | |||||||||
| 118 | } @lines; | ||||||||||
| 119 | |||||||||||
| 120 | 182 | return [@lines]; | |||||||||
| 121 | } | ||||||||||
| 122 | sub _extract_class { | ||||||||||
| 123 | 185 | ▶ | my ($line) = @_; | ||||||||
|
Subroutine
_extract_class: called CC=2 SCAR=6.9
| |||||||||||
| 124 | |||||||||||
| 125 | 185 | $line =~ s/^\s+|\s+$//g; # Trim front and back | |||||||||
| 126 | 185 | ▶ | return '' unless $line; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | |||||||||||
| 128 | 124 | $line =~ s/#//g; # Remove # markers | |||||||||
| 129 | 124 | return join ' ',(split /\s+/, $line); # Split and recombine words | |||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | 2 | 1; | |||||||||