/
Text.pm6
144 lines (127 loc) · 3.92 KB
/
Text.pm6
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
unit class Pod::To::Text;
method render($pod) {
pod2text($pod)
}
my &colored = sub ($text, $) {$text }
if %*ENV<POD_TO_TEXT_ANSI> {
(try require Terminal::ANSIColor <&colored>) !=== Nil
and &OUTER::colored = &colored
};
sub pod2text($pod) is export {
given $pod {
when Pod::Heading { heading2text($pod) }
when Pod::Block::Code { code2text($pod) }
when Pod::Block::Named { named2text($pod) }
when Pod::Block::Para { twrap( $pod.contents.map({pod2text($_)}).join("") ) }
when Pod::Block::Table { table2text($pod) }
when Pod::Block::Declarator { declarator2text($pod) }
when Pod::Item { item2text($pod).indent(2) }
when Pod::Defn { pod2text($pod.contents[0]) ~ "\n"
~ pod2text($pod.contents[1..*-1]) }
when Pod::FormattingCode { formatting2text($pod) }
when Positional { .flat».&pod2text.grep(?*).join: "\n\n" }
when Pod::Block::Comment { '' }
when Pod::Config { '' }
default { $pod.Str }
}
}
sub heading2text($pod) {
given $pod.level {
when 1 { pod2text($pod.contents) }
when 2 { ' ' ~ pod2text($pod.contents) }
default { ' ' ~ pod2text($pod.contents) }
}
}
sub code2text($pod) {
$pod.contents>>.&pod2text.join.indent(4)
}
sub item2text($pod) {
'* ' ~ pod2text($pod.contents).chomp.chomp
}
sub named2text($pod) {
given $pod.name {
when 'pod' { pod2text($pod.contents) }
when 'para' { para2text($pod.contents[0]) }
when 'config' { }
when 'nested' { }
default { $pod.name ~ "\n" ~ pod2text($pod.contents) }
}
}
sub para2text($pod) {
twine2text($pod.contents)
}
sub table2text($pod) {
my @rows = $pod.contents;
@rows.unshift($pod.headers.item) if $pod.headers;
my @maxes;
my $cols = [max] @rows.map({ .elems });
for ^$cols -> $i {
@maxes.push([max] @rows.map({ $i < $_ ?? $_[$i].chars !! 0 }));
}
@maxes[*-1] = 0; # Don't pad last column with spaces
my $ret;
if $pod.config<caption> {
$ret = $pod.config<caption> ~ "\n"
}
for @rows -> $row {
# Gutter of two spaces between columns
$ret ~= ' ' ~ join ' ',
(@maxes Z=> @$row).map: { .value.fmt("%-{.key}s") };
$ret ~= "\n";
}
$ret
}
sub declarator2text($pod) {
next unless $pod.WHEREFORE.WHY;
my $what = do given $pod.WHEREFORE {
when Method {
my @params=$_.signature.params[1..*];
@params.pop if @params.tail.name eq '%_';
'method ' ~ $_.name ~ signature2text(@params)
}
when Sub {
'sub ' ~ $_.name ~ signature2text($_.signature.params)
}
when .HOW ~~ Metamodel::ClassHOW {
'class ' ~ $_.perl
}
when .HOW ~~ Metamodel::ModuleHOW {
'module ' ~ $_.perl
}
when .HOW ~~ Metamodel::PackageHOW {
'package ' ~ $_.perl
}
default {
''
}
}
"$what\n{$pod.WHEREFORE.WHY.contents}"
}
sub signature2text($params) {
$params.elems ??
"(\n\t" ~ $params.map(¶m2text).join("\n\t") ~ "\n)"
!! "()";
}
sub param2text($p) {
$p.perl ~ ',' ~ ( $p.WHY ?? ' # ' ~ $p.WHY !! ' ')
}
my %formats =
C => "bold",
L => "underline",
D => "underline",
R => "inverse"
;
sub formatting2text($pod) {
my $text = $pod.contents>>.&pod2text.join;
$pod.type ~~ %formats
?? colored($text, %formats{$pod.type})
!! $text
}
sub twine2text($_) {
.map({ when Pod::Block { twine2text .contents }; .&pod2text }).join
}
sub twrap($text is copy, :$wrap=75 ) {
$text ~~ s:g/(. ** {$wrap} <[\s]>*)\s+/$0\n/;
$text
}
# vim: ft=perl6