diff --git a/.gitmodules b/.gitmodules index 19cdd7f..a5a158f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -31,3 +31,6 @@ [submodule "libreto/vendor/html2opendocument"] path = libreto/vendor/html2opendocument url = https://github.com/CatoTH/html2opendocument.git +[submodule "libreto/vendor/Markdownify"] + path = libreto/vendor/Markdownify + url = https://github.com/Elephant418/Markdownify.git diff --git a/config.php b/config.php index 63d71b4..2d1d748 100644 --- a/config.php +++ b/config.php @@ -4,12 +4,51 @@ $providers = array( 'name' => "Framapad", 'url' => "https://annuel2.framapad.org", 'default_text' => "–––––", + 'markdown' => true, + 'html' => true, ), 'board' => array( 'name' => "Board", 'url' => "https://board.net", 'default_text' => "--", - ) + 'markdown' => true, + 'html' => true, + ), + 'wikimedia' => array( + 'name' => "Wikimedia", + 'url' => "https://etherpad.wikimedia.org", + 'default_text' => "Welcome to WMF etherpad installation.", + 'markdown' => false, + 'html' => true, + ), + 'allmende' => array( + 'name' => "Allmende", + 'url' => "https://text.allmende.io", + 'default_text' => "", + 'markdown' => false, + 'html' => true, + ), + 'factor' => array( + 'name' => "Factor", + 'url' => "https://factor.cc/pad", + 'default_text' => "Welcome to factor.cc Pad!", + 'markdown' => false, + 'html' => true, + ), + 'etherpad' => array( + 'name' => "Etherpad.net", + 'url' => "https://etherpad.net", + 'default_text' => "Welcome to etherpad.net Pad!", + 'markdown' => false, + 'html' => true, + ), + 'lqdn' => array( + 'name' => "La Quadrature du Net", + 'url' => "https://pad.lqdn.fr", + 'default_text' => "Welcome to Etherpad!", + 'markdown' => false, + 'html' => true, + ), ); $options = array( diff --git a/index.php b/index.php index 6a34011..e385be7 100644 --- a/index.php +++ b/index.php @@ -10,7 +10,7 @@ require __DIR__ . DS . 'libreto' . DS . 'helpers.php'; // load all core classes function loadClass($classe) { - require __DIR__ . DS . 'libreto' . DS . $classe . '.php'; + require __DIR__ . DS . 'libreto' . DS . strtolower($classe) . '.php'; } spl_autoload_register('loadClass'); diff --git a/libreto/helpers.php b/libreto/helpers.php index 3d7ae0e..20099e6 100644 --- a/libreto/helpers.php +++ b/libreto/helpers.php @@ -41,3 +41,20 @@ function endsWith($haystack, $needle) return $length === 0 || (substr($haystack, -$length) === $needle); } + +function url_get_contents($Url) { + if (!function_exists('curl_init')){ + die('CURL is not installed!'); + } + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $Url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $output = curl_exec($ch); + curl_close($ch); + return $output; +} + +function br2nl($text){ + $breaks = array("
","
","
"); + return str_ireplace($breaks, "\r\n", $text); +} diff --git a/libreto/libreto.php b/libreto/libreto.php index 3961bda..6266666 100644 --- a/libreto/libreto.php +++ b/libreto/libreto.php @@ -16,7 +16,7 @@ class Libreto public function defaults(){ $defaults = array( 'name' => "Libreto", - 'url' => $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER["SERVER_NAME"], + 'url' => ( isset($_SERVER["HTTPS"]) ? 'https' : 'http' ) . '://' . $_SERVER["SERVER_NAME"], 'default_provider' => 'framapad', ); @@ -137,10 +137,6 @@ class Libreto return $this->pads; } - public function credits() { - $Parsedown->text(file_get_contents("/libreto/assets/texts/colophon.md")); - } - public function mode(){ return $this->mode; } diff --git a/libreto/pad.php b/libreto/pad.php index 989abb5..f727d5b 100644 --- a/libreto/pad.php +++ b/libreto/pad.php @@ -74,7 +74,8 @@ class Pad "pad" => $url . $libreto->options('pads_params'), "reader" => "/reader/" . urlencode($libreto->name()) . '/' . urlencode($this->name), "txt" => $url . "/export/txt", - "markdown" => $url . "/export/markdown" + "markdown" => $url . "/export/markdown", + "html" => $url . "/export/html", ); } @@ -125,19 +126,51 @@ class Pad public function html() { - global $Parsedown, $Purifier; + global $Parsedown, $Purifier, $Markdownify, $libreto; - $markdown = file_get_contents($this->url('markdown')); - // remove \url{} tags - $markdown = preg_replace('#\\\url\{(.+)\}#i', '$1', $markdown); - // replace underline tags - $markdown = preg_replace('#underline(.+)underline#', '$1', $markdown); - // strip slashes - $markdown = stripslashes($markdown); - // parse - $html = $Parsedown->text($markdown); - // sanitize - $html = $Purifier->purify($html); + // we prefer to get html from markdown but if markdown export is not activated on etherpad instance, we use html export + if($libreto->provider('markdown')): + $markdown = file_get_contents($this->url('markdown')); + // remove \url{} tags + $markdown = preg_replace('#\\\url\{(.+)\}#i', '$1', $markdown); + // replace underline tags + $markdown = preg_replace('#underline(.+)underline#', '$1', $markdown); + // strip slashes + $markdown = stripslashes($markdown); + // parse + $html = $Parsedown->text($markdown); + // sanitize + $html = $Purifier->purify($html); + else: + $html = file_get_contents($this->url('html')); + // dom load + libxml_use_internal_errors(true); //Prevents Warnings, remove if desired + $dom = new DOMDocument(); + $dom->loadHTML($html); + // remove hidden divs + foreach($dom->getElementsByTagName("div") as $div) { + if ($div->getAttribute('style') == 'display:none') { + $div->parentNode->removeChild($div); + } + } + // get body part + $body = ""; + foreach($dom->getElementsByTagName("body")->item(0)->childNodes as $child) { + $body .= $dom->saveHTML($child); + } + // remove links + $body = preg_replace('#(.*?)#i', '\1', $body); + // remove
JavaScript license information
+ $body = preg_replace('#(.*?)#i', '\1', $body); + // convert
to newline + $body = br2nl($body); + // convert to markdown + //$markdown = $Markdownify->parseString($body); + // parse to html + $html = $Parsedown->text($body); + // sanitize + $html = $Purifier->purify($html); + endif; // return return $html; diff --git a/libreto/vendor/Markdownify b/libreto/vendor/Markdownify new file mode 160000 index 0000000..53fae3d --- /dev/null +++ b/libreto/vendor/Markdownify @@ -0,0 +1 @@ +Subproject commit 53fae3d777a2474801787a2b1a2259a016724768 diff --git a/libreto/vendor/load.php b/libreto/vendor/load.php index 79b4fb3..948b4c8 100644 --- a/libreto/vendor/load.php +++ b/libreto/vendor/load.php @@ -6,7 +6,11 @@ require_once 'html2opendocument/Base.php'; require_once 'html2opendocument/Text.php'; require_once 'htmlpurifier/library/HTMLPurifier.auto.php'; require_once 'spyc/Spyc.php'; +require_once 'Markdownify/src/Converter.php'; +require_once 'Markdownify/src/ConverterExtra.php'; +require_once 'Markdownify/src/Parser.php'; $Purifier = new HTMLPurifier(); +$Markdownify = new Markdownify\ConverterExtra; $Parsedown = new ParsedownExtra(); $Parsedown = $Parsedown->setBreaksEnabled(true); diff --git a/views/snippets/nav.php b/views/snippets/nav.php index d34a485..48b15bb 100644 --- a/views/snippets/nav.php +++ b/views/snippets/nav.php @@ -3,6 +3,7 @@