Permalink
Browse files

updated.

  • Loading branch information...
1 parent eca2c1b commit 296c6b724a4b94eaa557220e8e4bd1d709c7d186 @yusukebe committed Feb 6, 2012
Showing with 176 additions and 31 deletions.
  1. +0 −27 README
  2. +18 −0 README.mkdn
  3. +43 −4 lib/Yomico.pm
  4. +69 −0 lib/Yomico/Web.pm
  5. +3 −0 templates/footer.tt
  6. +13 −0 templates/header.tt
  7. +30 −0 yomico.pl
View
27 README
@@ -1,27 +0,0 @@
-This is Perl module Yomico.
-
-INSTALLATION
-
-Yomico installation is straightforward. If your CPAN shell is set up,
-you should just be able to do
-
- % cpan Yomico
-
-Download it, unpack it, then build it as per the usual:
-
- % perl Makefile.PL
- % make && make test
-
-Then install it:
-
- % make install
-
-DOCUMENTATION
-
-Yomico documentation is available as in POD. So you can do:
-
- % perldoc Yomico
-
-to read the documentation online with your favorite pager.
-
-Yusuke Wada
View
@@ -0,0 +1,18 @@
+# Yomico - もう一つのMarkdownビューアー
+
+Yomico(よみこ)はPerl製のMarkdownビューアーです。
+
+## インストール
+
+## ライセンス
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+## 作者
+
+Yusuke Wada ( @yusukebe )
+
+---
+
+*Yomico - Yet Another Markdown Viewer*
View
@@ -1,29 +1,68 @@
package Yomico;
use strict;
use warnings;
+use Carp qw/croak/;
+use Yomico::Web;
+use Path::Class qw/file dir/;
+use Plack::Runner;
+
our $VERSION = '0.01';
+sub base_dir {
+ my $path = ref $_[0] || $_[0];
+ $path =~ s!::!/!g;
+ if ( my $libpath = $INC{"$path.pm"} ) {
+ $libpath =~ s!(?:blib/)?lib/+$path\.pm$!!;
+ File::Spec->rel2abs( $libpath || './' );
+ }
+ else {
+ File::Spec->rel2abs('./');
+ }
+}
+
+sub new {
+ my ( $class, %opt ) = @_;
+ croak "new method requred doc opt" unless $opt{doc};
+ croak "opt method must be file or directory path"
+ unless ( -f $opt{doc} || -d $opt{doc} );
+ my $self = bless { doc => $opt{doc} }, $class;
+ $self;
+}
+
+sub run {
+ my ($self, $opt) = @_;
+ my $web;
+ $web = Yomico::Web->new( root => dir($self->{doc}) ) if -d $self->{doc};
+ $web = Yomico::Web->new( root => file($self->{doc}) ) if -f $self->{doc};
+ my $app = $web->app;
+ my $runner = Plack::Runner->new;
+ $runner->parse_options(@$opt);
+ $runner->run( $app );
+}
+
1;
+
__END__
=head1 NAME
-Yomico -
+Yomico - Yet Another Markdown Viewer.
=head1 SYNOPSIS
use Yomico;
+ my $yomico = Yomico->new( doc => 'README.mkdn' );
+ $yomico->run();
+
=head1 DESCRIPTION
-Yomico is
+Yomico is Core module of Yomico.
=head1 AUTHOR
Yusuke Wada E<lt>yusuke@kamawada.comE<gt>
-=head1 SEE ALSO
-
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
View
@@ -0,0 +1,69 @@
+package Yomico::Web;
+use strict;
+use warnings;
+use Path::Class qw/file dir/;
+use Plack::Request;
+use File::Spec;
+use Text::Markdown qw/markdown/;
+use Yomico;
+use Text::Xslate qw/mark_raw/;
+
+sub new {
+ my ( $class, %opt ) = @_;
+ my $self = bless { %opt }, $class;
+ $self;
+}
+
+sub app {
+ my $self = shift;
+ return sub {
+ my $env = shift;
+ my $req = Plack::Request->new( $env );
+ my $path_info = $req->path_info;
+ my $path = $self->make_path($path_info);
+ if( $self->is_markdown($path) ) {
+ my $file = file( $path );
+ my $mkdn = $file->slurp;
+ my $html = markdown( $mkdn );
+ return $self->render_content( $html );
+ }
+ return [200, ['Content-Type' => 'text/html' ] , [$path] ];
+ }
+}
+
+sub render_content {
+ my ( $self, $content_html ) = @_;
+ my $header = file(Yomico->base_dir, 'templates/header.tt')->slurp;
+ my $footer = file(Yomico->base_dir, 'templates/footer.tt')->slurp;
+ my $tx = Text::Xslate->new(
+ syntax => 'TTerse',
+ );
+ my $html = $tx->render_string(
+ q{[% header %][% content %][% footer %]},
+ {
+ content => mark_raw($content_html),
+ header => mark_raw($header),
+ footer => mark_raw($footer),
+ }
+ );
+ return [ 200, [ 'Content-Type' => 'text/html', 'Content-Length' => length $html],
+ [$html] ];
+}
+
+sub make_path {
+ my ( $self, $path_info ) = @_;
+ return $self->{root} if $path_info =~ m!^/$!;
+ $path_info =~ s!^/!!;
+ return File::Spec->catfile( $self->{root}, $path_info );
+}
+
+sub is_markdown {
+ my ( $self, $path ) = @_;
+ return unless -f $path;
+ if( $path =~ m!\.(?:txt|md|mkdn|markdown|mkd|mark|mdown)! ) {
+ return $path;
+ }
+ return;
+}
+
+1;
View
@@ -0,0 +1,3 @@
+ </div>
+ </body>
+</html>
View
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8">
+ <title>Yomico... Yet Another Markdown Viewer</title>
+ <!--[if lt IE 9]>
+ <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
+ <![endif]-->
+ <link href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" rel="stylesheet">
+ </head>
+ <body>
+ <div class="container">
+
View
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use FindBin;
+use lib "$FindBin::Bin/lib/";
+use Yomico;
+use Pod::Usage;
+use Getopt::Long;
+
+my %opt;
+Getopt::Long::GetOptions(\%opt, qw/ reload=s R=s port=i /);
+my $doc = $ARGV[0] or pod2usage(2);
+my $yomico = Yomico->new( doc => $doc );
+my @opt;
+for my $key ( keys %opt ) {
+ push @opt, ( "-$key" => $opt{$key} ),
+}
+$yomico->run(\@opt);
+
+__END__
+
+=head1 NAME
+
+yomico.pl - Yet Another Markdown Viewer.
+
+=head1 SYNOPSIS
+
+yomico.pl [options] [file or directory ...]
+
+=cut

0 comments on commit 296c6b7

Please sign in to comment.