« blib/lib/Web/DataService/Plugin/TemplateToolkit.pm
↑ Summary
blib/lib/Web/DataService/IDocument.pm »
| 1 | # | ||
| 2 | # Web::DataService::Render | ||
| 3 | # | ||
| 4 | # This module provides a role that is used by 'Web::DataService'. It | ||
| 5 | # implements routines for generating documentation pages and output pages by | ||
| 6 | # rendering templates. | ||
| 7 | # | ||
| 8 | # Author: Michael McClennen | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | |||
| 12 | package Web::DataService::Render; | ||
| 13 | |||
| 14 | use Carp 'croak'; | ||
| 15 | |||
| 16 | use Moo::Role; | ||
| 17 | |||
| 18 | |||
| 19 | # check_doc ( template ) | ||
| 20 | # | ||
| 21 | # Return the given template filename if the corresponding file path exists and | ||
| 22 | # is readable under the documentation template directory. Throw an exception | ||
| 23 | # if the file exists but is not readable. Return undefined (false) if the | ||
| 24 | # file is not there. | ||
| 25 | |||
| 26 | sub check_doc { | ||
| 27 | |||
| 28 | my ($ds, $template_path) = @_; | ||
| 29 | |||
| 30 | return unless defined $template_path && $template_path ne ''; | ||
| 31 | |||
| 32 | my $full_path = $Web::DataService::FOUNDATION->file_path($ds->{doc_template_dir}, $template_path); | ||
| 33 | |||
| 34 | if ( -e $full_path ) | ||
| 35 | { | ||
| 36 | return $template_path if -r $full_path; | ||
| 37 | croak "template $template_path: $!"; | ||
| 38 | } | ||
| 39 | |||
| 40 | return; | ||
| 41 | } | ||
| 42 | |||
| 43 | |||
| 44 | # read_doc_partial ( path ) | ||
| 45 | # | ||
| 46 | # Read the first 2048 characters of the specified template file and return | ||
| 47 | # them. | ||
| 48 | |||
| 49 | sub read_doc_partial { | ||
| 50 | |||
| 51 | my ($ds, $template_path) = @_; | ||
| 52 | |||
| 53 | my $full_path = $Web::DataService::FOUNDATION->file_path($ds->{doc_template_dir}, $template_path); | ||
| 54 | |||
| 55 | open(my $fh, "<", $full_path) || die "cannot read documentation template '$full_path': $!"; | ||
| 56 | sysread($fh, my $contents, 2048); | ||
| 57 | |||
| 58 | return $contents; | ||
| 59 | } | ||
| 60 | |||
| 61 | |||
| 62 | # render_doc ( template, definitions, header, footer, vars ) | ||
| 63 | # | ||
| 64 | # Render the specified template, with the specified parameters. If | ||
| 65 | # $definitions and/or $header are defined, process them before the main | ||
| 66 | # template. If $footer is defined, process it after the main template. If | ||
| 67 | # $vars is defined, it must be a hashref of variable definitions that will be | ||
| 68 | # passed to the template rendering engine. | ||
| 69 | |||
| 70 | sub render_doc { | ||
| 71 | |||
| 72 | my ($ds, $main, $defs, $header, $footer, $vars) = @_; | ||
| 73 | |||
| 74 | # Throw an exception if no templating module was selected. | ||
| 75 | |||
| 76 | croak "you must select a templating module" | ||
| 77 | unless defined $ds->{templating_plugin}; | ||
| 78 | |||
| 79 | my $templates = { defs => $defs, header => $header, | ||
| 80 | main => $main, footer => $footer }; | ||
| 81 | |||
| 82 | $ds->{templating_plugin}->render_template($ds, $ds->{doc_engine}, $vars, $templates); | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | # check_output ( template ) | ||
| 87 | # | ||
| 88 | # Return the given template filename if the corresponding file path exists and | ||
| 89 | # is readable under the output template directory. Throw an exception if the | ||
| 90 | # file exists but is not readable. Return undefined (false) if the file is | ||
| 91 | # not there. | ||
| 92 | |||
| 93 | sub check_output { | ||
| 94 | |||
| 95 | my ($ds, $template) = @_; | ||
| 96 | |||
| 97 | return unless defined $template && $template ne ''; | ||
| 98 | |||
| 99 | my $template_file = $Web::DataService::FOUNDATION->file_path($ds->{output_templates}, $template); | ||
| 100 | |||
| 101 | if ( -e $template_file ) | ||
| 102 | { | ||
| 103 | return $template if -r $template_file; | ||
| 104 | croak "template $template_file: $!"; | ||
| 105 | } | ||
| 106 | |||
| 107 | return; | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | # render_output ( template, definitions, header, footer, vars ) | ||
| 112 | # | ||
| 113 | # Render the specified template, with the specified parameters. If | ||
| 114 | # $definitions and/or $header are defined, process them before the main | ||
| 115 | # template. If $footer is defined, process it after the main template. If | ||
| 116 | # $vars is defined, it must be a hashref of variable definitions that will be | ||
| 117 | # passed to the template rendering engine. | ||
| 118 | |||
| 119 | sub render_output { | ||
| 120 | |||
| 121 | my $ds = shift; | ||
| 122 | |||
| 123 | # Throw an exception if no templating module was selected. | ||
| 124 | |||
| 125 | croak "you must select a templating module" | ||
| 126 | unless defined $ds->{templating_plugin}; | ||
| 127 | |||
| 128 | $ds->{templating_plugin}->render_template($ds->{doc_templates}, @_); | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | 1; |