Created
September 1, 2012 18:41
-
-
Save Songmu/3582850 to your computer and use it in GitHub Desktop.
md_preview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use utf8; | |
=head1 SYNOPSIS | |
% md_preview.pl markdown.md | |
=cut | |
use Encode qw/encode_utf8/; | |
use Plack::Builder; | |
use PocketIO; | |
use Path::Class qw/file/; | |
use Filesys::Notify::Simple; | |
use Text::Markup::Any; | |
use Data::Section::Simple qw/get_data_section/; | |
use Getopt::Long qw/GetOptions :config auto_help pass_through/; | |
use Pod::Usage qw/pod2usage/; | |
use Plack::Runner; | |
GetOptions(\my %options, qw/ | |
markup=s | |
/) or pod2usage(2); | |
my $file = shift; | |
$file or pod2usage(2); | |
die "no file: $file" unless -f $file; | |
my $target = file($file); | |
my $app = builder { | |
mount '/socket.io' => PocketIO->new( | |
handler => sub { | |
my $socket = shift; | |
my $markup_class = $options{markup} || 'Markdown'; | |
my $markupper = markupper $markup_class; | |
my $watcher = Filesys::Notify::Simple->new([$target->dir]); | |
$watcher->wait(sub { | |
my @events = @_; | |
for my $event (@events) { | |
next if $event->{path} ne $target->absolute; | |
my $text = $target->slurp(iomode => '<:encoding(UTF-8)'); | |
$socket->send($markupper->markup($text)); | |
} | |
}); | |
}, | |
); | |
mount '/' => sub { | |
my $env = shift; | |
my $content = encode_utf8 get_data_section('index.tt'); | |
[ | |
200, | |
[ | |
'Content-Type' => 'text/html', | |
'Content-Length' => length($content), | |
], | |
[$content], | |
]; | |
}, | |
}; | |
my $runner = Plack::Runner->new; | |
$runner->parse_options(@ARGV); | |
$runner->{server} ||= 'Twiggy'; | |
$runner->run($app); | |
__DATA__ | |
@@ index.tt | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>preview</title> | |
<script type="text/javascript" src="https://raw.github.com/LearnBoost/socket.io-client/master/dist/socket.io.js"></script> | |
<script type="text/javascript"> | |
var socket = io.connect(); | |
socket.on('message', function (msg) { | |
document.getElementById('preview').innerHTML = msg | |
}); | |
</script> | |
</head> | |
<body> | |
<div id="preview"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment