Skip to content

Commit

Permalink
Merge branch 'master' into inao
Browse files Browse the repository at this point in the history
  • Loading branch information
gfx committed Mar 10, 2014
2 parents af2da17 + dc69e67 commit 4a3b0c2
Show file tree
Hide file tree
Showing 21 changed files with 317 additions and 55 deletions.
6 changes: 6 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

0.10 2014-03-10 08:32:33+0900
- Add --version option to md2inao.pl

0.09 2014-03-09 23:33:52+0900
- Resolve #24, #74, #81, #79, #80, #78

0.08 2014-03-04 21:55:40+0900
- Fix a packaging issue for older perls

Expand Down
3 changes: 3 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ lib/Text/Md2Inao/Util.pm
Makefile.PL
MANIFEST This list of files
md2inao.run
META.json
META.yml
old_files/00_sample.md
old_files/01_sample_inao.txt
old_files/02_sample_id.txt
Expand Down Expand Up @@ -71,5 +73,6 @@ t/99_rt_paragraph.t
t/99_rt_quotes.t
t/99_rt_ruby_in_footnote.t
t/99_rt_table_indent.t
t/issue_14_xxx_cmd.t
t/x_input.txt
t/x_output.txt
9 changes: 9 additions & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ use warnings;

use ExtUtils::MakeMaker;
use ExtUtils::MakeMaker::CPANfile;
use File::ShareDir::Install;

install_share dist => 'config';

{
package MY;
use File::ShareDir::Install qw(postamble);
}

WriteMakefile(
NAME => 'Text::Md2Inao',
Expand All @@ -19,3 +27,4 @@ WriteMakefile(
},
},
);

22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,34 @@ Markdown to Inao-Format
概要
----

Markdown で書かれたテキストを「inaoフォーマット」に変換します
Markdown で書かれたテキストをWEB+DB編集部で使用しているInDesign用フォーマットに変換します。主にWEB+DB編集部の方のためのツールですが、執筆者にとってもmarkdownの原稿がWEB+DBの原稿のスタイルに従っているかどうかをチェックするツールとして使えます

- bin/md2inao.pl : CUIコマンド版
- http://md2inao.bloghackers.net/ : Web版

markdown2inao.pl 改め md2inao.pl のこれまでについては https://gist.github.com/inao/baea09bc6fc53551886b を見て下さい。
markdown2inao.pl 改め md2inao.pl のこれまでについては https://gist.github.com/inao/baea09bc6fc53551886b を見て下さい。なお、md2inaoは当初「inao記法」に変換していましたが、現在はWEB+DB編集部用InDesignフォーマットを出力します。

使い方
-----

### CUI版

CPANからインストール
% md2inao.pl your_markdown.md > path/to/output.txt

#### CPANからインストール

% cpanm Text::Md2Inao
# webからcpanmをDLしてそのままそれを使ってmd2inaoをインストール
% curl -L http://cpanmin.us | perl - --sudo Text::Md2Inao
% md2inao.pl your_markdown.md > path/to/output.txt

#### リポジトリからインストール

リポジトリからインストール
リポジトリからインストールする場合、Perl/CPANの知識が必要です。

% cpanm Carton
% carton
% carton install
% carton exec -- perl bin/md2inao.pl your_markdown.md > path/to/output.txt

cpanmは以下のコマンドでインストールできます

% curl -L http://cpanmin.us > ~/bin/cpanm && chmod +x ~/cpanm

### Web版

- http://md2inao.bloghackers.net/
Expand Down Expand Up @@ -102,7 +102,7 @@ Markdown は Markdown プロセッサで解釈すると、通常は HTML に変

また、Markdown で定義されてない記述も HTML を使うことによって定義することもできます。一部の記法は HTML で書くことが前提になっています。

いまのところ GitHub Flavored Markdown などの拡張には対応していませんのでその点に注意してください
いまのところ GitHub Flavored Markdown などの拡張すべてには対応していませんのでその点に注意してください

#### 文中に HTML を書く際の注意事項

