From 45f35827dc5e3cc685803884c41aaac6e2b5fe42 Mon Sep 17 00:00:00 2001 From: "Joel C. Llano" Date: Thu, 27 Oct 2016 11:37:51 +0800 Subject: [PATCH] Markdown plugin Added initial stand-alone plugin for markdown, moved all dependencies to the plugin folder. - revert CoreFormatting back to its original state, as well as other, unneeded previous changes - disable all processing in MantisCoreFormatting if enabled - turn MantisCoreFormatting back on if Markdown is uninstalled - hook EVENT_CORE_HEADERS to enable images referenced from the internet --- .gitignore | 1 + config_defaults_inc.php | 10 -- core.php | 7 +- docbook/Admin_Guide/en-US/config/display.xml | 9 -- .../MantisCoreFormatting.php | 38 ++---- plugins/MantisMarkdown/MantisMarkdown.php | 128 ++++++++++++++++++ .../MantisMarkdown/core/MantisMarkdown.php | 19 +-- .../MantisMarkdown/lang/strings_english.txt | 45 ++++++ .../MantisMarkdown/library}/LICENSE.txt | 0 .../MantisMarkdown/library}/Parsedown.php | 0 .../MantisMarkdown/tests}/MarkdownTest.php | 3 +- tests/Mantis/AllTests.php | 2 - 12 files changed, 195 insertions(+), 67 deletions(-) create mode 100644 plugins/MantisMarkdown/MantisMarkdown.php rename core/classes/MantisMarkdown.class.php => plugins/MantisMarkdown/core/MantisMarkdown.php (96%) create mode 100644 plugins/MantisMarkdown/lang/strings_english.txt rename {library/parsedown => plugins/MantisMarkdown/library}/LICENSE.txt (100%) rename {library/parsedown => plugins/MantisMarkdown/library}/Parsedown.php (100%) rename {tests/Mantis => plugins/MantisMarkdown/tests}/MarkdownTest.php (96%) diff --git a/.gitignore b/.gitignore index 7643e2afb1..7c9132ad96 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ plugins/* !plugins/MantisGraph !plugins/XmlImportExport !plugins/Gravatar +!plugins/MantisMarkdown # IDE/Editor temporary files *~ diff --git a/config_defaults_inc.php b/config_defaults_inc.php index d45ccca280..10be2c5fbd 100644 --- a/config_defaults_inc.php +++ b/config_defaults_inc.php @@ -1208,16 +1208,6 @@ */ $g_private_news_threshold = DEVELOPER; -############ -# Markdown # -############ - -/** - * Enables markdown support - once enabled all issue titles, text, and notes are formatted based on - * markdown syntax. Default to OFF since it is experimental feature for now. - */ -$g_markdown_enabled = OFF; - ################################ # MantisBT Default Preferences # ################################ diff --git a/core.php b/core.php index 539b23ece7..0ee61d2f44 100644 --- a/core.php +++ b/core.php @@ -274,11 +274,6 @@ function __autoload( $p_class ) { # Set HTTP response headers require_api( 'http_api.php' ); - -if( MantisMarkdown::enabled() ) { # if markdown enabled, then images can be referenced from internet. - http_csp_add( 'img-src', "*" ); -} - event_signal( 'EVENT_CORE_HEADERS' ); http_all_headers(); @@ -292,4 +287,4 @@ function __autoload( $p_class ) { if( !defined( 'PLUGINS_DISABLED' ) && !defined( 'MANTIS_MAINTENANCE_MODE' ) ) { require_api( 'event_api.php' ); event_signal( 'EVENT_CORE_READY' ); -} \ No newline at end of file +} diff --git a/docbook/Admin_Guide/en-US/config/display.xml b/docbook/Admin_Guide/en-US/config/display.xml index 5343c52e02..2b18148287 100644 --- a/docbook/Admin_Guide/en-US/config/display.xml +++ b/docbook/Admin_Guide/en-US/config/display.xml @@ -194,14 +194,5 @@ - - $g_markdown_enabled - - - Enables markdown support - once enabled all issue titles, text, and notes are formatted based on - markdown syntax. Default to OFF since it is experimental feature for now. - - - diff --git a/plugins/MantisCoreFormatting/MantisCoreFormatting.php b/plugins/MantisCoreFormatting/MantisCoreFormatting.php index b718c08631..1fd85e0ab3 100644 --- a/plugins/MantisCoreFormatting/MantisCoreFormatting.php +++ b/plugins/MantisCoreFormatting/MantisCoreFormatting.php @@ -78,21 +78,13 @@ function text( $p_event, $p_string, $p_multiline = true ) { } if( ON == $s_text ) { - if ( MantisMarkdown::enabled() ) { - if ( $p_multiline ) { - $t_string = MantisMarkdown::convert_text( $t_string ); - } else { - $t_string = MantisMarkdown::convert_line( $t_string ); - } - } else { - $t_string = string_strip_hrefs( $t_string ); - $t_string = string_html_specialchars( $t_string ); - $t_string = string_restore_valid_html_tags( $t_string, $p_multiline ); - - if( $p_multiline ) { - $t_string = string_preserve_spaces_at_bol( $t_string ); - $t_string = string_nl2br( $t_string ); - } + $t_string = string_strip_hrefs( $t_string ); + $t_string = string_html_specialchars( $t_string ); + $t_string = string_restore_valid_html_tags( $t_string, $p_multiline ); + + if( $p_multiline ) { + $t_string = string_preserve_spaces_at_bol( $t_string ); + $t_string = string_nl2br( $t_string ); } } @@ -123,7 +115,7 @@ function formatted( $p_event, $p_string, $p_multiline = true ) { $s_buglinks = plugin_config_get( 'process_buglinks' ); } - if( ON == $s_urls && !MantisMarkdown::enabled() ) { + if( ON == $s_urls ) { $t_string = string_insert_hrefs( $t_string ); } @@ -155,14 +147,10 @@ function rss( $p_event, $p_string ) { } if( ON == $s_text ) { - if ( MantisMarkdown::enabled() ) { - $t_string = MantisMarkdown::convert_text( $t_string ); - } else { - $t_string = string_strip_hrefs( $t_string ); - $t_string = string_html_specialchars( $t_string ); - $t_string = string_restore_valid_html_tags( $t_string ); - $t_string = string_nl2br( $t_string ); - } + $t_string = string_strip_hrefs( $t_string ); + $t_string = string_html_specialchars( $t_string ); + $t_string = string_restore_valid_html_tags( $t_string ); + $t_string = string_nl2br( $t_string ); } if( ON == $s_urls ) { @@ -208,4 +196,4 @@ function email( $p_event, $p_string ) { return $t_string; } -} +} \ No newline at end of file diff --git a/plugins/MantisMarkdown/MantisMarkdown.php b/plugins/MantisMarkdown/MantisMarkdown.php new file mode 100644 index 0000000000..f04f62f1db --- /dev/null +++ b/plugins/MantisMarkdown/MantisMarkdown.php @@ -0,0 +1,128 @@ +. + * + * @copyright Copyright 2002 MantisBT Team - mantisbt-dev@lists.sourceforge.net + */ + +require_api( 'mention_api.php' ); +require_once( 'core/MantisMarkdown.php' ); + +/** + * Mantis Markdown Plugin + */ +class MantisMarkdownPlugin extends MantisPlugin { + + /** + * Initialized any needed methods, api's, etc... + * + * Make sure to turn Text Processing off, causing markdown to not render properly + * @return void + */ + function init() { + + if ( ON == config_get( 'plugin_MantisCoreFormatting_process_text' ) ) { + config_set( 'plugin_MantisCoreFormatting_process_text', OFF ); + } + + if ( ON == config_get( 'plugin_MantisCoreFormatting_process_urls' ) ) { + config_set( 'plugin_MantisCoreFormatting_process_urls', OFF ); + } + + if ( ON == config_get( 'plugin_MantisCoreFormatting_process_buglinks' ) ) { + config_set( 'plugin_MantisCoreFormatting_process_buglinks', OFF ); + } + + } + + /** + * Make sure to turn Text Processing back on uninstall + * reset it back to MantisCoreFormatting position + */ + function uninstall() { + config_set( 'plugin_MantisCoreFormatting_process_text', ON ); + config_set( 'plugin_MantisCoreFormatting_process_urls', ON ); + config_set( 'plugin_MantisCoreFormatting_process_buglinks', ON ); + } + + /** + * A method that populates the plugin information and minimum requirements. + * @return void + */ + function register() { + $this->name = lang_get( 'plugin_markdown_title' ); + $this->description = lang_get( 'plugin_markdown_description' ); + + $this->version = MANTIS_VERSION; + $this->requires = array( + 'MantisCore' => '2.0.0', + ); + + $this->author = 'MantisBT Team'; + $this->contact = 'mantisbt-dev@lists.sourceforge.net'; + $this->url = 'http://www.mantisbt.org'; + } + + /** + * Event hook declaration. + * @return array + */ + function hooks() { + return array( + 'EVENT_DISPLAY_FORMATTED' => 'markdown', # Formatted String Display + 'EVENT_CORE_HEADERS' => 'csp_headers' + ); + } + + /** + * Add img-src directives to enable to referenced images from internet. + * @return void + */ + function csp_headers() { + http_csp_add( 'img-src', "*" ); + } + + /** + * Markdown processing. + * + * Performs markdown and bug links processing + * + * @param string $p_event Event name. + * @param string $p_string Raw text to process. + * @param boolean $p_multiline True for multiline text (default), false for single-line. + * + * @return string The html text + */ + function markdown( $p_event, $p_string, $p_multiline = true ) { + + $t_string = $p_string; + + # Process bug links + $t_string = string_process_bug_link( $t_string ); + $t_string = string_process_bugnote_link( $t_string ); + + $t_string = mention_format_text( $t_string, true ); + + # Markdown processing + if ( $p_multiline ) { + $t_string = MantisMarkdown::convert_text( $t_string ); + } else { + $t_string = MantisMarkdown::convert_line( $t_string ); + } + + return $t_string; + } +} \ No newline at end of file diff --git a/core/classes/MantisMarkdown.class.php b/plugins/MantisMarkdown/core/MantisMarkdown.php similarity index 96% rename from core/classes/MantisMarkdown.class.php rename to plugins/MantisMarkdown/core/MantisMarkdown.php index 2562082ef3..c9565bdf19 100644 --- a/core/classes/MantisMarkdown.class.php +++ b/plugins/MantisMarkdown/core/MantisMarkdown.php @@ -29,14 +29,14 @@ * To meet and match the MantisBT styles and logic requirements, we have to override and control with it * For example: #2 is treated as header markdown * So, to make sure #2 treated as bug link (not a header), then we have to change the logic in blockHeader and blockSetextHeader method - * + * * @package MantisBT * @subpackage parsedown * * @uses Parsedown Library */ -require_once( dirname( dirname( __FILE__ ) ) . '../../library/parsedown/Parsedown.php' ); +require_once( dirname( dirname( __FILE__ ) ) . '/library/Parsedown.php' ); /** * A class that overrides default Markdown parsing for Mantis specific scenarios. @@ -69,15 +69,6 @@ public function __construct() { $this->inline_style = 'border-color:#847d7d'; } - /** - * Check if markdown is enabled. - * - * @return bool true for enabled, false otherwise. - */ - public static function enabled() { - return config_get( 'markdown_enabled' ) != OFF; - } - /** * Convert a field that supports multiple lines form markdown to html. * @param string $p_text The text to convert. @@ -151,7 +142,7 @@ protected function blockSetextHeader( $line, array $block = null ) { /** * Add a class attribute on a table markdown elements - * + * * @param string $line The Markdown syntax to parse * @param array $block A block-level element * @param string $fn the function name to call (blockTable or blockTableContinue) @@ -192,7 +183,7 @@ protected function blockTableContinue( $line, array $block ) { /** * Add an inline style on a blockquote markdown elements - * + * * @param string $line The Markdown syntax to parse * @param array $block A block-level element * @param string $fn the function name to call (blockQuote or blockQuoteContinue) @@ -241,4 +232,4 @@ private static function init() { } return static::$mantis_markdown; } -} \ No newline at end of file +} diff --git a/plugins/MantisMarkdown/lang/strings_english.txt b/plugins/MantisMarkdown/lang/strings_english.txt new file mode 100644 index 0000000000..c60f21a644 --- /dev/null +++ b/plugins/MantisMarkdown/lang/strings_english.txt @@ -0,0 +1,45 @@ +. + +/** + * Language File - English (English) + * + * ********************************************************************** + * ** This file is generated from translations stored in translatewiki ** + * ** Information on Copyright/License for translatewiki translations ** + * ** is available at http://translatewiki.net/wiki/Project:About ** + * ********************************************************************** + * ** ** + * ** DO NOT UPDATE MANUALLY ** + * ** ** + * ** To improve a translation please visit http://translatewiki.net ** + * ** Detailed instructions on how to create or update translations at ** + * ** http://www.mantisbt.org/wiki/doku.php/mantisbt:translationshowto ** + * ********************************************************************** + * + * See the qqq 'language' for message documentation incl. usage of parameters + * + * @ingroup Language + * @file + * @copyright Copyright 2002 MantisBT Team - mantisbt-dev@lists.sourceforge.net + * @link http://www.mantisbt.org + * + */ + +$s_plugin_markdown_title = 'MantisBT Markdown'; +$s_plugin_markdown_description = 'Official Markdown plugin.'; + + diff --git a/library/parsedown/LICENSE.txt b/plugins/MantisMarkdown/library/LICENSE.txt similarity index 100% rename from library/parsedown/LICENSE.txt rename to plugins/MantisMarkdown/library/LICENSE.txt diff --git a/library/parsedown/Parsedown.php b/plugins/MantisMarkdown/library/Parsedown.php similarity index 100% rename from library/parsedown/Parsedown.php rename to plugins/MantisMarkdown/library/Parsedown.php diff --git a/tests/Mantis/MarkdownTest.php b/plugins/MantisMarkdown/tests/MarkdownTest.php similarity index 96% rename from tests/Mantis/MarkdownTest.php rename to plugins/MantisMarkdown/tests/MarkdownTest.php index b64c7fcd8b..10d3c2cde0 100644 --- a/tests/Mantis/MarkdownTest.php +++ b/plugins/MantisMarkdown/tests/MarkdownTest.php @@ -24,7 +24,8 @@ */ # Includes -require_once dirname( dirname( __FILE__ ) ) . '/TestConfig.php'; +require_once( dirname( dirname( __FILE__ ) ) . '../../../tests/TestConfig.php' ); +require_once( dirname( dirname( __FILE__ ) ) . '/core/MantisMarkdown.php' ); # MantisBT Core API require_mantis_core(); diff --git a/tests/Mantis/AllTests.php b/tests/Mantis/AllTests.php index ee1537e607..49c9be9bcf 100644 --- a/tests/Mantis/AllTests.php +++ b/tests/Mantis/AllTests.php @@ -33,7 +33,6 @@ require_once 'MentionParsingTest.php'; require_once 'StringTest.php'; require_once 'ConfigParserTest.php'; -require_once 'MarkdownTest.php'; /** * All Test Cases @@ -53,7 +52,6 @@ public static function suite() { $t_suite->addTestSuite( 'MantisStringTest' ); $t_suite->addTestSuite( 'MentionParsingTest' ); $t_suite->addTestSuite( 'MantisConfigParserTest' ); - $t_suite->addTestSuite( 'MantisMarkdownTest' ); return $t_suite; }