App-Basis-ConvertText2

 view release on metacpan or  search on metacpan

lib/App/Basis/ConvertText2/Plugin/Text.pm  view on Meta::CPAN


 parameters

=cut

sub yamlasjson {
    my $self = shift;
    my ( $tag, $content, $params, $cachedir ) = @_;

    # make sure we have an extra linefeed at the end to make sure
    # YAML is correct
    $content .= "\n\n" ;

    $content =~ s/~~~~{\.yaml}//gsm;
    $content =~ s/~~~~//gsm;

    my $data = Load($content);
    return "\n~~~~{.json}\n" . to_json( $data, { utf8 => 1, pretty => 1 } ) . "\n~~~~\n\n";
}

# ----------------------------------------------------------------------------

sub _split_csv_data {
    my ( $data, $separator ) = @_;
    my @d = ();

    $separator ||= ',';

    my $j = 0;
    foreach my $line ( split( /\n/, $data ) ) {
        last if ( !$line );
        my @row = split( /$separator/, $line );

        for ( my $i = 0; $i <= $#row; $i++ ) {
            undef $row[$i] if ( $row[$i] eq 'undef' );

            # dont' bother with any zero values either
            undef $row[$i] if ( $row[$i] =~ /^0\.?0?$/ );
            push @{ $d[$j] }, $row[$i];
        }
        $j++;
    }

    return @d;
}

# ----------------------------------------------------------------------------

=item table

create a basic html table

 parameters
    data   - comma separated lines of table data

    hashref params of
        class   - HTML/CSS class name
        id      - HTML/CSS class
        width   - width of the table
        style   - style the table if not doing anything else
        legends - flag to indicate that the top row is the legends
        separator - characters to be used to separate the fields

=cut

sub table {
    my $self = shift;
    my ( $tag, $content, $params, $cachedir ) = @_;

    $params->{title} ||= "";

    $content =~ s/^\n//gsm;
    $content =~ s/\n$//gsm;

    # open the csv file, read contents, calc max, add into data array
    my @data = _split_csv_data( $content, $params->{separator} );

    my $out = "<table ";
    $out .= "class='$params->{class}' " if ( $params->{class} );
    $out .= "id='$params->{id}' "       if ( $params->{id} );
    $out .= "width='$params->{width}' " if ( $params->{width} );
    $out .= "class='$params->{style}' " if ( $params->{style} );
    $out .= ">\n";

    for ( my $i = 0; $i < scalar(@data); $i++ ) {
        $out .= "<tr>";

        # decide if the top row has the legends
        my $tag = ( !$i && $params->{legends} ) ? 'th' : 'td';
        map { $out .= "<$tag>$_</$tag>"; } @{ $data[$i] };
        $out .= "</tr>\n";
    }

    $out .= "</table>\n";
    return $out;
}

# ----------------------------------------------------------------------------

=item version

create a version table

 parameters
    data   - sections of version information
        version YYYY-MM-DD
          change text
          more changes


    hashref params of
        class   - HTML/CSS class name
        id      - HTML/CSS class
        width   - width of the table
        style   - style the table if not doing anything else
        separator - characters to be used to separate the fields

=cut

sub version {
    my $self = shift;
    my ( $tag, $content, $params, $cachedir ) = @_;

    $content =~ s/^\n//gsm;
    $content =~ s/\n$//gsm;

    my $out = "<table ";
    $out .= "class='$params->{class}' " if ( $params->{class} );
    $out .= "id='$params->{id}' "       if ( $params->{id} );
    $out .= "width='$params->{width}' " if ( $params->{width} );
    $out .= "class='$params->{style}' " if ( $params->{style} );
    $out .= ">\n";

    $out .= "<tr><th>Version</th><th>Date</th><th>Changes</th></tr>\n";

    my $section = '^(.*?)\s+(\d{2,4}[-\/]\d{2}[-\/]\d{2,4})' ;

    my @data = split( /\n/, $content );
    for ( my $i = 0; $i < scalar(@data); $i++ ) {
        if ( $data[$i] =~ /$section/ ) {
            my $vers = $1;
            my $date = $2;
            $i++;
            my $c = "";

            # get all the lines in this section
            while ( $i < scalar(@data) && $data[$i] !~ /$section/ ) {
                $c .= "$data[$i]\n";
                $i++;



( run in 1.640 second using v1.01-cache-2.11-cpan-39bf76dae61 )