Skip to content

Commit

Permalink
Add command line switches for hyphenation
Browse files Browse the repository at this point in the history
  • Loading branch information
mdom committed Apr 7, 2019
1 parent 75194db commit 2046d4a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 26 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,19 @@ will be saved and restored.

Many text movement commands are compatible with _less(1)_.

If the optional library [Text::Hyphen](https://metacpan.org/pod/Text::Hyphen) is installed, words will be
devided to fit more evenly on the screen. Currently only english
hyphenation rules are supported. This will likely change in the future.
The text will be hyphenated if the hyphenation patterns from hunspells
libhyphen are installed.

# OPTIONS

- --\[no-\]hyphenation

Hyphenate text. Defaults to true.

- --lang LANGUAGE\_TAG

Set the language used for hyphenation. Defaults to the books language or
'en-US' if not specified.

# KEY BINDINGS

Expand Down
30 changes: 25 additions & 5 deletions lib/App/termpub.pm
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
package App::termpub;
use Mojo::Base 'App::termpub::Pager::HTML';
use Mojo::Util 'decode';
use Mojo::Util 'decode', 'getopt';
use Mojo::URL;
use Mojo::File 'tempfile';
use Mojo::JSON 'encode_json', 'decode_json';
use App::termpub::Hyphen;
use App::termpub::Epub;
use Curses;

our $VERSION = '1.04';

has 'epub';
has epub => sub {
my $self = shift;
App::termpub::Epub->new( filename => $self->filename );
};
has chapters => sub { shift->epub->chapters };
has chapter => sub { shift->epub->start_chapter };
has history => sub { [ shift->chapter ] };
has history_index => 0;

has hyphenation => 1;
has language => 'en-US';
has 'filename';

has hyphenator => sub {
my $self = shift;
my $lang = $self->epub->language || 'en-US';
my $h = App::termpub::Hyphen->new( lang => $lang );
return if !$self->hyphenation;
my $lang = $self->epub->language || $self->language;
my $h = App::termpub::Hyphen->new( lang => $lang );
return if !$h->installed;
return $h;
};

sub load_config {
my ( $self, $argv ) = @_;
my $handler = sub { my ( $n, $v ) = @_; $self->$n($v) };
local $SIG{__WARN__} = sub { die @_ };
getopt( $argv, 'language|l=s' => $handler, 'hyphenation!' => $handler );
die "Missing filename for epub.\n" if !$argv->[0];
$self->filename( $argv->[0] );
}

sub run {
my $self = shift;
my ( $self, $argv ) = @_;

$self->load_config($argv);

$self->title( $self->chapters->[ $self->chapter ]->title );

Expand Down
36 changes: 18 additions & 18 deletions script/termpub
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,14 @@ use Curses;

use lib 'lib';
use App::termpub;
use App::termpub::Epub;
use Pod::Usage;

initscr;
noecho;
cbreak;
curs_set(0);

my $filename = shift;

if ( !$filename ) {
endwin;
pod2usage(
-message => 'Missing file',
-exitstatus => 1,
-verbose => 0,
);
}

my $epub = App::termpub::Epub->new( filename => $filename );

eval { App::termpub->new( epub => $epub )->run };
eval { App::termpub->new->run( \@ARGV ) };

if ($@) {
endwin;
Expand Down Expand Up @@ -64,9 +50,23 @@ will be saved and restored.
Many text movement commands are compatible with I<less(1)>.
If the optional library L<Text::Hyphen> is installed, words will be
devided to fit more evenly on the screen. Currently only english
hyphenation rules are supported. This will likely change in the future.
The text will be hyphenated if the hyphenation patterns from hunspells
libhyphen are installed.
=head1 OPTIONS
=over 4
=item --[no-]hyphenation
Hyphenate text. Defaults to true.
=item --lang LANGUAGE_TAG
Set the language used for hyphenation. Defaults to the books language or
'en-US' if not specified.
=back
=head1 KEY BINDINGS
Expand Down

0 comments on commit 2046d4a

Please sign in to comment.