File Coverage

blib/lib/Web/MREST/Util.pm
Criterion Covered Total %
statement 38 44 86.3
branch n/a
condition n/a
subroutine 11 12 91.6
pod 2 2 100.0
total 51 58 87.9


line stmt bran cond sub pod time code
1             # *************************************************************************
2             # Copyright (c) 2014-2016, SUSE LLC
3             #
4             # All rights reserved.
5             #
6             # Redistribution and use in source and binary forms, with or without
7             # modification, are permitted provided that the following conditions are met:
8             #
9             # 1. Redistributions of source code must retain the above copyright notice,
10             # this list of conditions and the following disclaimer.
11             #
12             # 2. Redistributions in binary form must reproduce the above copyright
13             # notice, this list of conditions and the following disclaimer in the
14             # documentation and/or other materials provided with the distribution.
15             #
16             # 3. Neither the name of SUSE LLC nor the names of its contributors may be
17             # used to endorse or promote products derived from this software without
18             # specific prior written permission.
19             #
20             # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21             # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22             # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23             # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24             # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25             # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26             # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27             # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28             # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29             # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30             # POSSIBILITY OF SUCH DAMAGE.
31             # *************************************************************************
32              
33             package Web::MREST::Util;
34              
35 22     22   502 use 5.012;
  22         83  
36 22     22   129 use strict;
  22         51  
  22         547  
37 22     22   112 use warnings;
  22         71  
  22         786  
38              
39 22     22   127 use App::CELL qw( $log );
  22         56  
  22         1917  
40 22     22   168 use File::Spec;
  22         56  
  22         712  
41 22     22   128 use JSON;
  22         52  
  22         211  
42 22     22   2699 use Params::Validate qw( :all );
  22         58  
  22         3675  
43 22     22   9224 use Pod::Simple::HTML;
  22         832128  
  22         932  
44 22     22   7823 use Pod::Simple::Text;
  22         94079  
  22         1491  
45              
46              
47             our $JSON = JSON->new->allow_nonref->convert_blessed->utf8->pretty;
48              
49              
50              
51             =head1 NAME
52              
53             Web::MREST::Util - Miscellaneous utilities
54              
55              
56              
57              
58             =head1 SYNOPSIS
59              
60             Miscellaneous utilities
61              
62              
63              
64              
65              
66             =head1 EXPORTS
67              
68             This module provides the following exports:
69              
70             =over
71              
72             =item C<$JSON> (singleton)
73              
74             =item C<pod_to_html> (function)
75              
76             =item C<pod_to_text> (function)
77              
78             =back
79              
80             =cut
81              
82 22     22   177 use Exporter qw( import );
  22         52  
  22         4716  
83             our @EXPORT_OK = qw(
84             $JSON
85             pod_to_html
86             pod_to_text
87             );
88              
89              
90              
91              
92             =head1 FUNCTIONS
93              
94              
95             =head2 pod_to_html
96              
97             Every L<Web::MREST> resource definition includes a 'documentation'
98             property containing a POD string. Our 'docu/html' resource converts this
99             POD string into HTML with a little help from this routine.
100              
101             =cut
102              
103             sub pod_to_html {
104 1     1 1 3 my ( $pod_str ) = @_;
105 1         8 $log->debug( "Entering " . __PACKAGE__ . "::pod_to_html" );
106              
107             #$log->debug( "pod_to_html before: $pod_str" );
108 1         141 my $p = Pod::Simple::HTML->new;
109 1         461 $p->output_string(\my $html_str);
110 1         906 $p->parse_string_document($pod_str);
111              
112             # now $html_str contains a full-blown HTML file, of which only one part is
113             # of interest to us. That part starts with the line <!-- start doc --> and
114             # ends with <!-- end doc -->
115              
116 1         1889 $html_str =~ s/.*<!-- start doc -->//s;
117 1         10 $html_str =~ s/<!-- end doc -->.*//s;
118              
119 1         9 $log->debug( "pod_to_html after: $html_str" );
120 1         193 return $html_str;
121             }
122              
123              
124             =head2 pod_to_text
125              
126             Convert POD string into text
127              
128             =cut
129              
130             sub pod_to_text {
131 0     0 1   my $pod_str = shift;
132 0           $log->debug( "Entering " . __PACKAGE__ . "::pod_to_text" );
133              
134 0           my $p = Pod::Simple::Text->new;
135 0           $p->output_string(\my $text_str);
136 0           $p->parse_string_document($pod_str);
137 0           return $text_str;
138             }
139              
140              
141             1;