| 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 | ▶ | use 5.012; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 36 | 22 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 37 | 22 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 38 | |||
| 39 | 22 | ▶ | use App::CELL qw( $log ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 40 | 22 | ▶ | use File::Spec; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 41 | 22 | ▶ | use JSON; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 42 | 22 | ▶ | use Params::Validate qw( :all ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 43 | 22 | ▶ | use Pod::Simple::HTML; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 44 | 22 | ▶ | use Pod::Simple::Text; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 45 | |||
| 46 | |||
| 47 | 22 | 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 | ▶ | use Exporter qw( import ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 83 | 22 | 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 | 1 | ▶ | sub pod_to_html { |
|
Subroutine
pod_to_html: called CC=1 SCAR=0.0
Pod
documented
| |||
| 104 | 1 | my ( $pod_str ) = @_; | |
| 105 | 1 | $log->debug( "Entering " . __PACKAGE__ . "::pod_to_html" ); | |
| 106 | |||
| 107 | #$log->debug( "pod_to_html before: $pod_str" ); | ||
| 108 | 1 | my $p = Pod::Simple::HTML->new; | |
| 109 | 1 | $p->output_string(\my $html_str); | |
| 110 | 1 | $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 | $html_str =~ s/.*<!-- start doc -->//s; | |
| 117 | 1 | $html_str =~ s/<!-- end doc -->.*//s; | |
| 118 | |||
| 119 | 1 | $log->debug( "pod_to_html after: $html_str" ); | |
| 120 | 1 | return $html_str; | |
| 121 | } | ||
| 122 | |||
| 123 | |||
| 124 | =head2 pod_to_text | ||
| 125 | |||
| 126 | Convert POD string into text | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | 0 | ▶ | sub pod_to_text { |
|
Subroutine
pod_to_text: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 131 | 0 | 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 | 22 | 1; | |