work now with instances without markdown export

This commit is contained in:
Ventricule 2018-03-19 17:58:12 +01:00
commit 9f9482a0bb
10 changed files with 115 additions and 22 deletions

3
.gitmodules vendored
View file

@ -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

View file

@ -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(

View file

@ -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');

View file

@ -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("<br />","<br>","<br/>");
return str_ireplace($breaks, "\r\n", $text);
}

View file

@ -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;
}

View file

@ -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#', '<u>$1</u>', $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#', '<u>$1</u>', $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('#<a.*?>(.*?)</a>#i', '\1', $body);
// remove <div>JavaScript license information</div>
$body = preg_replace('#<a.*?>(.*?)</a>#i', '\1', $body);
// convert <br> 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;

1
libreto/vendor/Markdownify vendored Submodule

@ -0,0 +1 @@
Subproject commit 53fae3d777a2474801787a2b1a2259a016724768

View file

@ -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);

View file

@ -3,6 +3,7 @@
<ul class="helpers">
<li class='switcher_button <?= $_SESSION['mode'] == 'read' ? 'active' : ''; ?>'><a href="?mode=read"><?= l("read", false) ?></a></li>
<li class='switcher_button <?= $_SESSION['mode'] == 'write' ? 'active' : ''; ?>'><a href="?mode=write"><?= l("write", false) ?></a></li>
<li class='' style="flex: 0;"><a href='' class="refresh" data-name-encoded='' alt='<?= l("update", false) ?>'></a></li>
</ul>
<ul class="menu">
@ -19,7 +20,6 @@
<ul class="helpers">
<li class=""><a href='/bindery/<?= $libreto->name() ?>' class="" data-name-encoded=''><?= l("export", false) ?></a></li>
<li class=""><a href='/export/<?= $libreto->name() ?>' class="" data-name-encoded='' download><?= l("download", false) ?></a></li>
<li class='' style="flex: 0;"><a href='' class="refresh" data-name-encoded='' alt='<?= l("update", false) ?>'></a></li>
</ul>
<?php endif; ?>
</nav>

View file

@ -2,7 +2,7 @@
<?php
if(isset($_POST['new_name'])):
if($new_name = $_POST['new_name']) :
header('Location: ' . $libreto->options('url') . '/' . urlencode($new_name) );
header('Location:' . $libreto->options('url') . '/' . urlencode($new_name) );
endif;
endif;
?>