-
Notifications
You must be signed in to change notification settings - Fork 7
/
p6advent-to-md.raku
143 lines (125 loc) · 5.7 KB
/
p6advent-to-md.raku
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
# Helper script to do the initial conversion of HTML as produced in the
# Perl 6 Advent calendars to markdown. Please note that this is only a
# rough translation, manual work should be done afterwards to complete
# the work.
my %*SUB-MAIN-OPTS = :named-anywhere;
my @month = <NaM
January February March April May June
July August September October November December
>;
sub MAIN(IO() $source where .starts-with('Conserve/') && .IO.f, :$force) {
my $author = $source.Str.split('/').skip.head;
sub cleanup(\content) {
content = content
.subst('”', '"' , :global)
.subst('“', '"' , :global)
.subst('<p>', "\n" , :global)
.subst('</p>' , :global)
.subst('<br />' , :global)
.subst('<em>', '*' , :global)
.subst('</em>', '*' , :global)
.subst('<ul>', "\n" , :global)
.subst('</ul>', "\n" , :global)
.subst("\n<li>", "\n- " , :global)
.subst('</li>' , :global)
.subst('<i>', '*' , :global)
.subst('</i>', '*' , :global)
.subst('<b>', '**' , :global)
.subst('</b>', '**' , :global)
.subst('<h2>', "\n## " , :global)
.subst('</h2>' , :global)
.subst('<h3>', "\n### " , :global)
.subst('</h3>' , :global)
.subst('Perl 6', 'Raku' , :global)
.subst('Perl 6', 'Raku' , :global) # non-breaking space
.subst('P6', 'Raku' , :global)
.subst('PERL6', 'RAKU' , :global)
.subst('Perl 5', 'Perl' , :global)
.subst('Perl 5', 'Perl' , :global) # non-breaking space
.subst('P5', 'Perl' , :global)
.subst('Parcel', 'List' , :global)
.subst("\n<code>", "\n\n```` raku" , :global)
.subst("</code>\n", "````\n" , :global)
.subst('<code>', '`' , :global)
.subst('</code>', '`' , :global)
.subst("\n<pre>", "\n\n```` raku\n", :global)
.subst("</pre>\n", "\n````\n" , :global)
.subst(' ', ' ' , :global)
.subst('>', '>' , :global)
.subst('<', '<' , :global)
.subst('&', '&' , :global)
.subst('™', '™' , :global)
.subst('–', '-' , :global)
.subst('TimToady', '*TimToady*' , :global)
.subst(/ perl6 ['-' \w]? /, 'raku' , :global)
.subst(/ '&#' \d+ ';' /, { $/.substr(2,*-1).chr } , :global)
.subst(
/ '<a href="' (<-["]>+) '">' (<-[<]>*) '</a>' /,
{ "[$1]($0)" },
:global)
.subst(/ (\w+) '++' /, { "*$0*++" } , :global)
.subst(/ (\w+) '()' /, { "`$0`" } , :global)
.subst( / ^ 'Day ' \d+ ' – ' / , :global)
;
}
my $content = $source.slurp;
my $header;
my $footer;
($header,$content) = $content.split(/ '<div class="entry-content">' \s+ /);
($content,$footer) = $content.split('<div id="jp-post-flair"');
$header ~~ / '<h1 class="entry-title">' (<-[<]>+) /;
my $title = (~$0).subst(/ ^ 'Day' \s* \d+ \s* ':' \s* /);
$header ~~ / '<span class="posted-on"><a href="' (<-["]>+) /;
my $url = ~$0;
$header ~~ / '"entry-date published" datetime="' (<-["]>+) /;
my $date = $0.substr(0,10).Date;
my $published = .day ~ " @month[.month] " ~ .year given $date;
cleanup $content;
cleanup $title;
$content = qq:to/HEADER/;
# $title
*Originally published on [$published]($url) by $author.*
$content.trim()
HEADER
my $destination = $title;
$destination = $destination
.subst(',', :global)
.subst('.', :global)
.subst('(', :global)
.subst(')', :global)
.subst(';', :global)
.subst('+', :global)
.subst('!', :global)
.subst('?', :global)
.subst("'", :global)
.subst('’', :global)
.subst('“', :global)
.subst('”', :global)
.subst('"', :global)
.subst('…', '-', :global)
.subst('/', '-', :global)
.subst(':', '-', :global)
.subst(' ', '-', :global)
.subst('–', '-', :global) # EN DASH
.subst(/ '-'+ /, '-', :global)
;
$destination = $destination.substr(1) if $destination.starts-with('-');
$destination = $destination.chop if $destination.ends-with('-');
$destination = "Remaster/$author/$destination.md".IO;
if $force or !$destination.f {
$destination.IO.spurt($content);
my $readme = "Remaster/$author/README.md".IO;
my $prefix = "- $date.yyyy-mm-dd()";
my @links = $readme.lines.grep: *.starts-with("- ") if $readme.e;
@links = @links.grep: { !.contains($prefix) } if $force;
@links.push: "$prefix [$title]($destination.basename())";
$readme.spurt: qq:to/INDEX/;
This directory contains remastered versions of {+@links} blog posts by $author.
@links.sort.join("\n")
INDEX
say $destination.basename;
}
else {
note "File $destination already exists and --force not specified"
}
}