Aion-Format

 view release on metacpan or  search on metacpan

lib/Aion/Format.pm  view on Meta::CPAN

	%properties = %{_extends_ddp_properties(\%properties)};
	DDP::np $arg, %properties
}

#@category Ловушки

# Ловушка для STDERR
sub trapperr(&) {
	my $sub = shift;
	local *STDERR;
	open STDERR, '>:utf8', \my $f;
	$sub->();
	close STDERR;
	utf8::decode($f) unless utf8::is_utf8($f);
	$f
}

# Ловушка для STDOUT
sub trappout(&) {
	my $sub = shift;
	local *STDOUT;
	open STDOUT, '>:utf8', \my $f;
	$sub->();
	close STDOUT;
	utf8::decode($f) unless utf8::is_utf8($f);
	$f
}

#@category Цвет

# Колоризирует текст escape-последовательностями: coloring("#{BOLD RED}ya#{}100!#RESET"), а затем - заменяет формат sprintf-ом
sub coloring(@) {
	my $s = shift;
	$s =~ s!#\{(?<x>[\w \t]*)\}|#(?<x>\w+)!
		my $x = $+{x};

lib/Aion/Format.pm  view on Meta::CPAN


	$s //= "N";
	$s =~ s/ \z//;
	$s
}

# Использованы символы из кодировки cp1251, что нужно для корректной записи в таблицы
our $CIF = join "", "0".."9", "A".."Z", "a".."z", "_-", # 64 символа для 64-ричной системы счисления
	(map chr, ord "А" .. ord "Я"), "ЁЂЃЉЊЌЋЏЎЈҐЄЇІЅ",
	(map chr, ord "а" .. ord "я"), "ёђѓљњќћџўјґєїіѕ",
	"‚„…†‡€‰‹‘’“”•–—™›¤¦§©«¬­®°±µ¶·№»",	do { no utf8; chr 0xa0 }, # небуквенные символы из cp1251
	"!\"#\$%&'()*+,./:;<=>?\@[\\]^`{|}~", # символы пунктуации ASCII
	" ", # пробел
	(map chr, 0 .. 0x1F, 0x7F), # управляющие символы ASCII
	# символ 152 (0x98) в cp1251 отсутствует.
;
# Переводит натуральное число в заданную систему счисления
sub to_radix($;$) {
	use bigint;
	my ($n, $radix) = @_;
	$radix //= 64;

lib/Aion/Format.pm  view on Meta::CPAN


sub flesch_index_human($) {
	my ($flesch_index) = @_;
	$FLESCH_INDEX_NAMES{int($flesch_index / 10) * 10} // "несвязный русский текст"
}

1;

__END__

=encoding utf-8

=head1 NAME

Aion::Format - a Perl extension for formatting numbers, coloring output, etc.

=head1 VERSION

0.1.0

=head1 SYNOPSIS

lib/Aion/Format/Html.pm  view on Meta::CPAN


	s{<(/\s*)?([a-z][a-z\d:-]*)([^<>]*)>|<!--(?:.*?)-->}{ $f->() }igse;

	$_
}

1;

__END__

=encoding utf-8

=head1 NAME

Aion::Format::Html - library for HTML formatting

=head1 SYNOPSIS

	use Aion::Format::Html;
	
	from_html "<b>&excl;</b>" # => !

lib/Aion/Format/Json.pm  view on Meta::CPAN


# Из json
sub from_json(;$) {
	$JSON->decode(@_ == 0? $_: @_)
}

1;

__END__

=encoding utf-8

=head1 NAME

Aion::Format::Json - Perl extension for JSON formatting

=head1 SYNOPSIS

	use Aion::Format::Json;
	
	to_json {a => 10}    # => {\n   "a": 10\n}\n

lib/Aion/Format/Url.pm  view on Meta::CPAN

	while($s =~ /[а-яё]+/gi) {
		my $x = $&;
		if($x =~ /^[А-ЯЁа-яё][а-яё]*$/) { $c += length $x } else { $c -= length $x }
	}
	$c
}

