Skip to content

Commit

Permalink
Merge pull request #81 from naoya/error_msg_to_web
Browse files Browse the repository at this point in the history
web appでもerror messagesを出すように
  • Loading branch information
gfx committed Mar 9, 2014
2 parents 2cccbd2 + 1eba119 commit 27e55a7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
17 changes: 16 additions & 1 deletion app.psgi
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,19 @@ post '/upload' => sub {
$builder->load_filter_config('./config/id_filter.json');
$p->builder($builder);
}
$self->render(text => $p->parse(decode_utf8 $md), format => 'txt');

my @errors;
local $Text::Md2Inao::Logger::LOG = sub {
my($type, $message) = @_;
push @errors, { type => $type, message => $message };
};
$self->render(
json => {
content => $p->parse(decode_utf8 $md),
errors => \@errors,
},
format => 'json',
);
};

app->types->type(txt => "text/plain;charset=UTF-8");
Expand Down Expand Up @@ -69,6 +81,9 @@ __DATA__
</fieldset>
% end
<div id="error">
</div>
<div id="result">
<p class="text-right"><a href="" id="download">ダウンロード (Google Chromeのみ)</a></p>
<textarea id="content"></textarea>
Expand Down
8 changes: 6 additions & 2 deletions lib/Text/Md2Inao/Logger.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use Term::ANSIColor;
use parent qw/Exporter/;
our @EXPORT = qw/log/;

our $LOG;
our $STOP;

my %COLOR = (
Expand All @@ -19,8 +20,11 @@ my %COLOR = (

sub log ($$) {
my ($type, $msg) = @_;
my $color ||= $COLOR{$type};
if (not $STOP) {
if ($LOG) {
$LOG->($type, $msg);
}
elsif (not $STOP) {
my $color ||= $COLOR{$type};
print STDERR encode_utf8(Term::ANSIColor::colored sprintf("[%s] %s\n", $type, $msg), $color);
}
return;
Expand Down
13 changes: 13 additions & 0 deletions public/css/md2inao.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,16 @@
#indicator {
display: none;
}

.success {
color: lime;
}
.warn {
color: red;
}
.error {
color: red;
}
.info {
color: gray;
}
17 changes: 14 additions & 3 deletions public/js/md2inao.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@ $(function () {
processData: false,
contentType: false,
data: new FormData($(this)[0]),
dataType: 'text'
dataType: 'json'
}).done(function(data) {
$('#result').slideDown("fast").find('textarea').val(data);
$('#result').slideDown("fast").find('textarea').val(data.content);

var error = $('#error');
error.empty();
for (var i = 0; i < data.errors.length; ++i) {
var element = $('<pre/>');
element.addClass(data.errors[i].type);
element.text(data.errors[i].message);
error.append(element);
}
$('#error').slideDown("fast").find('textarea').val(data.error);

// Download Anchor
var filename = $('#form').find('.fileupload-preview').text().replace(/.md$/, ".txt");
var blob = new Blob([ data ], {"type": "text/plain"});
var blob = new Blob([ data.content ], {"type": "text/plain"});
window.URL = window.URL || window.webkitURL;
$('#download').attr("href", window.URL.createObjectURL(blob)).attr("download", filename);
}).fail(function(data) {
Expand Down Expand Up @@ -44,3 +54,4 @@ $(function () {
$('#blank_style').toggle();
});
});
// vim: set tabstop=2 expandtab:

0 comments on commit 27e55a7

Please sign in to comment.