Aozora2Epub

 view release on metacpan or  search on metacpan

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

    # 半濁点付きカタカナ フ 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,
);

our %kindle_ok_font_over0xffff = map { $_ => 1 } (
    0x20d58, 0x20e97, 0x20ed7, 0x210e4, 0x2124f, 0x2296b,
    0x22d07, 0x22e42, 0x22feb, 0x233fe, 0x23cbe, 0x249ad,
    0x24e04, 0x24ff2, 0x2546e, 0x2567f, 0x259cc, 0x2688a,
    0x279b4, 0x280e9, 0x28e17, 0x29170, 0x2a2b2,
);

sub kindle_unicode_hex2chr {
    my $unicode_hex = shift;
    my $unicode = hex($unicode_hex);

    return if $kindle_broken_font_unicode{$unicode};

    # kindle font is almost not avaliable in this range.
    return if $unicode > 0xffff && !$kindle_ok_font_over0xffff{$unicode};

    return chr($unicode);
}

sub _conv_gaiji_title_author {
    my ($unicode, $men, $ku, $ten) = @_;
    if ($unicode) {
        my $ch = kindle_unicode_hex2chr($unicode);
        return $ch if $ch;
        return;
    }
    my $ch = kindle_jis2chr(0+$men, 0+$ku, 0+$ten);
    return $ch if $ch;
    return;
}

sub conv_gaiji_title_author {
    my $s = shift;

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

            $style =~ s/margin-right/margin-bottom/sg;
            $div->attr('style', $style);
        })
        ->process('span.notes', sub {
            my $span = shift;
            my $note = $span->as_text;
            return unless $note =~ m{[#[^\]]+?、([^\]]+)]};
            my $desc = $1;
            my $ch = do {
                if ($desc =~ /U\+([A-fa-f0-9]+)/) {
                    kindle_unicode_hex2chr($1);
                } elsif ($desc =~ /第\d水準(\d)-(\d+)-(\d+)/) {
                    kindle_jis2chr(0+$1, 0+$2, 0+$3);
                }
            };
            return unless $ch;

            # find nearest ※ and replace it to $ch
            my $left = $span->left;
            unless ($left->isa('HTML::Element')) {
                if ($left =~ s/※$/$ch/) {

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

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 {
    my $s = shift;
    $s =~ s|\\x\{([0-9a-fA-F]+)\}|chr(hex($1))|esg;
    return $s;
}

filters {
    html => 'chomp',
    expected => ['chomp', 'eval_unicode_notation'],
};

run {
    my $block = shift;
    Aozora2Epub::Gensym->reset_counter;

    my $doc = Aozora2Epub->new($block->html, no_fetch_assets=>1);
    my $got = join('', map { $_->as_html } @{$doc->files});
    is_deeply($got, $block->expected, $block->name);
};

__DATA__

=== simple unicode
--- html
※<span class="notes">[#「てへん+去」、U+62BE、369-2]</span>
--- expected
\x{62be}

=== non gaiji note
--- html
あああ<span class="notes">[# あああはママ]</span>
--- expected
あああ<span class="notes">[# あああはママ]</span>

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

<img src="../../../gaiji/2-15/2-15-73.png" />
--- expected
<img src="../gaiji/2-15/2-15-73.png" />

=== kindle font broken jis 3
--- html
<img src="../../../gaiji/1-06/1-06-88.png" />
--- expected
<img src="../gaiji/1-06/1-06-88.png" />

=== kindle font broken unicode
--- html
※<span class="notes">[#「あああ」、U+2152、369-2]</span>
--- expected
※<span class="notes">[#「あああ」、U+2152、369-2]</span>

=== kindle font broken unicode 2
--- html
※<span class="notes">[#「あああ」、U+2189、369-2]</span>
--- expected
※<span class="notes">[#「あああ」、U+2189、369-2]</span>

=== kindle font broken unicode 3
--- html
※<span class="notes">[#「あああ」、U+26BD、369-2]</span>
--- expected
※<span class="notes">[#「あああ」、U+26BD、369-2]</span>

=== kindle font broken unicode 4
--- html
※<span class="notes">[#「あああ」、U+26BE、369-2]</span>
--- expected
※<span class="notes">[#「あああ」、U+26BE、369-2]</span>

=== kindle font broken unicode 5
--- html
※<span class="notes">[#「あああ」、U+3244、369-2]</span>
--- expected
※<span class="notes">[#「あああ」、U+3244、369-2]</span>

=== kindle font broken unicode over 0xffff
--- html
※<span class="notes">[#「あああ」、U+1F130、369-2]</span>
--- expected
※<span class="notes">[#「あああ」、U+1F130、369-2]</span>

=== kindle font broken unicode over 0xffff but ok
--- html
※<span class="notes">[#「あああ」、U+2a2b2、369-2]</span>
--- expected
\x{2a2b2}

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

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;
    $s =~ s|\\x\{([0-9a-fA-F]+)\}|chr(hex($1))|esg;
    return $s;
}

filters {
    input => 'chomp',
    expected => ['chomp', 'eval_unicode_notation'],
};

run {
    my $block = shift;

    my $got = Aozora2Epub::XHTML::conv_gaiji_title_author($block->input);
    is $got, $block->expected, $block->name;
};

__DATA__

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

歌 ※[#ローマ数字1、1-13-21]・※[#ローマ数字2、1-13-22]
--- expected
歌 \x{2160}・\x{2161}

=== not kome
--- input
(2[#「2」はローマ数字、1-13-22])
--- expected
(\x{2161})

=== unicode
--- input
たま※[#「ころもへん+攀」、U+897B]
--- expected
たま\x{897b}

=== unicode bad font
--- input
失※[#「人がしら/二/心」、U+2B779、表紙]術講義
--- expected
失※[#「人がしら/二/心」、U+2B779、表紙]術講義
--- note
2b779はkindleだと豆腐になる

=== no chuuki
--- input
あいうえお



( run in 0.323 second using v1.01-cache-2.11-cpan-88abd93f124 )