sub from_url_param(;$) {
	my ($param) = @_ == 0? $_: @_;

	utf8::encode($param) if utf8::is_utf8($param);

	{
		no utf8;
		use bytes;
		$param =~ tr!\+! !;
		$param =~ s!%([\da-f]{2})! chr hex $1 !iage;
	}

	eval { $param = Encode::decode_utf8($param, Encode::FB_CROAK) };

	if($@) { # видимо тут кодировка cp1251 или koi8-r
		my $cp  = Encode::decode('cp1251', $param);
		my $koi = Encode::decode('koi8-r', $param);
		# выбираем перекодировку в которой меньше больших букв внутри слова
		$param = _bohemy($koi) > _bohemy($cp)? $koi: $cp;
	}

	$param
}

lib/Aion/Format/Url.pm  view on Meta::CPAN


# Нормализует url
sub normalize_url($;$$) {
	parse_url($_[0], $_[1], $_[2])->{link}
}

1;

__END__

=encoding utf-8

=head1 NAME

Aion::Format::Url - utilities for encoding and decoding URLs

=head1 SYNOPSIS

	use Aion::Format::Url;
	
	to_url_params {a => 1, b => [[1,2],3,{x=>10}]} # => a&b[][]&b[][1]=2&b[1]=3&b[2][x]=10

lib/Aion/Format/Yaml.pm  view on Meta::CPAN


# Из yaml
sub from_yaml(;$) {
	scalar YAML::Syck::Load(@_ == 0? $_: @_)
}

1;

__END__

=encoding utf-8

=head1 NAME

Aion::Format::Yaml - converter from/to yaml

=head1 SYNOPSIS

	use Aion::Format::Yaml qw/from_yaml to_yaml/;
	
	to_yaml {foo => 'bar'} # -> "foo: bar\n"

t/aion/format.t  view on Meta::CPAN

use common::sense; use open qw/:std :utf8/;  use Carp qw//; use Cwd qw//; use File::Basename qw//; use File::Find qw//; use File::Slurper qw//; use File::Spec qw//; use File::Path qw//; use Scalar::Util qw//;  use Test::More 0.98;  use String::Diff q...
# # NAME
# 
# Aion::Format - расширение Perl для форматирования чисел, раскрашивания вывода и т.п.
# 
# # VERSION
# 
# 0.0.10
# 
# # SYNOPSIS
# 

t/aion/format/html.t  view on Meta::CPAN

