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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
#!/usr/bin/env perl
use v5.34;
use warnings;
use feature 'signatures';
no warnings 'experimental';
use Getopt::Std ();
use CommonMark ();
BEGIN { *CM:: = *CommonMark:: }
sub usage($fh) {
print $fh <<~'EOF';
Usage:
md
md -h
EOF
}
sub help($fh) {
print $fh <<~'EOF';
Options:
-h, --help show this message
EOF
}
for (@ARGV) {
last if $_ eq '--';
if ($_ eq '--help') {
usage *STDOUT;
help *STDOUT;
exit;
}
}
my %opts;
if (!Getopt::Std::getopts('h', \%opts)) {
usage *STDERR;
exit 2;
}
if ($opts{h}) {
usage *STDOUT;
help *STDOUT;
exit;
}
sub into_table($s) {
my @lines = ();
my $i = 1;
for (split "\n", $s) {
my $line = '<tr><td>' . $i++ . '</td><td>' . $_ . '</td></tr>';
push @lines, $line;
}
return join "\n", @lines;
}
sub with_file($f, $mode, $block) {
\$block();
}
given (shift @ARGV) {
shift @ARGV if defined($ARGV[0]) and $ARGV[0] eq '--';
when ('snippets') {
for (@ARGV) {
open my $fh, '<', $_ or die "Can't open $_";
my $doc = CM->parse(file => $fh);
my $iter = $doc->iterator;
my $i = 0;
while (my ($event_type, $node) = $iter->next) {
next if $node->get_type != CM::NODE_CODE_BLOCK;
my $f = "$_.$i.txt";
$i++;
say 123;
with_file $f, '>', sub {
say 456;
};
open my $fh, '>', $f or die "Can't open $f";
print $fh $node->get_literal;
close $fh or die "Can't close $f";
}
close $fh or die "Can't close $_";
}
}
when ('render') {
my $s = CM->parse(file => *STDIN)->render(format => 'html');
my @ret = ();
my $in_block = 0;
for (split "\n", $s) {
if (/^<pre><code/) {
say $_;
}
push @ret, $_;
}
break;
my $s2 = join("\n", @ret) . "\n";
# say "antes: $s";
# say "depois: " . join "\n", @ret;
open my $fh1, '>', 'antes';
print $fh1 $s;
close $fh1;
open my $fh2, '>', 'depois';
print $fh2 $s2;
close $fh2;
say '=========' if $s eq $s2;
break;
say "$s";
my $doc = CM->parse(file => *STDIN);
my $iter = $doc->iterator;
while (my ($event_type, $node) = $iter->next) {
# say $node->get_on_exit;
# say $node->get_type_string;
# next;
# next if $node->get_type == CM::NODE_DOCUMENT;
# next unless $event_type == CM::EVENT_ENTER;
if ($node->get_type == CM::NODE_CODE_BLOCK) {
# say $node->get_on_exit;
# say $node->get_on_enter;
# say 123;
# next;
# $node->set_literal(into_table($node->get_literal));
# say 123;
} else {
# say $node->render(format => 'html');
# use Data::Dumper;
# say Dumper($node->get_type_string);
}
}
say $doc->render(format => 'html', unsafe => 1);
}
default {
print STDERR "Bad ACTION \"$_\".\n\n";
usage *STDERR;
exit 2;
}
}
__END__
# use Data::Dumper;
# print Dumper(\%opts);
# exit: extra newline in output
# no warnings ('experimental::signatures', );
# no warnings ('experimental::when', );
# $Getopt::Std::STANDARD_HELP_VERSION = 1;
perlcritic
use builtin qw(true);
if (true) {
say 'true';
exit 3;
}
|