Silki/Help/Dir.pm untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 71 CC 2 SCAR 10.0
CC
2
Cov
0%
_build_files
6.0
_build_content
2.0
BEGIN
2.0
CRAP 2.7
1 package Silki::Help::Dir;
2 {
3 $Silki::Help::Dir::VERSION = '0.29';
4 }
5  
6 use strict;
7 use warnings;
8 use namespace::autoclean;
9  
10 use Silki::Config;
11 use Silki::Help::File;
12 use Silki::Types qw( ArrayRef Str );
13 use Text::TOC::HTML;
14  
15 use Moose;
16  
17 has locale_code => (
18 is => 'ro',
19 isa => Str,
20 required => 1,
21 );
22  
23 has _files => (
24 traits => ['Array'],
25 is => 'ro',
26 isa => ArrayRef ['Silki::Help::File'],
27 init_arg => undef,
28 lazy => 1,
29 builder => '_build_files',
30 handles => { files => 'elements' },
31 );
32  
33 has content => (
34 is => 'ro',
35 isa => Str,
36 init_arg => undef,
37 lazy => 1,
38 builder => '_build_content',
39 );
40  
41 # XXX - need some sane locale fallback, and possibly also handling of partial
42 # translations.
43 sub _build_files {
44 my $self = shift;
45  
46 my $help_dir = Silki::Config->instance()->share_dir()->subdir('help');
47  
48 my $lang_dir = $help_dir->subdir( $self->locale_code() );
49  
50 die "No $lang_dir exists (bad locale?)"
51 unless -d $lang_dir;
52  
53 return [
54 map {
55 Silki::Help::File->new(
56 file => $_,
57 locale_code => $self->locale_code(),
58 )
59 }
60 sort { $a cmp $b }
61 grep { !$_->is_dir() } $lang_dir->children()
62 ];
63 }
64  
65 my $toc_filter = sub {
66 my $node = shift;
67  
68 return if $node->parentNode()->className() =~ /markdown-output/;
69  
70 return $node->tagName() =~ /^h[2-4]$/i;
71 };
72  
73 sub _build_content {
74 my $self = shift;
75  
76 my $toc = Text::TOC::HTML->new( filter => $toc_filter );
77  
78 $toc->add_file(
79 file => $_->file(),
80 content => $_->content(),
81 ) for $self->files();
82  
83 my $page = $toc->html_for_toc();
84 $page .= "\n";
85 $page .= join "\n",
86 map { $toc->html_for_document( $_->file() ) } $self->files();
87  
88 $page =~ s{<html><head></head><body>(.+)</body></html>}{$1}s;
89  
90 return $page;
91 }
92  
93 __PACKAGE__->meta()->make_immutable();
94  
95 1;
96  
97 # ABSTRACT: A directory of help files
98  
99 __END__