/
Pod.pm
122 lines (101 loc) · 2.64 KB
/
Pod.pm
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
my class Pod::Block {
has %.config;
has @.contents;
submethod BUILD(:%!config, :@!contents --> Nil) {}
sub pod-gist(Pod::Block $pod, $level = 0) {
my $leading = ' ' x $level;
my %confs;
my @chunks;
for <config name level caption type> {
my $thing = $pod.?"$_"();
if $thing {
%confs{$_} = nqp::istype($thing,Iterable)
?? $thing.perl
!! $thing.Str;
}
}
@chunks = $leading, $pod.^name, (%confs.perl if %confs), "\n";
for $pod.contents.flat -> $c {
if nqp::istype($c,Pod::Block) {
@chunks.push: pod-gist($c, $level + 2);
}
elsif nqp::istype($c,Positional) {
@chunks.append: $c>>.Str.perl.indent($level + 2), "\n";
}
else {
@chunks.append: $c.Str.indent($level + 2), "\n";
}
}
@chunks.join;
}
multi method gist(Pod::Block:D:) {
pod-gist(self)
}
}
my class Pod::Block::Para is Pod::Block {
}
my class Pod::Block::Named is Pod::Block {
has $.name;
}
my class Pod::Block::Comment is Pod::Block { }
my class Pod::Block::Code is Pod::Block {
has @.allowed;
}
my class Pod::Block::Declarator is Pod::Block {
has $.WHEREFORE;
has @!leading;
has @!trailing;
submethod BUILD(:@!leading, :@!trailing --> Nil) {}
method set_docee($d) {
$!WHEREFORE = $d
}
method Str {
@.contents.join('')
}
multi method gist(Pod::Block::Declarator:D:) {
@.contents.join('')
}
method contents {
if @!leading && @!trailing {
[ $.leading ~ "\n" ~ $.trailing ]
} elsif @!leading {
[ $.leading ]
} elsif @!trailing {
[ $.trailing ]
} else {
[]
}
}
method leading { @!leading ?? @!leading.join(' ') !! Nil }
method trailing { @!trailing ?? @!trailing.join(' ') !! Nil }
method _add_leading($addition) {
@!leading.push: ~$addition;
}
method _add_trailing($addition) {
@!trailing.push: ~$addition;
}
}
my class Pod::Block::Table is Pod::Block {
has $.caption;
has @.headers; # optional, may be empty
}
my class Pod::FormattingCode is Pod::Block {
has $.type;
has @.meta;
}
my class Pod::Heading is Pod::Block {
has $.level;
}
my class Pod::Item is Pod::Block {
has $.level;
}
class Pod::Config {
has $.type;
has %.config;
}
# for passing raw instructions to specific backends
class Pod::Raw {
has $.target;
has @.contents;
}
# vim: ft=perl6 expandtab sw=4