blib/lib/Yukki/Web/View.pm

filter: Statementstmt 93.1% 27 / 29 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 94.8% 37 / 39 CC 1 SCAR 0.3
CC
1
Cov
93%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Yukki.pm ↑ Summary
1 package Yukki::Web::View;
2 {
3 $Yukki::Web::View::VERSION = '0.140290';
4 }
5 8 ▶use 5.12.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ▶use MooseX::Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Scalar::Util qw( blessed reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Spreadsheet::Engine;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Template::Semantic;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Text::MultiMarkdown;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 0 ▶use XML::Twig;
Subroutine BEGIN: called CC=1 SCAR=2.6
16  
17 # ABSTRACT: base class for Yukki::Web views
18  
19  
20 has app => (
21 is => 'ro',
22 isa => 'Yukki::Web',
23 required => 1,
24 weak_ref => 1,
25 handles => 'Yukki::Role::App',
26 );
27  
28  
29 has markdown => (
30 is => 'ro',
31 isa => 'Text::MultiMarkdown',
32 required => 1,
33 lazy_build => 1,
34 handles => {
35 'format_markdown' => 'markdown',
36 },
37 );
38  
39 sub _build_markdown {
40 Text::MultiMarkdown->new(
41 markdown_in_html_blocks => 1,
42 heading_ids => 0,
43 );
44 }
45  
46  
47 has semantic => (
48 is => 'ro',
49 isa => 'Template::Semantic',
50 required => 1,
51 lazy_build => 1,
52 );
53  
54 sub _build_semantic {
55 my $self = shift;
56  
57 my $semantic = Template::Semantic->new;
58  
59 # TODO Maybe nice to have?
60 # $semantic->define_filter(markdown => sub { \ $self->format_markdown($_) });
61 # $semantic->define_filter(yukkitext => sub { \ $self->yukkitext($_) });
62  
63 return $semantic;
64 }
65  
66  
67 sub render_page {
68 my ($self, $template, $ctx, $vars) = validated_list(\@_,
69 template => { isa => 'Str', coerce => 1 },
70 context => { isa => 'Yukki::Web::Context' },
71 vars => { isa => 'HashRef', default => {} },
72 );
73  
74 my $messages = $self->render(
75 template => 'messages.html',
76 vars => {
77 '.error' => [ map { +{ '.' => $_ } } $ctx->list_errors ],
78 '.warning' => [ map { +{ '.' => $_ } } $ctx->list_warnings ],
79 '.info' => [ map { +{ '.' => $_ } } $ctx->list_info ],
80 },
81 );
82  
83 my ($main_title, $title);
84 if ($ctx->response->has_page_title) {
85 $title = $ctx->response->page_title;
86 $main_title = $ctx->response->page_title . ' - Yukki';
87 }
88 else {
89 $title = $main_title = 'Yukki';
90 }
91  
92 my $b = sub { $ctx->rebase_url($_[0]) };
93 my %menu_vars = map {
94 ("#nav-$_ .navigation" => [ map {
95 { 'a' => $_->{label}, 'a@href' => $b->($_->{href}) },
96 } $self->available_menu_items($ctx, $_) ])
97 } $ctx->response->navigation_menu_names;
98  
99 $menu_vars{"#nav-$_ .navigation"} //= []
100 for (@{ $self->app->settings->menu_names });
101  
102 my @scripts = $self->app->settings->all_scripts;
103 my @styles = $self->app->settings->all_styles;
104  
105 my $view = $ctx->request->parameters->{view} // 'default';
106 my $view_args = $self->app->settings->page_views->{ $view }
107 // { template => 'shell.html' };
108 $view_args->{vars} //= {};
109  
110 return $self->render(
111 template => $view_args->{template},
112 vars => {
113 %{ $view_args->{vars} },
114 'head script.local' => [
115 map { { '@src' => $b->($_) } }
116 (@scripts, @{ $view_args->{vars}{'head script.local'} }) ],
117 'head link.local' => [
118 map { { '@href' => $b->($_) } }
119 (@styles, @{ $view_args->{vars}{'head link.local'} }) ],
120 '#messages' => $messages,
121 'title' => $main_title,
122 '.masthead-title' => $title,
123 %menu_vars,
124 '#breadcrumb li' => [ map {
125 { 'a' => $_->{label}, 'a@href' => $b->($_->{href}) },
126 } $ctx->response->breadcrumb_links ],
127 '#content' => $self->render(template => $template, vars => $vars),
128 },
129 )->{dom}->toStringHTML;
130 }
131  
132  
133 sub available_menu_items {
134 my ($self, $ctx, $name) = @_;
135  
136 return grep {
137 my $url = $_->{href}; $url =~ s{\?.*$}{};
138  
139 my $match = $self->app->router->match($url);
140 return unless $match;
141 my $access_level_needed = $match->access_level;
142 $self->check_access(
143 user => $ctx->session->{user},
144 repository => $match->mapping->{repository} // '-',
145 needs => $access_level_needed,
146 );
147 } $ctx->response->navigation_menu($name);
148 }
149  
150  
151 sub render_links {
152 my ($self, $ctx, $links) = validated_list(\@_,
153 context => { isa => 'Yukki::Web::Context' },
154 links => { isa => 'ArrayRef[HashRef]' },
155 );
156  
157 my $b = sub { $ctx->rebase_url($_[0]) };
158  
159 return $self->render(
160 template => 'links.html',
161 vars => {
162 'li' => [ map {
163 { 'a' => $_->{label}, 'a@href' => $b->($_->{href}) },
164 } @$links ],
165 },
166 );
167 }
168  
169  
170 sub render {
171 my ($self, $template, $vars) = validated_list(\@_,
172 template => { isa => 'Str', coerce => 1 },
173 vars => { isa => 'HashRef', default => {} },
174 );
175
176 my $template_file = $self->locate('template_path', $template);
177
178 return $self->semantic->process($template_file, $vars);
179 }
180  
181 1;
182  
183 __END__
« blib/lib/Yukki.pm ↑ Summary