nieuws: dynamic article system
authorMischa POSLAWSKY <perl@shiar.org>
Wed, 4 Oct 2017 23:04:11 +0000 (01:04 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Fri, 8 Jun 2018 21:14:52 +0000 (23:14 +0200)
Copied from lijtweg site v2.4-25-g7cfaa2f8d4 (2017-10-05).
Simple js to create pages in /nieuws/year/month/day-title.html
and overview to concatenate them chronologically.

nieuws.inc.php [new file with mode: 0644]
nieuws.php [new file with mode: 0644]
nieuws/edit.js [new file with mode: 0644]

diff --git a/nieuws.inc.php b/nieuws.inc.php
new file mode 100644 (file)
index 0000000..ca16b4a
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+$monthname = ['?',
+       'januari', 'februari', 'maart', 'april', 'mei', 'juni',
+       'juli', 'augustus', 'september', 'oktober', 'november', 'december',
+];
+
+function shownewsdate($url)
+{
+       $parts = pathinfo($url);
+       $year = pathinfo($parts['dirname'], PATHINFO_FILENAME);
+       @list ($month, $day) = explode('-', $parts['filename'], 3);
+       global $monthname;
+       return sprintf('<small class="date">%s %s %s</small>',
+               intval($day), $monthname[intval($month)], $year);
+}
+
+function shownewsarticle($url, $link = TRUE, $title = NULL)
+{
+       $html = ob_get_clean();
+       $date = shownewsdate($url);
+       ob_start();
+       print $title ?: '\1';
+       print '[[1]]';
+
+       $title = sprintf(
+               $link ? '<h3><a href="/%2$s">%s</a></h3>' : '<h2>%s</h2>',
+               getoutput([1 => ' <small class="date">'.$date.'</small>']),
+               preg_replace('/\.html$/', '', $url)
+       );
+       return preg_replace('{<h2>(.*?)</h2>}', $title, $html);
+}
+
+function shownews($root, $limit = 5)
+{
+       foreach (array_reverse(glob("$root/*/*.html")) as $url) {
+               print "<article>";
+               ob_start();
+               include $url;
+               print shownewsarticle($url);
+               print "</article>\n\n";
+
+               if (--$limit <= 0) break;
+       }
+}
diff --git a/nieuws.php b/nieuws.php
new file mode 100644 (file)
index 0000000..157986c
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+include 'nieuws.inc.php';
+
+if ($Args) {
+       $edit = !empty($User['admin']) ? htmlspecialchars(@$_GET['edit']) : NULL;
+       print shownewsarticle($Args, FALSE, $edit);
+       return;
+}
+
+ob_clean();
+print "<h2>Nieuwsarchief</h2>\n\n";
+
+print '<div id="news">'."\n\n";
+shownews($Page, 20);
+print "</div>\n\n";
+
+if (!empty($User['admin'])) {
+       print '<script src="/nieuws/edit.js"></script>'."\n";
+}
diff --git a/nieuws/edit.js b/nieuws/edit.js
new file mode 100644 (file)
index 0000000..13ca8da
--- /dev/null
@@ -0,0 +1,18 @@
+var editlink = document.createElement('a');
+editlink.className = 'nav';
+editlink.appendChild(document.createTextNode('Nieuw artikel'));
+editlink.onclick = function () {
+       var today = new Date().toJSON().slice(0, 10);
+       var input = prompt('Artikelnaam (plaatsingsdatum en paginatitel)', today + ' ');
+       if (!input) return false;
+       if (!/^\d{4}-\d\d-\d\d[- :]+\S/.test(input)) {
+               alert('Artikelnaam moet beginnen met een datum in de vorm jaar-mm-dd.');
+               return false;
+       }
+       var url = input.toLowerCase().replace(/[^a-z0-9]+/g, ' ').trim()
+                                  .replace(/ /g, '-').replace('-', '/');
+       var title = encodeURIComponent(input.substr(11).trim());
+       window.location = '/nieuws/'+url+'?edit='+title+'#edit';
+       return false;
+};
+document.getElementById('news').appendChild(editlink);