| 1 | package PerlIO::via::PrepareCP1251; | ||||||||||
| 2 | 1 | $PerlIO::via::PrepareCP1251::VERSION = '0.02'; | |||||||||
| 3 | # $Id$ | ||||||||||
| 4 | # ABSTRACT: prepare unicode stream to be encoded as cp1251 | ||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | use charnames ':full'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 1 | ▶ | use Unicode::Normalize; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | |||||||||||
| 16 | |||||||||||
| 17 | |||||||||||
| 18 | 1 | our $INCOMPATIBLE_CHAR_MODE = 'default_char'; | |||||||||
| 19 | 1 | our $DEFAULT_CHAR = q{?}; | |||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | 1 | our %CMAP = ( | |||||||||
| 23 | |||||||||||
| 24 | # Kazakh | ||||||||||
| 25 | |||||||||||
| 26 | "\x{04D8}" => "\x{0410}", # SCHWA | ||||||||||
| 27 | "\x{04D9}" => "\x{0430}", | ||||||||||
| 28 | "\x{0492}" => "\x{0413}", # GHE WITH STROKE | ||||||||||
| 29 | "\x{0493}" => "\x{0433}", | ||||||||||
| 30 | "\x{04A2}" => "\x{041D}", # EN WITH DESCENDER | ||||||||||
| 31 | "\x{04A3}" => "\x{043D}", | ||||||||||
| 32 | "\x{04AE}" => "\x{0423}", # STRAIGHT U | ||||||||||
| 33 | "\x{04AF}" => "\x{0443}", | ||||||||||
| 34 | "\x{04B0}" => "\x{0423}", # STRAIGHT U WITH STROKE | ||||||||||
| 35 | "\x{04B1}" => "\x{0443}", | ||||||||||
| 36 | "\x{04BA}" => "\x{0425}", # SHHA | ||||||||||
| 37 | "\x{04BB}" => "\x{0445}", | ||||||||||
| 38 | "\x{049A}" => "\x{041A}", # KA WITH DESCENDER | ||||||||||
| 39 | "\x{049B}" => "\x{043A}", | ||||||||||
| 40 | "\x{04E8}" => "\x{041E}", # BARRED O | ||||||||||
| 41 | "\x{04E9}" => "\x{043E}", | ||||||||||
| 42 | ); | ||||||||||
| 43 | |||||||||||
| 44 | |||||||||||
| 45 | 1 | _init(); | |||||||||
| 46 | 1 | 1; | |||||||||
| 47 | |||||||||||
| 48 | |||||||||||
| 49 | { | ||||||||||
| 50 | 1 | my %codepage; | |||||||||
| 51 | |||||||||||
| 52 | 1 | ▶ | sub _init { | ||||||||
|
Subroutine
_init: called CC=7 SCAR=19.5
| |||||||||||
| 53 | |||||||||||
| 54 | # load cp1251 to unicode table | ||||||||||
| 55 | 1 | ▶ | while ( <DATA> ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 275 | ▶ | next unless my ( $code, $ucode ) = m{ 0x ([0-9A-F]{2}) \s+ 0x ([0-9A-F]{4}) }xms; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 255 | $codepage{ chr hex $ucode } = chr hex $code; | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | # remove diacritics | ||||||||||
| 61 | 1 | for my $codepoint ( 0x000 .. 0x2FFF ) { | |||||||||
| 62 | 12288 | my $chr = chr $codepoint; | |||||||||
| 63 | 12288 | ▶ | next if exists $codepage{$chr}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 12033 | ▶ | next if exists $CMAP{$chr}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | |||||||||||
| 66 | 12017 | my $nfd = substr NFD( $chr ), 0, 1; | |||||||||
| 67 | 12017 | ▶ | next unless exists $codepage{$nfd}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | |||||||||||
| 69 | 530 | $CMAP{$chr} = $nfd; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 1 | return; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | |||||||||||
| 76 | 10 | ▶ | sub _convert_symbol { | ||||||||
|
Subroutine
_convert_symbol: called CC=7 SCAR=19.7
| |||||||||||
| 77 | 10 | my ($char) = @_; | |||||||||
| 78 | |||||||||||
| 79 | 10 | ▶ | return $char if exists $codepage{$char}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 8 | ▶ | return $CMAP{$char} if exists $CMAP{$char}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 3 | ▶ | return q{} if $INCOMPATIBLE_CHAR_MODE eq 'skip'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 2 | ▶ | return $DEFAULT_CHAR if $INCOMPATIBLE_CHAR_MODE eq 'default_char'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 1 | ▶ | return $char if $INCOMPATIBLE_CHAR_MODE eq 'pass'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | |||||||||||
| 85 | 1 | ▶ | if ( my $name = charnames::viacode(ord $char) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 1 | return "\\N{$name}"; | |||||||||
| 87 | } | ||||||||||
| 88 | 0 | return sprintf '\x{%04x}', ord $char; | |||||||||
| 89 | } | ||||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | |||||||||||
| 93 | |||||||||||
| 94 | 1 | ▶ | sub PUSHED { | ||||||||
|
Subroutine
PUSHED: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 95 | 7 | my ($class) = @_; | |||||||||
| 96 | 7 | return bless \*PUSHED, $class; | |||||||||
| 97 | }; | ||||||||||
| 98 | |||||||||||
| 99 | |||||||||||
| 100 | |||||||||||
| 101 | 7 | ▶ | sub UTF8 { | ||||||||
|
Subroutine
UTF8: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 102 | 7 | 1; | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | |||||||||||
| 106 | |||||||||||
| 107 | 7 | ▶ | sub WRITE { | ||||||||
|
Subroutine
WRITE: called CC=2 SCAR=7.0
| |||||||||||
| 108 | 7 | my ( undef, $line, $handle ) = @_; | |||||||||
| 109 | 7 | utf8::decode $line; | |||||||||
| 110 | my $out = join q{}, | ||||||||||
| 111 | 7 | map { _convert_symbol($_) } | |||||||||
| 112 | split m//, $line; | ||||||||||
| 113 | 7 | ▶ | return ( print {$handle} $out ) ? length($line) : -1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | =pod | ||||||||||
| 117 | |||||||||||
| 118 | =encoding UTF-8 | ||||||||||
| 119 | |||||||||||
| 120 | =head1 NAME | ||||||||||
| 121 | |||||||||||
| 122 | PerlIO::via::PrepareCP1251 - prepare unicode stream to be encoded as cp1251 | ||||||||||
| 123 | |||||||||||
| 124 | =head1 VERSION | ||||||||||
| 125 | |||||||||||
| 126 | version 0.02 | ||||||||||
| 127 | |||||||||||
| 128 | =head1 SYNOPSIS | ||||||||||
| 129 | |||||||||||
| 130 | require PerlIO::via::PrepareCP1251; | ||||||||||
| 131 | |||||||||||
| 132 | # note the filter order | ||||||||||
| 133 | open my $fh, '>:encoding(cp1251):via(PrepareCP1251)', $filename; | ||||||||||
| 134 | print {$fh} $unicode_string; | ||||||||||
| 135 | close $fh; | ||||||||||
| 136 | |||||||||||
| 137 | =head2 $INCOMPATIBLE_CHAR_MODE | ||||||||||
| 138 | |||||||||||
| 139 | Behaviour for incompatible chars: | ||||||||||
| 140 | |||||||||||
| 141 | pass - put original symbol | ||||||||||
| 142 | default_char - put $DEFAULT_CHAR | ||||||||||
| 143 | skip - don't put anything | ||||||||||
| 144 | charname - put \N{charname} | ||||||||||
| 145 | |||||||||||
| 146 | =head1 PerlIO::via interface | ||||||||||
| 147 | |||||||||||
| 148 | =head2 PUSHED | ||||||||||
| 149 | |||||||||||
| 150 | Constructor | ||||||||||
| 151 | |||||||||||
| 152 | =head2 UTF8 | ||||||||||
| 153 | |||||||||||
| 154 | Require utf8 stream | ||||||||||
| 155 | |||||||||||
| 156 | =head2 WRITE | ||||||||||
| 157 | |||||||||||
| 158 | Filter text | ||||||||||
| 159 | |||||||||||
| 160 | =head1 AUTHOR | ||||||||||
| 161 | |||||||||||
| 162 | liosha <liosha@cpan.org> | ||||||||||
| 163 | |||||||||||
| 164 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 165 | |||||||||||
| 166 | This software is copyright (c) 2015 by liosha. | ||||||||||
| 167 | |||||||||||
| 168 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 169 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 170 | |||||||||||
| 171 | =cut | ||||||||||
| 172 | |||||||||||
| 173 | __DATA__ | ||||||||||