use common::sense; use open qw/:std :utf8/;  use Carp qw//; use Cwd qw//; use File::Basename qw//; use File::Find qw//; use File::Slurper qw//; use File::Spec qw//; use File::Path qw//; use Scalar::Util qw//;  use Test::More 0.98;  use String::Diff q...
# # NAME
# 
# Aion::Format::Html - библиотека для форматирования HTML
# 
# # SYNOPSIS
# 
subtest 'SYNOPSIS' => sub { 
use Aion::Format::Html;

local ($::_g0 = do {from_html "<b>&excl;</b>"}, $::_e0 = "!"); ::ok $::_g0 eq $::_e0, 'from_html "<b>&excl;</b>" # => !' or ::diag ::_string_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;

t/aion/format/json.t  view on Meta::CPAN

use common::sense; use open qw/:std :utf8/;  use Carp qw//; use Cwd qw//; use File::Basename qw//; use File::Find qw//; use File::Slurper qw//; use File::Spec qw//; use File::Path qw//; use Scalar::Util qw//;  use Test::More 0.98;  use String::Diff q...
# # NAME
# 
# Aion::Format::Json - расширение Perl для форматирования JSON
# 
# # SYNOPSIS
# 
subtest 'SYNOPSIS' => sub { 
use Aion::Format::Json;

local ($::_g0 = do {to_json {a => 10}}, $::_e0 = "{\n   \"a\": 10\n}\n"); ::ok $::_g0 eq $::_e0, 'to_json {a => 10}    # => {\n   "a": 10\n}\n' or ::diag ::_string_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;

t/aion/format/url.pm  view on Meta::CPAN

use common::sense; use open qw/:std :utf8/;  use Carp qw//; use Cwd qw//; use File::Basename qw//; use File::Find qw//; use File::Slurper qw//; use File::Spec qw//; use File::Path qw//; use Scalar::Util qw//;  use Test::More 0.98;  use String::Diff q...
# 
# use common::sense;
# 
# use List::Util qw//;
# use Encode qw//;
# 
# use Exporter qw/import/;
# our @EXPORT = our @EXPORT_OK = grep {
# 	ref \$Aion::Format::Url::{$_} eq "GLOB"
# 		&& *{$Aion::Format::Url::{$_}}{CODE} && !/^(_|(NaN|import)\z)/n

t/aion/format/url.pm  view on Meta::CPAN

# 	while($s =~ /[а-яё]+/gi) {
# 		my $x = $&;
# 		if($x =~ /^[А-ЯЁа-яё][а-яё]*$/) { $c += length $x } else { $c -= length $x }
# 	}
# 	$c
# }
# 
# sub from_url_param(;$) {
# 	my ($param) = @_ == 0? $_: @_;
# 
# 	utf8::encode($param) if utf8::is_utf8($param);
# 
# 	{
# 		no utf8;
# 		use bytes;
# 		$param =~ s!\+! !g;
# 		$param =~ s!%([\da-f]{2})! chr hex $1 !age;
# 	}
# 
# 	eval { $param = Encode::decode_utf8($param, Encode::FB_CROAK) };
# 
# 	if($@) { # видимо тут кодировка cp1251 или koi8-r
# 		my $cp  = Encode::decode('cp1251', $param);
# 		my $koi = Encode::decode('koi8-r', $param);
# 		# выбираем перекодировку в которой меньше больших букв внутри слова
# 		$param = _bohemy($koi) > _bohemy($cp)? $koi: $cp;
# 	}
# 
# 	$param
# }

t/aion/format/url.pm  view on Meta::CPAN

# 
# # Нормализует url
# sub normalize_url($;$$) {
# 	parse_url($_[0], $_[1], $_[2])->{link}
# }
# 
# 1;
# 
# __END__
# 
# =encoding utf-8
# 
# =head1 NAME
# 
# Aion::Format::Url - utilities for encoding and decoding URLs
# 
# =head1 SYNOPSIS
# 
# 	use Aion::Format::Url;
# 	
# 	to_url_params {a => 1, b => [[1,2],3,{x=>10}]} # => a&b[][]&b[][]=2&b[]=3&b[][x]=10

t/aion/format/url.t  view on Meta::CPAN

use common::sense; use open qw/:std :utf8/;  use Carp qw//; use Cwd qw//; use File::Basename qw//; use File::Find qw//; use File::Slurper qw//; use File::Spec qw//; use File::Path qw//; use Scalar::Util qw//;  use Test::More 0.98;  use String::Diff q...
# # NAME
# 
# Aion::Format::Url - утилиты для кодирования и декодирования URL-адресов
# 
# # SYNOPSIS
# 
subtest 'SYNOPSIS' => sub { 
use Aion::Format::Url;

local ($::_g0 = do {to_url_params {a => 1, b => [[1,2],3,{x=>10}]}}, $::_e0 = "a&b[][]&b[][1]=2&b[1]=3&b[2][x]=10"); ::ok $::_g0 eq $::_e0, 'to_url_params {a => 1, b => [[1,2],3,{x=>10}]} # => a&b[][]&b[][1]=2&b[1]=3&b[2][x]=10' or ::diag ::_string_d...

t/aion/format/yaml.t  view on Meta::CPAN

use common::sense; use open qw/:std :utf8/;  use Carp qw//; use Cwd qw//; use File::Basename qw//; use File::Find qw//; use File::Slurper qw//; use File::Spec qw//; use File::Path qw//; use Scalar::Util qw//;  use Test::More 0.98;  use String::Diff q...
# # NAME
# 
# Aion::Format::Yaml - конвертер из/в yaml
# 
# # SYNOPSIS
# 
subtest 'SYNOPSIS' => sub { 
use Aion::Format::Yaml qw/from_yaml to_yaml/;

local ($::_g0 = do {to_yaml {foo => 'bar'}}, $::_e0 = do {"foo: bar\n"}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, 'to_yaml {foo => \'bar\'} # -> "foo: bar\n"' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;



( run in 0.313 second using v1.01-cache-2.11-cpan-9ff20fc0ed8 )