Skip to content

Commit 06b4afc

Browse files
authored
Update posts.php
1 parent 8530b9d commit 06b4afc

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

posts.php

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,39 @@
33
// Load Composer's autoload
44
require_once __DIR__ . '/vendor/autoload.php';
55

6-
// Load the RSS feed
7-
$feed = Feed::loadAtom('https://sharpapi.com/feed')->toArray();
6+
try {
7+
// Load the RSS feed and convert to an array
8+
$feed = Feed::loadAtom('https://sharpapi.com/feed')->toArray();
9+
} catch (Exception $e) {
10+
echo "Failed to load feed: ", $e->getMessage();
11+
exit(1);
12+
}
813

9-
// Generate the list of blog posts
14+
// Limit to the latest 5 posts (adjust if needed)
1015
$posts = '';
11-
foreach (array_slice($feed['item'], 0, 555) as $post) {
12-
$date = date('d/m/Y', strtotime($post['pubDate']));
16+
foreach (array_slice($feed['item'], 0, 5) as $post) {
17+
$date = date('d/m/Y', strtotime($post['pubDate']));
1318
$posts .= sprintf("\n* **[%s]** [%s](%s \"%s\")", $date, $post['title'], $post['link'], $post['title']);
1419
}
1520

16-
// Generate the new content
17-
$content = preg_replace(
18-
'#<!-- posts -->.*<!-- /posts -->#s',
19-
sprintf('<!-- posts -->%s<!-- /posts -->', $posts),
20-
file_get_contents('README.md')
21-
);
21+
// Load README.md content
22+
$readmePath = 'README.md';
23+
$readmeContent = file_get_contents($readmePath);
24+
25+
// Check if README.md contains the posts section and replace or append
26+
if (strpos($readmeContent, '<!-- posts -->') !== false) {
27+
// Replace content between <!-- posts --> and <!-- /posts -->
28+
$newContent = preg_replace(
29+
'#<!-- posts -->.*<!-- /posts -->#s',
30+
sprintf('<!-- posts -->%s<!-- /posts -->', $posts),
31+
$readmeContent
32+
);
33+
} else {
34+
// Append new posts section at the end if placeholders are missing
35+
$newContent = $readmeContent . "\n\n<!-- posts -->" . $posts . "<!-- /posts -->";
36+
}
37+
38+
// Write the updated content to README.md
39+
file_put_contents($readmePath, $newContent);
2240

23-
// Overwrite the file
24-
file_put_contents('README.md', $content);
41+
echo "README.md updated successfully with the latest blog posts.\n";

0 commit comments

Comments
 (0)