Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 122 lines (108 sloc) 3.381 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 }
f5ef031 @tadzik [Pod::To::Text] Fix handling of table caption
tadzik authored
64 my $ret = $pod.config<caption> ~ "\n" // '';
f147bf8 @tadzik [Pod::To::Text] Align table columns
tadzik authored
65 for @rows -> $row {
66 for 0..($row.elems - 1) -> $i {
89353cb @tadzik [Pod::To::Text] Make tables a bit more pleasant to look at
tadzik authored
67 $ret ~= $row[$i].fmt("%-{@maxes[$i]}s") ~ " ";
f147bf8 @tadzik [Pod::To::Text] Align table columns
tadzik authored
68 }
69 $ret ~= "\n";
70 }
71 return $ret;
77c62ce @tadzik [Pod::To::Text] Generate proper output for Tables and Comments, fix outp...
tadzik authored
72 }
73
4472ab8 @tadzik Add declarator blocks support to Pod::To::Text
tadzik authored
74 sub declarator2text($pod) {
75 next unless $pod.WHEREFORE.WHY;
b3e3008 @tadzik [Pod::To::Text] Make object stringification in declarator2text less impl...
tadzik authored
76 my $what = do given $pod.WHEREFORE {
4472ab8 @tadzik Add declarator blocks support to Pod::To::Text
tadzik authored
77 when Method {
43445e5 @marcusramberg Fix method stringification in Pod::To::Text, fix slurpy star in Paramete...
marcusramberg authored
78 my @params=$_.signature.params[1..*];
79 @params.pop if @params[*-1].name eq '%_';
80 'method ' ~ $_.name ~ signature2text(@params)
4472ab8 @tadzik Add declarator blocks support to Pod::To::Text
tadzik authored
81 }
82 when Sub {
43445e5 @marcusramberg Fix method stringification in Pod::To::Text, fix slurpy star in Paramete...
marcusramberg authored
83 'sub ' ~ $_.name ~ signature2text($_.signature.params)
4472ab8 @tadzik Add declarator blocks support to Pod::To::Text
tadzik authored
84 }
85 when nqp::p6bool(nqp::istype($_.HOW, Metamodel::ClassHOW)) {
b3e3008 @tadzik [Pod::To::Text] Make object stringification in declarator2text less impl...
tadzik authored
86 'class ' ~ $_.perl
4472ab8 @tadzik Add declarator blocks support to Pod::To::Text
tadzik authored
87 }
88 when nqp::p6bool(nqp::istype($_.HOW, Metamodel::ModuleHOW)) {
b3e3008 @tadzik [Pod::To::Text] Make object stringification in declarator2text less impl...
tadzik authored
89 'module ' ~ $_.perl
4472ab8 @tadzik Add declarator blocks support to Pod::To::Text
tadzik authored
90 }
083287a @tadzik Small tunes in Pod::To::Text, sanitize newline handling, interpret packa...
tadzik authored
91 when nqp::p6bool(nqp::istype($_.HOW, Metamodel::PackageHOW)) {
b3e3008 @tadzik [Pod::To::Text] Make object stringification in declarator2text less impl...
tadzik authored
92 'package ' ~ $_.perl
083287a @tadzik Small tunes in Pod::To::Text, sanitize newline handling, interpret packa...
tadzik authored
93 }
ada28b6 @tadzik [Pod::To::Text] Tune and clarify declarator2text() a bit
tadzik authored
94 default {
95 ''
96 }
97 }
9b16150 @marcusramberg Reformat --doc output
marcusramberg authored
98 return "$what\n{$pod.WHEREFORE.WHY.content}"
99 }
100
43445e5 @marcusramberg Fix method stringification in Pod::To::Text, fix slurpy star in Paramete...
marcusramberg authored
101 sub signature2text($params) {
102 $params.elems ??
103 "(\n\t" ~ $params.map({ $_.perl }).join(", \n\t") ~ "\n)"
9b16150 @marcusramberg Reformat --doc output
marcusramberg authored
104 !! "()";
4472ab8 @tadzik Add declarator blocks support to Pod::To::Text
tadzik authored
105 }
106
5f145c6 @tadzik Implement simple FormattinCode handling in Pod::To::Text
tadzik authored
107 sub formatting2text($pod) {
108 twine2text($pod.content)
109 }
110
111 sub twine2text($twine) {
112 return '' unless $twine.elems;
113 my $r = $twine[0];
114 for $twine[1..*] -> $f, $s {
115 $r ~= twine2text($f.content);
116 $r ~= $s;
117 }
118 return $r;
119 }
120
8797ffa @tadzik Implement DOC phasers. Fix compilation options. Add a simple Pod::To::Te...
tadzik authored
121 # vim: ft=perl6
Something went wrong with that request. Please try again.