Aozora2Epub

 view release on metacpan or  search on metacpan

lib/Aozora2Epub.pm  view on Meta::CPAN

package Aozora2Epub;
use utf8;
use strict;
use warnings;
use Aozora2Epub::Gensym;
use Aozora2Epub::CachedGet qw/http_get/;
use Aozora2Epub::Epub;
use Aozora2Epub::XHTML;
use Path::Tiny;
use URI;
use HTML::Escape qw/escape_html/;

lib/Aozora2Epub.pm  view on Meta::CPAN

    return $base;
}

sub _get_file {
    my $url_or_path = "" . shift; # force to string.

    if ($url_or_path =~ m{^https?://}) {
        return http_get($url_or_path);
    }
    if ($url_or_path =~ m{\.html$}) {
        return path($url_or_path)->slurp_utf8;
    }
    return path($url_or_path)->slurp_raw;
}

sub _get_content {
    my $xhtml = shift;
    if ($xhtml =~ m{/card\d+\.html$}) { # 図書カード
        unless ($xhtml =~ m{^https?://}) { # $xhtml shuld be \d+/card\d+.html
            $xhtml = "$AOZORA_CARDS_URL/$xhtml";
        }

lib/Aozora2Epub/CachedGet.pm  view on Meta::CPAN

package Aozora2Epub::CachedGet;
use strict;
use warnings;
use utf8;
use Carp qw/croak/;
use HTTP::Tiny;
use Cache::FileCache;
use Encode qw/decode/;
use Path::Tiny;
use File::HomeDir;
use parent 'Exporter';

our @EXPORT = qw(http_get);

lib/Aozora2Epub/Epub.pm  view on Meta::CPAN

package Aozora2Epub::Epub;
use strict;
use warnings;
use utf8;
use File::ShareDir qw(dist_dir);
use Path::Tiny;
use Text::Xslate qw/mark_raw/;
use UUID qw/uuid/;
use HTTP::Date qw/time2isoz/;
use File::Find qw//;
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
use Aozora2Epub::CachedGet;
use Aozora2Epub::Gensym;
use base qw/Class::Accessor/;

lib/Aozora2Epub/Epub.pm  view on Meta::CPAN

}

sub copy {
    my ($self, $file) = @_;
    my $dest = $self->dest_file($file);
    path($self->{sharedir}, $file)->copy($dest);
}

sub slurp {
    my ($self, $file) = @_;
    return path($self->{sharedir}, $file)->slurp_utf8;
}

sub render_to {
    my ($self, $template, $to, $args) = @_;
    my $dest = $self->dest_file($to);
    my $text = $self->{xslate}->render_string($self->slurp($template), $args);
    $dest->spew_utf8($text);
}

sub render {
    my ($self, $template, $args) = @_;
    $self->render_to($template, $template, $args);
}

sub write_string {
    my ($self, $bin, $to) = @_;
    my $dest = $self->dest_file($to);

lib/Aozora2Epub/File.pm  view on Meta::CPAN

package Aozora2Epub::File;
use strict;
use warnings;
use utf8;
use Aozora2Epub::Gensym;
use HTML::Element;
use base qw(Class::Accessor);
__PACKAGE__->mk_accessors(qw/content name/);

our $VERSION = '0.05';

sub new {
    my ($class, $content) = @_;
    return bless {

lib/Aozora2Epub/Gensym.pm  view on Meta::CPAN

package Aozora2Epub::Gensym;
use strict;
use warnings;
use utf8;
use base qw/Exporter/;
our @EXPORT = qw(gensym);

our $VERSION = '0.05';

my $gensym_counter = 0;

sub gensym { sprintf("g%09d", $gensym_counter++); }

sub reset_counter {

lib/Aozora2Epub/XHTML.pm  view on Meta::CPAN

package Aozora2Epub::XHTML;
use strict;
use warnings;
use utf8;
use Aozora2Epub::CachedGet qw/http_get/;
use Aozora2Epub::XHTML::Tree;
use Aozora2Epub::Gensym;
use Aozora2Epub::File;
use HTML::Element;
use Encode::JISX0213;
use Encode qw/decode/;
use base qw(Class::Accessor);
__PACKAGE__->mk_accessors(qw/title subtitle author
                             contents
                             bib_info notation_notes gaiji fig/);

our $VERSION = "0.05";

sub jisx0213_to_utf8 {
    my ($men, $ku, $ten) = @_;
    $ku += 0xa0;
    $ten += 0xa0;
    my $euc = join('', ($men == 2 ? chr(0x8f) : ()),
                   chr($ku), chr($ten));
    my $utf8 = decode('euc-jp-2004', $euc);
    return $utf8;
}

sub kindle_jis2chr {
    my ($men, $ku, $ten) = @_;

    # 半濁点付きカタカナ フ kindleだと2文字に見えるのなんとかならんか?
    return if $men == 1 && $ku == 6 && $ten == 88;

    # kindle font of these characters are broken.
    return if $men == 1 && $ku == 90 && $ten == 61;
    return if $men == 2 && $ku == 15 && $ten == 73;
    return jisx0213_to_utf8($men, $ku, $ten);
}

# kindle font of these characters are broken.
our %kindle_broken_font_unicode = map { $_ => 1 } (
    0x2152,
    0x2189,
    0x26bd,
    0x26be,
    0x3244,
);

lib/Aozora2Epub/XHTML/Tree.pm  view on Meta::CPAN

package Aozora2Epub::XHTML::Tree;
use strict;
use warnings;
use utf8;
use HTML::TreeBuilder::XPath;
use HTML::Selector::XPath qw/selector_to_xpath/;

our $VERSION = '0.05';

sub new {
    my ($class, $str) = @_;

    my $tree= HTML::TreeBuilder::XPath->new;
    $tree->ignore_unknown(0);

script/aozora2epub  view on Meta::CPAN

#!perl
use utf8;
use strict;
use warnings;
use Aozora2Epub;
use Getopt::Long;
use Path::Tiny;

our $VERSION = '0.03';

binmode(STDERR, "utf8");
binmode(STDOUT, "utf8");

my $cmd_name = path($0)->basename;

my ($output, $title, $author, $cover, $no_okuzuke, $use_subtitle);
Getopt::Long::Configure ("bundling");
GetOptions("o|output=s" => \$output,
           "t|title=s" => \$title,
           "a|author=s" => \$author,
           "use-subtitle" => \$use_subtitle,
           "c|cover=s" => \$cover,

t/Util.pm  view on Meta::CPAN

package t::Util;
use strict;
use warnings;
use utf8;
use Path::Tiny;
use File::ShareDir;
use Test::More;

$File::ShareDir::DIST_SHARE{'Aozora2Epub'} = path('share')->absolute;

binmode(STDERR, "utf8");
binmode(STDOUT, "utf8");
{
    # utf8 hack from Amon2
    binmode Test::More->builder->$_, ":utf8" for qw/output failure_output todo_output/;
    no warnings 'redefine';
    my $code = \&Test::Builder::child;
    *Test::Builder::child = sub {
        my $builder = $code->(@_);
        binmode $builder->output,         ":utf8";
        binmode $builder->failure_output, ":utf8";
        binmode $builder->todo_output,    ":utf8";
        return $builder;
    };
}

1;

t/files.t  view on Meta::CPAN

use strict;
use warnings;
use utf8;
use Test::More;
use Test::Base;
use Aozora2Epub;
use Aozora2Epub::Gensym;
use lib qw/./;
use t::Util;

plan tests => 1 * blocks;

sub drop_nlsp {

t/gaiji-replace.t  view on Meta::CPAN

use strict;
use warnings;
use utf8;
use Test::More;
use Test::Base;
use Aozora2Epub;
use Aozora2Epub::Gensym;
use lib qw/./;
use t::Util;

plan tests => 1 * blocks;

sub eval_unicode_notation {

t/gaiji-title-author.t  view on Meta::CPAN

use strict;
use warnings;
use utf8;
use Test::More;
use Test::Base;
use Aozora2Epub::XHTML;
use lib qw/./;
use t::Util;

plan tests => 1 * blocks;

sub eval_unicode_notation {
    my $s = shift;

t/okuzuke.t  view on Meta::CPAN

use strict;
use warnings;
use utf8;
use Test::More;
use Test::Base;
use Aozora2Epub::XHTML;
use Aozora2Epub::Gensym;
use lib qw/./;
use t::Util;

sub drop_nlsp {
    my $s = shift;
    $s =~ s/\n *//sg;

t/toc.t  view on Meta::CPAN

use strict;
use warnings;
use utf8;
use Test::More;
use Test::Base;
use Aozora2Epub;
use Aozora2Epub::Gensym;
use lib qw/./;
use t::Util;

sub drop_nlsp {
    my $s = shift;
    $s =~ s/\n *//sg;

xt/ZipDiff.pm  view on Meta::CPAN

package xt::ZipDiff;
use strict;
use warnings;
use utf8;
use Path::Tiny;

sub new {
    my $class = shift;

    my $workdir = Path::Tiny->tempdir();
    #my $workdir = path("./diff_tmp")->absolute;
    return bless { workdir => $workdir }, $class;
}

xt/ZipDiff.pm  view on Meta::CPAN

                         "mkdir got",
                         "mkdir expected",
                         "cd got",
                         "unzip -qq -x $got",
                         "cd ../expected",
                         "unzip -qq -x $abs_expected",
                         "cd ..",
                         "diff -r got expected",
                         "true");
    #print STDERR $shell_cmd, "\n";
    open my $diffout, "-|:utf8", $shell_cmd
        or die "can't exec shell command: $!";
    my @output;
    while (<$diffout>) {
        chomp;
        next if is_ok_pattern($_);
        push @output, "$_";
    }
    return join("\n", @output);
}

xt/epub.t  view on Meta::CPAN

use strict;
use warnings;
use utf8;
use Test::More;
use Aozora2Epub;
use lib qw/./;
use xt::ZipDiff;
use t::Util;
use Path::Tiny;
use Aozora2Epub::Gensym;

{
    local($Aozora2Epub::AOZORA_CARDS_URL) = 'xt/input';

xt/gappon.t  view on Meta::CPAN

use strict;
use warnings;
use utf8;
use Test::More;
use Aozora2Epub;
use lib qw/./;
use xt::ZipDiff;
use t::Util;
use Path::Tiny;
use Aozora2Epub::Gensym;

{
    local($Aozora2Epub::AOZORA_CARDS_URL) = 'xt/input';

xt/live.t  view on Meta::CPAN

use strict;
use warnings;
use utf8;
use Test::More;
use Aozora2Epub;
use Aozora2Epub::CachedGet;
use lib qw/./;
use t::Util;
use Path::Tiny;

plan skip_all => "LIVE_TEST not enabled" unless $ENV{LIVE_TEST};

sub dotest {



( run in 1.139 second using v1.01-cache-2.11-cpan-49f99fa48dc )