2019-12-26 08:14:04 +00:00
< ? php
/**
* Name : Markdown
* Description : Parse Markdown code when creating new items
* Version : 0.1
* Author : Michael Vogel < https :// pirati . ca / profile / heluecht >
*/
use Friendica\App ;
use Friendica\Core\Hook ;
use Friendica\Content\Text\Markdown ;
use Friendica\Core\Renderer ;
2020-01-18 15:50:56 +00:00
use Friendica\DI ;
2019-12-26 08:14:04 +00:00
function markdown_install () {
Hook :: register ( 'post_local_start' , __FILE__ , 'markdown_post_local_start' );
Hook :: register ( 'addon_settings' , __FILE__ , 'markdown_addon_settings' );
Hook :: register ( 'addon_settings_post' , __FILE__ , 'markdown_addon_settings_post' );
}
function markdown_addon_settings ( App $a , & $s )
{
if ( ! local_user ()) {
return ;
}
2020-01-18 15:50:56 +00:00
$enabled = intval ( DI :: pConfig () -> get ( local_user (), 'markdown' , 'enabled' ));
2019-12-26 08:14:04 +00:00
$t = Renderer :: getMarkupTemplate ( 'settings.tpl' , 'addon/markdown/' );
$s .= Renderer :: replaceMacros ( $t , [
2020-01-18 19:52:33 +00:00
'$title' => DI :: l10n () -> t ( 'Markdown' ),
'$enabled' => [ 'enabled' , DI :: l10n () -> t ( 'Enable Markdown parsing' ), $enabled , DI :: l10n () -> t ( 'If enabled, self created items will additionally be parsed via Markdown.' )],
'$submit' => DI :: l10n () -> t ( 'Save Settings' ),
2019-12-26 08:14:04 +00:00
]);
}
function markdown_addon_settings_post ( App $a , & $b )
{
if ( ! local_user () || empty ( $_POST [ 'markdown-submit' ])) {
return ;
}
2020-01-18 15:54:49 +00:00
DI :: pConfig () -> set ( local_user (), 'markdown' , 'enabled' , intval ( $_POST [ 'enabled' ]));
2019-12-26 08:14:04 +00:00
}
function markdown_post_local_start ( App $a , & $request ) {
2020-01-18 15:50:56 +00:00
if ( empty ( $request [ 'body' ]) || ! DI :: pConfig () -> get ( local_user (), 'markdown' , 'enabled' )) {
2019-12-26 08:14:04 +00:00
return ;
}
2019-12-27 01:35:15 +00:00
// Elements that shouldn't be parsed
2019-12-30 02:25:01 +00:00
$elements = [ 'code' , 'noparse' , 'nobb' , 'pre' , 'share' , 'url' , 'img' , 'bookmark' ,
'audio' , 'video' , 'youtube' , 'vimeo' , 'attachment' , 'iframe' , 'map' , 'mail' ];
2019-12-27 01:35:15 +00:00
foreach ( $elements as $element ) {
2019-12-29 21:01:38 +00:00
$request [ 'body' ] = preg_replace_callback ( " / \ [ " . $element . " (.*?) \ ](.*?) \ [ \ / " . $element . " \ ]/ism " ,
function ( $match ) use ( $element ) {
return '[' . $element . '-b64' . base64_encode ( $match [ 1 ]) . ']' . base64_encode ( $match [ 2 ]) . '[/b64-' . $element . ']' ;
2019-12-27 06:09:09 +00:00
},
$request [ 'body' ]
);
2019-12-27 01:35:15 +00:00
}
2019-12-26 08:14:04 +00:00
$request [ 'body' ] = Markdown :: toBBCode ( $request [ 'body' ]);
2019-12-27 01:35:15 +00:00
2019-12-27 06:09:09 +00:00
foreach ( array_reverse ( $elements ) as $element ) {
2019-12-29 21:01:38 +00:00
$request [ 'body' ] = preg_replace_callback ( " / \ [ " . $element . " -b64(.*?) \ ](.*?) \ [ \ /b64- " . $element . " \ ]/ism " ,
function ( $match ) use ( $element ) {
return '[' . $element . base64_decode ( $match [ 1 ]) . ']' . base64_decode ( $match [ 2 ]) . '[/' . $element . ']' ;
2019-12-27 06:09:09 +00:00
},
$request [ 'body' ]
);
2019-12-27 01:35:15 +00:00
}
2019-12-26 08:14:04 +00:00
}