Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: 2012.03
Fetching contributors…

Cannot retrieve contributors at this time

file 108 lines (97 sloc) 2.997 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
module Pod::To::Text;

sub pod2text($pod) is export {
    my @declarators;
    given $pod {
        when Pod::Heading { heading2text($pod) }
        when Pod::Block::Code { code2text($pod) }
        when Pod::Block::Named { named2text($pod) }
        when Pod::Block::Para { para2text($pod) }
        when Pod::Block::Table { table2text($pod) }
        when Pod::Block::Declarator { declarator2text($pod) }
        when Pod::Item { item2text($pod) }
        when Pod::FormattingCode { formatting2text($pod) }
        when Positional { $pod.map({pod2text($_)}).join("\n\n")}
        when Pod::Block::Comment { }
        default { $pod.Str }
    }
}

sub heading2text($pod) {
    given $pod.level {
        when 1 { pod2text($pod.content) }
        when 2 { ' ' ~ pod2text($pod.content) }
        default { ' ' ~ pod2text($pod.content) }
    }
}

sub code2text($pod) {
    " " ~ $pod.content.subst(/\n/, "\n ", :g)
}

sub item2text($pod) {
    ' * ' ~ pod2text($pod.content).chomp.chomp
}

sub named2text($pod) {
    given $pod.name {
        when 'pod' { pod2text($pod.content) }
        when 'para' { para2text($pod.content[0]) }
        when 'defn' { pod2text($pod.content[0]) ~ "\n"
                    ~ pod2text($pod.content[1..*-1]) }
        when 'config' { }
        when 'nested' { }
        default { $pod.name ~ "\n" ~ pod2text($pod.content) }
    }
}

sub para2text($pod) {
    twine2text($pod.content)
}

sub table2text($pod) {
    my @rows = $pod.content;
    @rows.unshift($pod.headers.item) if $pod.headers;
    my @maxes;
    for 0..(@rows[1].elems - 1) -> $i {
        @maxes.push([max] @rows.map({ $_[$i].chars }));
    }
    my $ret = $pod.config<caption> ~ "\n" // '';
    for @rows -> $row {
        for 0..($row.elems - 1) -> $i {
            $ret ~= $row[$i].fmt("%-{@maxes[$i]}s") ~ " ";
        }
        $ret ~= "\n";
    }
    return $ret;
}

sub declarator2text($pod) {
    next unless $pod.WHEREFORE.WHY;
    my $what = do given $pod.WHEREFORE {
        when Method {
            'method ' ~ $_.name
        }
        when Sub {
            'sub ' ~ $_.name
        }
        when nqp::p6bool(nqp::istype($_.HOW, Metamodel::ClassHOW)) {
            'class ' ~ $_.perl
        }
        when nqp::p6bool(nqp::istype($_.HOW, Metamodel::ModuleHOW)) {
            'module ' ~ $_.perl
        }
        when nqp::p6bool(nqp::istype($_.HOW, Metamodel::PackageHOW)) {
            'package ' ~ $_.perl
        }
        default {
            ''
        }
    }
    return "$what: {$pod.WHEREFORE.WHY.content}"
}

sub formatting2text($pod) {
    twine2text($pod.content)
}

sub twine2text($twine) {
    return '' unless $twine.elems;
    my $r = $twine[0];
    for $twine[1..*] -> $f, $s {
        $r ~= twine2text($f.content);
        $r ~= $s;
    }
    return $r;
}

# vim: ft=perl6
Something went wrong with that request. Please try again.