File Coverage

blib/lib/PFT/Text.pm
Criterion Covered Total %
statement 44 44 100.0
branch 8 10 80.0
condition n/a
subroutine 11 11 100.0
pod 3 4 75.0
total 66 69 95.6


line stmt bran cond sub pod time code
1             # Copyright 2014-2016 - Giovanni Simoni
2             #
3             # This file is part of PFT.
4             #
5             # PFT is free software: you can redistribute it and/or modify it under the
6             # terms of the GNU General Public License as published by the Free
7             # Software Foundation, either version 3 of the License, or (at your
8             # option) any later version.
9             #
10             # PFT is distributed in the hope that it will be useful, but WITHOUT ANY
11             # WARRANTY; without even the implied warranty of MERCHANTABILITY or
12             # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13             # for more details.
14             #
15             # You should have received a copy of the GNU General Public License along
16             # with PFT. If not, see .
17             #
18             package PFT::Text v1.3.0;
19              
20             =encoding utf8
21              
22             =head1 NAME
23              
24             PFT::Text - Wrapper around content text
25              
26             =head1 SYNOPSIS
27              
28             PFT::Text->new($page);
29              
30             =head1 DESCRIPTION
31              
32             Semantic wrapper around content text.
33              
34             It knows how the text should be parsed, abstracts away inner data
35             retrieval.
36              
37             The constructor expects a C object as parameter.
38              
39             =cut
40              
41 3     3   19 use utf8;
  3         5  
  3         15  
42 3     3   93 use v5.16;
  3         10  
43 3     3   15 use strict;
  3         6  
  3         64  
44 3     3   15 use warnings;
  3         5  
  3         90  
45              
46 3     3   2025 use PFT::Text::Symbol;
  3         7  
  3         93  
47 3     3   1775 use Text::Markdown qw/markdown/;
  3         56231  
  3         176  
48 3     3   22 use Carp;
  3         6  
  3         944  
49              
50             sub new {
51 17     17 0 33 my $cls = shift;
52 17         21 my $page = shift;
53              
54 17 50       60 confess 'Expecting PFT::Content::Entry'
55             unless $page->isa('PFT::Content::Entry');
56              
57 17         97 bless {
58             page => $page,
59             html => undef,
60             }, $cls;
61             }
62              
63             =head2 Properties
64              
65             =over 1
66              
67             =item html
68              
69             Returns the content in HTML form.
70              
71             The conetnet will retain symbol placeholders. See the C method.
72              
73             =cut
74              
75             sub html {
76 29     29 1 40 my $self = shift;
77 29         56 my $html = $self->{html};
78 29 100       72 return $html if defined $html;
79              
80 17         24 my $md = do {
81 17         26 my $page = $self->{page};
82 17 50       46 confess "$page is virtual" unless $page->exists;
83 17         113 my $fd = $page->read;
84 17         67 local $/ = undef;
85 17         673 <$fd>;
86             };
87 17 100       130 $self->{html} = defined $md ? markdown($md) : ''
88             }
89              
90             =item symbols
91              
92             =cut
93              
94             sub symbols {
95 23     23 1 36 my $self = shift;
96 23         50 PFT::Text::Symbol->scan_html($self->html);
97             }
98              
99             =item html_resolved
100              
101             Given an ordered list of HTML representation of symbols, returns the
102             complete HTML with symbol placeholders replaced.
103              
104             The strings must be ordered consistently with a previoulsy retrieved list
105             of symbols (wretrieved with the C method.
106              
107             Something like:
108              
109             $text->html_resolved(map resolve($_), $text->symbols)
110              
111             =cut
112              
113             sub html_resolved {
114 6     6 1 25 my $self = shift;
115              
116 6         15 my $html = $self->html;
117 6         10 my $offset = 0;
118 6         12 for my $sym ($self->symbols) {
119 12 100       27 if (my $repl = shift) {
120 9         23 substr($html, $offset + $sym->start, $sym->len) = $repl;
121 9         26 $offset += length($repl) - $sym->len
122             }
123             }
124 6         23 $html;
125             }
126              
127             =back
128              
129             =cut
130              
131             1;