| 1 | package Insight::Scriptures; | ||
| 2 | 1 | ▶ | use 5.006; use strict; use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | our $VERSION = '0.03'; | ||
| 4 | 1 | ▶ | use JSON::Lines; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use JSON; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | |||
| 7 | sub new { | ||
| 8 | my ($pkg, $args) = @_; | ||
| 9 | my $self = bless { | ||
| 10 | scriptures => undef, | ||
| 11 | scripture => undef, | ||
| 12 | scripture_file => undef, | ||
| 13 | index => -1, | ||
| 14 | jsonl => JSON::Lines->new(), | ||
| 15 | json => JSON->new->pretty(1)->canonical, | ||
| 16 | directory => 'directory', | ||
| 17 | %{ $args || {} }, | ||
| 18 | }, $pkg; | ||
| 19 | $self->{scriptures} = $self->directory($self->{directory}); | ||
| 20 | $self->scripture($self->{scripture}) if ($self->{scripture}); | ||
| 21 | return $self; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub architect { | ||
| 25 | my ($self) = @_; | ||
| 26 | |||
| 27 | while (1) { | ||
| 28 | print "insight: "; | ||
| 29 | my $in = <STDIN>; | ||
| 30 | chomp($in); | ||
| 31 | |||
| 32 | my %options = ( | ||
| 33 | abolish => 1, | ||
| 34 | current => 1, | ||
| 35 | next => 1, | ||
| 36 | previous => 1, | ||
| 37 | start => 1, | ||
| 38 | end => 1, | ||
| 39 | salvage => 1, | ||
| 40 | ); | ||
| 41 | |||
| 42 | if ($in eq 'help') { | ||
| 43 | print qq| | ||
| 44 | Welcome to Insight::Scriptures architect. This is a small module to assist in writing your own scriptures. | ||
| 45 | |||
| 46 | Help: | ||
| 47 | scripture \$scripture - loads the file containing the scripture lines | ||
| 48 | feather \$text - extends the scripture from the current index | ||
| 49 | abolish - deletes the current index from the scripture | ||
| 50 | current - print the line for the current index | ||
| 51 | next - move to the next index of the scripture and print the line | ||
| 52 | previous - move to the previous index of the scripture and print line | ||
| 53 | salvage - saves the scripture | ||
| 54 | exit - quits the command line application | ||
| 55 | |||
| 56 | |; | ||
| 57 | } elsif ($options{$in}) { | ||
| 58 | if ($in eq 'salvage') { | ||
| 59 | $self->$in(); | ||
| 60 | print qq|Scripture salvaged\n|; | ||
| 61 | } else { | ||
| 62 | my $enc = $self->{json}->encode($self->$in()); | ||
| 63 | print $self->{index} . ": " . $enc; | ||
| 64 | } | ||
| 65 | } elsif ($in =~ m/scripture (.*)/) { | ||
| 66 | $self->scripture($1); | ||
| 67 | print qq|Scripture $1 read into memory\n|; | ||
| 68 | } elsif ($in =~ m/chapter (.*)/) { | ||
| 69 | $self->feather( chapter => 'scripture', text => $1 ); | ||
| 70 | print qq|Chapter added to the scripture\n|; | ||
| 71 | } elsif ($in =~ m/feather (.*)/) { | ||
| 72 | $self->feather( text => $1 ); | ||
| 73 | print qq|Text added to the scripture\n|; | ||
| 74 | } elsif ($in eq 'exit') { | ||
| 75 | last; | ||
| 76 | } else { | ||
| 77 | print qq|Unknown options\n|; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | sub compose { | ||
| 83 | my ($self, %args) = @_; | ||
| 84 | |||
| 85 | if (! $self->{scripture} ) { | ||
| 86 | die "Please select a scripture first before composing"; | ||
| 87 | } | ||
| 88 | |||
| 89 | my ($chapter, $verse) = (0, 0); | ||
| 90 | |||
| 91 | my $scripture = ""; | ||
| 92 | for (@{ $self->{scripture} }) { | ||
| 93 | if ($_->{chapter}) { | ||
| 94 | $chapter++; | ||
| 95 | $scripture .= sprintf "Chapter %s - %s\n", $chapter, $_->{text}; | ||
| 96 | } else { | ||
| 97 | $verse++; | ||
| 98 | $scripture .= sprintf "%s:%s %s\n", $chapter, $verse, $_->{text}; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | print $scripture; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub directory { | ||
| 106 | my ($self, $path) = @_; | ||
| 107 | |||
| 108 | mkdir $path if (! -d $path); | ||
| 109 | |||
| 110 | my (@scriptures); | ||
| 111 | opendir my $dh, $path or die "Cannot open directory for reading: $!"; | ||
| 112 | for (readdir $dh) { | ||
| 113 | next if $_ =~ m/^\./; | ||
| 114 | (my $scripture = $_) =~ s/\..*$//; | ||
| 115 | push @scriptures, [$scripture, "$path/$_"] | ||
| 116 | } | ||
| 117 | closedir $dh; | ||
| 118 | return \@scriptures; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub scriptures { | ||
| 122 | my ($self) = @_; | ||
| 123 | my @scriptures; | ||
| 124 | for (@{ $self->{scriptures} }) { | ||
| 125 | push @scriptures, $_->[0]; | ||
| 126 | } | ||
| 127 | return \@scriptures; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub scripture { | ||
| 131 | my ($self, $name) = @_; | ||
| 132 | my $scripture; | ||
| 133 | for (@{ $self->{scriptures} }) { | ||
| 134 | if ($_->[0] =~ m/^$name$/) { | ||
| 135 | $scripture = $_; | ||
| 136 | last; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | if (! $scripture ) { | ||
| 141 | my $file = sprintf "%s/%s.lines", $self->{directory}, $name; | ||
| 142 | open my $fh, '>', $file or die 'Cannot open file for writing'; | ||
| 143 | print $fh ""; | ||
| 144 | close $fh; | ||
| 145 | $scripture = [ $name, $file ]; | ||
| 146 | push @{ $self->{scriptures} }, $scripture; | ||
| 147 | $self->{scripture} = []; | ||
| 148 | } | ||
| 149 | |||
| 150 | $self->{scripture_file} = $scripture; | ||
| 151 | |||
| 152 | $scripture = $self->{jsonl}->decode_file($scripture->[-1]); | ||
| 153 | |||
| 154 | $self->{scripture} = $scripture; | ||
| 155 | $self->{index} = -1; | ||
| 156 | |||
| 157 | return $scripture; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub feather { | ||
| 161 | my ($self, %args) = @_; | ||
| 162 | |||
| 163 | $self->{index}++; | ||
| 164 | |||
| 165 | if ($args{chapter}) { | ||
| 166 | splice @{ $self->{scripture} }, $self->{index}, 0, \%args; | ||
| 167 | return; | ||
| 168 | } | ||
| 169 | |||
| 170 | my %line = ( | ||
| 171 | time => time, | ||
| 172 | %args, | ||
| 173 | ); | ||
| 174 | |||
| 175 | splice @{ $self->{scripture} }, $self->{index}, 0, \%line; | ||
| 176 | |||
| 177 | return \%line; | ||
| 178 | } | ||
| 179 | |||
| 180 | sub salvage { | ||
| 181 | my ($self) = @_; | ||
| 182 | $self->{jsonl}->encode_file($self->{scripture_file}->[-1], $self->{scripture}); | ||
| 183 | return $self; | ||
| 184 | } | ||
| 185 | |||
| 186 | sub abolish { | ||
| 187 | my ($self) = @_; | ||
| 188 | splice @{ $self->{scripture} }, $self->{index}, 1; | ||
| 189 | $self->{index}--; | ||
| 190 | return $self->current(); | ||
| 191 | } | ||
| 192 | |||
| 193 | sub current { | ||
| 194 | my ($self) = @_; | ||
| 195 | $self->{index} = 0 if ($self->{index} < 0); | ||
| 196 | return $self->{scripture}->[$self->{index}]; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub next { | ||
| 200 | my ($self) = @_; | ||
| 201 | $self->{index}++; | ||
| 202 | my $next = $self->{scripture}->[$self->{index}]; | ||
| 203 | return $next; | ||
| 204 | } | ||
| 205 | |||
| 206 | sub previous { | ||
| 207 | my ($self) = @_; | ||
| 208 | $self->{index}--; | ||
| 209 | return $self->{scripture}->[$self->{index}]; | ||
| 210 | } | ||
| 211 | |||
| 212 | sub start { | ||
| 213 | my ($self) = @_; | ||
| 214 | $self->{index} = 0; | ||
| 215 | return $self->{scripture}->[$self->{index}]; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub end { | ||
| 219 | my ($self) = @_; | ||
| 220 | $self->{index} = scalar @{ $self->{scripture} } - 1; | ||
| 221 | return $self->{scripture}->[$self->{index}]; | ||
| 222 | } | ||
| 223 | |||
| 224 | 1; | ||
| 225 | |||
| 226 | __END__ | ||