| 1 | # | ||
| 2 | # This file is part of Pod-Weaver-Role-Section-Formattable | ||
| 3 | # | ||
| 4 | # This software is Copyright (c) 2012 by Chris Weyl. | ||
| 5 | # | ||
| 6 | # This is free software, licensed under: | ||
| 7 | # | ||
| 8 | # The GNU Lesser General Public License, Version 2.1, February 1999 | ||
| 9 | # | ||
| 10 | package Pod::Weaver::Role::Section::Formattable; | ||
| 11 | our $AUTHORITY = 'cpan:RSRCHBOY'; | ||
| 12 | # git description: 64fd832 | ||
| 13 | $Pod::Weaver::Role::Section::Formattable::VERSION = '0.001'; | ||
| 14 | |||
| 15 | # ABSTRACT: Role for a formattable section | ||
| 16 | |||
| 17 | 0 | ▶ | use Moose::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 18 | use namespace::autoclean; | ||
| 19 | use MooseX::AttributeShortcuts; | ||
| 20 | use MooseX::CurriedDelegation; | ||
| 21 | use MooseX::Types::Common::String ':all'; | ||
| 22 | use Moose::Autobox; | ||
| 23 | use Moose::Util::TypeConstraints 'class_type'; | ||
| 24 | |||
| 25 | with 'Pod::Weaver::Role::Section'; | ||
| 26 | |||
| 27 | use String::Formatter; | ||
| 28 | |||
| 29 | # debugging... | ||
| 30 | #use Smart::Comments '###'; | ||
| 31 | |||
| 32 | |||
| 33 | sub codes { | ||
| 34 | my ($self) = @_; | ||
| 35 | |||
| 36 | my %codes = ( | ||
| 37 | V => sub { shift->{version} }, | ||
| 38 | d => sub { shift->{zilla}->name }, | ||
| 39 | |||
| 40 | #mm => sub { shift->{zilla}->main_module }, | ||
| 41 | #tf => sub { shift->{zilla}->is_trial ? '-TRIAL' : q{} }, | ||
| 42 | |||
| 43 | n => sub { "\n" }, | ||
| 44 | t => sub { "\t" }, | ||
| 45 | |||
| 46 | p => sub { shift | ||
| 47 | ->{ppi_document} | ||
| 48 | ->find_first('PPI::Statement::Package') | ||
| 49 | ->namespace | ||
| 50 | ; | ||
| 51 | }, | ||
| 52 | |||
| 53 | $self->additional_codes, | ||
| 54 | ); | ||
| 55 | |||
| 56 | return \%codes; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub additional_codes { () } | ||
| 60 | |||
| 61 | |||
| 62 | sub default_formatter { | ||
| 63 | my $self = shift @_; | ||
| 64 | |||
| 65 | return String::Formatter->new({ | ||
| 66 | input_processor => 'require_single_input', | ||
| 67 | string_replacer => 'method_replace', | ||
| 68 | codes => $self->codes, | ||
| 69 | }); | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | has formatter => ( | ||
| 74 | is => 'ro', | ||
| 75 | isa => class_type('String::Formatter'), | ||
| 76 | lazy => 1, | ||
| 77 | builder => 'default_formatter', | ||
| 78 | handles => { format_section => { format => [ sub { shift->format } ] } }, | ||
| 79 | ); | ||
| 80 | |||
| 81 | |||
| 82 | has section_name => ( | ||
| 83 | is => 'ro', | ||
| 84 | isa => NonEmptySimpleStr, | ||
| 85 | lazy => 1, | ||
| 86 | builder => 'default_section_name', | ||
| 87 | ); | ||
| 88 | |||
| 89 | requires 'default_section_name'; | ||
| 90 | |||
| 91 | |||
| 92 | has format => ( | ||
| 93 | is => 'ro', | ||
| 94 | isa => 'Str', | ||
| 95 | lazy => 1, | ||
| 96 | builder => 'default_format', | ||
| 97 | ); | ||
| 98 | |||
| 99 | requires 'default_format'; | ||
| 100 | |||
| 101 | |||
| 102 | sub _para { | ||
| 103 | my ($self, $content) = @_; | ||
| 104 | |||
| 105 | return Pod::Elemental::Element::Pod5::Ordinary->new({ content => $content }); | ||
| 106 | } | ||
| 107 | |||
| 108 | sub build_content { | ||
| 109 | my ($self, $document, $input) = @_; | ||
| 110 | |||
| 111 | # we're going to make the assumption here that we always end up with text. | ||
| 112 | |||
| 113 | ### keys input: keys %$input | ||
| 114 | my $text = $self->format_section($input); | ||
| 115 | my @paragraphs; # = (); | ||
| 116 | my $paragraph = q{}; | ||
| 117 | |||
| 118 | ### $text | ||
| 119 | for my $line (split /\n/, $text) { | ||
| 120 | |||
| 121 | chomp $line; | ||
| 122 | if ($line =~ /^\s*$/) { | ||
| 123 | |||
| 124 | ### new: $line | ||
| 125 | ### $paragraph | ||
| 126 | push @paragraphs, $paragraph | ||
| 127 | unless $paragraph eq q{}; | ||
| 128 | $paragraph = q{}; | ||
| 129 | } | ||
| 130 | else { | ||
| 131 | |||
| 132 | ### append: $line | ||
| 133 | $line =~ s/\s+$/ /; | ||
| 134 | $paragraph .= "$line "; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | push @paragraphs, $paragraph | ||
| 138 | unless $paragraph eq q{}; | ||
| 139 | |||
| 140 | ### @paragraphs | ||
| 141 | return (map { $self->_para($_) } @paragraphs); | ||
| 142 | } | ||
| 143 | |||
| 144 | |||
| 145 | sub weave_section { | ||
| 146 | my ($self, $document, $input) = @_; | ||
| 147 | |||
| 148 | my $nested = Pod::Elemental::Element::Nested->new({ | ||
| 149 | command => 'head1', | ||
| 150 | content => $self->section_name, | ||
| 151 | children => [ $self->build_content($document, $input) ], | ||
| 152 | }); | ||
| 153 | |||
| 154 | ### $nested | ||
| 155 | $document->children->push($nested); | ||
| 156 | } | ||
| 157 | |||
| 158 | !!42; | ||
| 159 | |||
| 160 | __END__ | ||