Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 125 lines (111 sloc) 3.424 kb
626ef2a @tadzik Make perl6 --doc=HTML etc do what it should.
tadzik authored
1 class Pod::To::Text;
2
3 method render($pod) {
4 pod2text($pod)
5 }
8797ffa @tadzik Implement DOC phasers. Fix compilation options. Add a simple Pod::To::Te...
tadzik authored
6
7 sub pod2text($pod) is export {
4472ab8 @tadzik Add declarator blocks support to Pod::To::Text
tadzik authored
8 my @declarators;
8797ffa @tadzik Implement DOC phasers. Fix compilation options. Add a simple Pod::To::Te...
tadzik authored
9 given $pod {
10 when Pod::Heading { heading2text($pod) }
083287a @tadzik Small tunes in Pod::To::Text, sanitize newline handling, interpret packa...
tadzik authored
11 when Pod::Block::Code { code2text($pod) }
12 when Pod::Block::Named { named2text($pod) }
13 when Pod::Block::Para { para2text($pod) }
77c62ce @tadzik [Pod::To::Text] Generate proper output for Tables and Comments, fix outp...
tadzik authored
14 when Pod::Block::Table { table2text($pod) }
4472ab8 @tadzik Add declarator blocks support to Pod::To::Text
tadzik authored
15 when Pod::Block::Declarator { declarator2text($pod) }
083287a @tadzik Small tunes in Pod::To::Text, sanitize newline handling, interpret packa...
tadzik authored
16 when Pod::Item { item2text($pod) }
5f145c6 @tadzik Implement simple FormattinCode handling in Pod::To::Text
tadzik authored
17 when Pod::FormattingCode { formatting2text($pod) }
083287a @tadzik Small tunes in Pod::To::Text, sanitize newline handling, interpret packa...
tadzik authored
18 when Positional { $pod.map({pod2text($_)}).join("\n\n")}
77c62ce @tadzik [Pod::To::Text] Generate proper output for Tables and Comments, fix outp...
tadzik authored
19 when Pod::Block::Comment { }
1683397 @tadzik [Pod::To::Text] Avoid stringifying Pod::Config blocks
tadzik authored
20 when Pod::Config { }
585cd06 @tadzik The Pod tree should not contain bare strings. Use Pod::Block::Para, adju...
tadzik authored
21 default { $pod.Str }
8797ffa @tadzik Implement DOC phasers. Fix compilation options. Add a simple Pod::To::Te...
tadzik authored
22 }
23 }
24
25 sub heading2text($pod) {
26 given $pod.level {
585cd06 @tadzik The Pod tree should not contain bare strings. Use Pod::Block::Para, adju...
tadzik authored
27 when 1 { pod2text($pod.content) }
28 when 2 { ' ' ~ pod2text($pod.content) }
29 default { ' ' ~ pod2text($pod.content) }
8797ffa @tadzik Implement DOC phasers. Fix compilation options. Add a simple Pod::To::Te...
tadzik authored
30 }
31 }
32
33 sub code2text($pod) {
585cd06 @tadzik The Pod tree should not contain bare strings. Use Pod::Block::Para, adju...
tadzik authored
34 " " ~ $pod.content.subst(/\n/, "\n ", :g)
8797ffa @tadzik Implement DOC phasers. Fix compilation options. Add a simple Pod::To::Te...
tadzik authored
35 }
36
37 sub item2text($pod) {
585cd06 @tadzik The Pod tree should not contain bare strings. Use Pod::Block::Para, adju...
tadzik authored
38 ' * ' ~ pod2text($pod.content).chomp.chomp
8797ffa @tadzik Implement DOC phasers. Fix compilation options. Add a simple Pod::To::Te...
tadzik authored
39 }
40
41 sub named2text($pod) {
90e9c38 @tadzik Couple of more fixes to make the output generated from S26 more pleasant...
tadzik authored
42 given $pod.name {
43 when 'pod' { pod2text($pod.content) }
44 when 'para' { para2text($pod.content[0]) }
45 when 'defn' { pod2text($pod.content[0]) ~ "\n"
46 ~ pod2text($pod.content[1..*-1]) }
47 when 'config' { }
48 when 'nested' { }
49 default { $pod.name ~ "\n" ~ pod2text($pod.content) }
50 }
585cd06 @tadzik The Pod tree should not contain bare strings. Use Pod::Block::Para, adju...
tadzik authored
51 }
52
53 sub para2text($pod) {
5f145c6 @tadzik Implement simple FormattinCode handling in Pod::To::Text
tadzik authored
54 twine2text($pod.content)
8797ffa @tadzik Implement DOC phasers. Fix compilation options. Add a simple Pod::To::Te...
tadzik authored
55 }
56
77c62ce @tadzik [Pod::To::Text] Generate proper output for Tables and Comments, fix outp...
tadzik authored
57 sub table2text($pod) {
f147bf8 @tadzik [Pod::To::Text] Align table columns
tadzik authored
58 my @rows = $pod.content;
59 @rows.unshift($pod.headers.item) if $pod.headers;
60 my @maxes;
61 for 0..(@rows[1].elems - 1) -> $i {
62 @maxes.push([max] @rows.map({ $_[$i].chars }));
63 }
18d51ba @tadzik [Pod::To::Text] Avoid uninitialized variable warnings
tadzik authored
64 my $ret;
65 if $pod.config<caption> {
66 $ret = $pod.config<caption> ~ "\n"
67 }
f147bf8 @tadzik [Pod::To::Text] Align table columns
tadzik authored
68 for @rows -> $row {
69 for 0..($row.elems - 1) -> $i {
89353cb @tadzik [Pod::To::Text] Make tables a bit more pleasant to look at
tadzik authored
70 $ret ~= $row[$i].fmt("%-{@maxes[$i]}s") ~ " ";
f147bf8 @tadzik [Pod::To::Text] Align table columns
tadzik authored
71 }
72 $ret ~= "\n";
73 }
74 return $ret;
77c62ce @tadzik [Pod::To::Text] Generate proper output for Tables and Comments, fix outp...
tadzik authored
75 }
76
4472ab8 @tadzik Add declarator blocks support to Pod::To::Text
tadzik authored
77 sub declarator2text($pod) {
78 next unless $pod.WHEREFORE.WHY;
b3e3008 @tadzik [Pod::To::Text] Make object stringification in declarator2text less impl...
tadzik authored
79 my $what = do given $pod.WHEREFORE {
4472ab8 @tadzik Add declarator blocks support to Pod::To::Text
tadzik authored
80 when Method {
43445e5 @marcusramberg Fix method stringification in Pod::To::Text, fix slurpy star in Paramete...
marcusramberg authored
81 my @params=$_.signature.params[1..*];
82 @params.pop if @params[*-1].name eq '%_';
83 'method ' ~ $_.name ~ signature2text(@params)
4472ab8 @tadzik Add declarator blocks support to Pod::To::Text
tadzik authored
84 }
85 when Sub {
43445e5 @marcusramberg Fix method stringification in Pod::To::Text, fix slurpy star in Paramete...
marcusramberg authored
86 'sub ' ~ $_.name ~ signature2text($_.signature.params)
4472ab8 @tadzik Add declarator blocks support to Pod::To::Text
tadzik authored
87 }
88 when nqp::p6bool(nqp::istype($_.HOW, Metamodel::ClassHOW)) {
b3e3008 @tadzik [Pod::To::Text] Make object stringification in declarator2text less impl...
tadzik authored
89 'class ' ~ $_.perl
4472ab8 @tadzik Add declarator blocks support to Pod::To::Text
tadzik authored
90 }
91 when nqp::p6bool(nqp::istype($_.HOW, Metamodel::ModuleHOW)) {
b3e3008 @tadzik [Pod::To::Text] Make object stringification in declarator2text less impl...
tadzik authored
92 'module ' ~ $_.perl
4472ab8 @tadzik Add declarator blocks support to Pod::To::Text
tadzik authored
93 }
083287a @tadzik Small tunes in Pod::To::Text, sanitize newline handling, interpret packa...
tadzik authored
94 when nqp::p6bool(nqp::istype($_.HOW, Metamodel::PackageHOW)) {
b3e3008 @tadzik [Pod::To::Text] Make object stringification in declarator2text less impl...
tadzik authored
95 'package ' ~ $_.perl
083287a @tadzik Small tunes in Pod::To::Text, sanitize newline handling, interpret packa...
tadzik authored
96 }
ada28b6 @tadzik [Pod::To::Text] Tune and clarify declarator2text() a bit
tadzik authored
97 default {
98 ''
99 }
100 }
9b16150 @marcusramberg Reformat --doc output
marcusramberg authored
101 return "$what\n{$pod.WHEREFORE.WHY.content}"
102 }
103
43445e5 @marcusramberg Fix method stringification in Pod::To::Text, fix slurpy star in Paramete...
marcusramberg authored
104 sub signature2text($params) {
105 $params.elems ??
106 "(\n\t" ~ $params.map({ $_.perl }).join(", \n\t") ~ "\n)"
9b16150 @marcusramberg Reformat --doc output
marcusramberg authored
107 !! "()";
4472ab8 @tadzik Add declarator blocks support to Pod::To::Text
tadzik authored
108 }
109
5f145c6 @tadzik Implement simple FormattinCode handling in Pod::To::Text
tadzik authored
110 sub formatting2text($pod) {
111 twine2text($pod.content)
112 }
113
114 sub twine2text($twine) {
115 return '' unless $twine.elems;
116 my $r = $twine[0];
117 for $twine[1..*] -> $f, $s {
118 $r ~= twine2text($f.content);
119 $r ~= $s;
120 }
121 return $r;
122 }
123
8797ffa @tadzik Implement DOC phasers. Fix compilation options. Add a simple Pod::To::Te...
tadzik authored
124 # vim: ft=perl6
Something went wrong with that request. Please try again.