mirror of
https://github.com/sbrl/Pepperminty-Wiki.git
synced 2024-11-29 06:23:01 +00:00
Overhual image parsedown extension again.
Made a *ton* of bug fixes and stability improvements too.
This commit is contained in:
parent
7b638725ff
commit
03c52c06a1
6 changed files with 291 additions and 63 deletions
148
build/index.php
148
build/index.php
|
@ -506,7 +506,7 @@ function human_filesize($bytes, $decimals = 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates the time sincce a particular timestamp and returns a
|
* Calculates the time since a particular timestamp and returns a
|
||||||
* human-readable result.
|
* human-readable result.
|
||||||
* From http://goo.gl/zpgLgq.
|
* From http://goo.gl/zpgLgq.
|
||||||
* @param integer $time The timestamp to convert.
|
* @param integer $time The timestamp to convert.
|
||||||
|
@ -697,9 +697,12 @@ function mb_stripos_all($haystack, $needle) {
|
||||||
/**
|
/**
|
||||||
* Returns the system's mime type mappings, considering the first extension
|
* Returns the system's mime type mappings, considering the first extension
|
||||||
* listed to be cacnonical.
|
* listed to be cacnonical.
|
||||||
|
* From http://stackoverflow.com/a/1147952/1460422 by chaos.
|
||||||
|
* Edited by Starbeamrainbowlabs.
|
||||||
* @return array An array of mime type mappings.
|
* @return array An array of mime type mappings.
|
||||||
*/
|
*/
|
||||||
function system_mime_type_extensions() {
|
function system_mime_type_extensions()
|
||||||
|
{
|
||||||
global $settings;
|
global $settings;
|
||||||
$out = array();
|
$out = array();
|
||||||
$file = fopen($settings->mime_extension_mappings_location, 'r');
|
$file = fopen($settings->mime_extension_mappings_location, 'r');
|
||||||
|
@ -717,18 +720,62 @@ function system_mime_type_extensions() {
|
||||||
fclose($file);
|
fclose($file);
|
||||||
return $out;
|
return $out;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts a given mime type to it's associated file extension.
|
* Converts a given mime type to it's associated file extension.
|
||||||
|
* From http://stackoverflow.com/a/1147952/1460422 by chaos.
|
||||||
|
* Edited by Starbeamrainbowlabs.
|
||||||
* @param string $type The mime type to convert.
|
* @param string $type The mime type to convert.
|
||||||
* @return string The extension for the given mime type.
|
* @return string The extension for the given mime type.
|
||||||
*/
|
*/
|
||||||
function system_mime_type_extension($type) {
|
function system_mime_type_extension($type)
|
||||||
|
{
|
||||||
static $exts;
|
static $exts;
|
||||||
if(!isset($exts))
|
if(!isset($exts))
|
||||||
$exts = system_mime_type_extensions();
|
$exts = system_mime_type_extensions();
|
||||||
return isset($exts[$type]) ? $exts[$type] : null;
|
return isset($exts[$type]) ? $exts[$type] : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the system MIME type mapping of extensions to MIME types.
|
||||||
|
* From http://stackoverflow.com/a/1147952/1460422 by chaos.
|
||||||
|
* Edited by Starbeamrainbowlabs.
|
||||||
|
* @return array An array mapping file extensions to their associated mime types.
|
||||||
|
*/
|
||||||
|
function system_extension_mime_types()
|
||||||
|
{
|
||||||
|
global $settings;
|
||||||
|
$out = array();
|
||||||
|
$file = fopen($settings->mime_extension_mappings_location, 'r');
|
||||||
|
while(($line = fgets($file)) !== false) {
|
||||||
|
$line = trim(preg_replace('/#.*/', '', $line));
|
||||||
|
if(!$line)
|
||||||
|
continue;
|
||||||
|
$parts = preg_split('/\s+/', $line);
|
||||||
|
if(count($parts) == 1)
|
||||||
|
continue;
|
||||||
|
$type = array_shift($parts);
|
||||||
|
foreach($parts as $part)
|
||||||
|
$out[$part] = $type;
|
||||||
|
}
|
||||||
|
fclose($file);
|
||||||
|
return $out;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Converts a given file extension to it's associated mime type.
|
||||||
|
* From http://stackoverflow.com/a/1147952/1460422 by chaos.
|
||||||
|
* Edited by Starbeamrainbowlabs.
|
||||||
|
* @param string $ext The extension to convert.
|
||||||
|
* @return string The mime type associated with the given extension.
|
||||||
|
*/
|
||||||
|
function system_extension_mime_type($ext) {
|
||||||
|
static $types;
|
||||||
|
if(!isset($types))
|
||||||
|
$types = system_extension_mime_types();
|
||||||
|
$ext = strtolower($ext);
|
||||||
|
return isset($types[$ext]) ? $types[$ext] : null;
|
||||||
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -2603,6 +2650,14 @@ register_module([
|
||||||
add_action("preview", function() {
|
add_action("preview", function() {
|
||||||
global $settings, $env, $pageindex, $start_time;
|
global $settings, $env, $pageindex, $start_time;
|
||||||
|
|
||||||
|
if(empty($pageindex->{$env->page}->uploadedfilepath))
|
||||||
|
{
|
||||||
|
$im = errorimage("The page '$env->page' doesn't have an associated file.");
|
||||||
|
header("content-type: image/png");
|
||||||
|
imagepng($im);
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
|
||||||
$filepath = $env->storage_prefix . $pageindex->{$env->page}->uploadedfilepath;
|
$filepath = $env->storage_prefix . $pageindex->{$env->page}->uploadedfilepath;
|
||||||
$mime_type = $pageindex->{$env->page}->uploadedfilemime;
|
$mime_type = $pageindex->{$env->page}->uploadedfilemime;
|
||||||
|
|
||||||
|
@ -2875,6 +2930,18 @@ register_module([
|
||||||
"thing_url" => "https://github.com/sbrl/Pepprminty-Wiki",
|
"thing_url" => "https://github.com/sbrl/Pepprminty-Wiki",
|
||||||
"icon" => "https://avatars0.githubusercontent.com/u/9929737?v=3&s=24"
|
"icon" => "https://avatars0.githubusercontent.com/u/9929737?v=3&s=24"
|
||||||
],
|
],
|
||||||
|
"Mime type to file extension mapper" => [
|
||||||
|
"author" => "Chaos",
|
||||||
|
"author_url" => "https://stackoverflow.com/users/47529/chaos",
|
||||||
|
"thing_url" => "https://stackoverflow.com/a/1147952/1460422",
|
||||||
|
"icon" => "https://www.gravatar.com/avatar/aaee40db39ad6b164cfb89cb6ad4d176?s=328&d=identicon&s=24"
|
||||||
|
],
|
||||||
|
"Parsedown" => [
|
||||||
|
"author" => "erusev and others",
|
||||||
|
"author_url" => "https://github.com/erusev/",
|
||||||
|
"thing_url" => "https://github.com/erusev/parsedown/",
|
||||||
|
"icon" => "https://avatars1.githubusercontent.com/u/184170?v=3&s=24"
|
||||||
|
],
|
||||||
"Slightly modified version of Slimdown" => [
|
"Slightly modified version of Slimdown" => [
|
||||||
"author" => "Johnny Broadway",
|
"author" => "Johnny Broadway",
|
||||||
"author_url" => "https://github.com/jbroadway",
|
"author_url" => "https://github.com/jbroadway",
|
||||||
|
@ -4139,9 +4206,13 @@ class PeppermintParsedown extends ParsedownExtra
|
||||||
|
|
||||||
// Variable parsing
|
// Variable parsing
|
||||||
if(preg_match("/\{\{\{([^}]+)\}\}\}/", $fragment["text"], $matches))
|
if(preg_match("/\{\{\{([^}]+)\}\}\}/", $fragment["text"], $matches))
|
||||||
|
{
|
||||||
|
$params = [];
|
||||||
|
if(!empty($this->paramStack))
|
||||||
{
|
{
|
||||||
$stackEntry = array_slice($this->paramStack, -1)[0];
|
$stackEntry = array_slice($this->paramStack, -1)[0];
|
||||||
$params = !empty($stackEntry) ? $stackEntry["params"] : false;
|
$params = !empty($stackEntry) ? $stackEntry["params"] : false;
|
||||||
|
}
|
||||||
|
|
||||||
$variableKey = trim($matches[1]);
|
$variableKey = trim($matches[1]);
|
||||||
|
|
||||||
|
@ -4168,6 +4239,8 @@ class PeppermintParsedown extends ParsedownExtra
|
||||||
$variableValue .= "\t<li>" . $curStackEntry["pagename"] . "</li>\n";
|
$variableValue .= "\t<li>" . $curStackEntry["pagename"] . "</li>\n";
|
||||||
}
|
}
|
||||||
$variableValue .= "</ol>\n";
|
$variableValue .= "</ol>\n";
|
||||||
|
break;
|
||||||
|
// TODO: Add a option that displays a list of subpages here
|
||||||
}
|
}
|
||||||
if(isset($params[$variableKey]))
|
if(isset($params[$variableKey]))
|
||||||
{
|
{
|
||||||
|
@ -4199,11 +4272,11 @@ class PeppermintParsedown extends ParsedownExtra
|
||||||
|
|
||||||
protected function templateHandler($source)
|
protected function templateHandler($source)
|
||||||
{
|
{
|
||||||
global $pageindex, $paths;
|
global $pageindex, $env;
|
||||||
|
|
||||||
|
|
||||||
$parts = explode("|", trim($source, "{}"));
|
$parts = explode("|", trim($source, "{}"));
|
||||||
$parts = array_map(trim, $parts);
|
$parts = array_map("trim", $parts);
|
||||||
|
|
||||||
// Extract the name of the temaplate page
|
// Extract the name of the temaplate page
|
||||||
$templatePagename = array_shift($parts);
|
$templatePagename = array_shift($parts);
|
||||||
|
@ -4222,7 +4295,7 @@ class PeppermintParsedown extends ParsedownExtra
|
||||||
{
|
{
|
||||||
// This param contains an equals sign, so it's a named parameter
|
// This param contains an equals sign, so it's a named parameter
|
||||||
$keyValuePair = explode("=", $part, 2);
|
$keyValuePair = explode("=", $part, 2);
|
||||||
$keyValuePair = array_map(trim, $keyValuePair);
|
$keyValuePair = array_map("trim", $keyValuePair);
|
||||||
$params[$keyValuePair[0]] = $keyValuePair[1];
|
$params[$keyValuePair[0]] = $keyValuePair[1];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -4239,7 +4312,7 @@ class PeppermintParsedown extends ParsedownExtra
|
||||||
"params" => $params
|
"params" => $params
|
||||||
];
|
];
|
||||||
|
|
||||||
$templateFilePath = $paths->storage_prefix . $pageindex->$templatePagename->filename;
|
$templateFilePath = $env->storage_prefix . $pageindex->$templatePagename->filename;
|
||||||
|
|
||||||
$parsedTemplateSource = $this->text(file_get_contents($templateFilePath));
|
$parsedTemplateSource = $this->text(file_get_contents($templateFilePath));
|
||||||
|
|
||||||
|
@ -4290,7 +4363,7 @@ class PeppermintParsedown extends ParsedownExtra
|
||||||
|
|
||||||
protected function inlineExtendedImage($fragment)
|
protected function inlineExtendedImage($fragment)
|
||||||
{
|
{
|
||||||
if(preg_match('/^!\[(.*)\]\(([^ |)]+)\s*\|([^|)]*)(?:\|([^)]*))?\)/', $fragment["text"], $matches))
|
if(preg_match('/^!\[(.*)\]\(([^ |)]+)\s*(?:\|([^|)]*)(?:\|([^)]*))?)?\)/', $fragment["text"], $matches))
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* 0 - Everything
|
* 0 - Everything
|
||||||
|
@ -4300,41 +4373,81 @@ class PeppermintParsedown extends ParsedownExtra
|
||||||
* 4 - Second Param (optional)
|
* 4 - Second Param (optional)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var_dump($matches);
|
|
||||||
|
|
||||||
$altText = $matches[1];
|
$altText = $matches[1];
|
||||||
$imageUrl = $matches[2];
|
$imageUrl = str_replace("&", "&", $matches[2]); // Decode & to allow it in preview urls
|
||||||
$param1 = strtolower(trim($matches[3]));
|
$param1 = !empty($matches[3]) ? strtolower(trim($matches[3])) : false;
|
||||||
$param2 = empty($matches[4]) ? false : strtolower(trim($matches[4]));
|
$param2 = empty($matches[4]) ? false : strtolower(trim($matches[4]));
|
||||||
$floatDirection = false;
|
$floatDirection = false;
|
||||||
$imageSize = false;
|
$imageSize = false;
|
||||||
|
|
||||||
if($this->isFloatValue($param1))
|
if($this->isFloatValue($param1))
|
||||||
{
|
{
|
||||||
|
// Param 1 is a valid css float: ... value
|
||||||
$floatDirection = $param1;
|
$floatDirection = $param1;
|
||||||
$imageSize = $this->parseSizeSpec($param2);
|
$imageSize = $this->parseSizeSpec($param2);
|
||||||
}
|
}
|
||||||
else if($this->isFloatValue($param2))
|
else if($this->isFloatValue($param2))
|
||||||
{
|
{
|
||||||
|
// Param 2 is a valid css float: ... value
|
||||||
$floatDirection = $param2;
|
$floatDirection = $param2;
|
||||||
$imageSize = $this->parseSizeSpec($param1);
|
$imageSize = $this->parseSizeSpec($param1);
|
||||||
}
|
}
|
||||||
|
else if($param1 === false and $param2 === false)
|
||||||
|
{
|
||||||
|
// Neither params were specified
|
||||||
|
$floatDirection = false;
|
||||||
|
$imageSize = false;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
// Neither of them are floats, but at least one is specified
|
||||||
|
// This must mean that the first param is a size spec like
|
||||||
|
// 250x128.
|
||||||
$imageSize = $this->parseSizeSpec($param1);
|
$imageSize = $this->parseSizeSpec($param1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If they are both invalid then something very strange is going on
|
|
||||||
// Let the built in parsedown image handler deal with it
|
|
||||||
if($imageSize === false && $floatDirection === false)
|
|
||||||
return;
|
|
||||||
|
|
||||||
$style = "";
|
$style = "";
|
||||||
if($imageSize !== false)
|
if($imageSize !== false)
|
||||||
$style .= " max-width: " . $imageSize["x"] . "px; max-height: " . $imageSize["y"] . "px;";
|
$style .= " max-width: " . $imageSize["x"] . "px; max-height: " . $imageSize["y"] . "px;";
|
||||||
if($floatDirection)
|
if($floatDirection)
|
||||||
$style .= " float: $floatDirection;";
|
$style .= " float: $floatDirection;";
|
||||||
|
|
||||||
|
$urlExtension = pathinfo($imageUrl, PATHINFO_EXTENSION);
|
||||||
|
$urlType = system_extension_mime_type($urlExtension);
|
||||||
|
switch(substr($urlType, 0, strpos($urlType, "/")))
|
||||||
|
{
|
||||||
|
case "audio":
|
||||||
|
return [
|
||||||
|
"extent" => strlen($matches[0]),
|
||||||
|
"element" => [
|
||||||
|
"name" => "audio",
|
||||||
|
"text" => $altText,
|
||||||
|
"attributes" => [
|
||||||
|
"src" => $imageUrl,
|
||||||
|
"controls" => "controls",
|
||||||
|
"preload" => "metadata",
|
||||||
|
"style" => trim($style)
|
||||||
|
]
|
||||||
|
]
|
||||||
|
];
|
||||||
|
case "video":
|
||||||
|
return [
|
||||||
|
"extent" => strlen($matches[0]),
|
||||||
|
"element" => [
|
||||||
|
"name" => "video",
|
||||||
|
"text" => $altText,
|
||||||
|
"attributes" => [
|
||||||
|
"src" => $imageUrl,
|
||||||
|
"controls" => "controls",
|
||||||
|
"preload" => "metadata",
|
||||||
|
"style" => trim($style)
|
||||||
|
]
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
|
case "image":
|
||||||
|
default:
|
||||||
|
// If we can't work out what it is, then assume it's an image
|
||||||
return [
|
return [
|
||||||
"extent" => strlen($matches[0]),
|
"extent" => strlen($matches[0]),
|
||||||
"element" => [
|
"element" => [
|
||||||
|
@ -4348,6 +4461,7 @@ class PeppermintParsedown extends ParsedownExtra
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
# ~
|
# ~
|
||||||
# Utility Methods
|
# Utility Methods
|
||||||
|
|
53
core.php
53
core.php
|
@ -105,7 +105,7 @@ function human_filesize($bytes, $decimals = 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates the time sincce a particular timestamp and returns a
|
* Calculates the time since a particular timestamp and returns a
|
||||||
* human-readable result.
|
* human-readable result.
|
||||||
* From http://goo.gl/zpgLgq.
|
* From http://goo.gl/zpgLgq.
|
||||||
* @param integer $time The timestamp to convert.
|
* @param integer $time The timestamp to convert.
|
||||||
|
@ -296,9 +296,12 @@ function mb_stripos_all($haystack, $needle) {
|
||||||
/**
|
/**
|
||||||
* Returns the system's mime type mappings, considering the first extension
|
* Returns the system's mime type mappings, considering the first extension
|
||||||
* listed to be cacnonical.
|
* listed to be cacnonical.
|
||||||
|
* From http://stackoverflow.com/a/1147952/1460422 by chaos.
|
||||||
|
* Edited by Starbeamrainbowlabs.
|
||||||
* @return array An array of mime type mappings.
|
* @return array An array of mime type mappings.
|
||||||
*/
|
*/
|
||||||
function system_mime_type_extensions() {
|
function system_mime_type_extensions()
|
||||||
|
{
|
||||||
global $settings;
|
global $settings;
|
||||||
$out = array();
|
$out = array();
|
||||||
$file = fopen($settings->mime_extension_mappings_location, 'r');
|
$file = fopen($settings->mime_extension_mappings_location, 'r');
|
||||||
|
@ -316,18 +319,62 @@ function system_mime_type_extensions() {
|
||||||
fclose($file);
|
fclose($file);
|
||||||
return $out;
|
return $out;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts a given mime type to it's associated file extension.
|
* Converts a given mime type to it's associated file extension.
|
||||||
|
* From http://stackoverflow.com/a/1147952/1460422 by chaos.
|
||||||
|
* Edited by Starbeamrainbowlabs.
|
||||||
* @param string $type The mime type to convert.
|
* @param string $type The mime type to convert.
|
||||||
* @return string The extension for the given mime type.
|
* @return string The extension for the given mime type.
|
||||||
*/
|
*/
|
||||||
function system_mime_type_extension($type) {
|
function system_mime_type_extension($type)
|
||||||
|
{
|
||||||
static $exts;
|
static $exts;
|
||||||
if(!isset($exts))
|
if(!isset($exts))
|
||||||
$exts = system_mime_type_extensions();
|
$exts = system_mime_type_extensions();
|
||||||
return isset($exts[$type]) ? $exts[$type] : null;
|
return isset($exts[$type]) ? $exts[$type] : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the system MIME type mapping of extensions to MIME types.
|
||||||
|
* From http://stackoverflow.com/a/1147952/1460422 by chaos.
|
||||||
|
* Edited by Starbeamrainbowlabs.
|
||||||
|
* @return array An array mapping file extensions to their associated mime types.
|
||||||
|
*/
|
||||||
|
function system_extension_mime_types()
|
||||||
|
{
|
||||||
|
global $settings;
|
||||||
|
$out = array();
|
||||||
|
$file = fopen($settings->mime_extension_mappings_location, 'r');
|
||||||
|
while(($line = fgets($file)) !== false) {
|
||||||
|
$line = trim(preg_replace('/#.*/', '', $line));
|
||||||
|
if(!$line)
|
||||||
|
continue;
|
||||||
|
$parts = preg_split('/\s+/', $line);
|
||||||
|
if(count($parts) == 1)
|
||||||
|
continue;
|
||||||
|
$type = array_shift($parts);
|
||||||
|
foreach($parts as $part)
|
||||||
|
$out[$part] = $type;
|
||||||
|
}
|
||||||
|
fclose($file);
|
||||||
|
return $out;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Converts a given file extension to it's associated mime type.
|
||||||
|
* From http://stackoverflow.com/a/1147952/1460422 by chaos.
|
||||||
|
* Edited by Starbeamrainbowlabs.
|
||||||
|
* @param string $ext The extension to convert.
|
||||||
|
* @return string The mime type associated with the given extension.
|
||||||
|
*/
|
||||||
|
function system_extension_mime_type($ext) {
|
||||||
|
static $types;
|
||||||
|
if(!isset($types))
|
||||||
|
$types = system_extension_mime_types();
|
||||||
|
$ext = strtolower($ext);
|
||||||
|
return isset($types[$ext]) ? $types[$ext] : null;
|
||||||
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
|
@ -68,7 +68,7 @@
|
||||||
"author": "Starbeamrainbowlabs",
|
"author": "Starbeamrainbowlabs",
|
||||||
"description": "Adds the ability to upload files to Pepperminty Wiki. Uploaded files act as pages and have the special 'File:' prefix.",
|
"description": "Adds the ability to upload files to Pepperminty Wiki. Uploaded files act as pages and have the special 'File:' prefix.",
|
||||||
"id": "feature-upload",
|
"id": "feature-upload",
|
||||||
"lastupdate": 1460096484,
|
"lastupdate": 1460102225,
|
||||||
"optional": false
|
"optional": false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -77,7 +77,7 @@
|
||||||
"author": "Starbeamrainbowlabs",
|
"author": "Starbeamrainbowlabs",
|
||||||
"description": "Adds the credits page. You *must* have this module :D",
|
"description": "Adds the credits page. You *must* have this module :D",
|
||||||
"id": "page-credits",
|
"id": "page-credits",
|
||||||
"lastupdate": 1458906058,
|
"lastupdate": 1460100376,
|
||||||
"optional": false
|
"optional": false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -194,7 +194,7 @@
|
||||||
"author": "Emanuil Rusev & Starbeamrainbowlabs",
|
"author": "Emanuil Rusev & Starbeamrainbowlabs",
|
||||||
"description": "An upgraded (now default!) parser based on Emanuil Rusev's Parsedown Extra PHP library (https:\/\/github.com\/erusev\/parsedown-extra), which is licensed MIT. Please be careful, as this module adds a some weight to your installation, and also *requires* write access to the disk on first load.",
|
"description": "An upgraded (now default!) parser based on Emanuil Rusev's Parsedown Extra PHP library (https:\/\/github.com\/erusev\/parsedown-extra), which is licensed MIT. Please be careful, as this module adds a some weight to your installation, and also *requires* write access to the disk on first load.",
|
||||||
"id": "parser-parsedown",
|
"id": "parser-parsedown",
|
||||||
"lastupdate": 1459172723,
|
"lastupdate": 1460102415,
|
||||||
"optional": false
|
"optional": false
|
||||||
}
|
}
|
||||||
]
|
]
|
|
@ -182,6 +182,14 @@ register_module([
|
||||||
add_action("preview", function() {
|
add_action("preview", function() {
|
||||||
global $settings, $env, $pageindex, $start_time;
|
global $settings, $env, $pageindex, $start_time;
|
||||||
|
|
||||||
|
if(empty($pageindex->{$env->page}->uploadedfilepath))
|
||||||
|
{
|
||||||
|
$im = errorimage("The page '$env->page' doesn't have an associated file.");
|
||||||
|
header("content-type: image/png");
|
||||||
|
imagepng($im);
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
|
||||||
$filepath = $env->storage_prefix . $pageindex->{$env->page}->uploadedfilepath;
|
$filepath = $env->storage_prefix . $pageindex->{$env->page}->uploadedfilepath;
|
||||||
$mime_type = $pageindex->{$env->page}->uploadedfilemime;
|
$mime_type = $pageindex->{$env->page}->uploadedfilemime;
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,18 @@ register_module([
|
||||||
"thing_url" => "https://github.com/sbrl/Pepprminty-Wiki",
|
"thing_url" => "https://github.com/sbrl/Pepprminty-Wiki",
|
||||||
"icon" => "https://avatars0.githubusercontent.com/u/9929737?v=3&s=24"
|
"icon" => "https://avatars0.githubusercontent.com/u/9929737?v=3&s=24"
|
||||||
],
|
],
|
||||||
|
"Mime type to file extension mapper" => [
|
||||||
|
"author" => "Chaos",
|
||||||
|
"author_url" => "https://stackoverflow.com/users/47529/chaos",
|
||||||
|
"thing_url" => "https://stackoverflow.com/a/1147952/1460422",
|
||||||
|
"icon" => "https://www.gravatar.com/avatar/aaee40db39ad6b164cfb89cb6ad4d176?s=328&d=identicon&s=24"
|
||||||
|
],
|
||||||
|
"Parsedown" => [
|
||||||
|
"author" => "erusev and others",
|
||||||
|
"author_url" => "https://github.com/erusev/",
|
||||||
|
"thing_url" => "https://github.com/erusev/parsedown/",
|
||||||
|
"icon" => "https://avatars1.githubusercontent.com/u/184170?v=3&s=24"
|
||||||
|
],
|
||||||
"Slightly modified version of Slimdown" => [
|
"Slightly modified version of Slimdown" => [
|
||||||
"author" => "Johnny Broadway",
|
"author" => "Johnny Broadway",
|
||||||
"author_url" => "https://github.com/jbroadway",
|
"author_url" => "https://github.com/jbroadway",
|
||||||
|
|
|
@ -88,9 +88,13 @@ class PeppermintParsedown extends ParsedownExtra
|
||||||
|
|
||||||
// Variable parsing
|
// Variable parsing
|
||||||
if(preg_match("/\{\{\{([^}]+)\}\}\}/", $fragment["text"], $matches))
|
if(preg_match("/\{\{\{([^}]+)\}\}\}/", $fragment["text"], $matches))
|
||||||
|
{
|
||||||
|
$params = [];
|
||||||
|
if(!empty($this->paramStack))
|
||||||
{
|
{
|
||||||
$stackEntry = array_slice($this->paramStack, -1)[0];
|
$stackEntry = array_slice($this->paramStack, -1)[0];
|
||||||
$params = !empty($stackEntry) ? $stackEntry["params"] : false;
|
$params = !empty($stackEntry) ? $stackEntry["params"] : false;
|
||||||
|
}
|
||||||
|
|
||||||
$variableKey = trim($matches[1]);
|
$variableKey = trim($matches[1]);
|
||||||
|
|
||||||
|
@ -117,6 +121,8 @@ class PeppermintParsedown extends ParsedownExtra
|
||||||
$variableValue .= "\t<li>" . $curStackEntry["pagename"] . "</li>\n";
|
$variableValue .= "\t<li>" . $curStackEntry["pagename"] . "</li>\n";
|
||||||
}
|
}
|
||||||
$variableValue .= "</ol>\n";
|
$variableValue .= "</ol>\n";
|
||||||
|
break;
|
||||||
|
// TODO: Add a option that displays a list of subpages here
|
||||||
}
|
}
|
||||||
if(isset($params[$variableKey]))
|
if(isset($params[$variableKey]))
|
||||||
{
|
{
|
||||||
|
@ -148,11 +154,11 @@ class PeppermintParsedown extends ParsedownExtra
|
||||||
|
|
||||||
protected function templateHandler($source)
|
protected function templateHandler($source)
|
||||||
{
|
{
|
||||||
global $pageindex, $paths;
|
global $pageindex, $env;
|
||||||
|
|
||||||
|
|
||||||
$parts = explode("|", trim($source, "{}"));
|
$parts = explode("|", trim($source, "{}"));
|
||||||
$parts = array_map(trim, $parts);
|
$parts = array_map("trim", $parts);
|
||||||
|
|
||||||
// Extract the name of the temaplate page
|
// Extract the name of the temaplate page
|
||||||
$templatePagename = array_shift($parts);
|
$templatePagename = array_shift($parts);
|
||||||
|
@ -171,7 +177,7 @@ class PeppermintParsedown extends ParsedownExtra
|
||||||
{
|
{
|
||||||
// This param contains an equals sign, so it's a named parameter
|
// This param contains an equals sign, so it's a named parameter
|
||||||
$keyValuePair = explode("=", $part, 2);
|
$keyValuePair = explode("=", $part, 2);
|
||||||
$keyValuePair = array_map(trim, $keyValuePair);
|
$keyValuePair = array_map("trim", $keyValuePair);
|
||||||
$params[$keyValuePair[0]] = $keyValuePair[1];
|
$params[$keyValuePair[0]] = $keyValuePair[1];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -188,7 +194,7 @@ class PeppermintParsedown extends ParsedownExtra
|
||||||
"params" => $params
|
"params" => $params
|
||||||
];
|
];
|
||||||
|
|
||||||
$templateFilePath = $paths->storage_prefix . $pageindex->$templatePagename->filename;
|
$templateFilePath = $env->storage_prefix . $pageindex->$templatePagename->filename;
|
||||||
|
|
||||||
$parsedTemplateSource = $this->text(file_get_contents($templateFilePath));
|
$parsedTemplateSource = $this->text(file_get_contents($templateFilePath));
|
||||||
|
|
||||||
|
@ -239,7 +245,7 @@ class PeppermintParsedown extends ParsedownExtra
|
||||||
|
|
||||||
protected function inlineExtendedImage($fragment)
|
protected function inlineExtendedImage($fragment)
|
||||||
{
|
{
|
||||||
if(preg_match('/^!\[(.*)\]\(([^ |)]+)\s*\|([^|)]*)(?:\|([^)]*))?\)/', $fragment["text"], $matches))
|
if(preg_match('/^!\[(.*)\]\(([^ |)]+)\s*(?:\|([^|)]*)(?:\|([^)]*))?)?\)/', $fragment["text"], $matches))
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* 0 - Everything
|
* 0 - Everything
|
||||||
|
@ -249,41 +255,81 @@ class PeppermintParsedown extends ParsedownExtra
|
||||||
* 4 - Second Param (optional)
|
* 4 - Second Param (optional)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var_dump($matches);
|
|
||||||
|
|
||||||
$altText = $matches[1];
|
$altText = $matches[1];
|
||||||
$imageUrl = $matches[2];
|
$imageUrl = str_replace("&", "&", $matches[2]); // Decode & to allow it in preview urls
|
||||||
$param1 = strtolower(trim($matches[3]));
|
$param1 = !empty($matches[3]) ? strtolower(trim($matches[3])) : false;
|
||||||
$param2 = empty($matches[4]) ? false : strtolower(trim($matches[4]));
|
$param2 = empty($matches[4]) ? false : strtolower(trim($matches[4]));
|
||||||
$floatDirection = false;
|
$floatDirection = false;
|
||||||
$imageSize = false;
|
$imageSize = false;
|
||||||
|
|
||||||
if($this->isFloatValue($param1))
|
if($this->isFloatValue($param1))
|
||||||
{
|
{
|
||||||
|
// Param 1 is a valid css float: ... value
|
||||||
$floatDirection = $param1;
|
$floatDirection = $param1;
|
||||||
$imageSize = $this->parseSizeSpec($param2);
|
$imageSize = $this->parseSizeSpec($param2);
|
||||||
}
|
}
|
||||||
else if($this->isFloatValue($param2))
|
else if($this->isFloatValue($param2))
|
||||||
{
|
{
|
||||||
|
// Param 2 is a valid css float: ... value
|
||||||
$floatDirection = $param2;
|
$floatDirection = $param2;
|
||||||
$imageSize = $this->parseSizeSpec($param1);
|
$imageSize = $this->parseSizeSpec($param1);
|
||||||
}
|
}
|
||||||
|
else if($param1 === false and $param2 === false)
|
||||||
|
{
|
||||||
|
// Neither params were specified
|
||||||
|
$floatDirection = false;
|
||||||
|
$imageSize = false;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
// Neither of them are floats, but at least one is specified
|
||||||
|
// This must mean that the first param is a size spec like
|
||||||
|
// 250x128.
|
||||||
$imageSize = $this->parseSizeSpec($param1);
|
$imageSize = $this->parseSizeSpec($param1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If they are both invalid then something very strange is going on
|
|
||||||
// Let the built in parsedown image handler deal with it
|
|
||||||
if($imageSize === false && $floatDirection === false)
|
|
||||||
return;
|
|
||||||
|
|
||||||
$style = "";
|
$style = "";
|
||||||
if($imageSize !== false)
|
if($imageSize !== false)
|
||||||
$style .= " max-width: " . $imageSize["x"] . "px; max-height: " . $imageSize["y"] . "px;";
|
$style .= " max-width: " . $imageSize["x"] . "px; max-height: " . $imageSize["y"] . "px;";
|
||||||
if($floatDirection)
|
if($floatDirection)
|
||||||
$style .= " float: $floatDirection;";
|
$style .= " float: $floatDirection;";
|
||||||
|
|
||||||
|
$urlExtension = pathinfo($imageUrl, PATHINFO_EXTENSION);
|
||||||
|
$urlType = system_extension_mime_type($urlExtension);
|
||||||
|
switch(substr($urlType, 0, strpos($urlType, "/")))
|
||||||
|
{
|
||||||
|
case "audio":
|
||||||
|
return [
|
||||||
|
"extent" => strlen($matches[0]),
|
||||||
|
"element" => [
|
||||||
|
"name" => "audio",
|
||||||
|
"text" => $altText,
|
||||||
|
"attributes" => [
|
||||||
|
"src" => $imageUrl,
|
||||||
|
"controls" => "controls",
|
||||||
|
"preload" => "metadata",
|
||||||
|
"style" => trim($style)
|
||||||
|
]
|
||||||
|
]
|
||||||
|
];
|
||||||
|
case "video":
|
||||||
|
return [
|
||||||
|
"extent" => strlen($matches[0]),
|
||||||
|
"element" => [
|
||||||
|
"name" => "video",
|
||||||
|
"text" => $altText,
|
||||||
|
"attributes" => [
|
||||||
|
"src" => $imageUrl,
|
||||||
|
"controls" => "controls",
|
||||||
|
"preload" => "metadata",
|
||||||
|
"style" => trim($style)
|
||||||
|
]
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
|
case "image":
|
||||||
|
default:
|
||||||
|
// If we can't work out what it is, then assume it's an image
|
||||||
return [
|
return [
|
||||||
"extent" => strlen($matches[0]),
|
"extent" => strlen($matches[0]),
|
||||||
"element" => [
|
"element" => [
|
||||||
|
@ -297,6 +343,7 @@ class PeppermintParsedown extends ParsedownExtra
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
# ~
|
# ~
|
||||||
# Utility Methods
|
# Utility Methods
|
||||||
|
|
Loading…
Reference in a new issue