| 1 | 12 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 12 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | package Rubric::Entry::Formatter 0.157; | ||||||||||
| 4 | # ABSTRACT: a base class for entry body formatters | ||||||||||
| 5 | |||||||||||
| 6 | #pod =head1 DESCRIPTION | ||||||||||
| 7 | #pod | ||||||||||
| 8 | #pod This class serves as a single point of dispatch for attempts to format entry | ||||||||||
| 9 | #pod bodies from their native format into rendered output. | ||||||||||
| 10 | #pod | ||||||||||
| 11 | #pod =cut | ||||||||||
| 12 | |||||||||||
| 13 | 12 | ▶ | use Carp (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 12 | ▶ | use Rubric::Config; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | #pod =head1 METHODS | ||||||||||
| 17 | #pod | ||||||||||
| 18 | #pod =head2 C< format > | ||||||||||
| 19 | #pod | ||||||||||
| 20 | #pod my $formatted = Rubric::Entry::Formatter->format(\%arg); | ||||||||||
| 21 | #pod | ||||||||||
| 22 | #pod This method accepts a set of named arguments and returns formatted output in | ||||||||||
| 23 | #pod the requested format. If it is unable to do so, it throws an exception. | ||||||||||
| 24 | #pod | ||||||||||
| 25 | #pod Valid arguments are: | ||||||||||
| 26 | #pod | ||||||||||
| 27 | #pod markup - the markup format used to mark up the text (default: _default) | ||||||||||
| 28 | #pod text - the text that has been marked up and should be formatted (required) | ||||||||||
| 29 | #pod format - the requested output format (required) | ||||||||||
| 30 | #pod | ||||||||||
| 31 | #pod Formatting requests are dispatched according to the configuration in | ||||||||||
| 32 | #pod C<markup_formatter>. | ||||||||||
| 33 | #pod | ||||||||||
| 34 | #pod =cut | ||||||||||
| 35 | |||||||||||
| 36 | 18 | ▶ | sub _load_formatter { | ||||||||
|
Subroutine
_load_formatter: called CC=3 SCAR=11.3
| |||||||||||
| 37 | 18 | my ($class, $formatter) = @_; | |||||||||
| 38 | |||||||||||
| 39 | 18 | ▶ | return 1 if eval { $formatter->can('as_text'); }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | |||||||||||
| 41 | ## no critic (StringyEval) | ||||||||||
| 42 | 3 | ▶ | return 1 if eval qq{require $formatter}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | ## use critic | ||||||||||
| 44 | |||||||||||
| 45 | 0 | return 0; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 18 | ▶ | sub _formatter_for { | ||||||||
|
Subroutine
_formatter_for: called CC=3 SCAR=11.0
| |||||||||||
| 49 | 18 | my ($class, $markup) = @_; | |||||||||
| 50 | |||||||||||
| 51 | 18 | my $markup_formatter = Rubric::Config->markup_formatter; | |||||||||
| 52 | $markup_formatter->{_default} = 'Rubric::Entry::Formatter::Nil' | ||||||||||
| 53 | 18 | ▶ | unless $markup_formatter->{_default}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | |||||||||||
| 55 | Carp::croak "no formatter is registered for $markup markup" | ||||||||||
| 56 | 18 | ▶ | unless my $formatter = $markup_formatter->{ $markup }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | |||||||||||
| 58 | 18 | return $formatter; | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 18 | ▶ | sub format { | ||||||||
|
Subroutine
format: called CC=5 SCAR=19.1
Pod
documented
| |||||||||||
| 62 | 18 | my ($class, $arg) = @_; | |||||||||
| 63 | 18 | my $config = {}; # extra configuration for formatter code | |||||||||
| 64 | |||||||||||
| 65 | 18 | my $formatter = $class->_formatter_for($arg->{markup}); | |||||||||
| 66 | |||||||||||
| 67 | 18 | ▶ | if (ref $formatter) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 0 | $config = { %$formatter }; | |||||||||
| 69 | Carp::croak "formatter config for $arg->{markup} includes no class" | ||||||||||
| 70 | 0 | ▶ | unless $formatter = delete $config->{class}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 18 | ▶ | $class->_load_formatter($formatter) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | or Carp::croak "couldn't load formatter '$formatter': $@"; | ||||||||||
| 75 | |||||||||||
| 76 | 18 | ▶ | my $formatter_code = $formatter->can("as_$arg->{format}") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | or Carp::croak "$formatter does not implement formatting to $arg->{format}"; | ||||||||||
| 78 | |||||||||||
| 79 | 18 | $formatter_code->($formatter, $arg, $config); | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | #pod =head1 WRITING FORMATTERS | ||||||||||
| 83 | #pod | ||||||||||
| 84 | #pod Writing a formatter should be very simple; the interface is very simple, | ||||||||||
| 85 | #pod although it's also very young and so it may change when I figure out the | ||||||||||
| 86 | #pod problems in the current implementation. | ||||||||||
| 87 | #pod | ||||||||||
| 88 | #pod A formatter must implement an C<as_FORMAT> method for each format to which it | ||||||||||
| 89 | #pod claims to be able to output formatted text. When Rubric::Entry::Formatter | ||||||||||
| 90 | #pod wants to dispatch text for formatting, it will call that method as follows: | ||||||||||
| 91 | #pod | ||||||||||
| 92 | #pod my $formatted = Formatter->as_whatever(\%arg); | ||||||||||
| 93 | #pod | ||||||||||
| 94 | #pod The arguments in C<%arg> will be the same as those passed to | ||||||||||
| 95 | #pod Rubric::Entry::Formatter. | ||||||||||
| 96 | #pod | ||||||||||
| 97 | #pod Actually, the method is found and called via C<can>, so a suitably programmed | ||||||||||
| 98 | #pod module can respond to C<can> to allow it to render into all the format it likes | ||||||||||
| 99 | #pod -- or at least to claim to. | ||||||||||
| 100 | #pod | ||||||||||
| 101 | #pod =cut | ||||||||||
| 102 | |||||||||||
| 103 | 12 | 1; | |||||||||
| 104 | |||||||||||
| 105 | __END__ | ||||||||||