-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
146 lines (129 loc) · 4.92 KB
/
index.php
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
<?php
Kirby::plugin("mlbrgl/kirby-export-archive", [
"routes" => [
[
"pattern" => "export-archive",
"action" => function () {
// Copy the content/articles/ directory to archive/articles/ in an indempotent way
exec(
"rm -r archive/articles ; cp -rf content/articles/. archive/articles"
);
exec("rm -r archive/articles/*/article.txt");
exec("rm -r archive/articles/articles-list.txt");
// Copy the content/actualites/ directory to archive/actualites/ in an indempotent way
exec(
"rm -r archive/actualites ; cp -rf content/actualites/. archive/actualites"
);
exec("rm -r archive/actualites/*/news.txt");
exec("rm -r archive/actualites/news-list.txt");
// Remove the datetime prefix in the subdirectory names. The folder
// names are used as slugs by Honkit.
// Note: there are collisions in the target folder names when removing
// the datetime prefix. This means two articles are candidate for the
// same slug. By listing in reverse order, we ensure that the latest
// article (probably an udpated version of the original article) is the
// one that is kept (which is what is happening on Kirby's side as
// well).
exec("cd archive/articles && ls -r | rename 's/\d{12}_//'");
// Remove the colliding folders that weren't renamed
exec("cd archive/articles && ls | grep '^[0-9]\{12\}' | xargs rm -r");
// Same with actualites, but with sequential numbers instead of dates
exec("cd archive/actualites && ls -r | rename 's/\d+_//'");
$articles = page("articles")
->children()
->listed()
->flip();
$actualites = page("actualites")
->children()
->listed()
->flip();
$pages = $articles->merge($actualites)->sortBy("datetime", "desc");
$summary = [
"## Archive devsante.org (1976 - 2022)",
"- [Avant-propos](INTRO.md)",
];
// Transform the content of each page into a standard Markdown file,
// using standard frontmatter, and removing kirbytext.
foreach ($pages as $page) {
$frontmatterFields = [
"title" => $page->title()->value(),
"author" => $page->author()->value(),
"date" => $page->datetime()->toDate("%Y-%m-%d"),
];
$teaser = !empty($page->teaser()->value())
? '<div class="teaser">' . $page->teaser()->kirbytext() . "</div>"
: null;
$frontmatter = arrayToFrontmatter($frontmatterFields);
$text = processPandoc(
kirbytextImageToMarkdown($page->text()->value() ?? "")
);
$page_path = "{$page->parent()->slug()}/{$page->slug()}";
file_put_contents(
"archive/$page_path/index.md",
join(
"\n\n",
array_filter([$frontmatter, $teaser, $text], function ($value) {
return !empty($value);
})
)
);
$summary[] = "- [{$page->title()->value()}]($page_path/index.md)";
}
// Add a summary page to the book
file_put_contents(
"archive/SUMMARY.md",
join("\n", ["# Summary", ...$summary])
);
},
],
],
]);
function arrayToFrontmatter($array)
{
$frontmatter = "---\n";
foreach ($array as $key => $value) {
if (empty($value)) {
continue;
}
$escapedValue = addcslashes($value, "\"");
$frontmatter .= "$key: \"$escapedValue\"\n";
}
$frontmatter .= "---";
return $frontmatter;
}
// convert kirbytext images to markdown and start them on a new line. Images
// already starting on a new line will get caught by this regex and get an extra
// new line, but that's ok.
function kirbytextImageToMarkdown($kirbytext)
{
return preg_replace(
"/^(.*?)\(image:\s*(.*?)\)/mu",
"$1\n![]($2)",
$kirbytext
);
}
// This is mostly used to turn newlines into markdown newlines (two spaces at
// the end of a line) but introduces side-effects that are fixed a posteriori.
function processPandoc($markdown)
{
$processed = (new \Pandoc\Pandoc())
->from("gfm+hard_line_breaks")
->input($markdown)
->to("gfm")
->option("wrap", "preserve")
->run();
// Some post-processing fixes:
// - Pandoc converts **test ** to \*\*test \*\* due to the extra space
// between "test" and the second "**". This should ideally be fixed in the
// source document, but is good enough for now.
// EDIT: fixed in the source documents as of March 26, 2023.
// return preg_replace(
// "/\\\\\*\\\\\*\s*(.+?)\s*\\\\\*\\\\\*/",
// "**$1**",
// $processed
// );
// - Pandoc converts standalone "<" to "\<" and ">" to "\>", which are not
// recognized by Honkit and is printed respectively as "\<" and "\>".
return str_replace(["\<", "\>"], ["<", ">"], $processed);
// );
}