Bible-OBML

 view release on metacpan or  search on metacpan

lib/Bible/OBML.pm  view on Meta::CPAN

    # remove comments
    $obml =~ s/^\s*#.*?(?>\r?\n)//msg;

    # "unwrap" wrapped lines
    my @obml;
    for my $line ( split( /\n/, $obml ) ) {
        if ( not @obml or not length $line or not length $obml[-1] ) {
            push( @obml, $line );
        }
        else {
            my ($last_line_indent) = $obml[-1] =~ /^([ ]*)/;
            my ($this_line_indent) = $line     =~ /^([ ]*)/;

            if ( length $last_line_indent == 0 and length $this_line_indent == 0 ) {
                $line =~ s/^[ ]+//;
                $obml[-1] .= ' ' . $line;
            }
            else {
                push( @obml, $line );
            }
        }
    }
    $obml = join( "\n", @obml );

    $obml =~ s|~+[ ]*([^~]+?)[ ]*~+|<reference>$1</reference>|g;
    $obml =~ s|={2,}[ ]*([^=]+?)[ ]*={2,}|<sub_header>$1</sub_header>|g;
    $obml =~ s|=[ ]*([^=]+?)[ ]*=|<header>$1</header>|g;

    $obml =~ s|^([ ]+)(\S.*)$|
        '<indent level="'
        . int( ( length($1) + $self->indent_width * 0.5 ) / $self->indent_width )
        . '">'
        . $2
        . '</indent>'
    |mge;

    $obml =~ s|(\S)(?=\n\S)|$1<br>|g;

    $obml =~ s`(?:^|(?<=\n\n))(?!<(?:reference|sub_header|header)\b)`<p>`g;
    $obml =~ s`(?:$|(?=\n\n))`</p>`g;
    $obml =~ s`(?<=</reference>)</p>``g;
    $obml =~ s`(?<=</sub_header>)</p>``g;
    $obml =~ s`(?<=</header>)</p>``g;

    $obml =~ s!\|(\d+)\|\s*!<verse_number>$1</verse_number>!g;

    $obml =~ s|\*([^\*]+)\*|<woj>$1</woj>|g;
    $obml =~ s|\^([^\^]+)\^|<i>$1</i>|g;
    $obml =~ s|\\([^\\]+)\\|<small_caps>$1</small_caps>|g;

    $obml =~ s|\{|<crossref>|g;
    $obml =~ s|\}|</crossref>|g;

    $obml =~ s|\[|<footnote>|g;
    $obml =~ s|\]|</footnote>|g;

    return "<obml>$obml</obml>";
}

sub _accessor ( $self, $input = undef ) {
    my $want = ( split( '::', ( caller(1) )[3] ) )[-1];

    if ($input) {
        if ( ref $input ) {
            my $data_refs_ocd;
            $data_refs_ocd = sub ($node) {
                if (
                    $node->{tag} and $node->{children} and
                    ( $node->{tag} eq 'crossref' or $node->{tag} eq 'footnote' )
                ) {
                    for ( grep { $_->{text} } @{ $node->{children} } ) {
                        $_->{text} = $self->reference->acronyms(
                            $self->fnxref_acronym
                        )->clear->in( $_->{text} )->as_text;
                    }
                }
                if ( $node->{children} ) {
                    $data_refs_ocd->($_) for ( @{ $node->{children} } );
                }
                return;
            };
            $data_refs_ocd->($input);

            my $reference = ( grep { $_->{tag} eq 'reference' } @{ $input->{children} } )[0]{children}[0];
            my $runs      = $self->reference->acronyms(
                $self->reference_acronym
            )->clear->in( $reference->{text} )->as_runs;

            $reference->{text} = $runs->[0];
        }
        else {
            my $ref_ocd = sub ( $text, $acronyms ) {
                return $self->reference->acronyms($acronyms)->clear->in($text)->as_text;
            };

            $input =~ s!
                ((?:<(?:footnote|crossref)>|\{|\[)\s*.+?\s*(?:</(?:footnote|crossref)>|\}|\]))
            !
                $ref_ocd->( $1, $self->fnxref_acronym )
            !gex;

            $input =~ s!
                ((?:<reference>|~)\s*.+?\s*(?:</reference>|~))
            !
                $ref_ocd->( $1, $self->reference_acronym )
            !gex;
        }

        return $self->_load({ $want => $input });
    }

    return $self->_load->{data} if ( $want eq 'data' and $self->_load->{data} );

    unless ( $self->_load->{canonical}{$want} ) {
        if ( $self->_load->{html} ) {
            $self->_load->{clean_html} //= __cleanup_html( $self->_load->{html} );

            if ( $want eq 'obml' ) {
                $self->_load->{canonical}{obml} = $self->_clean_html_to_obml( $self->_load->{clean_html} );
            }
            elsif ( $want eq 'data' or $want eq 'html' ) {



( run in 5.092 seconds using v1.01-cache-2.11-cpan-2398b32b56e )