Expand Down
27 changes: 21 additions & 6 deletions app.psgi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!perl

use strict;
use warnings;
use Mojolicious::Lite;
use Project::Libs;
use Plack::Builder;
Expand Down Expand Up @@ -31,11 +32,22 @@ post '/upload' => sub {
});

if ($self->req->param('in_design')) {
my $builder = Text::Md2Inao::Builder::InDesign->new;
$builder->load_filter_config('./config/id_filter.json');
$p->builder($builder);
my $id_builder = Text::Md2Inao::Builder::InDesign->new();
$p->builder($id_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 All @@ -58,7 +70,7 @@ __DATA__
</div>
<label for='in_design' class="checkbox">
%= check_box in_design => 1, id => 'in_design'
%= check_box in_design => 1, id => 'in_design', checked => 'checked'
InDesign出力
</label>
<select name="blank_style" id="blank_style">
Expand All @@ -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
28 changes: 26 additions & 2 deletions bin/md2inao.pl
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@
use Encode;
use Pod::Usage;
use File::Spec;
use FindBin qw($Bin);
use FindBin::libs;
use Getopt::Long qw/:config posix_default no_ignore_case bundling auto_help/;
use Getopt::Long qw/:config posix_default no_ignore_case bundling auto_help auto_version/;

use Text::Md2Inao;

our $VERSION = $Text::Md2Inao::VERSION;
if (-e "$Bin/../.git/HEAD") {
chomp(my $rev = `git rev-parse --short HEAD`);
$VERSION .= " ($rev)";
}

GetOptions(
'output-encoding=s' => \my $output_encoding,
'format=s' => \my $format,
) or pod2usage(-1);

my $infile = $ARGV[0]
Expand All @@ -21,10 +29,26 @@
my $text = do { local $/; <$fh> };
close $fh;

my $builder;
if (!$format || $format eq 'in_design') {
require Text::Md2Inao::Builder::InDesign;
$builder = Text::Md2Inao::Builder::InDesign->new;
} elsif ($format eq 'inao') {
require Text::Md2Inao::Builder::Inao;
$builder = Text::Md2Inao::Builder::Inao->new;
} elsif ($format eq 'html') {
require Text::Md2Inao::Builder::Html;
$builder = Text::Md2Inao::Builder::Html->new;
}
else {
die "Unknown format (must be in_desing, inao or html): $format\n";
}

my $p = Text::Md2Inao->new({
default_list => 'disc',
max_list_length => 63,
max_inline_list_length => 55,
builder => $builder,
});

print encode($output_encoding // 'utf-8', $p->parse($text));
Expand All @@ -37,5 +61,5 @@ =head1 NAME
=head1 SYNOPSIS
md2inao.pl [--output-encoding=utf-8] your_markdown_text.md > inao_format.txt
md2inao.pl [--format=in_design|html|inao] your_markdown_text.md > inao_format.txt
=cut
8 changes: 7 additions & 1 deletion cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ requires 'Exporter::Lite';
requires 'Path::Tiny';
requires 'Tie::IxHash';
requires 'JSON', '>= 2.55';
requires 'File::ShareDir';

feature 'psgi', 'web app support' => sub {
requires 'Project::Libs';
Expand All @@ -27,9 +28,14 @@ on 'test' => sub {
requires 'Devel::Cover';
};

on 'development' => sub {
requires 'Data::Recursive::Encode';
};

on 'configure' => sub {
requires 'ExtUtils::MakeMaker' => '>= 6.74';
requires 'ExtUtils::MakeMaker::CPANfile'
requires 'ExtUtils::MakeMaker::CPANfile';
requires 'File::ShareDir::Install';
};

# vim: set ft=perl:
9 changes: 9 additions & 0 deletions cpanfile.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,15 @@ DISTRIBUTIONS
Proc::Wait3 0.04
requirements:
ExtUtils::MakeMaker 0
Project-Libs-0.02
pathname: K/KE/KENTARO/Project-Libs-0.02.tar.gz
provides:
Project::Libs 0.02
requirements:
ExtUtils::MakeMaker 6.59
FindBin::libs 0
Test::More 0.96
perl 5.008001
Scope-Guard-0.20
pathname: C/CH/CHOCOLATE/Scope-Guard-0.20.tar.gz
provides:
Expand Down
8 changes: 5 additions & 3 deletions lib/Text/Md2Inao.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use utf8;
use strict;
use warnings;

our $VERSION = '0.08';
our $VERSION = '0.10';

use Carp;
use Class::Accessor::Fast qw/antlers/;
Expand Down Expand Up @@ -62,7 +62,7 @@ sub prepare_text_for_markdown {
$text =~ s/^[ ]{1,3}([^ <])/ $1/mg;

## Work Around: リストの後にコードブロックが続くとだめな問題 (issue #6)
$text =~ s![-*+] (.*?)\n\n !- $1\n\n \n\n !g;
$text =~ s!([-*+] .*?)\n\n !$1\n\n \n\n !g;

return $text;
}
Expand All @@ -82,7 +82,9 @@ sub to_html_tree {
my $text = shift;

$text = prepare_text_for_markdown($text);
my $html = markdown($text);
my $html = markdown($text,
extensions => HOEDOWN_EXT_FENCED_CODE,
);
$html = prepare_html_for_inao($html);

my $tree = HTML::TreeBuilder->new;
Expand Down
9 changes: 6 additions & 3 deletions lib/Text/Md2Inao/Builder.pm
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ has after_filter_config => ( is => 'rw' );
my %singleton;
sub new {
my $class = shift;
$singleton{$class}
? return $singleton{$class}
: return $singleton{$class} = $class->SUPER::new({ dispatch_table => {} });
return $singleton{$class} //= $class->_new();
}
}

sub _new {
my $class = shift;
return $class->SUPER::new({ dispatch_table => {} });
}

sub dispatch {
my ($self, $select) = @_;
return $self->dispatch_table->{$select} || $self->dispatch_table->{default};
Expand Down
17 changes: 15 additions & 2 deletions lib/Text/Md2Inao/Builder/InDesign.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use Text::Md2Inao::Builder::DSL;

use Text::Md2Inao::Logger;
use Text::Md2Inao::Util;

use File::ShareDir qw(dist_dir);
use Path::Tiny;
use List::Util qw/max/;

tie my %meta2label, "Tie::IxHash",
Expand All @@ -23,6 +24,18 @@ tie my %meta2label, "Tie::IxHash",
twitter => 'Twitter',
;

sub _new {
my $class = shift;
my $self = $class->SUPER::_new(@_);
for my $dir('config', dist_dir('Text-Md2Inao')) {
if (-d $dir) {
$self->load_filter_config(path($dir, 'id_filter.json'));
last;
}
}
return $self;
}

sub prepend_metadata {
my ($self, $c, $text) = @_;
if ($c->metadata) {
Expand Down Expand Up @@ -268,7 +281,7 @@ case div => sub {
case ul => sub {
my ($c, $h) = @_;
my $label = $c->in_column ? 'コラム箇条書き' : '箇条書き';

if ($c->in_list) {
my $ret = "\n";
for ($h->content_list) {
Expand Down
4 changes: 3 additions & 1 deletion lib/Text/Md2Inao/Director.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package Text::Md2Inao::Director;
use strict;
use warnings;

use Carp;
use Class::Accessor::Fast qw/antlers/;

has builder => ( is => 'rw', isa => 'Text::Md2Inao::Builder' );
Expand All @@ -14,7 +15,8 @@ sub new {
sub process {
my ($self, $c, $h) = @_;
my $select = ref $h eq '' ? 'text' : $h->tag;
$self->builder->dispatch($select)->($c, $h);
my $proc = $self->builder->dispatch($select) or croak("Missing case: $select");
$proc->($c, $h);
}

sub process_before_filter {
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
Loading

0 comments on commit 4a3b0c2

Please sign in to comment.