akiym/Acme-Collector64 - GitHub
オリジナルのBase64を作ることができるモジュールです。
使い方はこんな感じです。
use strict;
use warnings;
use utf8;
use Acme::Collector64;
my $index_table = 'あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもらりるれろがぎぐげござじずぜぞばびぶべぼぱぴぷぺぽやゆよわ=';
my $japanese64 = Acme::Collector64->new(
index_table => $index_table,
);
$japanese64->encode('Perl'); #=> なきにばふあ==
$japanese64->decode('ぴぴさこぴぴきたぴぴきむ'); #=> らくだこれだけでは物足りないので、Mojolicious::LiteでAcme::Collector64アプリを作りました。
use utf8;
use Mojolicious::Lite;
use Acme::Collector64;
use Encode;
get '/' => 'index';
post '/c64' => sub {
my $self = shift;
my $data = $self->param('data');
my $index_table = $self->param('index_table');
die if length($index_table) != 0 && length($index_table) != 65;
my $type = $self->param('type');
my $encoding = $self->param('encoding');
die unless $encoding =~ /(?:utf8|shiftjis|euc-jp|iso-2022-jp)/;
my $c64 = Acme::Collector64->new(index_table => $index_table);
given ($type) {
when ('encode') {
return $self->render_text(
$c64->encode(encode($encoding, decode_utf8($data)))
);
}
when ('decode') {
return $self->render_text(
decode($encoding, $c64->decode($data))
);
}
default {
die;
}
}
};
app->start;
__DATA__
@@ c64.js
$(function () {
$('#index_table').focus().keyup(function () {
var index_table = $(this);
if (index_table.val().length != 65) {
index_table.css('color', '#d23d24');
} else {
index_table.css('color', '#55b05a');
}
});
$('#index_table').ready(function () {
$('#index_table').keyup();
});
$('#encode').focus().keyup(function () {
var data = $(this).val();
var index_table = $('#index_table').val();
var encoding = $('#encoding').val();
$.ajax({
type: 'POST',
url: '/c64',
data: {data: data, index_table: index_table, type: 'encode', encoding: encoding},
success: function (data) {
$('#decode').val(data);
}
});
});
$('#decode').focus().keyup(function () {
var data = $(this).val();
var index_table = $('#index_table').val();
var encoding = $('#encoding').val();
$.ajax({
type: 'POST',
url: '/c64',
data: {data: data, index_table: index_table, type: 'decode', encoding: encoding},
success: function (data) {
$('#encode').val(data);
}
});
});
$('#encoding').change(function () {
$('#encode').keyup();
});
});
@@ c64.css
#index_table {
width: 760px;
}
#encode, #decode {
width: 760px;
font-size: 130%;
}
#encode {
height: 150px;
}
#decode {
height: 300px;
}
dt {
clear: left;
float: left;
font-weight: bold;
width: 120px;
padding: 10px 5px;
}
dd {
margin-left: 0;
padding: 10px 5px;
width: 900px;
}
dd.index_table {
border-bottom: 1px solid #eee;
margin-bottom: 30px;
padding-bottom: 30px;
}
@@ index.html.ep
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="c64.js"></script>
<link rel="stylesheet" type="text/css" href="c64.css" />
</head>
<body>
<dl>
<dt>index_table</dt>
<dd class="index_table">
<input type="text" id="index_table" value="あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもらりるれろがぎぐげござじずぜぞばびぶべぼぱぴぷぺぽやゆよわ=" />
</dd>
<dt>
encode
<select id="encoding">
<option name="encoding" value="utf8" selected="selected">UTF-8</option>
<option name="encoding" value="shiftjis">Shift_JIS</option>
<option name="encoding" value="euc-jp">EUC-JP</option>
<option name="encoding" value="iso-2022-jp">ISO-2022-JP</option>
</select>
</dt>
<dd><textarea id="encode"></textarea></dd>
<dt>decode</dt>
<dd><textarea id="decode"></textarea></dd>
</dl>
<ul>
<li>鯨鮮魯鮎鯉鯛鰯鱗鱒鯵鰺鮒鮪鮫鮭鯖鰍鰐鰭鰹鰻鱈鯔鰡鰕鰥鱶魴鱚鮃鰤鮑鮓鮖鮗鮟鮠鮨鱸鮴鱇鮹鯀鯊鰰鯏鯑鱠鯒鯡鯢鯣鯤鯰鯱鯲鰄鰆鰈鰉鰊鰌鰒鰓魚</li>
<li>絵文字 Unicode 6.0</li>
</ul>
</body>
</html>個人的にこのモジュールは気に入っているので、いつかCPANにあげたいなーと思っています。
参考: Base64を再実装する