Skip to content

Commit

Permalink
added unindent function and method
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 25, 2014
1 parent bccb911 commit 912902f
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 11 deletions.
5 changes: 4 additions & 1 deletion Changes
@@ -1,13 +1,16 @@

4.70 2014-01-25
4.70 2014-01-26
- Added extract_usage method to Mojolicious::Command.
- Added unindent method to Mojo::ByteStream.
- Added unindent function to Mojo::Util.
- Updated jQuery to version 2.1.
- Improved all built-in commands to show usage information in their SYNOPSIS
sections.
- Improved tag helpers to make data attributes more convenient. (ravengerUA)
%= tag 'div', data => {my_id => 1, Name => 'test'} => 'some content'
is equivalent to
%= tag 'div', data-my-id => 1, data-name => 'test' => 'some content'
- Fixed indentation of code in documentation browser.

4.69 2014-01-24
- Improved router to allow format detection for bridges.
Expand Down
10 changes: 8 additions & 2 deletions lib/Mojo/ByteStream.pm
Expand Up @@ -12,8 +12,8 @@ our @EXPORT_OK = ('b');
my @UTILS = (
qw(b64_decode b64_encode camelize decamelize hmac_sha1_sum html_unescape),
qw(md5_bytes md5_sum punycode_decode punycode_encode quote sha1_bytes),
qw(sha1_sum slurp spurt squish trim unquote url_escape url_unescape),
qw(xml_escape xor_encode)
qw(sha1_sum slurp spurt squish trim unindent unquote url_escape),
qw(url_unescape xml_escape xor_encode)
);
for my $name (@UTILS) {
my $sub = Mojo::Util->can($name);
Expand Down Expand Up @@ -301,6 +301,12 @@ Stringify bytestream.
Trim whitespace characters from both ends of bytestream with
L<Mojo::Util/"trim">.
=head2 unindent
$stream = $stream->unindent;
Unindent bytestream with C<Mojo::Util/"unindent">.
=head2 unquote
$stream = $stream->unquote;
Expand Down
16 changes: 15 additions & 1 deletion lib/Mojo/Util.pm
Expand Up @@ -42,7 +42,8 @@ our @EXPORT_OK = (
qw(decode deprecated dumper encode get_line hmac_sha1_sum html_unescape),
qw(md5_bytes md5_sum monkey_patch punycode_decode punycode_encode quote),
qw(secure_compare sha1_bytes sha1_sum slurp split_header spurt squish),
qw(steady_time trim unquote url_escape url_unescape xml_escape xor_encode)
qw(steady_time trim unindent unquote url_escape url_unescape xml_escape),
qw(xor_encode)
);

sub b64_decode { decode_base64($_[0]) }
Expand Down Expand Up @@ -292,6 +293,13 @@ sub trim {
return $str;
}

sub unindent {
my $str = shift;
my ($whitespace) = $str =~ /^\n*(\s+)/;
$str =~ s/^$whitespace//gm;
return $str;
}

sub unquote {
my $str = shift;
return $str unless $str =~ s/^"(.*)"$/$1/g;
Expand Down Expand Up @@ -623,6 +631,12 @@ available through L<Time::HiRes>.
Trim whitespace characters from both ends of string.
=head2 unindent
my $unindented = unindent $str;
Unindent multiline text.
=head2 unquote
my $str = unquote $quoted;
Expand Down
5 changes: 2 additions & 3 deletions lib/Mojolicious/Command.pm
Expand Up @@ -9,7 +9,7 @@ use File::Spec::Functions qw(catdir catfile);
use Mojo::Loader;
use Mojo::Server;
use Mojo::Template;
use Mojo::Util 'spurt';
use Mojo::Util qw(spurt unindent);
use Pod::Usage 'pod2usage';

has app => sub { Mojo::Server->new->build_app('Mojo::HelloWorld') };
Expand Down Expand Up @@ -54,9 +54,8 @@ sub extract_usage {
pod2usage -exitval => 'noexit', -input => (caller)[1], -output => $fh;
$output =~ s/^.*\n//;
$output =~ s/\n$//;
$output =~ s/^\ {6}//gm;

return $output;
return unindent $output;
}

sub help {
Expand Down
5 changes: 3 additions & 2 deletions lib/Mojolicious/Plugin/PODRenderer.pm
Expand Up @@ -5,7 +5,7 @@ use Mojo::Asset::File;
use Mojo::ByteStream 'b';
use Mojo::DOM;
use Mojo::URL;
use Mojo::Util qw(slurp url_escape);
use Mojo::Util qw(slurp unindent url_escape);
use Pod::Simple::HTML;
use Pod::Simple::Search;

Expand Down Expand Up @@ -46,8 +46,9 @@ sub _html {
if $attrs->{href} =~ s!^http://search\.cpan\.org/perldoc\?!$perldoc!;
}

# Rewrite code blocks for syntax highlighting
# Rewrite code blocks for syntax highlighting and correct indentation
for my $e ($dom->find('pre')->each) {
$e->replace_content(unindent $e->content_xml);
next if $e->all_text =~ /^\s*\$\s+/m;
my $attrs = $e->attr;
my $class = $attrs->{class};
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/Mojolicious/templates/perldoc.html.ep
Expand Up @@ -30,6 +30,7 @@
color: #4d4d4c;
font: 0.9em Consolas, Menlo, Monaco, Courier, monospace;
line-height: 1.5em;
padding: 1em;
padding-bottom: 1.5em;
padding-top: 1.5em;
text-align: left;
Expand Down
4 changes: 4 additions & 0 deletions t/mojo/bytestream.t
Expand Up @@ -15,6 +15,10 @@ is b('foo_bar_baz')->camelize, 'FooBarBaz', 'right camelized result';
# decamelize
is b('FooBarBaz')->decamelize, 'foo_bar_baz', 'right decamelized result';

# unindent
is b(" test\n 123\n 456\n")->unindent, "test\n 123\n456\n",
'right unindented result';

# b64_encode
is b('foobar$%^&3217')->b64_encode, "Zm9vYmFyJCVeJjMyMTc=\n",
'right base64 encoded result';
Expand Down
13 changes: 12 additions & 1 deletion t/mojo/util.t
Expand Up @@ -13,7 +13,8 @@ use Mojo::Util
qw(decode dumper encode get_line hmac_sha1_sum html_unescape md5_bytes),
qw(md5_sum monkey_patch punycode_decode punycode_encode quote),
qw(secure_compare sha1_bytes sha1_sum slurp split_header spurt squish),
qw(steady_time trim unquote url_escape url_unescape xml_escape xor_encode);
qw(steady_time trim unindent unquote url_escape url_unescape xml_escape),
qw(xor_encode);

# camelize
is camelize('foo_bar_baz'), 'FooBarBaz', 'right camelized result';
Expand Down Expand Up @@ -91,6 +92,16 @@ $tree = [
];
is_deeply split_header($header), $tree, 'right result';

# unindent
is unindent(" test\n 123\n 456\n"), "test\n 123\n456\n",
'right unindented result';
is unindent("\ttest\n\t\t123\n\t456\n"), "test\n\t123\n456\n",
'right unindented result';
is unindent("\n\n\n test\n 123\n 456\n"), "\n\n\ntest\n 123\n456\n",
'right unindented result';
is unindent(" test\n 123\n 456\n"), "test\n 123\n456\n",
'right unindented result';

# b64_encode
is b64_encode('foobar$%^&3217'), "Zm9vYmFyJCVeJjMyMTc=\n",
'right base64 encoded result';
Expand Down

0 comments on commit 912902f

Please sign in to comment.