-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss_generator.php
More file actions
110 lines (93 loc) · 3.85 KB
/
Copy pathrss_generator.php
File metadata and controls
110 lines (93 loc) · 3.85 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
<?php
// Set the content-type to XML
header("Content-Type: application/rss+xml; charset=UTF-8");
function extractValueFromPattern($filename, $pattern) {
$markdownContent = file_get_contents("pages/posts/" . $filename . ".html");
if (preg_match($pattern, $markdownContent, $matches)) {
return trim($matches[1]);
}
return null;
}
// Function to get MIME type from file extension
function getMimeType($url) {
$path = parse_url($url, PHP_URL_PATH); // strips query string and fragment
$extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
$mimeTypes = [
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'webp' => 'image/webp',
// Add more extensions and MIME types as needed
];
return $mimeTypes[$extension] ?? 'application/octet-stream';
}
// Define patterns for the metadata you want to extract
$patterns = [
'pagetitle' => '/<!--\s+pagetitle:(.*?)\s+-->/s',
'pagedate' => '/<!--\s+pagedate:(.*?)\s+-->/s',
'pageexcerpt' => '/<!--\s+pageexcerpt:(.*?)\s+-->/s',
'pageimage' => '/<!--\s+pageimage:(.*?)\s+-->/s' // Image pattern
];
// Site information
$siteTitle = $WebsiteTitle;
$siteLink = $WebsiteURL;
$siteDescription = $WebsiteDescription;
$defaultImage = $WebsiteImage; // Default image URL
// Initialize the RSS feed
echo '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
echo '<rss version="2.0">' . "\n";
echo '<channel>' . "\n";
echo '<title>' . htmlspecialchars($siteTitle) . '</title>' . "\n";
echo '<link>' . htmlspecialchars($siteLink) . '</link>' . "\n";
echo '<description>' . htmlspecialchars($siteDescription) . '</description>' . "\n";
echo '<language>en-us</language>' . "\n";
// Channel-wide image
echo '<image>' . "\n";
echo '<url>' . htmlspecialchars($WebsiteImage) . '</url>' . "\n";
echo '<title>' . htmlspecialchars($siteTitle) . '</title>' . "\n";
echo '<link>' . htmlspecialchars($siteLink) . '</link>' . "\n";
echo '</image>' . "\n";
// Collect all items
$items = [];
$postsDir = __DIR__ . '/pages/posts';
foreach (new DirectoryIterator($postsDir) as $fileInfo) {
if ($fileInfo->isDot() || $fileInfo->getExtension() !== 'html') continue;
$filename = $fileInfo->getBasename('.html');
$pageData = [];
// Extract metadata using the patterns
foreach ($patterns as $variableName => $pattern) {
$pageData[$variableName] = extractValueFromPattern($filename, $pattern);
}
if (empty($pageData['pagetitle']) || empty($pageData['pagedate'])) continue;
$url = $siteLink . '/posts/' . $filename;
$pubDate = strtotime($pageData['pagedate']); // Use timestamp for sorting
$imageUrl = !empty($pageData['pageimage']) ? $siteLink . '/' . $pageData['pageimage'] : $defaultImage;
$mimeType = getMimeType($imageUrl);
// Add the item to the array
$items[] = [
'title' => htmlspecialchars($pageData['pagetitle']),
'link' => htmlspecialchars($url),
'description' => htmlspecialchars($pageData['pageexcerpt']),
'enclosure' => '<enclosure url="' . htmlspecialchars($imageUrl) . '" type="' . htmlspecialchars($mimeType) . '" length="0" />',
'pubDate' => date(DATE_RSS, $pubDate),
'timestamp' => $pubDate // Store timestamp for sorting
];
}
// Sort items by date, latest first
usort($items, function($a, $b) {
return $b['timestamp'] - $a['timestamp'];
});
// Output sorted items
foreach ($items as $item) {
echo '<item>' . "\n";
echo '<title>' . $item['title'] . '</title>' . "\n";
echo '<link>' . $item['link'] . '</link>' . "\n";
echo '<description>' . $item['description'] . '</description>' . "\n";
echo $item['enclosure'] . "\n";
echo '<pubDate>' . $item['pubDate'] . '</pubDate>' . "\n";
echo '</item>' . "\n";
}
echo '</channel>' . "\n";
echo '</rss>' . "\n";
?>