2015-10-04 12:37:12 +00:00
< ? php
register_module ([
" name " => " Parsedown " ,
2019-12-23 21:53:22 +00:00
" version " => " 0.10.1 " ,
2016-03-19 18:24:52 +00:00
" author " => " Emanuil Rusev & Starbeamrainbowlabs " ,
2019-08-17 19:47:51 +00:00
" 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 some weight to your installation. " ,
2019-02-26 23:01:09 +00:00
" extra_data " => [
2019-08-28 23:15:19 +00:00
/********** Parsedown versions **********
2019-08-30 12:29:15 +00:00
* Parsedown Core : 1.8 . 0 - beta - 7 *
2019-08-28 23:15:19 +00:00
* Parsedown Extra : 0.8 . 0 - beta - 1 *
* Parsedown Extreme : 0.1 . 6 *
****************************************/
2019-12-23 21:53:22 +00:00
" Parsedown.php " => " https://raw.githubusercontent.com/erusev/parsedown/3825db53a2be5d9ce54436a9cc557c6bdce1808a/Parsedown.php " ,
" ParsedownExtra.php " => " https://raw.githubusercontent.com/erusev/parsedown-extra/352d03d941fc801724e82e49424ff409175261fd/ParsedownExtra.php " ,
2019-08-28 23:15:19 +00:00
" ParsedownExtreme.php " => " https://raw.githubusercontent.com/BenjaminHoegh/parsedown-extreme/adae4136534ad1e4159fe04c74c4683681855b84/ParsedownExtreme.php "
// TODO: Add Parsedown Extreme support
2019-02-26 23:01:09 +00:00
],
2015-10-04 12:37:12 +00:00
" id " => " parser-parsedown " ,
" code " => function () {
2016-03-12 18:30:40 +00:00
global $settings ;
2016-03-12 15:26:30 +00:00
$parser = new PeppermintParsedown ();
$parser -> setInternalLinkBase ( " ?page=%s " );
2019-03-02 21:59:50 +00:00
add_parser ( " parsedown " , function ( $source , $untrusted ) use ( $parser ) {
2016-04-08 20:05:42 +00:00
global $settings ;
2019-03-02 21:59:50 +00:00
$parser -> setsafeMode ( $untrusted || $settings -> all_untrusted );
2019-01-05 16:23:12 +00:00
$parser -> setMarkupEscaped ( $settings -> clean_raw_html );
2016-03-12 15:26:30 +00:00
$result = $parser -> text ( $source );
2015-10-04 13:04:42 +00:00
return $result ;
2019-02-10 23:01:01 +00:00
}, function ( $source ) {
global $version , $settings , $pageindex ;
$id_text = " $version | $settings->parser | $source " ;
// Find template includes
preg_match_all (
'/\{\{\s*([^|]+)\s*(?:\|[^}]*)?\}\}/' ,
$source , $includes
);
foreach ( $includes [ 1 ] as $include_pagename ) {
if ( empty ( $pageindex -> $include_pagename ))
2019-02-10 23:08:16 +00:00
continue ;
2019-02-10 23:01:01 +00:00
$id_text .= " | $include_pagename : " . parsedown_pagename_resolve (
$pageindex -> $include_pagename -> lastmodified
);
}
return str_replace ([ " + " , " / " ], [ " - " , " _ " ], base64_encode ( hash (
" sha256 " ,
$id_text ,
true
)));
2015-10-04 12:37:12 +00:00
});
2016-03-12 18:30:40 +00:00
2019-10-20 20:42:13 +00:00
add_action ( " parsedown-render-ext " , function () {
global $settings , $env , $paths ;
2019-10-20 21:00:52 +00:00
if ( ! $settings -> parser_ext_renderers_enabled ) {
http_response_code ( 403 );
header ( " content-type: image/png " );
imagepng ( errorimage ( " Error: External diagram renderer support \n has been disabled on $settings->sitename . \n Try contacting { $settings -> admindetails_name } , $settings->sitename 's administrator. " ));
exit ();
}
2019-10-20 20:42:13 +00:00
if ( ! isset ( $_GET [ " source " ])) {
http_response_code ( 400 );
header ( " content-type: image/png " );
imagepng ( errorimage ( " Error: No source text \n specified. " ));
2019-10-20 20:54:00 +00:00
exit ();
2019-10-20 20:42:13 +00:00
}
2019-10-20 20:54:00 +00:00
if ( ! isset ( $_GET [ " language " ])) {
http_response_code ( 400 );
header ( " content-type: image/png " );
imagepng ( errorimage ( " Error: No external renderer \n language specified. " ));
exit ();
}
$source = $_GET [ " source " ];
$language = $_GET [ " language " ];
if ( ! isset ( $settings -> parser_ext_renderers -> $language )) {
$message = " Error: Unknown language { $_GET [ " language " ] } . \n Supported languages: \n " ;
foreach ( $settings -> parser_ext_renderers as $language => $spec )
$message .= " $spec->name ( $language ) \n " ;
http_response_code ( 400 );
header ( " content-type: image/png " );
imagepng ( errorimage ( trim ( $message )));
exit ();
}
2019-10-20 23:31:18 +00:00
$renderer = $settings -> parser_ext_renderers -> $language ;
$cache_id = hash ( " sha256 " ,
hash ( " sha256 " , $language ) .
hash ( " sha256 " , $source ) .
( $_GET [ " immutable_key " ] ? ? " " )
);
$cache_file_location = " { $paths -> cache_directory } /render_ext/ $cache_id . " . system_mime_type_extension ( $renderer -> output_format );
// If it exists on disk already, then serve that instead
if ( file_exists ( $cache_file_location )) {
header ( " cache-control: public, max-age=31536000, immutable " );
header ( " content-type: $renderer->output_format " );
header ( " content-length: " . filesize ( $cache_file_location ));
header ( " x-cache: render_ext/hit " );
readfile ( $cache_file_location );
exit ();
}
2019-10-24 20:08:18 +00:00
if ( ! $settings -> parser_ext_allow_anon && ! $env -> is_logged_in ) {
http_response_code ( 401 );
header ( " content-type: image/png " );
imagepng ( errorimage ( wordwrap ( " Error: You aren't logged in, that image hasn't yet been cached, and $settings->sitename does not allow anonymous users to invoke external renderers, so that image can't be generated right now. Try contacting $settings->admindetails_name , $settings->sitename 's administrator (their details can be found at the bottom of every page). " )));
exit ();
}
2019-10-20 23:31:18 +00:00
// Create the cache directory if doesn't exist already
if ( ! file_exists ( dirname ( $cache_file_location )))
mkdir ( dirname ( $cache_file_location ), 0750 , true );
2019-10-24 19:13:03 +00:00
$cli_to_execute = $renderer -> cli ;
$descriptors = [
0 => null , // stdin
1 => null , // stdout
2 => tmpfile () // stderr
];
2019-10-20 23:31:18 +00:00
2019-10-24 19:13:03 +00:00
switch ( $renderer -> cli_mode ) {
case " pipe " :
// Fill stdin with the input text
$descriptors [ 0 ] = tmpfile ();
fwrite ( $descriptors [ 0 ], $source );
fseek ( $descriptors [ 0 ], 0 );
// Pipe the output to be the cache file
$descriptors [ 1 ] = fopen ( $cache_file_location , " wb+ " );
break ;
2019-10-24 20:08:18 +00:00
2019-10-24 19:13:03 +00:00
case " substitution_pipe " :
// Update the command that we're going to execute
$cli_to_execute = str_replace (
" { input_text} " ,
escapeshellarg ( $source ),
$cli_to_execute
);
// Set the descriptors
$descriptors [ 0 ] = tmpfile ();
$descriptors [ 1 ] = fopen ( $cache_file_location , " wb+ " );
break ;
case " file " :
$descriptors [ 0 ] = tmpfile ();
fwrite ( $descriptors [ 0 ], $source );
$descriptors [ 1 ] = tmpfile ();
$cli_to_execute = str_replace (
[ " { input_file} " , " { output_file} " ],
[
escapeshellarg ( stream_get_meta_data ( $descriptors [ 0 ])[ " uri " ]),
escapeshellarg ( $cache_file_location )
],
$cli_to_execute
);
break ;
default :
http_response_code ( 503 );
header ( " cache-control: no-cache, no-store, must-revalidate " );
header ( " content-type: image/png " );
imagepng ( errorimage ( " Error: Unknown external renderer mode ' $renderer->cli_mode '. \n Please contact $settings->admindetails_name , $settings->sitename 's administrator. " ));
exit ();
break ;
}
if ( '\\' !== DIRECTORY_SEPARATOR ) {
// We're not on Windows, so we can use timeout to force-kill if it takes too long
$cli_to_execute = " timeout { $settings -> parser_ext_time_limit } $cli_to_execute " ;
}
2019-10-20 23:31:18 +00:00
$start_time = microtime ( true );
$process_handle = proc_open (
2019-10-24 19:13:03 +00:00
$cli_to_execute ,
$descriptors ,
2019-10-20 23:31:18 +00:00
$pipes ,
null , // working directory
null // environment variables
);
if ( ! is_resource ( $process_handle )) {
2019-10-24 19:13:03 +00:00
fclose ( $descriptors [ 0 ]);
fclose ( $descriptors [ 1 ]);
fclose ( $descriptors [ 2 ]);
2019-10-20 23:41:14 +00:00
2019-10-24 19:13:03 +00:00
if ( file_exists ( $cache_file_location )) unlink ( $cache_file_location );
2019-10-20 23:41:14 +00:00
2019-10-20 23:31:18 +00:00
http_response_code ( 503 );
header ( " cache-control: no-cache, no-store, must-revalidate " );
header ( " content-type: image/png " );
imagepng ( errorimage ( " Error: Failed to start external renderer. \n Is $renderer->name installed? " ));
exit ();
}
// Wait for it to exit
$exit_code = proc_close ( $process_handle );
2019-10-24 19:13:03 +00:00
fclose ( $descriptors [ 0 ]);
fclose ( $descriptors [ 1 ]);
2019-10-20 23:31:18 +00:00
$time_taken = round (( microtime ( true ) - $start_time ) * 1000 , 2 );
2019-10-24 19:13:03 +00:00
if ( $exit_code !== 0 || ! file_exists ( $cache_file_location )) {
fseek ( $descriptors [ 2 ], 0 );
$error_details = stream_get_contents ( $descriptors [ 2 ]);
2019-10-20 23:41:14 +00:00
// Delete the cache file, which is guaranteed to exist because
// we pre-emptively create it above
2019-10-24 19:13:03 +00:00
if ( file_exists ( $cache_file_location )) unlink ( $cache_file_location );
2019-10-20 23:31:18 +00:00
http_response_code ( 503 );
header ( " content-type: image/png " );
imagepng ( errorimage (
2019-10-24 19:13:03 +00:00
" Error: The external renderer ( $renderer->name ) \n exited with code $exit_code , \n or potentially did not create the output file. \n Details: \n " . wordwrap ( $error_details )
2019-10-20 23:31:18 +00:00
));
exit ();
}
header ( " cache-control: public, max-age=31536000, immutable " );
header ( " content-type: $renderer->output_format " );
header ( " content-length: " . filesize ( $cache_file_location ));
header ( " x-cache: render_ext/miss, renderer took { $time_taken } ms " );
readfile ( $cache_file_location );
2019-10-20 20:42:13 +00:00
});
2018-04-28 09:59:08 +00:00
/*
* ███████ ████████ █████ ████████ ██ ███████ ████████ ██ ██████ ███████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ███████ ██ ███████ ██ ██ ███████ ██ ██ ██ ███████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ███████ ██ ██ ██ ██ ██ ███████ ██ ██ ██████ ███████
*/
2017-07-14 19:35:55 +00:00
statistic_add ([
" id " => " wanted-pages " ,
" name " => " Wanted Pages " ,
2017-09-16 12:26:12 +00:00
" type " => " page " ,
2017-07-14 19:35:55 +00:00
" update " => function ( $old_stats ) {
global $pageindex , $env ;
$result = new stdClass (); // completed, value, state
$pages = [];
foreach ( $pageindex as $pagename => $pagedata ) {
2018-04-26 22:51:41 +00:00
if ( ! file_exists ( $env -> storage_prefix . $pagedata -> filename ))
2017-07-14 20:22:37 +00:00
continue ;
2017-07-14 19:35:55 +00:00
$page_content = file_get_contents ( $env -> storage_prefix . $pagedata -> filename );
2018-04-26 22:14:35 +00:00
$page_links = PeppermintParsedown :: extract_page_names ( $page_content );
foreach ( $page_links as $linked_page ) {
2017-07-14 19:35:55 +00:00
// We're only interested in pages that don't exist
2017-07-14 20:22:37 +00:00
if ( ! empty ( $pageindex -> $linked_page )) continue ;
2017-07-14 19:35:55 +00:00
if ( empty ( $pages [ $linked_page ]))
$pages [ $linked_page ] = 0 ;
$pages [ $linked_page ] ++ ;
}
}
arsort ( $pages );
$result -> value = $pages ;
$result -> completed = true ;
return $result ;
2017-07-14 21:13:13 +00:00
},
" render " => function ( $stats_data ) {
$result = " <h2> $stats_data->name </h2> \n " ;
$ result .= " <table class='wanted-pages'> \n " ;
$result .= " \t <tr><th>Page Name</th><th>Linking Pages</th></tr> \n " ;
foreach ( $stats_data -> value as $pagename => $linking_pages ) {
$result .= " \t <tr><td> $pagename </td><td> $linking_pages </td></tr> \n " ;
}
$result .= " </table> \n " ;
return $result ;
2017-07-14 19:35:55 +00:00
}
]);
2018-04-26 22:27:44 +00:00
statistic_add ([
" id " => " orphan-pages " ,
" name " => " Orphan Pages " ,
2018-04-28 10:06:23 +00:00
" type " => " page-list " ,
2018-04-26 22:27:44 +00:00
" update " => function ( $old_stats ) {
global $pageindex , $env ;
$result = new stdClass (); // completed, value, state
$pages = [];
foreach ( $pageindex as $pagename => $pagedata ) {
2018-04-26 22:51:41 +00:00
if ( ! file_exists ( $env -> storage_prefix . $pagedata -> filename ))
2018-04-26 22:27:44 +00:00
continue ;
$page_content = file_get_contents ( $env -> storage_prefix . $pagedata -> filename );
$page_links = PeppermintParsedown :: extract_page_names ( $page_content );
foreach ( $page_links as $linked_page ) {
// We're only interested in pages that exist
if ( empty ( $pageindex -> $linked_page )) continue ;
$pages [ $linked_page ] = true ;
}
}
$orphaned_pages = [];
foreach ( $pageindex as $pagename => $page_data ) {
if ( empty ( $pages [ $pagename ]))
$orphaned_pages [] = $pagename ;
}
2019-09-03 17:16:01 +00:00
$sorter = new Collator ( " " );
$sorter -> sort ( $orphaned_pages );
2018-04-26 22:27:44 +00:00
$result -> value = $orphaned_pages ;
$result -> completed = true ;
return $result ;
}
]);
2018-04-28 09:59:08 +00:00
statistic_add ([
" id " => " most-linked-to-pages " ,
" name " => " Most Linked-To Pages " ,
" type " => " page " ,
" update " => function ( $old_stats ) {
global $pageindex , $env ;
$result = new stdClass (); // completed, value, state
$pages = [];
foreach ( $pageindex as $pagename => $pagedata ) {
if ( ! file_exists ( $env -> storage_prefix . $pagedata -> filename ))
continue ;
$page_content = file_get_contents ( $env -> storage_prefix . $pagedata -> filename );
$page_links = PeppermintParsedown :: extract_page_names ( $page_content );
foreach ( $page_links as $linked_page ) {
// We're only interested in pages that exist
if ( empty ( $pageindex -> $linked_page )) continue ;
if ( empty ( $pages [ $linked_page ]))
$pages [ $linked_page ] = 0 ;
$pages [ $linked_page ] ++ ;
}
}
arsort ( $pages );
$result -> value = $pages ;
$result -> completed = true ;
return $result ;
},
" render " => function ( $stats_data ) {
global $pageindex ;
$result = " <h2> $stats_data->name </h2> \n " ;
$result .= " <table class='most-linked-to-pages'> \n " ;
$result .= " \t <tr><th>Page Name</th><th>Linking Pages</th></tr> \n " ;
foreach ( $stats_data -> value as $pagename => $link_count ) {
$pagename_display = ! empty ( $pageindex -> $pagename -> redirect ) && $pageindex -> $pagename -> redirect ? " <em> $pagename </em> " : $pagename ;
$result .= " \t <tr><td><a href='?page= " . rawurlencode ( $pagename ) . " '> $pagename_display </a></td><td> $link_count </td></tr> \n " ;
}
$result .= " </table> \n " ;
return $result ;
}
]);
2017-07-14 19:35:55 +00:00
2016-03-12 18:30:40 +00:00
add_help_section ( " 20-parser-default " , " Editor Syntax " ,
" <p> $settings->sitename 's editor uses an extended version of <a href='http://parsedown.org/'>Parsedown</a> to render pages, which is a fantastic open source Github flavoured markdown parser. You can find a quick reference guide on Github flavoured markdown <a href='https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet'>here</a> by <a href='https://github.com/adam-p/'>adam-p</a>, or if you prefer a book <a href='https://www.gitbook.com/book/roachhd/master-markdown/details'>Mastering Markdown</a> by KB is a good read, and free too!</p>
2016-08-19 19:28:47 +00:00
< h3 > Tips </ h3 >
< ul >
< li > Put 2 spaces at the end of a line to add a soft line break . Leave a blank line to add a head line break ( i . e . a new paragraph ) .</ li >
< li > You can add an id to a header that you can link to . Put it in curly braces after the heading name like this : < code > # Heading Name {#HeadingId}</code>. Then you can link to like like this: <code>[[Page name#HeadingId}]]</code>. You can also link to a heading id on the current page by omitting the page name: <code>[[#HeadingId]]</code>.</li>
</ ul >
2016-03-12 18:30:40 +00:00
< h3 > Extra Syntax </ h3 >
< p > $settings -> sitename 's editor also supports some extra custom syntax, some of which is inspired by <a href=' https :// mediawiki . org / ' > Mediawiki </ a >.
< table >
< tr >< th style = 'width: 40%' > Type this </ th >< th style = 'width: 20%' > To get this </ th >< th > Comments </ th ></ th >
< tr >< td >< code > [[ Internal link ]] </ code ></ td >< td >< a href = '?page=Internal%20link' > Internal Link </ a ></ td >< td > An internal link .</ td ></ tr >
< tr >< td >< code > [[ Display Text | Internal link ]] </ code ></ td >< td >< a href = '?page=Internal%20link' > Display Text </ a ></ td >< td > An internal link with some display text .</ td ></ tr >
< tr >< td >< code >! [ Alt text ]( http :// example . com / path / to / image . png | 256 x256 | right ) </ code ></ td >< td >< img src = 'http://example.com/path/to/image.png' alt = 'Alt text' style = 'float: right; max-width: 256px; max-height: 256px;' /></ td >< td > An image floating to the right of the page that fits inside a 256 px x 256 px box , preserving aspect ratio .</ td ></ tr >
2018-07-01 19:56:08 +00:00
< tr >< td >< code >! [ Alt text ]( http :// example . com / path / to / image . png | 256 x256 | caption ) </ code ></ td >< td >< figure >< img src = 'http://example.com/path/to/image.png' alt = 'Alt text' style = 'max-width: 256px; max-height: 256px;' />< figcaption > Alt text </ figcaption ></ figure ></ td >< td > An image with a caption that fits inside a 256 px x 256 px box , preserving aspect ratio . The presence of the word < code > caption </ code > in the regular braces causes the alt text to be taken and displayed below the image itself .</ td ></ tr >
2016-05-30 11:35:23 +00:00
< tr >< td >< code >! [ Alt text ]( Files / Cheese . png ) </ code ></ td >< td >< img src = 'index.php?action=preview&page=Files/Cheese.png' alt = 'Alt text' style = '' /></ td >< td > An example of the short url syntax for images . Simply enter the page name of an image ( or video / audio file ), and Pepperminty Wiki will sort out the url for you .</ td ></ tr >
2016-03-20 17:57:47 +00:00
</ table >
2016-05-30 11:35:23 +00:00
< p > Note that the all image image syntax above can be mixed and matched to your liking . The < code > caption </ code > option in particular must come last or next to last .</ p >
2016-03-20 17:57:47 +00:00
< h4 > Templating </ h4 >
< p > $settings -> sitename also supports including one page in another page as a < em > template </ em >. The syntax is very similar to that of Mediawiki . For example , < code > {{ Announcement banner }} </ code > will include the contents of the \ " Announcement banner \" page, assuming it exists.</p>
2016-04-09 09:55:44 +00:00
< p > You can also use variables . Again , the syntax here is very similar to that of Mediawiki - they can be referenced in the included page by surrrounding the variable name in triple curly braces ( e . g . < code > {{{ Announcement text }}} </ code > ), and set when including a page with the bar syntax ( e . g . < code > {{ Announcement banner | importance = high | text = Maintenance has been planned for tonight . }} </ code > ) . Currently the only restriction in templates and variables is that you may not include a closing curly brace ( < code > } </ code > ) in the page name , variable name , or value .</ p >
2016-05-30 11:35:23 +00:00
< h5 > Special Variables </ h5 >
2016-04-09 09:55:44 +00:00
< p > $settings -> sitename also supports a number of special built - in variables . Their syntax and function are described below :</ p >
< table >
< tr >< th > Type this </ th >< th > To get this </ th ></ tr >
< tr >< td >< code > {{{ @ }}} </ code ></ td >< td > Lists all variables and their values in a table .</ td ></ tr >
< tr >< td >< code > {{{ #}}}</code></td><td>Shows a 'stack trace', outlining all the parent includes of the current page being parsed.</td></tr>
2016-11-03 20:29:15 +00:00
< tr >< td >< code > {{{ ~ }}} </ code ></ td >< td > Outputs the requested page ' s name .</ td ></ tr >
2016-04-09 09:55:44 +00:00
< tr >< td >< code > {{{ * }}} </ code ></ td >< td > Outputs a comma separated list of all the subpages of the current page .</ td ></ tr >
< tr >< td >< code > {{{ + }}} </ code ></ td >< td > Shows a gallery containing all the files that are sub pages of the current page .</ td ></ tr >
2019-10-26 11:27:09 +00:00
</ table > " );
2019-10-26 14:19:05 +00:00
if ( $settings -> parser_ext_renderers_enabled ) {
$doc_help = " <p> $settings->sitename supports external renderers. External renderers take the content of a code fence block, like this:</p>
< pre >< code > `` ` language_code
Insert text here
`` ` </ code ></ pre >
< p >... and render it to an image . This is based on the < code > language_code </ code > specified , as is done in the above example . Precisely what the output of a external renderer is depends on the external renderers defined , but $settings -> sitename currently has the following external renderers registered :</ p >
< table >
< tr >< th > Name </ th >< th > Language code </ th >< th > Description </ th >< th > Reference Link </ th ></ tr >
" ;
foreach ( $settings -> parser_ext_renderers as $code => $renderer ) {
$row = array_map ( " htmlentities " , [
$renderer -> name ,
$code ,
$renderer -> description ,
$renderer -> url
]);
$row [ 3 ] = " <a href=' $row[3] '>🔗</a> " ;
$doc_help .= " <tr><td> " . implode ( " </td><td> " , $row ) . " </td></tr> \n " ;
}
$doc_help .= " </table>
$settings -> admindetails_name can register more external renderers - see the < a href = 'https://starbeamrainbowlabs.com/labs/peppermint/__nightdocs/06.8-External-Renderers.html' > documentation </ a > for more information .</ p > " ;
add_help_section ( " 24-external-renderers " , " External Renderers " , $doc_help );
}
2015-10-04 12:37:12 +00:00
}
]);
2019-03-02 13:32:12 +00:00
require_once ( " $paths->extra_data_directory /parser-parsedown/Parsedown.php " );
require_once ( " $paths->extra_data_directory /parser-parsedown/ParsedownExtra.php " );
2019-08-28 23:15:19 +00:00
require_once ( " $paths->extra_data_directory /parser-parsedown/ParsedownExtreme.php " );
2015-10-04 12:37:12 +00:00
2019-02-10 23:01:01 +00:00
/**
* Attempts to 'auto-correct' a page name by trying different capitalisation
* combinations .
* @ param string $pagename The page name to auto - correct .
* @ return string The auto - corrected page name .
*/
function parsedown_pagename_resolve ( $pagename ) {
global $pageindex ;
// If the page doesn't exist, check varying different
// capitalisations to see if it exists under some variant.
if ( ! empty ( $pageindex -> $pagename ))
return $pagename ;
$pagename = ucfirst ( $pagename );
if ( ! empty ( $pageindex -> $pagename ))
return $pagename ;
$pagename = ucwords ( $pagename );
return $pagename ;
}
2015-12-26 12:55:19 +00:00
/*
2016-03-12 15:26:30 +00:00
* ██████ █████ ██████ ███████ ███████ ██████ ██████ ██ ██ ███ ██
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██
* ██████ ███████ ██████ ███████ █████ ██ ██ ██ ██ ██ █ ██ ██ ██ ██
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██ ██
* ██ ██ ██ ██ ██ ███████ ███████ ██████ ██████ ███ ███ ██ ████
*
* ███████ ██ ██ ████████ ███████ ███ ██ ███████ ██ ██████ ███ ██ ███████
* ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ████ ██ ██
* █████ ███ ██ █████ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ███████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ███████ ██ ██ ██ ███████ ██ ████ ███████ ██ ██████ ██ ████ ███████
*/
2019-03-02 13:49:12 +00:00
/**
* The Peppermint - flavoured Parsedown parser .
*/
2019-08-28 23:15:19 +00:00
class PeppermintParsedown extends ParsedownExtreme
2015-10-04 12:37:12 +00:00
{
2019-03-02 13:49:12 +00:00
/**
* The base directory with which internal links will be resolved .
* @ var string
*/
2016-03-12 15:26:30 +00:00
private $internalLinkBase = " ./%s " ;
2019-03-02 13:49:12 +00:00
/**
* The parameter stack . Used for recursive templating .
* @ var array
*/
2016-03-20 16:16:55 +00:00
protected $paramStack = [];
2019-03-02 13:49:12 +00:00
/**
* Creates a new Peppermint Parsedown instance .
*/
2016-03-12 15:26:30 +00:00
function __construct ()
{
2019-09-21 10:35:50 +00:00
parent :: __construct ();
2016-03-12 15:26:30 +00:00
// Prioritise our internal link parsing over the regular link parsing
array_unshift ( $this -> InlineTypes [ " [ " ], " InternalLink " );
// Prioritise our image parser over the regular image parser
array_unshift ( $this -> InlineTypes [ " ! " ], " ExtendedImage " );
2015-10-04 14:20:47 +00:00
2016-03-20 14:05:55 +00:00
$this -> inlineMarkerList .= " { " ;
2016-03-25 17:52:32 +00:00
if ( ! isset ( $this -> InlineTypes [ " { " ]) or ! is_array ( $this -> InlineTypes [ " { " ]))
2016-03-20 14:05:55 +00:00
$this -> InlineTypes [ " { " ] = [];
$this -> InlineTypes [ " { " ][] = " Template " ;
}
2016-04-09 06:51:29 +00:00
/*
* ████████ ███████ ███ ███ ██████ ██ █████ ████████ ██ ███ ██ ██████
* ██ ██ ████ ████ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██
* ██ █████ ██ ████ ██ ██████ ██ ███████ ██ ██ ██ ██ ██ ██ ███
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██ ███████ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ████ ██████
*/
2019-03-02 13:49:12 +00:00
/**
* Parses templating definitions .
* @ param string $fragment The fragment to parse it out from .
*/
2016-03-20 14:05:55 +00:00
protected function inlineTemplate ( $fragment )
{
2016-04-09 06:51:29 +00:00
global $env , $pageindex ;
2016-03-20 17:05:16 +00:00
2016-03-20 16:16:55 +00:00
// Variable parsing
if ( preg_match ( " / \ { \ { \ { ([^}]+) \ } \ } \ }/ " , $fragment [ " text " ], $matches ))
{
2016-04-08 08:01:12 +00:00
$params = [];
if ( ! empty ( $this -> paramStack ))
{
$stackEntry = array_slice ( $this -> paramStack , - 1 )[ 0 ];
$params = ! empty ( $stackEntry ) ? $stackEntry [ " params " ] : false ;
}
2016-03-20 17:05:16 +00:00
2016-03-20 16:16:55 +00:00
$variableKey = trim ( $matches [ 1 ]);
2016-03-20 16:38:36 +00:00
$variableValue = false ;
2016-03-20 17:05:16 +00:00
switch ( $variableKey )
{
2016-04-09 09:55:44 +00:00
case " @ " : // Lists all variables and their values
2019-01-05 17:51:51 +00:00
if ( ! empty ( $params )) {
2016-03-20 17:05:16 +00:00
$variableValue = " <table>
< tr >< th > Key </ th >< th > Value </ th ></ tr > \n " ;
foreach ( $params as $key => $value )
{
$variableValue .= " \t <tr><td> " . $this -> escapeText ( $key ) . " </td><td> " . $this -> escapeText ( $value ) . " </td></tr> \n " ;
}
$variableValue .= " </table> " ;
}
2019-01-05 17:51:51 +00:00
else {
$variableValue = " <em>(no parameters have been specified)</em> " ;
}
2016-03-20 17:05:16 +00:00
break ;
2016-04-09 06:51:29 +00:00
case " # " : // Shows a stack trace
2016-03-20 17:05:16 +00:00
$variableValue = " <ol start= \" 0 \" > \n " ;
$variableValue .= " \t <li> $env->page </li> \n " ;
foreach ( $this -> paramStack as $curStackEntry )
{
$variableValue .= " \t <li> " . $curStackEntry [ " pagename " ] . " </li> \n " ;
}
$variableValue .= " </ol> \n " ;
2016-04-08 08:01:12 +00:00
break ;
2016-04-09 06:51:29 +00:00
case " ~ " : // Show requested page's name
2016-04-08 08:50:13 +00:00
if ( ! empty ( $this -> paramStack ))
$variableValue = $this -> escapeText ( $env -> page );
2016-04-09 06:51:29 +00:00
break ;
case " * " : // Lists subpages
$subpages = get_subpages ( $pageindex , $env -> page );
$variableValue = [];
foreach ( $subpages as $pagename => $depth )
{
$variableValue [] = $pagename ;
}
$variableValue = implode ( " , " , $variableValue );
if ( strlen ( $variableValue ) === 0 )
$variableValue = " <em>(none yet!)</em> " ;
break ;
2016-04-09 09:55:44 +00:00
case " + " : // Shows a file gallery for subpages with files
2016-04-09 08:26:18 +00:00
// If the upload module isn't present, then there's no point
// in checking for uploaded files
if ( ! module_exists ( " feature-upload " ))
break ;
$variableValue = [];
$subpages = get_subpages ( $pageindex , $env -> page );
foreach ( $subpages as $pagename => $depth )
{
// Make sure that this is an uploaded file
if ( ! $pageindex -> $pagename -> uploadedfile )
continue ;
$mime_type = $pageindex -> $pagename -> uploadedfilemime ;
$previewSize = 300 ;
$previewUrl = " ?action=preview&size= $previewSize &page= " . rawurlencode ( $pagename );
$previewHtml = " " ;
switch ( substr ( $mime_type , 0 , strpos ( $mime_type , " / " )))
{
case " video " :
$previewHtml .= " <video src=' $previewUrl ' controls preload='metadata'> $pagename </video> \n " ;
break ;
case " audio " :
$previewHtml .= " <audio src=' $previewUrl ' controls preload='metadata'> $pagename </audio> \n " ;
break ;
case " application " :
case " image " :
default :
$previewHtml .= " <img src=' $previewUrl ' /> \n " ;
break ;
}
$previewHtml = " <a href='?page= " . rawurlencode ( $pagename ) . " '> $previewHtml $pagename </a> " ;
$variableValue [ $pagename ] = " <li style='min-width: $previewSize " . " px; min-height: $previewSize " . " px;'> $previewHtml </li> " ;
}
if ( count ( $variableValue ) === 0 )
$variableValue [ " default " ] = " <li><em>(No files found)</em></li> \n " ;
$variableValue = implode ( " \n " , $variableValue );
$variableValue = " <ul class='file-gallery'> $variableValue </ul> " ;
break ;
2016-03-20 17:05:16 +00:00
}
if ( isset ( $params [ $variableKey ]))
2016-03-20 16:42:21 +00:00
{
2016-03-20 17:05:16 +00:00
$variableValue = $params [ $variableKey ];
2016-03-20 16:42:21 +00:00
$variableValue = $this -> escapeText ( $variableValue );
}
2016-03-20 16:38:36 +00:00
2016-04-09 06:51:29 +00:00
if ( $variableValue !== false )
2016-03-20 16:16:55 +00:00
{
return [
" extent " => strlen ( $matches [ 0 ]),
2016-03-20 16:38:36 +00:00
" markup " => $variableValue
2016-03-20 16:16:55 +00:00
];
}
}
else if ( preg_match ( " / \ { \ { ([^}]+) \ } \ }/ " , $fragment [ " text " ], $matches ))
2016-03-20 14:05:55 +00:00
{
$templateElement = $this -> templateHandler ( $matches [ 1 ]);
if ( ! empty ( $templateElement ))
{
return [
" extent " => strlen ( $matches [ 0 ]),
" element " => $templateElement
];
}
}
}
2019-03-02 13:49:12 +00:00
/**
* Handles parsing out templates - recursively - and the parameter stack associated with it .
2019-03-02 21:59:50 +00:00
* @ param string $source The source string to process .
* @ return array The parsed result
2019-03-02 13:49:12 +00:00
*/
2016-03-20 14:05:55 +00:00
protected function templateHandler ( $source )
{
2016-04-08 08:01:12 +00:00
global $pageindex , $env ;
2016-03-20 14:05:55 +00:00
2016-03-20 16:16:55 +00:00
2016-08-22 08:36:22 +00:00
$parts = preg_split ( " / \\ ||¦/ " , trim ( $source , " { } " ));
2016-04-08 08:01:12 +00:00
$parts = array_map ( " trim " , $parts );
2016-03-20 14:05:55 +00:00
2016-04-10 10:49:50 +00:00
// Extract the name of the template page
2016-03-20 14:05:55 +00:00
$templatePagename = array_shift ( $parts );
// If the page that we are supposed to use as the tempalte doesn't
// exist, then there's no point in continuing.
if ( empty ( $pageindex -> $templatePagename ))
return false ;
// Parse the parameters
$params = [];
$i = 0 ;
foreach ( $parts as $part )
{
if ( strpos ( $part , " = " ) !== false )
{
// This param contains an equals sign, so it's a named parameter
$keyValuePair = explode ( " = " , $part , 2 );
2016-04-08 08:01:12 +00:00
$keyValuePair = array_map ( " trim " , $keyValuePair );
2016-03-20 14:05:55 +00:00
$params [ $keyValuePair [ 0 ]] = $keyValuePair [ 1 ];
}
else
{
// This isn't a named parameter
2016-03-20 16:16:55 +00:00
$params [ " $i " ] = trim ( $part );
2016-03-20 14:05:55 +00:00
$i ++ ;
}
}
2016-03-20 16:16:55 +00:00
// Add the parsed parameters to the parameter stack
2016-03-20 17:05:16 +00:00
$this -> paramStack [] = [
" pagename " => $templatePagename ,
" params " => $params
];
2016-03-20 14:05:55 +00:00
2016-04-08 08:01:12 +00:00
$templateFilePath = $env -> storage_prefix . $pageindex -> $templatePagename -> filename ;
2016-03-20 14:05:55 +00:00
$parsedTemplateSource = $this -> text ( file_get_contents ( $templateFilePath ));
2016-03-20 16:16:55 +00:00
// Remove the parsed parameters from the stack
array_pop ( $this -> paramStack );
2016-03-20 14:05:55 +00:00
return [
" name " => " div " ,
2019-03-02 22:07:58 +00:00
" rawHtml " => $parsedTemplateSource ,
2016-03-20 14:05:55 +00:00
" attributes " => [
" class " => " template "
]
];
2016-03-12 15:26:30 +00:00
}
2016-08-22 08:40:17 +00:00
/*
* ██ ███ ██ ████████ ███████ ██████ ███ ██ █████ ██
* ██ ████ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██
* ██ ██ ██ ██ ██ █████ ██████ ██ ██ ██ ███████ ██
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██ ██ ████ ██ ███████ ██ ██ ██ ████ ██ ██ ███████
*
* ██ ██ ███ ██ ██ ██ ███████
* ██ ██ ████ ██ ██ ██ ██
* ██ ██ ██ ██ ██ █████ ███████
* ██ ██ ██ ██ ██ ██ ██ ██
* ███████ ██ ██ ████ ██ ██ ███████
*/
2019-03-02 13:49:12 +00:00
/**
* Parses internal links
* @ param string $fragment The fragment to parse .
*/
2016-03-12 15:26:30 +00:00
protected function inlineInternalLink ( $fragment )
{
2016-08-19 18:39:53 +00:00
global $pageindex , $env ;
2016-04-08 08:28:59 +00:00
2019-09-21 10:35:50 +00:00
if ( preg_match ( '/^\[\[([^\]]*)\]\]([^\s!?",;.()\[\]{}*=+\/]*)/u' , $fragment [ " text " ], $matches ) === 1 ) {
2019-01-05 17:48:27 +00:00
// 1: Parse parameters out
// -------------------------------
$link_page = trim ( $matches [ 1 ]);
$display = $link_page . trim ( $matches [ 2 ]);
2016-08-22 08:36:22 +00:00
if ( strpos ( $matches [ 1 ], " | " ) !== false || strpos ( $matches [ 1 ], " ¦ " ) !== false )
2016-03-12 15:26:30 +00:00
{
// We have a bar character
2016-08-22 08:36:22 +00:00
$parts = preg_split ( " / \\ ||¦/ " , $matches [ 1 ], 2 );
2019-01-05 17:48:27 +00:00
$link_page = trim ( $parts [ 0 ]); // The page to link to
2016-08-19 09:59:49 +00:00
$display = trim ( $parts [ 1 ]); // The text to display
2016-03-12 15:26:30 +00:00
}
2019-01-05 17:48:27 +00:00
// 2: Parse the hash out
// -------------------------------
$hash_code = " " ;
if ( strpos ( $link_page , " # " ) !== false )
2016-08-19 18:39:53 +00:00
{
// We want to link to a subsection of a page
2019-01-05 17:48:27 +00:00
$hash_code = substr ( $link_page , strpos ( $link_page , " # " ) + 1 );
$link_page = substr ( $link_page , 0 , strpos ( $link_page , " # " ));
2016-08-19 18:39:53 +00:00
2019-01-05 17:48:27 +00:00
// If $link_page is empty then we want to link to the current page
if ( strlen ( $link_page ) === 0 )
$link_page = $env -> page ;
2016-08-19 18:39:53 +00:00
}
2019-01-05 17:48:27 +00:00
// 3: Page name auto-correction
// -------------------------------
$is_interwiki_link = module_exists ( " feature-interwiki-links " ) && is_interwiki_link ( $link_page );
2019-02-10 23:01:01 +00:00
// Try different variants on the pagename to try and get it to
// match something automagically
if ( ! $is_interwiki_link && empty ( $pageindex -> $link_page ))
$link_page = parsedown_pagename_resolve ( $link_page );
2016-08-19 09:59:49 +00:00
2019-01-05 17:48:27 +00:00
// 4: Construct the full url
// -------------------------------
$link_url = null ;
// If it's an interwiki link, then handle it as such
if ( $is_interwiki_link )
$link_url = interwiki_get_pagename_url ( $link_page );
// If it isn't (or it failed), then try it as a normal link instead
if ( empty ( $link_url )) {
$link_url = str_replace (
" %s " , rawurlencode ( $link_page ),
$this -> internalLinkBase
);
// We failed to handle it as an interwiki link, so we should
// tell everyone that
$is_interwiki_link = false ;
}
2016-03-12 15:26:30 +00:00
2019-02-10 23:01:01 +00:00
// 5: Construct the title
// -------------------------------
$title = $link_page ;
if ( $is_interwiki_link )
$title = interwiki_pagename_resolve ( $link_page ) -> name . " : " . interwiki_pagename_parse ( $link_page )[ 1 ] . " (Interwiki) " ;
2019-01-05 17:48:27 +00:00
if ( strlen ( $hash_code ) > 0 )
$link_url .= " # $hash_code " ;
2016-08-19 18:39:53 +00:00
2019-01-05 21:44:24 +00:00
2019-01-05 17:48:27 +00:00
// 6: Result encoding
// -------------------------------
2016-04-08 08:28:59 +00:00
$result = [
2016-03-12 15:26:30 +00:00
" extent " => strlen ( $matches [ 0 ]),
" element " => [
" name " => " a " ,
" text " => $display ,
2019-02-10 23:01:01 +00:00
2016-03-12 15:26:30 +00:00
" attributes " => [
2019-02-10 23:01:01 +00:00
" href " => $link_url ,
" title " => $title
2016-03-12 15:26:30 +00:00
]
]
];
2016-04-08 08:28:59 +00:00
2019-01-05 17:48:27 +00:00
// Attach some useful classes based on how we handled it
$class_list = [];
// Interwiki links can never be redlinks
if ( ! $is_interwiki_link && empty ( $pageindex -> { makepathsafe ( $link_page )}))
$class_list [] = " redlink " ;
if ( $is_interwiki_link )
$class_list [] = " interwiki_link " ;
$result [ " element " ][ " attributes " ][ " class " ] = implode ( " " , $class_list );
2016-04-08 08:28:59 +00:00
return $result ;
2016-03-12 15:26:30 +00:00
}
}
2016-08-22 08:40:17 +00:00
/*
* ███████ ██ ██ ████████ ███████ ███ ██ ██████ ███████ ██████
* ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██
* █████ ███ ██ █████ ██ ██ ██ ██ ██ █████ ██ ██
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ███████ ██ ██ ██ ███████ ██ ████ ██████ ███████ ██████
*
* ██ ███ ███ █████ ██████ ███████ ███████
* ██ ████ ████ ██ ██ ██ ██ ██
* ██ ██ ████ ██ ███████ ██ ███ █████ ███████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██ ██ ██ ██ ██ ██████ ███████ ███████
*/
2019-03-02 13:49:12 +00:00
/**
* Parses the extended image syntax .
* @ param string $fragment The source fragment to parse .
*/
2016-03-12 15:26:30 +00:00
protected function inlineExtendedImage ( $fragment )
{
2016-05-30 09:54:09 +00:00
global $pageindex ;
2016-08-21 11:00:40 +00:00
2016-08-22 10:14:49 +00:00
if ( preg_match ( '/^!\[(.*)\]\(([^|¦)]+)\s*(?:(?:\||¦)([^|¦)]*))?(?:(?:\||¦)([^|¦)]*))?(?:(?:\||¦)([^)]*))?\)/' , $fragment [ " text " ], $matches ))
2015-10-04 14:20:47 +00:00
{
2016-03-12 15:26:30 +00:00
/*
* 0 - Everything
* 1 - Alt text
* 2 - Url
2016-04-10 10:49:50 +00:00
* 3 - First param ( optional )
2016-05-29 19:34:34 +00:00
* 4 - Second param ( optional )
* 5 - Third param ( optional )
2016-03-12 15:26:30 +00:00
*/
$altText = $matches [ 1 ];
2016-10-11 18:26:42 +00:00
$imageUrl = trim ( str_replace ( " & " , " & " , $matches [ 2 ])); // Decode & to allow it in preview urls
2016-05-29 19:34:34 +00:00
$param1 = empty ( $matches [ 3 ]) ? false : strtolower ( trim ( $matches [ 3 ]));
2016-03-12 15:26:30 +00:00
$param2 = empty ( $matches [ 4 ]) ? false : strtolower ( trim ( $matches [ 4 ]));
2016-05-29 19:34:34 +00:00
$param3 = empty ( $matches [ 5 ]) ? false : strtolower ( trim ( $matches [ 5 ]));
2016-03-12 15:26:30 +00:00
$floatDirection = false ;
$imageSize = false ;
2016-05-29 19:34:34 +00:00
$imageCaption = false ;
2016-10-11 18:44:01 +00:00
$shortImageUrl = false ;
2016-03-12 15:26:30 +00:00
if ( $this -> isFloatValue ( $param1 ))
{
2016-04-08 08:01:12 +00:00
// Param 1 is a valid css float: ... value
2016-03-12 15:26:30 +00:00
$floatDirection = $param1 ;
$imageSize = $this -> parseSizeSpec ( $param2 );
}
else if ( $this -> isFloatValue ( $param2 ))
{
2016-04-08 08:01:12 +00:00
// Param 2 is a valid css float: ... value
2016-03-12 15:26:30 +00:00
$floatDirection = $param2 ;
$imageSize = $this -> parseSizeSpec ( $param1 );
}
2016-05-29 19:34:34 +00:00
else if ( $this -> isFloatValue ( $param3 ))
{
$floatDirection = $param3 ;
$imageSize = $this -> parseSizeSpec ( $param1 );
}
2016-04-08 08:01:12 +00:00
else if ( $param1 === false and $param2 === false )
{
// Neither params were specified
$floatDirection = false ;
$imageSize = false ;
}
2016-03-12 15:26:30 +00:00
else
{
2016-04-08 08:01:12 +00:00
// Neither of them are floats, but at least one is specified
// This must mean that the first param is a size spec like
// 250x128.
2016-03-12 15:26:30 +00:00
$imageSize = $this -> parseSizeSpec ( $param1 );
}
2015-10-04 14:20:47 +00:00
2016-06-03 08:32:26 +00:00
if ( $param1 !== false && strtolower ( trim ( $param1 )) == " caption " )
2016-05-29 19:34:34 +00:00
$imageCaption = true ;
2016-06-03 08:32:26 +00:00
if ( $param2 !== false && strtolower ( trim ( $param2 )) == " caption " )
$imageCaption = true ;
2016-05-29 19:34:34 +00:00
if ( $param3 !== false && strtolower ( trim ( $param3 )) == " caption " )
$imageCaption = true ;
2016-08-22 16:24:22 +00:00
//echo("Image url: $imageUrl, Pageindex entry: " . var_export(isset($pageindex->$imageUrl), true) . "\n");
2016-05-30 09:54:09 +00:00
if ( isset ( $pageindex -> $imageUrl ) and $pageindex -> $imageUrl -> uploadedfile )
{
// We have a short url! Expand it.
2016-10-11 18:44:01 +00:00
$shortImageUrl = $imageUrl ;
2016-05-30 09:54:09 +00:00
$imageUrl = " index.php?action=preview&size= " . max ( $imageSize [ " x " ], $imageSize [ " y " ]) . " &page= " . rawurlencode ( $imageUrl );
}
2016-03-12 15:26:30 +00:00
$style = " " ;
if ( $imageSize !== false )
2016-03-12 18:32:58 +00:00
$style .= " max-width: " . $imageSize [ " x " ] . " px; max-height: " . $imageSize [ " y " ] . " px; " ;
2016-03-12 15:26:30 +00:00
if ( $floatDirection )
$style .= " float: $floatDirection ; " ;
2015-10-04 14:20:47 +00:00
2016-04-08 08:01:12 +00:00
$urlExtension = pathinfo ( $imageUrl , PATHINFO_EXTENSION );
$urlType = system_extension_mime_type ( $urlExtension );
2016-05-29 19:34:34 +00:00
$result = [];
2016-04-08 08:01:12 +00:00
switch ( substr ( $urlType , 0 , strpos ( $urlType , " / " )))
{
case " audio " :
2016-05-29 19:34:34 +00:00
$result = [
2016-04-08 08:01:12 +00:00
" extent " => strlen ( $matches [ 0 ]),
" element " => [
" name " => " audio " ,
" text " => $altText ,
" attributes " => [
" src " => $imageUrl ,
" controls " => " controls " ,
" preload " => " metadata " ,
" style " => trim ( $style )
]
]
];
2016-05-29 19:34:34 +00:00
break ;
2016-04-08 08:01:12 +00:00
case " video " :
2016-05-29 19:34:34 +00:00
$result = [
2016-04-08 08:01:12 +00:00
" extent " => strlen ( $matches [ 0 ]),
" element " => [
" name " => " video " ,
" text " => $altText ,
" attributes " => [
" src " => $imageUrl ,
" controls " => " controls " ,
" preload " => " metadata " ,
" style " => trim ( $style )
]
]
];
2016-05-29 19:34:34 +00:00
break ;
2016-04-08 08:01:12 +00:00
case " image " :
default :
// If we can't work out what it is, then assume it's an image
2016-05-29 19:34:34 +00:00
$result = [
2016-04-08 08:01:12 +00:00
" extent " => strlen ( $matches [ 0 ]),
" element " => [
" name " => " img " ,
" attributes " => [
" src " => $imageUrl ,
" alt " => $altText ,
2016-04-10 10:49:50 +00:00
" title " => $altText ,
2016-04-08 08:01:12 +00:00
" style " => trim ( $style )
]
]
];
2016-05-29 19:34:34 +00:00
break ;
2016-04-08 08:01:12 +00:00
}
2016-05-29 19:34:34 +00:00
2016-10-11 18:44:01 +00:00
// ~ Image linker ~
$imageHref = $shortImageUrl !== false ? " ?page= " . rawurlencode ( $shortImageUrl ) : $imageUrl ;
$result [ " element " ] = [
" name " => " a " ,
" attributes " => [
" href " => $imageHref
],
" text " => [ $result [ " element " ]],
" handler " => " elements "
];
// ~
2019-10-20 15:44:32 +00:00
if ( $imageCaption ) {
2018-04-05 08:47:29 +00:00
$rawStyle = $result [ " element " ][ " text " ][ 0 ][ " attributes " ][ " style " ];
2016-05-29 19:34:34 +00:00
$containerStyle = preg_replace ( '/^.*float/' , " float " , $rawStyle );
$mediaStyle = preg_replace ( '/\s*float.*;/' , " " , $rawStyle );
$result [ " element " ] = [
" name " => " figure " ,
2016-10-11 18:44:01 +00:00
" attributes " => [
" style " => $containerStyle
],
2016-05-29 19:34:34 +00:00
" text " => [
$result [ " element " ],
[
" name " => " figcaption " ,
2016-05-30 09:54:09 +00:00
" text " => $altText
2016-05-29 19:34:34 +00:00
],
],
" handler " => " elements "
];
$result [ " element " ][ " text " ][ 0 ][ " attributes " ][ " style " ] = $mediaStyle ;
}
return $result ;
2015-10-04 14:20:47 +00:00
}
2016-03-12 15:26:30 +00:00
}
2019-10-20 23:31:18 +00:00
/*
* ██████ ██████ ██████ ███████ ██████ ██ ██████ ██████ ██ ██
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██ ██ ██ ██ ██ █████ ██████ ██ ██ ██ ██ █████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██████ ██████ ██████ ███████ ██████ ███████ ██████ ██████ ██ ██
*
* ██ ██ ██████ ██████ ██████ █████ ██████ ███████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██ ██ ██████ ██ ███ ██████ ███████ ██ ██ █████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██████ ██ ██████ ██ ██ ██ ██ ██████ ███████
*/
2019-10-20 17:05:57 +00:00
protected function blockFencedCodeComplete ( $block ) {
2019-10-20 19:54:50 +00:00
global $settings ;
2019-10-20 17:05:57 +00:00
$result = parent :: blockFencedCodeComplete ( $block );
2019-10-20 19:54:50 +00:00
$language = preg_replace ( " /^language-/ " , " " , $block [ " element " ][ " element " ][ " attributes " ][ " class " ]);
if ( ! isset ( $settings -> parser_ext_renderers -> $language ))
return $result ;
$text = $result [ " element " ][ " element " ][ " text " ];
$renderer = $settings -> parser_ext_renderers -> $language ;
$result [ " element " ] = [
2019-10-24 23:16:08 +00:00
" name " => " p " ,
" element " => [
" name " => " img " ,
" attributes " => [
" alt " => " Diagram rendered by { $renderer -> name } " ,
2019-10-25 11:12:57 +00:00
" src " => " ?action=parsedown-render-ext&language= " . rawurlencode ( $language ) . " &immutable_key= " . hash ( " crc32b " , json_encode ( $renderer )) . " &source= " . rawurlencode ( $text )
2019-10-24 23:16:08 +00:00
]
2019-10-20 19:54:50 +00:00
]
];
2019-10-25 11:12:57 +00:00
if ( ! empty ( $renderer -> output_classes ))
$result [ " element " ][ " element " ][ " attributes " ][ " class " ] = implode ( " " , $renderer -> output_classes );
2019-10-20 19:54:50 +00:00
2019-10-20 17:05:57 +00:00
return $result ;
}
2019-10-20 15:44:32 +00:00
/*
* ██ ██ ███████ █████ ██████ ███████ ██████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ███████ █████ ███████ ██ ██ █████ ██████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██ ██ ███████ ██ ██ ██████ ███████ ██ ██
*/
private $headingIdsUsed = [];
protected function blockHeader ( $line ) {
// This function overrides the header function defined in ParsedownExtra
$result = parent :: blockHeader ( $line );
// If this heading doesn't have an id already, add an automatic one
if ( ! isset ( $result [ " element " ][ " attributes " ][ " id " ])) {
$heading_id = str_replace ( " " , " - " ,
mb_strtolower ( makepathsafe (
$result [ " element " ][ " handler " ][ " argument " ]
))
);
$suffix = " " ;
while ( in_array ( $heading_id . $suffix , $this -> headingIdsUsed )) {
$heading_number = intval ( str_replace ( " _ " , " " , $suffix ));
if ( $heading_number == 0 ) $heading_number ++ ;
$suffix = " _ " . ( $heading_number + 1 );
}
$result [ " element " ][ " attributes " ][ " id " ] = $heading_id . $suffix ;
$this -> headingIdsUsed [] = $result [ " element " ][ " attributes " ][ " id " ];
}
return $result ;
}
2018-04-26 22:14:35 +00:00
# ~
# Static Methods
# ~
2019-03-02 13:49:12 +00:00
/**
* Extracts the page names from internal links in a given markdown source .
* Does not actually _parse_ the source - only extracts via a regex .
* @ param string $page_text The source text to extract a list of page names from .
* @ return array A list of page names that the given source text links to .
*/
2018-04-26 22:14:35 +00:00
public static function extract_page_names ( $page_text ) {
global $pageindex ;
preg_match_all ( " / \ [ \ [([^ \ ]]+) \ ] \ ]/ " , $page_text , $linked_pages );
if ( count ( $linked_pages [ 1 ]) === 0 )
return []; // No linked pages here
$result = [];
foreach ( $linked_pages [ 1 ] as $linked_page ) {
// Strip everything after the | and the #
if ( strpos ( $linked_page , " | " ) !== false )
$linked_page = substr ( $linked_page , 0 , strpos ( $linked_page , " | " ));
if ( strpos ( $linked_page , " # " ) !== false )
$linked_page = substr ( $linked_page , 0 , strpos ( $linked_page , " # " ));
if ( strlen ( $linked_page ) === 0 )
continue ;
// Make sure we try really hard to find this page in the
// pageindex
$altered_linked_page = $linked_page ;
if ( ! empty ( $pageindex -> { ucfirst ( $linked_page )}))
$altered_linked_page = ucfirst ( $linked_page );
else if ( ! empty ( $pageindex -> { ucwords ( $linked_page )}))
$altered_linked_page = ucwords ( $linked_page );
else // Our efforts were in vain, so reset to the original
$altered_linked_page = $linked_page ;
$result [] = $altered_linked_page ;
}
return $result ;
}
2019-10-20 15:44:32 +00:00
/*
* ██ ██ ████████ ██ ██ ██ ████████ ██ ███████ ███████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██ ██ ██ ██ ██ ██ ██ ██ █████ ███████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██████ ██ ██ ███████ ██ ██ ██ ███████ ███████
*/
2016-03-20 16:16:55 +00:00
2019-03-02 13:49:12 +00:00
/**
* Returns whether a string is a valid float : XXXXXX ; value .
* Used in parsing the extended image syntax .
* @ param string $value The value check .
* @ return bool Whether it ' s valid or not .
*/
private function isFloatValue ( string $value )
2016-03-12 15:26:30 +00:00
{
return in_array ( strtolower ( $value ), [ " left " , " right " ]);
}
2019-03-02 13:49:12 +00:00
/**
* Parses a size specifier into an array .
* @ param string $text The source text to parse . e . g . " 256x128 "
* @ return array | bool The parsed size specifier . Example : [ " x " => 256 , " y " => 128 ] . Returns false if parsing failed .
*/
private function parseSizeSpec ( string $text )
2016-03-12 15:26:30 +00:00
{
if ( strpos ( $text , " x " ) === false )
return false ;
$parts = explode ( " x " , $text , 2 );
2015-10-04 14:20:47 +00:00
2016-03-12 15:26:30 +00:00
if ( count ( $parts ) != 2 )
return false ;
2015-10-04 14:20:47 +00:00
2016-03-12 15:26:30 +00:00
array_map ( " trim " , $parts );
array_map ( " intval " , $parts );
if ( in_array ( 0 , $parts ))
return false ;
return [
" x " => $parts [ 0 ],
" y " => $parts [ 1 ]
];
}
2019-03-02 13:49:12 +00:00
/**
* Escapes the source text via htmlentities .
* @ param string $text The text to escape .
* @ return string The escaped string .
*/
2016-03-20 16:42:21 +00:00
protected function escapeText ( $text )
{
return htmlentities ( $text , ENT_COMPAT | ENT_HTML5 );
}
2016-03-12 15:26:30 +00:00
/**
* Sets the base url to be used for internal links . '%s' will be replaced
* with a URL encoded version of the page name .
* @ param string $url The url to use when parsing internal links .
*/
public function setInternalLinkBase ( $url )
{
$this -> internalLinkBase = $url ;
}
2015-10-04 12:37:12 +00:00
}
?>