Skip to content

Instantly share code, notes, and snippets.

@akiym
Created April 2, 2011 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akiym/899607 to your computer and use it in GitHub Desktop.
Save akiym/899607 to your computer and use it in GitHub Desktop.
preview in real-time 2.
use Mojolicious::Lite;
use feature qw/switch/;
use utf8;
use Text::Markdown qw/markdown/;
use Text::Xatena;
use Pod::Simple::XHTML;
get '/' => 'index';
my $xatena = Text::Xatena->new();
post '/realtime' => sub {
my $self = shift;
my $text = $self->param('q');
if (!$text && !($text eq '0')) {
return $self->render(text => '');
}
my $format = $self->param('format');
my $html;
given ($format) {
when ('markdown') {
$html = markdown($text);
}
when ('xatena') {
$html = $xatena->format($text);
}
when ('pod') {
my $pod = Pod::Simple::XHTML->new();
$pod->html_header('');
$pod->html_footer('');
$pod->output_string(\$html);
$pod->parse_string_document($text);
}
}
$self->render(text => $html);
};
app->start;
__DATA__
@@ realtime.js
$(function () {
$('#realtime').focus().keyup(function () {
var text = $(this).val();
var format = $('input:radio[name=format]:checked').val();
$.ajax({
type: 'POST',
url: '/realtime',
data: {q: text, format: format},
success: function (html) {
$('#preview').html(html);
}
});
});
});
@@ realtime.css
#realtime {
width: 100%;
height: 250px;
}
#preview {
color: #333;
}
@@ index.html.ep
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>realtime.pl</title>
<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript" src="realtime.js"></script>
<link rel="stylesheet" type="text/css" href="realtime.css" />
</head>
<body>
<input type="radio" name="format" value="markdown" checked="checked" />Markdown
<input type="radio" name="format" value="xatena" />はてな記法
<input type="radio" name="format" value="pod" />Pod
<textarea id="realtime"></textarea>
<div id="preview"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment