File Coverage

blib/lib/Mira/Config.pm
Criterion Covered Total %
statement 68 102 66.6
branch 14 44 31.8
condition 11 33 33.3
subroutine 13 14 92.8
pod 0 1 0.0
total 106 194 54.6


line stmt bran cond sub pod time code
1             package Mira::Config;
2              
3 1     1   905 use strict;
  1         2  
  1         23  
4 1     1   2 use warnings;
  1         2  
  1         17  
5 1     1   3 use utf8;
  1         1  
  1         3  
6             our $VERSION = $Mira::VERSION;
7              
8 1     1   378 use YAML;
  1         7002  
  1         41  
9 1     1   6 use File::Spec;
  1         0  
  1         15  
10 1     1   404 use File::Spec::Functions;
  1         566  
  1         59  
11 1     1   5 use File::Basename qw/basename/;
  1         1  
  1         46  
12 1     1   3 use Carp;
  1         1  
  1         71  
13 1     1   446 use Encode;
  1         8116  
  1         56  
14 1     1   401 use Encode::Locale;
  1         2379  
  1         37  
15 1     1   4 use File::Path qw(make_path);
  1         1  
  1         40  
16 1     1   13 use 5.012;
  1         2  
17              
18             sub new {
19 1     1 0 1701 my $class = shift;
20 1         2 my $source = shift;
21 1         2 my $self = {};
22             #$self->{_source} = $source;
23              
24 1         3 my $yaml;
25 1 50       7 if (-f catfile($source, 'config.yml') )
26             {
27             {
28 1 50       30 open my $fh, '<:encoding(UTF-8)', 'config.yml' or die $!;
  1         26  
29 1         40 local $/ = undef;
30 1         18 $yaml = <$fh>;
31 1         48 close $fh;
32             }
33 1         1 eval { #must read yaml message and print it in error output
34 1         6 $self->{_default} = Load( $yaml );
35             };
36 1 50       17531 if ($@) {croak "$source/config.yml have problem";}
  0         0  
37             } else
38             {
39 0         0 croak " ! - you are not in true path or --source is invalid path or /conf/config.yml isn't valid file";
40             }
41              
42 1 50 33     11 $self->{_default}->{root} = "/" unless (exists $self->{_default}->{root} and $self->{_default}->{root});
43 1         3 $self->{_default}->{root} =~ s{^(.*)?:/+}{/}g;
44 1         5 $self->{_default}->{root} =~ s{/+}{/}g;
45 1 50 33     7 $self->{_default}->{url} = "/" unless (exists $self->{_default}->{url} and $self->{_default}->{url});
46 1         7 $self->{_default}->{url} =~ s{(?<!:)/+}{/}g;
47              
48 1 50 33     6 $self->{_default}->{post_num} = "5" unless (exists $self->{_default}->{post_num} and $self->{_default}->{post_num});
49 1 50 33     18 $self->{_default}->{archive_post_num} = "20" unless (exists $self->{_default}->{archive_post_num} and $self->{_default}->{archive_post_num});
50 1 50 33     6 $self->{_default}->{feed_post_num} = "20" unless (exists $self->{_default}->{feed_post_num} and $self->{_default}->{feed_post_num});
51 1 50 33     22 $self->{_default}->{default_floor} = "blog" unless (exists $self->{_default}->{default_floor} and $self->{_default}->{default_floor});
52 1 50 33     5 $self->{_default}->{date_format} = "gregorian" unless (exists $self->{_default}->{date_format} and $self->{_default}->{date_format});
53 1 50 33     6 $self->{_default}->{permalink} = ":year/:month/:day/:title/" unless (exists $self->{_default}->{permalink} and $self->{_default}->{permalink});
54 1 50 33     17 $self->{_default}->{default_markup} = "markdown" unless (exists $self->{_default}->{default_markup} and $self->{_default}->{default_markup});
55 1 50 33     5 $self->{_default}->{default_extension} = "md" unless (exists $self->{_default}->{default_extension} and $self->{_default}->{default_extension});
56 1 50 33     7 $self->{_default}->{static} = "/static" unless (exists $self->{_default}->{static} and $self->{_default}->{static});
57              
58 1         12 my $glob = catfile($source, 'content', '*');
59              
60 1         14 my @floors = glob encode(locale_fs => $glob);
61 1         207 @floors = grep {-d} @floors;
  0         0  
62 1         2 @floors = map {decode(locale_fs => basename($_))} @floors;
  0         0  
63              
64 1         3 foreach my $floor (@floors)
65             {
66 0 0       0 if (-f catfile($source, 'config', "$floor.yml") )
67             {
68 0         0 my $flyaml = catfile($source, 'config', "$floor.yml");
69             {
70 0 0       0 open my $fh, '<:encoding(UTF-8)', $flyaml or die $!;
  0         0  
71 0         0 local $/ = undef;
72 0         0 $yaml = <$fh>;
73 0         0 close $fh;
74             }
75 0         0 my $floorconf;
76 0         0 eval { #must read yaml message and print it in error output
77 0         0 $floorconf = Load($yaml);
78             };
79 0 0       0 if ($@)
80             {
81 0         0 carp " # - $floor\.yml have problem, use default configs for floor: $floor";
82 0         0 $floorconf = _not_valids($floor, $self);
83             }
84 0         0 $self->{$floor} = $floorconf;
85 0 0       0 $self->{$floor}->{title} = $floor unless ($self->{$floor}->{title});
86 0 0       0 $self->{$floor}->{root} = "$self->{_default}->{root}/$floor/" unless ($self->{$floor}->{root});
87 0 0       0 $self->{$floor}->{url} = "$self->{_default}->{url}/$floor/" unless ($self->{$floor}->{url});
88 0 0       0 $self->{$floor}->{static} = "$self->{$floor}->{root}/static/" unless ($self->{$floor}->{static});
89             } else
90             {
91 0         0 $self->{$floor} = _not_valids($floor, $self);
92             }
93              
94 0         0 $self->{$floor}->{url} =~ s{(?<!:)/+}{/}g;
95             #$self->{$floor}->{url} =~ s{((?:(?!.*?:)/+))}{/}g;
96 0         0 $self->{$floor}->{root} =~ s{^(.*?):/+}{/}g;
97 0         0 $self->{$floor}->{root} =~ s{/+}{/}g;
98 0         0 $self->{$floor}->{static} =~ s{/+}{/}g;
99              
100 0         0 foreach my $key (keys %{ $self->{_default} })
  0         0  
101             {
102             $self->{$floor}->{$key} = $self->{_default}->{$key}
103 0 0       0 if (not exists $self->{$floor}->{$key});
104             }
105              
106             }
107              
108 1         5 return $self;
109             }
110              
111             sub _not_valids {
112 0     0     my $floor = shift;
113 0           my $self = shift;
114 0           my $configs = {
115             title => "$floor",
116             root => "$self->{_default}->{root}/$floor/",
117             url => "$self->{_default}->{url}/$floor",
118             static => "$self->{_default}->{root}/$floor/static",
119             };
120              
121 0           return $configs;
122             }
123              
124             1;