-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharticle.php
More file actions
174 lines (144 loc) · 5.83 KB
/
article.php
File metadata and controls
174 lines (144 loc) · 5.83 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
function addTabsOutsidePre($body, $tabs) {
$preBlocks = [];
// Temporarily remove content inside <pre> tags by replacing it with a placeholder
$body = preg_replace_callback('/<pre\b([^>]*)>(.*?)<\/pre>/is', function ($matches) use (&$preBlocks) {
$placeholder = "__PRE_BLOCK_" . count($preBlocks) . "__";
// Saves the entire tag to restore later
$preBlocks[$placeholder] = "<pre" . $matches[1] . ">" . $matches[2] . "</pre>";
return $placeholder;
}, $body);
// Add tabs to the content outside <pre> tags
$body = preg_replace('/^/m', str_repeat("\t", $tabs), $body);
// Restores <pre> blocks
foreach ($preBlocks as $placeholder => $original) {
$body = str_replace($placeholder, $original, $body);
}
return $body;
}
$body = file_get_contents($page_file);
// Extract metadata from yaml
$filename = pathinfo($page_file, PATHINFO_FILENAME);
$dirname = dirname($page_file);
// Special cases: README.* e LICENSE.*
if (strcasecmp($filename, 'README') === 0) {
$yaml = $dirname . '/metadata-readme.yml';
} elseif (strcasecmp($filename, 'LICENSE') === 0) {
$yaml = $dirname . '/metadata-license.yml';
} else {
// Normal cases: article.yaml or article.yml
$base = preg_replace('/\.[^.]+$/', '', $page_file);
if (is_file($base . '.yaml')) {
$yaml = $base . '.yaml';
} elseif (is_file($base . '.yml')) {
$yaml = $base . '.yml';
} else {
$yaml = null;
}
}
if ($yaml !== null && is_file($yaml)) {
$metadata = file_get_contents($yaml);
} else {
$metadata = null;
}
if ( !empty($body) ) {
// Process extracted metadata
[$article, $author, $columns, $date, $email, $image] = _getParams($metadata, $yaml);
// If email is not missing, link it to the author
if ( !empty($email) ){
$author = '<a href="mailto:'.$email.'">'.$author.'</a>';
}
// If featured image is not missing and it is not set to 'none', insert it
if ( !empty($image) && mb_strtolower($image) != "none" ){
// Remove leading slash, if present
if (strpos($image, '/') === 0) {
$image = ltrim($image, '/');
}
$parts = explode('_', $page_file);
if (ctype_digit(substr($parts[0], -1))) {
$postFolder = '/' . $parts[0] . '/';
}
else {
$postFolder = '/';
}
echo tab(3) . '<img src="' . BASE_PATH . $postFolder . $image . '" alt="Article Thumbnail"' .
' width="640" height="auto" style="margin: 1px auto 1px; display: block;"' .
' class="responsive-img">' . "\n";
echo tab(3) . '<hr width="75%">' . "\n";
}
// Insert title, author and publication date in the page, using Unicode characters as icons.
echo tab(3) . '<h1 style="margin-bottom:0; text-align:center;">' . $article . "</h1>\n";
echo tab(3) . '<p style="margin-top:0; font-size:80%; text-align:center;">' . '📅 ' .
$date . ' 👤 ' . $author . "</p>\n";
echo tab(3) . "<br>\n";
echo tab(3) . '<div id="columns" class="columns" style="column-count:' . $columns . ';">' . "\n";
if ( mb_strtolower(substr($page_file, -2)) == 'md' ) {
$body = $Parsedown->text($body);
// Add class="responsive-img" to <img> tags
$body = preg_replace_callback(
'/<img\s+([^>]*?)\/?>/i',
function ($matches) {
$attrs = $matches[1];
if (preg_match('/class=["\'].*\bresponsive-img\b.*["\']/', $attrs)) {
return "<img $attrs>";
}
if (preg_match('/class=(["\'])(.*?)\1/', $attrs, $classMatch)) {
$quote = $classMatch[1];
$classes = $classMatch[2];
$newClass = 'class=' . $quote . 'responsive-img ' . $classes . $quote;
$attrs = str_replace($classMatch[0], $newClass, $attrs);
} else {
$attrs .= ' class="responsive-img"';
}
return "<img $attrs>";
},
$body
);
$body .= "\n";
}
// Convert blockquotes of type [!Note], [!Tip], [!Warning]
$body = preg_replace_callback(
'/<blockquote>\s*<p>\[!(\w+)\]\s*(.*?)<\/p>(.*?)<\/blockquote>/is',
function ($matches) {
$type = strtolower($matches[1]);
$content = $matches[2] . $matches[3]; // join first line + rest
$map = [
'note' => [
'class' => 'alert alert-note',
'icon' => '📝', // 📝
'label' => NOTE
],
'tip' => [
'class' => 'alert alert-tip',
'icon' => '💡', // 💡
'label' => TIP
],
'warning' => [
'class' => 'alert alert-warning',
'icon' => '⚠️', // ⚠️
'label' => WARNING
],
];
if (!isset($map[$type])) {
return $matches[0]; // If you didn't recognize it, leave it as it was.
}
$icon = $map[$type]['icon'];
$label = $map[$type]['label'];
$class = $map[$type]['class'];
return '<div class="' . $class . '">' . "\n" .
$icon . ' <strong>' . $label . ':</strong> ' . $content .
"\n" . '</div>';
},
$body
);
// Responsiveness: scrollable tables when needed
$body = preg_replace('/<table([^>]*)>/', "<div class=\"table-container\">\n<table$1>", $body);
$body = preg_replace('/<\/table>/', "</table>\n</div>", $body);
$body = addTabsOutsidePre($body, 4);
echo $body;
echo tab(3) . "</div>\n";
backButton(3,4);
} else {
include('404.php');
}
?>