]> 91.132.146.200 Git - dolphin.git/commitdiff
s9y export to md and hugo static search
authorBanana <banana@optimus.de>
Sun, 9 Oct 2016 14:10:41 +0000 (16:10 +0200)
committerBanana <banana@optimus.de>
Sun, 9 Oct 2016 14:10:41 +0000 (16:10 +0200)
s9y-to-md/README [new file with mode: 0644]
s9y-to-md/export.php [new file with mode: 0644]
s9y-to-md/output/.gitignore [new file with mode: 0644]
static-hugo-search/README [new file with mode: 0644]
static-hugo-search/search.php [new file with mode: 0644]

diff --git a/s9y-to-md/README b/s9y-to-md/README
new file mode 100644 (file)
index 0000000..97a3bc1
--- /dev/null
@@ -0,0 +1,5 @@
+A very simple export of the serendipity entries to a mardown file.
+Use this as a base to get started and write your own export.
+
+This usese the pandoc commandline tool to convert the HTML to MD...
+http://pandoc.org/
diff --git a/s9y-to-md/export.php b/s9y-to-md/export.php
new file mode 100644 (file)
index 0000000..a6237d4
--- /dev/null
@@ -0,0 +1,131 @@
+<?php
+/**
+ *  dolphin. Collection of useful PHP skeletons.
+ *  Copyright (C) 2016  Johannes 'Banana' Keßler
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
+ *
+ * You should have received a copy of the
+ * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
+ * along with this program.  If not, see http://www.sun.com/cddl/cddl.html
+ */
+
+/**
+ * this is a very simple serendipity to md export.
+ * user this as a base to improve your own export.
+ */
+
+mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); # throw exeptions
+$DB = new mysqli('127.0.0.1', 'root', 'pasword', 'blog');
+if ($DB->connect_errno) exit('Can not connect to MySQL Server');
+#$DB->set_charset("utf8mb4");
+#$DB->query("SET collation_connection = 'utf8mb4_bin'");
+
+$workingDir = getcwd();
+
+date_default_timezone_set('Europe/Berlin');
+
+# get the tags as array per entrie
+$tags = array();
+$queryStr = "SELECT * FROM serendipity_entrytags";
+$query = $DB->query($queryStr);
+while($result = $query->fetch_assoc()) {
+       $tags[$result['entryid']][] = $result['tag'];
+}
+
+$categories = array();
+$queryStr = "SELECT ec.entryid, c.category_name FROM `serendipity_entrycat` AS ec
+                               LEFT JOIN serendipity_category as c ON ec.categoryid = c.categoryid";
+$query = $DB->query($queryStr);
+while($result = $query->fetch_assoc()) {
+       $categories[$result['entryid']][] = $result['category_name'];
+}
+
+# get the links and preformat them
+$links = array();
+$queryStr = "SELECT * FROM serendipity_permalinks WHERE `type` = 'entry'";
+$query = $DB->query($queryStr);
+while($result = $query->fetch_assoc()) {
+       $_link = $result['permalink'];
+       $_link = str_replace('archives/'.$result['entry_id'].'-','',$_link);
+       $_link = str_replace('.html','',$_link);
+       $_link = trim($_link, ' .,');
+       $links[$result['entry_id']] = $_link;
+}
+
+# get the entries
+$queryStr = "SELECT * FROM serendipity_entries WHERE `isdraft` = 'false'";
+$query = $DB->query($queryStr);
+while($result = $query->fetch_assoc()) {
+       $_tags = '';
+       if(isset($tags[$result['id']])) {
+               $_tags = '"'.implode('","',$tags[$result['id']]).'"';
+       }
+       $tagstring = $_tags;
+       
+       $_categories = '';
+       if(isset($categories[$result['id']])) {
+               $_categories = '"'.implode('","',$categories[$result['id']]).'"';
+       }
+       $category = '"Dev",'.$_categories;
+       $category = trim($category, " ,");
+
+       $filename = $result['id'];
+       if(isset($links[$result['id']])) {
+               $filename = $links[$result['id']];
+       }
+       
+       $year = date("Y",$result['timestamp']);
+       $month = date("m",$result['timestamp']);
+
+       if(!file_exists('output/'.$year)) {
+               mkdir('output/'.$year);
+       }
+
+       if(!file_exists('output/'.$year.'/'.$month)) {
+               mkdir('output/'.$year.'/'.$month);
+       }
+
+       $targetfile = 'output/'.$year.'/'.$month.'/'.$filename.".md";
+
+       $date = date("Y-m-d",$result['timestamp']);
+       $title = trim($result['title']);
+       $title = str_replace('"','',$title);
+       
+       $filedata = <<<FDATA
++++
+date        = "$date"
+title       = "$title"
+description = ""
+tags        = [ $tagstring ]
+topics      = [ $category ]
+subheadline    = ""
+
+$year = "$month"
++++
+
+FDATA;
+
+       $body = nl2br($result['body'],false);
+       # create the md content from the existing content
+       file_put_contents('body.html',$body);
+
+       
+       $command = "pandoc --no-wrap --parse-raw -f html -t markdown -o ".$workingDir."/body.md ".$workingDir."/body.html";
+       exec($command);
+
+       $mdData = file_get_contents('body.md');
+
+       file_put_contents($targetfile,$filedata.$mdData);
+
+       unlink('body.html');
+       unlink('body.md');
+
+       #var_dump($targetfile);
+       #var_dump($result);
+}
+
+
+
+$DB->close();
diff --git a/s9y-to-md/output/.gitignore b/s9y-to-md/output/.gitignore
new file mode 100644 (file)
index 0000000..d6b7ef3
--- /dev/null
@@ -0,0 +1,2 @@
+*
+!.gitignore
diff --git a/static-hugo-search/README b/static-hugo-search/README
new file mode 100644 (file)
index 0000000..d7e8d7f
--- /dev/null
@@ -0,0 +1,2 @@
+This is a very simple file search for the generated HTML files from hugo.
+http://gohugo.io/
diff --git a/static-hugo-search/search.php b/static-hugo-search/search.php
new file mode 100644 (file)
index 0000000..6dee5d5
--- /dev/null
@@ -0,0 +1,167 @@
+<?php
+
+/**
+ *  dolphin. Collection of useful PHP skeletons.
+ *  Copyright (C) 2016  Johannes 'Banana' Keßler
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
+ *
+ * You should have received a copy of the
+ * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
+ * along with this program.  If not, see http://www.sun.com/cddl/cddl.html
+ */
+
+
+mb_http_output('UTF-8');
+mb_internal_encoding('UTF-8');
+ini_set('error_reporting',-1); // E_ALL & E_STRICT
+
+if(ini_get("magic_quotes_gpc") == 1) {
+       die('Magic quotes is set to "on", and system is not able to change it. Please update Your php.ini file');
+}
+
+## check request
+$_urlToParse = filter_var($_SERVER['QUERY_STRING'],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
+if(!empty($_urlToParse)) {
+    # see http://de2.php.net/manual/en/regexp.reference.unicode.php
+    if(preg_match('/[\p{C}\p{M}\p{Sc}\p{Sk}\p{So}\p{Zl}\p{Zp}]/u',$_urlToParse) === 1) {
+        die('Malformed request. Make sure you know what you are doing.');
+    }
+}
+
+# time settings
+date_default_timezone_set('Europe/Berlin');
+
+define('DEBUG',false);
+
+## set the error reporting
+ini_set('log_errors',true);
+ini_set('error_log','error.log');
+if(DEBUG === true) {
+    ini_set('display_errors',true);
+}
+else {
+    ini_set('display_errors',false);
+}
+
+$requestString = false;
+$searchStr = false;
+$searchResult = false;
+$searchCount = 0;
+$tagLink = false;
+$catLink = false;
+
+
+if(isset($_POST['query'])) {
+       $searchStr = trim($_POST['query']);
+       $searchStr = strtolower($searchStr);
+
+       $searchStr = preg_replace("/[^\p{L}\p{N}\p{Zs}]/u","",$searchStr);
+
+       # kinda full txt search
+       if(!empty($searchStr)) {
+               $files = glob('20*/*/*/*.html');        
+               $limit = 0;
+               if(!empty($files)) {
+                       foreach($files as $file) {
+                               
+                               if($limit > 30) break;
+
+                               $stream = new SplFileObject($file); 
+                               $grepped = new RegexIterator($stream, '/'.$searchStr.'/'); 
+                               foreach($grepped as $found) {
+                                       $_text = $file;
+
+                                       
+                                       $headlines = new RegexIterator($stream,"/<h2>([^<]+)<\/h2>/");
+                                       foreach($headlines as $headline) {
+                                               $_text = trim($headline);
+                                               $_text = strip_tags($_text);
+                                               break;
+                                       }
+                                       
+                                       
+                                       $searchResult[] = array(
+                                               'href' => 'https://url.to/'.$file,
+                                               'text' => $_text
+                                       );
+                                       $limit++;
+                                       
+                                       #var_dump($found);
+                                       #var_dump($file);
+                                       #var_dump($searchResult);
+                                       #exit();
+
+                                       unset($headlines);
+                                       unset($_text);
+                                       break;
+                               }
+                               unset($stream);
+                               unset($grepped);
+                       }
+                       $searchCount = count($searchResult);
+               }
+       }
+
+       # is it a tag?
+       $tagDir = 'tags/'.$searchStr;
+       if(file_exists($tagDir)) {
+               $tagLink = $searchStr;
+       }
+
+       # is it a category?
+       $topicsDir = 'topics/'.$searchStr;
+       if(file_exists($topicsDir)) {
+               $catLink = $searchStr;
+       }       
+}
+
+header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
+header("Cache-Control: post-check=0, pre-check=0", false);
+header("Pragma: no-cache");
+?>
+<!DOCTYPE HTML>
+<html>
+       <head>
+               <title>Suche nach: <?php echo $searchStr; ?></title>
+               <meta charset="utf-8" />
+               <meta name="robots" content="noindex,nofollow" />
+               <meta name="viewport" content="width=device-width, initial-scale=1" />
+       </head>
+
+       <body>
+               <div >
+
+                       <article >
+                               <header>
+                                       <h2>Suche</h2>
+                                       <p>Gesucht: <?php echo $searchStr; ?> | Gefunden: <?php echo $searchCount; ?></p>
+                               </header>
+
+                               <?php if(!empty($tagLink)) { ?>
+                               <p>Tag: <a href="https://www.url.to/tags/<?php echo $tagLink; ?>"><?php echo $tagLink; ?></a></p>
+                               <?php } ?> 
+                               <?php if(!empty($catLink)) { ?>
+                               <p>Kategorie: <a href="https://www.url.to/topics/<?php echo $catLink; ?>"><?php echo $catLink; ?></a></p>
+                               <?php } ?> 
+                               
+                               <?php if(!empty($searchResult)) { ?>
+                               <ul>
+                               <?php 
+                                       foreach($searchResult as $result) { 
+                                               echo "<li><a href='".$result['href']."'>".$result['text']."</a></li>\n";
+                                       }
+                               ?>
+                               </ul>
+                               <?php } else { ?>
+                                       <div>
+                        <p>Leider nichts gefunden.</p>
+                    </div>
+                               <?php } ?>
+                               
+                       </article>
+               </div>
+       </body>
+</html>
+