...
 
Commits (5)
......@@ -14,11 +14,9 @@ use File::Spec::Functions;
use Time::HiRes;
use utf8;
binmode STDOUT, ":utf8";
binmode STDOUT, ":encoding(UTF-8)";
my $cwd = cwd;
my $source;
my $config;
sub abstract { 'site builder' }
......@@ -26,10 +24,11 @@ sub description { 'builder script for Mira static site generator' }
sub opt_spec {
return (
['directory|d=s','application path (default: current directory)',{ default => $cwd }],
['floor|f=s', 'floor you want build'],
['directory|d=s','application path (default: current directory)',
{ default => $cwd }],
['floor|f=s', 'floor you want build' ],
['draft|d', 'build with draft files'],
[ 'help|h', 'this help' ],
[ 'help|h', 'this help' ],
);
}
......@@ -38,17 +37,17 @@ sub validate_args {
my $path = $opt->{directory};
-d $path or $self->usage_error("directory '$path' does not exist");
-f catfile( $path, 'config.yml' ) or _usage_error
(
"directory '$path' does not valid address.\ncant't find config.yml"
);
(
"directory '$path' does not valid address.\ncant't find config.yml"
);
-d catdir( $path, 'content' ) or _usage_error
(
"directory '$path' does not valid address.\ncant't find content folder"
);
(
"directory '$path' does not valid address.\ncant't find content folder"
);
-d catdir( $path, 'template' ) or _usage_error
(
"directory '$path' does not valid address.\ncant't find template folder"
);
(
"directory '$path' does not valid address.\ncant't find template folder"
);
if ($opt->{floor})
{
-d catdir( $path, 'content', $opt->{floor} ) or _usage_error
......@@ -62,20 +61,18 @@ sub execute {
my ( $self, $opt, $args ) = @_;
my $start_time = [ Time::HiRes::gettimeofday() ];
$source = $opt->{directory};
my $confset = Mira::Config->new();
$config = $confset->set($source);
my $source = $opt->{directory};
my $floorsource = $opt->{floor} ? catdir('content', $opt->{floor}) : '';
my $draft = $opt->{draft} ? '' : '.draft';
my $draft_ext = $opt->{draft} ? '' : '.draft';
my $config = Mira::Config->set($source);
######################
use Mira::Control::Content;
my $bases = Mira::Control::Content->preparator(
source => $source ,
ext => $draft ,
draft_ext => $draft_ext ,
config => $config ,
floorsource => $floorsource,
);
......
......@@ -17,7 +17,7 @@ use File::Path qw(make_path);
use File::ShareDir ':ALL';
use utf8;
binmode STDOUT, ":utf8";
binmode STDOUT, "::encoding(UTF-8)";
......
......@@ -16,7 +16,7 @@ use Encode;
use Encode::Locale;
use utf8;
binmode STDOUT, ":utf8";
binmode STDOUT, ":encoding(UTF-8)";
use Mira::Config;
......
......@@ -42,8 +42,7 @@ sub execute {
my ($self, $opt, $args) = @_;
my $source = -d $opt->{directory} ? $opt->{directory} : $cwd;
my $confset = Mira::Config->new();
my $config = $confset->set($source);
my $config = Mira::Config->set($source);
my $publishDIR = $config->{_default}->{publishDIR};
our $localdir = catdir($source, $publishDIR);
......
......@@ -14,17 +14,11 @@ use Encode::Locale;
use 5.012;
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
return $self;
}
sub set {
my $self = shift;
my $class = shift;
my $source = shift;
my $self = {};
bless $self, $class;
$self = $self->_default_config($source);
$self = $self->_floors_config($source);
......@@ -36,7 +30,7 @@ sub _default_config {
my ($self, $source) = @_;
my $config_file = catfile($source, 'config.yml');
if (-f $config_file)
if ( -e $config_file && !-d _ ) # -f $config_file
{
$self->{_default} = $self->_doYaml_to_Config($config_file);
} else
......@@ -84,7 +78,7 @@ sub _floors_config {
foreach my $floor (@floors)
{
my $config_file = catfile($source, 'config', "$floor.yml");
if (-f $config_file)
if ( -e $config_file && !-d _ )
{
$self->{$floor} = $self->_doYaml_to_Config($config_file, $floor);
$self->{$floor}->{title} = $floor unless ($self->{$floor}->{title});
......@@ -116,26 +110,26 @@ sub _doYaml_to_Config {
my $self = shift;
my $config_file = shift;
my $is_floor = shift;
my $file_content;
my $yaml;
my $conf;
{
open my $fh, '<:encoding(UTF-8)', $config_file or die $!;
local $/ = undef;
$yaml = <$fh>;
$file_content = <$fh>;
close $fh;
}
eval { #must read yaml message and print it in error output
$conf = Load( $yaml );
$yaml = Load( $file_content );
}; if ($@ and not $is_floor)
{
say "$config_file have problem" and exit;
} elsif ($@ and $is_floor)
{
say " # - $is_floor\.yml have problem, use default configs for floor: $is_floor";
$conf = $self->_not_valids($is_floor);
$yaml = $self->_not_valids($is_floor);
}
return $conf;
return $yaml;
}
sub _not_valids {
......
......@@ -7,14 +7,12 @@ use 5.012;
sub preparator {
say "v1.00.00";
my $class = shift;
my %switches = @_;
my $source = $switches{source};
my $floorsource = $switches{floorsource};
my $ext = $switches{ext}; # ? $switches{ext} : '.draft';
my $draft_ext = $switches{draft_ext};
my $config = $switches{config};
......@@ -31,7 +29,7 @@ sub preparator {
my $content = Mira::Control::Content::Load->new(
source => $source,
ext => $ext,
draft_ext => $draft_ext,
floorsource => $floorsource,
);
my $floors = $content->floors;
......@@ -53,17 +51,12 @@ sub preparator {
my $parser = Mira::Control::Parser::Entry->parse(entry => $file, floor => $floor);
next unless $parser;
my $utid = $parser->{utid};
my $utid = $parser->{utid};
my $values = $parser->{values};
if (not exists $data->{$utid})
{
Mira::Control::Content::Date->date($values, $config->{$floor}->{timezone});
# $values->{body} = Mira::Control::Parser::img->replace(
# $values->{body},
# _img_url($floor, $config),
# $config,
# );
$values->{body} = Mira::Control::Parser::Markup->markup(
$values->{body},
_markup_lang($values, $config),
......@@ -124,28 +117,6 @@ sub _markup_lang {
}
sub _img_url {
my $floor = shift;
my $config = shift;
my $imgurl;
if ($config->{$floor} and $config->{$floor}->{imageurl})
{
$imgurl = $config->{$floor}->{imageurl};
} elsif ($config->{_default}->{imageurl})
{
$imgurl = $config->{_default}->{imageurl};
} elsif ($config->{$floor} and $config->{$floor}->{root})
{
$imgurl = "/$config->{$floor}->{root}/static/img/";
} else
{
$imgurl = "/$floor/static/img/";
}
$imgurl =~ s:/+:/:g;
return $imgurl;
}
1;
__END__
......
......@@ -12,18 +12,17 @@ use Carp;
use Encode;
use Encode::Locale;
sub new {
my $class = shift;
my %switches = @_;
my $source = $switches{source};
my $floorsource = $switches{floorsource};
my $ext = $switches{ext}; # ? $switches{ext} : '.draft';
my $draft_ext = $switches{draft_ext};
my $self = {
source => $source,
ext => $ext,
draft_ext => $draft_ext,
floorsource => $floorsource,
};
......@@ -31,48 +30,50 @@ sub new {
return $self;
}
sub floors {
my $self = shift;
my $source = $self->{source};
my $floorsource = $self->{floorsource};
if ($floorsource and -d $floorsource)
if ( $floorsource and -d $floorsource )
{
my $floor = basename($floorsource);
return [$floor];
}
my $glob = catfile('content', '*');
my $glob = catfile( 'content', '*' );
my @content_directory_list = glob encode(locale_fs => $glob);
@content_directory_list = grep {-d} @content_directory_list;
my @content_directory_list = glob encode( locale_fs => $glob );
@content_directory_list = grep { -d } @content_directory_list;
my @floors = map {decode(locale_fs => basename($_))} @content_directory_list;
my @floors =
map { decode( locale_fs => basename($_) ) } @content_directory_list;
return \@floors;
}
sub files {
my $self = shift;
my $floors = shift;
my $source = $self->{source};
my $ext = $self->{ext};
my $self = shift;
my $floors = shift;
my $source = $self->{source};
my $draft_ext = $self->{draft_ext};
my $files = {};
foreach my $floor (@$floors)
{
my $glob = catfile('content', $floor , "*");
my @path = glob encode(locale_fs => $glob);
my $glob = catfile( 'content', $floor, "*" );
my @path = glob encode( locale_fs => $glob );
my @files = _room(@path);
my @entries;
@entries = $ext ? grep {-f and not /($ext)$/} @files : grep {-f} @files;
my @entries =
$draft_ext
? grep { -e and !-d and not /($draft_ext)$/ } @files
: grep { -e and !-d } @files;
foreach my $entry (@entries)
{
$entry = decode(locale_fs => $entry);
$entry = decode( locale_fs => $entry );
push @{ $files->{$floor} }, $entry;
}
}
......@@ -81,7 +82,7 @@ sub files {
}
sub statics {
my $self = shift;
my $self = shift;
my $floors = shift;
my $source = $self->{source};
......@@ -89,14 +90,15 @@ sub statics {
foreach my $floor (@$floors)
{
my $glob = catfile($source, 'content', $floor , "*");
my @path = glob encode(locale_fs => $glob);
@path = grep {-d} @path;
my $glob = catfile( $source, 'content', $floor, "*" );
my @path = glob encode( locale_fs => $glob );
@path = grep { -d } @path;
my @statics = _static_rooms(@path);
#@statics = grep {-d and /statics$/} @statics;
foreach my $static (@statics)
{
$static = decode(locale_fs => $static);
$static = decode( locale_fs => $static );
push @{ $statics->{$floor} }, $static;
}
}
......@@ -105,20 +107,17 @@ sub statics {
}
sub _room {
my @path = @_;
my @files;
foreach my $path (@path)
{
next if (-d $path && basename($path) =~ /^_/);
(-f $path) && (push @files, $path) && next;# if -f $path;
if (-d $path)
next if ( -d $path && basename($path) =~ /^_/ );
( -e $path && !-d _ ) && ( push @files, $path ) && next; # if -f $path;
if ( -d $path )
{
my $glob = catfile($path , "*");
my @paths = glob encode(locale_fs => $glob);
my $glob = catfile( $path, "*" );
my @paths = glob encode( locale_fs => $glob );
push @files, _room(@paths);
}
}
......@@ -131,10 +130,10 @@ sub _static_rooms {
foreach my $path (@path)
{
next if not -d $path;
(push @dirs, $path) && next if (-d $path && basename($path) =~ /^_/);
my $glob = catfile($path , "*");
my @paths = glob encode(locale_fs => $glob);
@path = grep {-d} @path;
(push @dirs, $path) && next if ( -d $path && basename($path) =~ /^_/ );
my $glob = catfile( $path, "*" );
my @paths = glob encode( locale_fs => $glob );
@path = grep { -d } @path;
push @dirs, _static_rooms(@paths);
}
return @dirs;
......
......@@ -9,7 +9,6 @@ use 5.012;
use YAML;
use Carp;
use File::Basename qw/basename/;
use Encode::Guess;
use POSIX 'strftime';
sub parse {
......@@ -19,78 +18,118 @@ sub parse {
my $entry = $switches{entry} or croak "entry parser need entry field";
my $floor = $switches{floor} or croak "entry parser need floor field";
my $filename = basename($entry);
my $content;
my $utid;
my $content = _read_file($entry);
return unless my $post_data = _read_content($content);
my $detail = $post_data->{detail};
my $body = $post_data->{body};
return if ($detail =~ /^_type\s*:\s*draft\s*$/m);
my $top;
eval
{
open my $fh, "<", $entry or die $!;
local $/ = undef;
$content = <$fh>;
my $enc = guess_encoding($content);
ref($enc) or do {say "can't encoding $entry, plz fix it."; return;};
$content = $enc->decode($content);
close $fh;
$top = Load($detail);
}; if ($@)
{
say "--- WARNING: problem in HEADER, content HEADER isn't in YAML standard format:
$entry\n";
return;
}
if ($content =~
m/
^(---+)?\s*
(?<detail>[\w\W]+?)
\n+---+\s*
(?<body>[\w\W]*)$
/mx)
return if ( $top->{_type} and $top->{_type} =~ /draft/ );
my $utid = _utid_finder($top, $entry);
$utid =~ s/[^\d]//g;
$self->{utid} = $utid;
foreach my $field (keys %$top)
{
my $detail = $+{detail};
my $body = $+{body};
$detail =~ s/\s*(?<!\\)#.*//g;
$detail =~ s/(?<!\\)\\#/#/g;
$detail =~ s/\\\\#/\\#/g;
$body =~ s/\n\s*$//;
# if ($detail =~ /^utid\s*:(?<utid>.*)$/m)
# {
my $top;
eval
{
$top = Load($detail);
}; if ($@)
if (ref($top->{$field}) eq "ARRAY")
{
say "problem in HEADER, content HEADER isn't in YAML standard format:
$entry\n";
return;
@{ $top->{$field} } = grep {$_} @{ $top->{$field} };
delete $top->{$field} unless @{ $top->{$field} };
}
#$utid = delete $top->{utid};
if ($top->{utid})
{
$utid = $top->{utid};
}
} elsif ($top->{date})
{
if ( $top->{date} =~
/^(\d{4})-(\d{1,2})-(\d{1,2})[ T](\d{1,2}):(\d{1,2}):(\d{1,2})/
) {
my $year = $1;
my $month = $2;
my $day = $3;
my $hour = $4;
my $min = $5;
my $sec = $6;
$utid = sprintf "%04d%02d%02d%02d%02d%02d",
$year, $month, $day, $hour, $min, $sec;
} elsif ( $top->{date} =~ /^(\d{4})-(\d{1,2})-(\d{1,2})/ )
{
my $year = $1;
my $month = $2;
my $day = $3;
$utid = sprintf "%04d%02d%02d", $year, $month, $day;
my ( $sec, $min, $hour ) = localtime( (lstat $entry)[9] );
my $datefix = sprintf "%02d%02d%02d", $hour, $min, $sec;
$utid = $utid . $hour . $min . $sec;
}
} elsif ( $filename =~ /^(\d{4})-(\d{1,2})-(\d{1,2})/ )
$self->{values}->{SPEC}->{file_address} = $entry;
@{$self->{values}}{keys %$top} = values %$top;
$self->{values}->{floor} = $floor;
$self->{values}->{body} = $body;
unless ($self->{values}->{title})
{
$self->{values}->{title} = basename($entry);
$self->{values}->{title} =~ s/^\d{4}-\d{1,2}-\d{1,2}-//;
$self->{values}->{title} =~ s/\.(.*)$//;
}
return $self;
}
##################
sub _read_file {
my $entry = shift;
open my $fh, "<:encoding(UTF-8)", $entry or die $!;
local $/ = undef;
my $content = <$fh>;
close $fh;
return $content;
}
# for support all encoding, not just utf8;
# use Encode::Guess;
# my $enc = guess_encoding($content);
# ref($enc) or do {say "can't encoding $entry, plz fix it."; return;};
# $content = $enc->decode($content);
sub _read_content {
my $content = shift;
if ($content =~
m/
^(---+)?\s*
(?<detail>[\w\W]+?)
\n+---+\s*
(?<body>[\w\W]*)$
/mx)
{
return {
detail => $+{detail},
body => $+{body}
}
} else {
return;
}
}
sub _utid_finder {
my $top = shift;
my $entry = shift;
my $filename = basename($entry);
my $utid;
if ($top->{utid})
{
$utid = $top->{utid};
} elsif ($top->{date})
{
if ( $top->{date} =~
/^(\d{4})-(\d{1,2})-(\d{1,2})[ T](\d{1,2}):(\d{1,2}):(\d{1,2})/
) {
my $year = $1;
my $month = $2;
my $day = $3;
my $hour = $4;
my $min = $5;
my $sec = $6;
$utid = sprintf "%04d%02d%02d%02d%02d%02d",
$year, $month, $day, $hour, $min, $sec;
} elsif ( $top->{date} =~ /^(\d{4})-(\d{1,2})-(\d{1,2})/ )
{
my $year = $1;
my $month = $2;
......@@ -99,41 +138,22 @@ sub parse {
my ( $sec, $min, $hour ) = localtime( (lstat $entry)[9] );
my $datefix = sprintf "%02d%02d%02d", $hour, $min, $sec;
$utid = $utid . $hour . $min . $sec;
} else
{
$utid = strftime("%Y%m%d%H%M%S", localtime( (lstat $entry)[9]) );
}
$utid =~ s/[^\d]//g;
$self->{utid} = $utid;
foreach my $field (keys %$top)
{
if (ref($top->{$field}) eq "ARRAY")
{
@{ $top->{$field} } = grep {$_} @{ $top->{$field} };
delete $top->{$field} unless @{ $top->{$field} };
}
}
$self->{values}->{SPEC}->{file_address} = $entry;
@{$self->{values}}{keys %$top} = values %$top;
$self->{values}->{floor} = $floor;
$self->{values}->{body} = $body;
unless ($self->{values}->{title})
{
$self->{values}->{title} = basename($entry);
$self->{values}->{title} =~ s/^\d{4}-\d{1,2}-\d{1,2}-//;
$self->{values}->{title} =~ s/\.(.*)$//;
}
# }
} elsif ( $filename =~ /^(\d{4})-(\d{1,2})-(\d{1,2})/ )
{
my $year = $1;
my $month = $2;
my $day = $3;
$utid = sprintf "%04d%02d%02d", $year, $month, $day;
my ( $sec, $min, $hour ) = localtime( (lstat $entry)[9] );
my $datefix = sprintf "%02d%02d%02d", $hour, $min, $sec;
$utid = $utid . $hour . $min . $sec;
} else
{
$utid = strftime("%Y%m%d%H%M%S", localtime( (lstat $entry)[9]) );
}
return $self;
return $utid;
}
1;