-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss.php
More file actions
107 lines (78 loc) · 3.05 KB
/
rss.php
File metadata and controls
107 lines (78 loc) · 3.05 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
<?php
include 'render_board.php';
// rss
// header( "content-type: application/xml; charset=UTF-8" );
$baseUrl = "https://hd.marceloexc.com";
$contentDir = __DIR__ . "/content";
$masterXml = new DOMDocument("1.0", "UTF-8");
$rss = $masterXml->createElement("rss");
$rss->setAttribute("version", "2.0");
$rss->setAttribute("xmlns:content", "http://purl.org/rss/1.0/modules/content/");
$masterXml->appendChild($rss);
$channel = $masterXml->createElement("channel");
$channel->appendChild($masterXml->createElement("title", "harddrive"));
$channel->appendChild($masterXml->createElement("link", $baseUrl));
$channel->appendChild($masterXml->createElement("description", "recent links"));
$channel->appendChild($masterXml->createElement("language", "en"));
$rss->appendChild($channel);
$b = new BoardListingsRenderer("content");
$hi = $b->populateBoards();
$boards = $b->getBoards();
foreach ($boards as $board) {
$index = $board->path . "/index.html";
if (!file_exists($index)) {
continue;
}
// load html into a domdoc
$html = file_get_contents($index);
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTML($html);
libxml_clear_errors();
// get metadata
$title = $board->title;
$link = $baseUrl . $board->getCleanPath();
$guid = $link; // its the same for me, idc
$description = $doc->getElementsByTagName("title")->item(0)->textContent;
$date = date(DATE_RFC822, $board->date_unix);
$item = $masterXml->createElement("item");
$item->appendChild($masterXml->createElement("title", $title));
$item->appendChild($masterXml->createElement("link", $link));
$item->appendChild($masterXml->createElement("guid", $guid));
$item->appendChild($masterXml->createElement("description", $description));
$item->appendChild($masterXml->createElement("pubDate", $date));
$xpath = new DOMXPath($doc);
$contentNode = $xpath
->query('//div[@id="content"]')
->item(0);
$innerHtml = "";
if ($contentNode) {
foreach ($contentNode->childNodes as $child) {
$innerHtml .= $doc->saveHTML($child);
}
}
// replace all relative links to absolute links
$innerHtml = preg_replace_callback(
'/src="([^"]+)"/',
function ($matches) use ($link) {
$src = $matches[1];
if (str_starts_with($src, "http")) {
return $matches[0];
}
return 'src="' . $link . $src . '"';
},
$innerHtml
);
// get rid of the "Figure X" <span>'s that org mode html export loves to make
// this is done by regex...
$pattern = '/<span[^>]*>\s*Figure\s*\d+[:.]?\s*<\/span>/';
$innerHtml = preg_replace($pattern, '', $innerHtml);
$content = $masterXml->createElement("content:encoded");
$cdata = $masterXml->createCDATASection($innerHtml);
$content->appendChild($cdata);
$item->appendChild($content);
$channel->appendChild($item);
}
$masterXml->formatOutput = true;
file_put_contents("rss.xml", $masterXml->saveXML());
echo "rss xml generated";