Skip to content

Commit

Permalink
use File::ShareDir::dist_dir() to find the path of id_filter.json
Browse files Browse the repository at this point in the history
  • Loading branch information
gfx committed Mar 9, 2014
1 parent ccf2c50 commit 24114ca
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 29 deletions.
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(
},
},
);

8 changes: 4 additions & 4 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,9 +32,8 @@ 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);
}

my @errors;
Expand Down
1 change: 0 additions & 1 deletion bin/md2inao.pl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
if (!$format || $format eq 'in_design') {
require Text::Md2Inao::Builder::InDesign;
$builder = Text::Md2Inao::Builder::InDesign->new;
$builder->load_filter_config('./config/id_filter.json');
} elsif ($format eq 'inao') {
require Text::Md2Inao::Builder::Inao;
$builder = Text::Md2Inao::Builder::Inao->new;
Expand Down
4 changes: 3 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 @@ -33,7 +34,8 @@ on 'development' => sub {

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: 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
1 change: 0 additions & 1 deletion t/21_indesign_builder.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use Text::Md2Inao;
use Text::Md2Inao::Builder::InDesign;

my $builder = Text::Md2Inao::Builder::InDesign->new;
$builder->load_filter_config('./config/id_filter.json');

my $p = Text::Md2Inao->new({
default_list => 'disc',
Expand Down
9 changes: 5 additions & 4 deletions t/30_indesign_basic_syntax.t
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use 5.10.0;
use utf8;
use strict;
use warnings;

use Test::Base;
use Text::Md2Inao;
Expand All @@ -11,13 +14,11 @@ plan tests => 1 * blocks;
run_is in => 'expected';

sub md2inao {
my $builder = Text::Md2Inao::Builder::InDesign->new;
$builder->load_filter_config('./config/id_filter.json');
my $p = Text::Md2Inao->new({
state $p = Text::Md2Inao->new({
default_list => 'disc',
max_list_length => 63,
max_inline_list_length => 55,
builder => $builder,
builder => Text::Md2Inao::Builder::InDesign->new,
});
my $out = $p->parse($_);
$out =~ s/^<SJIS-MAC>\n//; # テストに毎回書くのめんどくさいので
Expand Down
11 changes: 6 additions & 5 deletions t/32_indesign_free_replacer.t
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use 5.10.0;
use strict;
use warnings;
use utf8;

use Test::Base;
Expand All @@ -10,13 +13,11 @@ plan tests => 1 * blocks;
run_is in => 'expected';

sub md2inao {
my $builder =Text::Md2Inao::Builder::InDesign->new;
$builder->load_filter_config('./config/id_filter.json');
my $p = Text::Md2Inao->new({
state $p = Text::Md2Inao->new({
default_list => 'disc',
max_list_length => 63,
max_inline_list_length => 55,
builder => $builder,
builder => Text::Md2Inao::Builder::InDesign->new,
});
my $out = $p->parse($_);
$out =~ s/^<SJIS-MAC>\n//; # テストに毎回書くのめんどくさいので
Expand Down Expand Up @@ -300,7 +301,7 @@ __END__
--- expected
<ParaStyle:本文><CharStyle:赤字>□<CharStyle:>
===
===
--- in md2inao
㈱(株)
--- expected
Expand Down
14 changes: 7 additions & 7 deletions t/33_indesign_escape_html.t
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
use 5.10.0;
use strict;
use warnings;
use utf8;

use Test::Base;
use Text::Md2Inao;
use Encode;
use Text::Md2Inao::Builder::InDesign;

$Text::Md2Inao::Logger::STOP = 1;

plan tests => 1 * blocks;

run_is in => 'expected';

sub md2inao {
$Text::Md2Inao::Logger::STOP = 1;

my $builder =Text::Md2Inao::Builder::InDesign->new;
$builder->load_filter_config('./config/id_filter.json');
my $p = Text::Md2Inao->new({
state $p = Text::Md2Inao->new({
default_list => 'disc',
max_list_length => 63,
max_inline_list_length => 55,
builder => $builder,
builder => Text::Md2Inao::Builder::InDesign->new,
});
return $p->parse($_);
}
Expand Down

0 comments on commit 24114ca

Please sign in to comment.