Aion-Format

 view release on metacpan or  search on metacpan

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


	to_radix 10_000 				# => 2SG
	to_radix 10_000, 64 			# => 2SG
	to_radix 10_000, 255 			# => dt
	eval { to_radix 0, 256 }; $@ 	# ~> The number system 256 is too large. Use NS before 256

=head2 kb_size ($number)

Adds numeric digits and adds a unit of measurement.

	kb_size 102             # => 102b
	kb_size 1024            # => 1k
	kb_size 1023            # => 1\x{a0}023b
	kb_size 1024*1024       # => 1M
	kb_size 1000_002_000_001_000    # => 931\x{a0}324G

=head2 replace ($subject, @rules)

Multiple text transformations in one pass.

	my $s = replace "33*pi",
	    qr/(?<num> \d+)/x   => sub { "($+{num})" },
	    qr/\b pi \b/x       => sub { 3.14 },
	    qr/(?<op> \*)/x     => sub { " $& " },
	;
	
	$s # => (33) * 3.14

=head2 matches ($subject, @rules)

Synonym for C<replace>. B<DEPRECATED>.

	my $s = matches "33*pi",
	    qr/(?<num> \d+)/x   => sub { "($+{num})" },
	    qr/\b pi \b/x       => sub { 3.14 },
	    qr/(?<op> \*)/x     => sub { " $& " },
	;
	
	$s # => (33) * 3.14

=head2 nous ($templates)

A simplified regular expression language for text recognition in HTML documents.

=over

=item 1. Removes all spaces at the beginning and end.

=item 2. From the beginning of each line, 4 spaces or 0-3 spaces and a tab are removed.

=item 3. Spaces at the end of a line and strings of spaces are replaced with C<\s*>.

=item 4. All variables in C<{{ var }}> are replaced with C<.*?>. Those. everything is recognized.

=item 5. All variables in C<< {{E<gt> var }} >> are replaced with C<< [^E<lt>E<gt>]*? >>. Those. HTML tags are not recognized.

=item 6. All variables in C<{{: var }}> are replaced with C<[^\n]*>. Those. must be on one line.

=item 7. Expressions in double square brackets (C<[[ ... ]]>) may not exist.

=item 8. Double brackets (C<(( ... ))> are used as parentheses.

=item 9. C<||> - or.

=back

	my $re = nous [
	q{
		<body>
		<center>
		<h2><a href={{> author_link }}>{{: author_name }}</a><br>
		{{ title }}</h2>
	},
	q{
	    <li><A HREF="{{ comments_link }}">((Comments: {{ comments }}, last from {{ last_comment_posted }}.||Added comment))</A>
		<li><a href="{{ author_link }}">{{ author_name }}</a>
		[[ (translate: {{ interpreter_name }})]]
		 (<u>{{ author_email }}</u>) 
		<li>Year: {{ posted }}
	},
	q{
		<li><B><font color=#393939>Annotation:</font></b><br><i>{{ annotation_html }}</i></ul>
		</ul></font>
		</td></tr>
	},
	q{
		<!----------- The work itself --------------->
		{{ html }}
		<!------------------------------------------->
	},
	];
	
	my $s = q{
	<body>
	<center>
	<h2><a href=/to/book/link>A. Alis</a><br>
	Grivus campf</h2>
	
	Any others...
	
	<!----------- The work itself --------------->
	This book text!
	<!------------------------------------------->
	};
	
	$s =~ $re;
	my $result = {%+};
	$result # --> {author_link => "/to/book/link", author_name => "A. Alis", title => "Grivus campf"}

=head2 num ($number)

Adds separators between digits of a number.

	num +0         # => 0
	num -1000.3    # => -1 000.3

The default separator is a non-breaking space. Set the separator and decimal point the same way:

	num [1000, "#"]         		# => 1#000
	num [-1000.3003003, "_", ","]   # => -1_000,3003003



( run in 1.229 second using v1.01-cache-2.11-cpan-2398b32b56e )