File Coverage

blib/lib/Rubric/Entry/Formatter.pm
Criterion Covered Total %
statement 28 31 90.3
branch 9 16 56.2
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 45 55 81.8


line stmt bran cond sub pod time code
1 12     12   62 use strict;
  12         17  
  12         275  
2 12     12   49 use warnings;
  12         15  
  12         372  
3             # ABSTRACT: a base class for entry body formatters
4              
5             #pod =head1 DESCRIPTION
6             #pod
7             #pod This class serves as a single point of dispatch for attempts to format entry
8             #pod bodies from their native format into rendered output.
9             #pod
10             #pod =cut
11              
12             use Carp ();
13 12     12   48 use Rubric::Config;
  12         26  
  12         155  
14 12     12   44  
  12         18  
  12         79  
15             #pod =head1 METHODS
16             #pod
17             #pod =head2 C< format >
18             #pod
19             #pod my $formatted = Rubric::Entry::Formatter->format(\%arg);
20             #pod
21             #pod This method accepts a set of named arguments and returns formatted output in
22             #pod the requested format. If it is unable to do so, it throws an exception.
23             #pod
24             #pod Valid arguments are:
25             #pod
26             #pod markup - the markup format used to mark up the text (default: _default)
27             #pod text - the text that has been marked up and should be formatted (required)
28             #pod format - the requested output format (required)
29             #pod
30             #pod Formatting requests are dispatched according to the configuration in
31             #pod C<markup_formatter>.
32             #pod
33             #pod =cut
34              
35             my ($class, $formatter) = @_;
36              
37 18     18   85 return 1 if eval { $formatter->can('as_text'); };
38              
39 18 100       28 ## no critic (StringyEval)
  18         131  
40             return 1 if eval qq{require $formatter};
41             ## use critic
42 3 50       130  
43             return 0;
44             }
45 0         0  
46             my ($class, $markup) = @_;
47              
48             my $markup_formatter = Rubric::Config->markup_formatter;
49 18     18   28 $markup_formatter->{_default} = 'Rubric::Entry::Formatter::Nil'
50             unless $markup_formatter->{_default};
51 18         62  
52             Carp::croak "no formatter is registered for $markup markup"
53 18 100       48 unless my $formatter = $markup_formatter->{ $markup };
54              
55             return $formatter;
56 18 50       43 }
57              
58 18         32 my ($class, $arg) = @_;
59             my $config = {}; # extra configuration for formatter code
60              
61             my $formatter = $class->_formatter_for($arg->{markup});
62 18     18 1 1713  
63 18         30 if (ref $formatter) {
64             $config = { %$formatter };
65 18         44 Carp::croak "formatter config for $arg->{markup} includes no class"
66             unless $formatter = delete $config->{class};
67 18 50       172 }
68 0         0  
69             $class->_load_formatter($formatter)
70 0 0       0 or Carp::croak "couldn't load formatter '$formatter': $@";
71              
72             my $formatter_code = $formatter->can("as_$arg->{format}")
73 18 50       40 or Carp::croak "$formatter does not implement formatting to $arg->{format}";
74              
75             $formatter_code->($formatter, $arg, $config);
76 18 50       93 }
77              
78             #pod =head1 WRITING FORMATTERS
79 18         53 #pod
80             #pod Writing a formatter should be very simple; the interface is very simple,
81             #pod although it's also very young and so it may change when I figure out the
82             #pod problems in the current implementation.
83             #pod
84             #pod A formatter must implement an C<as_FORMAT> method for each format to which it
85             #pod claims to be able to output formatted text. When Rubric::Entry::Formatter
86             #pod wants to dispatch text for formatting, it will call that method as follows:
87             #pod
88             #pod my $formatted = Formatter->as_whatever(\%arg);
89             #pod
90             #pod The arguments in C<%arg> will be the same as those passed to
91             #pod Rubric::Entry::Formatter.
92             #pod
93             #pod Actually, the method is found and called via C<can>, so a suitably programmed
94             #pod module can respond to C<can> to allow it to render into all the format it likes
95             #pod -- or at least to claim to.
96             #pod
97             #pod =cut
98              
99             1;
100              
101              
102             =pod
103              
104             =encoding UTF-8
105              
106             =head1 NAME
107              
108             Rubric::Entry::Formatter - a base class for entry body formatters
109              
110             =head1 VERSION
111              
112             version 0.157
113              
114             =head1 DESCRIPTION
115              
116             This class serves as a single point of dispatch for attempts to format entry
117             bodies from their native format into rendered output.
118              
119             =head1 PERL VERSION
120              
121             This code is effectively abandonware. Although releases will sometimes be made
122             to update contact info or to fix packaging flaws, bug reports will mostly be
123             ignored. Feature requests are even more likely to be ignored. (If someone
124             takes up maintenance of this code, they will presumably remove this notice.)
125             This means that whatever version of perl is currently required is unlikely to
126             change -- but also that it might change at any new maintainer's whim.
127              
128             =head1 METHODS
129              
130             =head2 C< format >
131              
132             my $formatted = Rubric::Entry::Formatter->format(\%arg);
133              
134             This method accepts a set of named arguments and returns formatted output in
135             the requested format. If it is unable to do so, it throws an exception.
136              
137             Valid arguments are:
138              
139             markup - the markup format used to mark up the text (default: _default)
140             text - the text that has been marked up and should be formatted (required)
141             format - the requested output format (required)
142              
143             Formatting requests are dispatched according to the configuration in
144             C<markup_formatter>.
145              
146             =head1 WRITING FORMATTERS
147              
148             Writing a formatter should be very simple; the interface is very simple,
149             although it's also very young and so it may change when I figure out the
150             problems in the current implementation.
151              
152             A formatter must implement an C<as_FORMAT> method for each format to which it
153             claims to be able to output formatted text. When Rubric::Entry::Formatter
154             wants to dispatch text for formatting, it will call that method as follows:
155              
156             my $formatted = Formatter->as_whatever(\%arg);
157              
158             The arguments in C<%arg> will be the same as those passed to
159             Rubric::Entry::Formatter.
160              
161             Actually, the method is found and called via C<can>, so a suitably programmed
162             module can respond to C<can> to allow it to render into all the format it likes
163             -- or at least to claim to.
164              
165             =head1 AUTHOR
166              
167             Ricardo SIGNES <rjbs@semiotic.systems>
168              
169             =head1 COPYRIGHT AND LICENSE
170              
171             This software is copyright (c) 2004 by Ricardo SIGNES.
172              
173             This is free software; you can redistribute it and/or modify it under
174             the same terms as the Perl 5 programming language system itself.
175              
176             =cut