From 848ad4c6ac3d88ec9990ff89f7f0a2b6af9b0c70 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Mon, 9 Jan 2017 11:35:33 +1300 Subject: [PATCH 1/7] SS4 compat: Rename classes for namespacing support --- {code/forms => src/Forms}/MarkdownEditor.php | 0 {code/model/fieldtypes => src/Model/FieldTypes}/Markdown.php | 0 {code/renderer => src/Renderer}/GithubMarkdownRenderer.php | 0 {code/renderer => src/Renderer}/IMarkdownRenderer.php | 0 {code/renderer => src/Renderer}/PHPMarkdownMarkdownRenderer.php | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename {code/forms => src/Forms}/MarkdownEditor.php (100%) rename {code/model/fieldtypes => src/Model/FieldTypes}/Markdown.php (100%) rename {code/renderer => src/Renderer}/GithubMarkdownRenderer.php (100%) rename {code/renderer => src/Renderer}/IMarkdownRenderer.php (100%) rename {code/renderer => src/Renderer}/PHPMarkdownMarkdownRenderer.php (100%) diff --git a/code/forms/MarkdownEditor.php b/src/Forms/MarkdownEditor.php similarity index 100% rename from code/forms/MarkdownEditor.php rename to src/Forms/MarkdownEditor.php diff --git a/code/model/fieldtypes/Markdown.php b/src/Model/FieldTypes/Markdown.php similarity index 100% rename from code/model/fieldtypes/Markdown.php rename to src/Model/FieldTypes/Markdown.php diff --git a/code/renderer/GithubMarkdownRenderer.php b/src/Renderer/GithubMarkdownRenderer.php similarity index 100% rename from code/renderer/GithubMarkdownRenderer.php rename to src/Renderer/GithubMarkdownRenderer.php diff --git a/code/renderer/IMarkdownRenderer.php b/src/Renderer/IMarkdownRenderer.php similarity index 100% rename from code/renderer/IMarkdownRenderer.php rename to src/Renderer/IMarkdownRenderer.php diff --git a/code/renderer/PHPMarkdownMarkdownRenderer.php b/src/Renderer/PHPMarkdownMarkdownRenderer.php similarity index 100% rename from code/renderer/PHPMarkdownMarkdownRenderer.php rename to src/Renderer/PHPMarkdownMarkdownRenderer.php From ca606037b8a897bed0664cf88c58fd6b86ba57ab Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Mon, 9 Jan 2017 13:04:27 +1300 Subject: [PATCH 2/7] SS4 compat: Update readme, composer.json. Add upgrade map, editorconfig and injector config --- .editorconfig | 17 +++++++++ README.md | 85 ++++++++++++++++++++++++++++---------------- _config.php | 1 - _config/injector.yml | 6 ++++ composer.json | 53 +++++++++++++++------------ upgrade.yml | 6 ++++ 6 files changed, 114 insertions(+), 54 deletions(-) create mode 100644 .editorconfig create mode 100644 _config/injector.yml create mode 100644 upgrade.yml diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..ce6751f --- /dev/null +++ b/.editorconfig @@ -0,0 +1,17 @@ +# For more information about the properties used in +# this file, please see the EditorConfig documentation: +# http://editorconfig.org/ + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false + +[*.{yml,js,json,css,scss,eslintrc}] +indent_size = 2 diff --git a/README.md b/README.md index bef4cf1..f4246a9 100644 --- a/README.md +++ b/README.md @@ -1,76 +1,101 @@ -Markdown Editor and DBField -================= +# Markdown Editor and DBField -Adds a field and a data type that allows for Markdown editing, uses a supported renderer (default is the github api) to render the html +Adds a field and a data type that allows for Markdown editing, uses a supported renderer (default is the GitHub API) +to render the HTML. ## Requirements -* SilverStripe 3.x -* PHP Curl Support + +* SilverStripe 4.x +* PHP cURL Support ## Installation -* Download the module from here https://github.com/UndefinedOffset/silverstripe-markdown/downloads -* Extract the downloaded archive into your site root so that the destination folder is called markdown, opening the extracted folder should contain _config.php in the root along with other files/folders -* Run dev/build?flush=all to regenerate the manifest -* Upon entering the cms and using MarkdownEditor for the first time you make need to add ?flush=all to the end of the address to force the templates to regenerate + +* Install with [composer](https://getcomposer.org): `composer require undefinedoffset/silverstripe-markdown ^2.0` +* Run `dev/build?flush=all` to regenerate the manifest ## Usage + Use the Markdown data type as your fields data type, then use the MarkdownEditor field in the cms for editing. -###Page class: +### Page class + ```php -class MyPage extends Page { - public static $db=array( - 'MarkdownContent'=>'Markdown' - ); +use UndefinedOffset\Markdown\Forms\MarkdownEditor; + +class MyPage extends Page +{ + public static $db = array( + 'MarkdownContent' => 'Markdown' + ); - public function getCMSFields() { - $fields=parent::getCMSFields(); + public function getCMSFields() + { + $fields = parent::getCMSFields(); $editor = new MarkdownEditor('MarkdownContent', 'Page Content (Markdown)'); $editor->setRows(15); //optional, set number of rows in CMS $editor->setWrapMode(true); //optional, turn on word wrapping - $fields->addFieldToTab("Root.Main", $editor); + $fields->addFieldToTab('Root.Main', $editor); return $fields; } } ``` +### Template -###Template: ```html
- $MarkdownContent + $MarkdownContent
``` -You may also request the markdown using Github Flavored Markdown by calling $YourField.AsHTML(true) in your template by default Github Flavored Markdown is not used just regular Markdown is used. +You may also request the markdown using Github Flavored Markdown by calling $YourField.AsHTML(true) in your template +by default Github Flavored Markdown is not used just regular Markdown is used. + ```html
$MarkdownContent.AsHTML(true)
``` -###Configuration: -The default renderer is the Github renderer. However, other renderers are supported. +### Configuration + +The default renderer is the GitHub renderer. However, other renderers are supported. -To set what renderer to use, in **_config.php** do the following: +To set what renderer to use, in `_config.php` do the following: ```php -Markdown::setRenderer('GithubMarkdownRenderer'); //Class name of any implementation of IMarkdownRenderer will work +use UndefinedOffset\Markdown\Model\FieldTypes\Markdown; + +// Fully qualified (namespaced) class name of any implementation of IMarkdownRenderer will work: +Markdown::setRenderer('UndefinedOffset\\Markdown\\Renderer\\GithubMarkdownRenderer'); ``` -####GithubMarkdownRenderer +#### GithubMarkdownRenderer + The following options are available on the default GithubMarkdownRenderer: + ```php -GithubMarkdownRenderer::useBasicAuth('github username', 'github password'); //authenticate to the Github API to get 5,000 requests per hour instead of 60 -GithubMarkdownRenderer::setUseGFM(true); //whether or not to use Github Flavoured Markdown +use UndefinedOffset\Markdown\Renderer\GitHubMarkdownRenderer; + +// authenticate to the Github API to get 5,000 requests per hour instead of 60 +GithubMarkdownRenderer::useBasicAuth('github username', 'github password'); +// whether or not to use Github Flavoured Markdown +GithubMarkdownRenderer::setUseGFM(true); ``` -####PHPMarkdownMarkdownRenderer -PHPMarkdownMarkdownRenderer is simple and has no options. Use this to avoid the delay on page load the first time after editing that comes from using the Github renderer (especially if the page has many sections of markdown). You will need to install [PHP Markdown](https://github.com/michelf/php-markdown) for this to work - it can be installed with composer. +#### PHPMarkdownMarkdownRenderer + +PHPMarkdownMarkdownRenderer is simple and has no options. Use this to avoid the delay on page load the first time +after editing that comes from using the Github renderer (especially if the page has many sections of markdown). You +will need to install [PHP Markdown](https://github.com/michelf/php-markdown) for this to work - it can be installed +with composer. **Note:** This renderer does not support Github Flavoured Markdown. + ```php -Markdown::setRenderer('PHPMarkdownMarkdownRenderer'); +use UndefinedOffset\Markdown\Model\FieldTypes\Markdown; + +Markdown::setRenderer('UndefinedOffset\\Markdown\\Renderer\\PHPMarkdownMarkdownRenderer'); ``` diff --git a/_config.php b/_config.php index b6c5d93..a3a7032 100644 --- a/_config.php +++ b/_config.php @@ -1,3 +1,2 @@ \ No newline at end of file diff --git a/_config/injector.yml b/_config/injector.yml new file mode 100644 index 0000000..df07ebf --- /dev/null +++ b/_config/injector.yml @@ -0,0 +1,6 @@ +--- +Name: markdowninjector +--- +SilverStripe\Core\Injector\Injector: + Markdown: + class: UndefinedOffset\Markdown\Model\FieldTypes\Markdown diff --git a/composer.json b/composer.json index cc7a66b..042a7ea 100644 --- a/composer.json +++ b/composer.json @@ -1,26 +1,33 @@ { - "name": "undefinedoffset/silverstripe-markdown", - "description": "Adds a field and a data type that allows for Markdown editing, uses the github api to render the html", - "type": "silverstripe-module", - "keywords": ["silverstripe", "markdown"], - "license": "BSD-3-Clause", - "authors": [ - { - "name": "Ed Chipman", - "homepage": "http://www.edchipman.ca", - "role": "Developer" - } - ], + "name": "undefinedoffset/silverstripe-markdown", + "description": "Adds a field and a data type that allows for Markdown editing, uses the github api to render the html", + "type": "silverstripe-module", + "keywords": ["silverstripe", "markdown"], + "license": "BSD-3-Clause", + "authors": [ + { + "name": "Ed Chipman", + "homepage": "http://www.edchipman.ca", + "role": "Developer" + } + ], - "require": - { - "silverstripe/framework": "3.*", - "composer/installers": "*" - }, - "support": { - "issues": "https://github.com/undefinedoffset/silverstripe-markdown/issues" - }, - "extra": { - "installer-name": "markdown" - } + "require": { + "silverstripe/framework": "^4.0@dev", + "composer/installers": "*" + }, + "support": { + "issues": "https://github.com/undefinedoffset/silverstripe-markdown/issues" + }, + "extra": { + "installer-name": "markdown", + "branch-alias": { + "dev-master": "2.x-dev" + } + }, + "autoload": { + "psr-4": { + "UndefinedOffset\\Markdown\\": "src/" + } + } } diff --git a/upgrade.yml b/upgrade.yml new file mode 100644 index 0000000..1284658 --- /dev/null +++ b/upgrade.yml @@ -0,0 +1,6 @@ +mappings: + MarkdownEditor: UndefinedOffset\Markdown\Forms\MarkdownEditor + Markdown: UndefinedOffset\Markdown\Model\FieldTypes\Markdown + GithubMarkdownRenderer: UndefinedOffset\Markdown\Renderer\GithubMarkdownRenderer + IMarkdownRenderer: UndefinedOffset\Markdown\Renderer\IMarkdownRenderer + PHPMarkdownMarkdownRenderer: UndefinedOffset\Markdown\Renderer\PHPMarkdownMarkdownRenderer From cac383f0cabe2adc71ec0d9a2a014efde2f1fc1e Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Mon, 9 Jan 2017 13:09:41 +1300 Subject: [PATCH 3/7] SS4 compat: Implement namespaces. Tweak SS framework changes in code. --- src/Forms/MarkdownEditor.php | 19 +++-- src/Model/FieldTypes/Markdown.php | 73 ++++++++++++-------- src/Renderer/GithubMarkdownRenderer.php | 32 +++++---- src/Renderer/IMarkdownRenderer.php | 6 +- src/Renderer/PHPMarkdownMarkdownRenderer.php | 6 +- 5 files changed, 82 insertions(+), 54 deletions(-) diff --git a/src/Forms/MarkdownEditor.php b/src/Forms/MarkdownEditor.php index 4891144..31820a8 100644 --- a/src/Forms/MarkdownEditor.php +++ b/src/Forms/MarkdownEditor.php @@ -1,4 +1,10 @@ wrap_mode=$mode; return $this; } - + /** * Returns the field holder used by templates * @return {string} HTML to be used */ public function FieldHolder($properties=array()) { $this->extraClasses['stacked']='stacked'; - - + + Requirements::css(MARKDOWN_MODULE_BASE.'/css/MarkdownEditor.css'); - + Requirements::javascript(MARKDOWN_MODULE_BASE.'/javascript/external/ace/ace.js'); Requirements::javascript(MARKDOWN_MODULE_BASE.'/javascript/external/ace/mode-markdown.js'); Requirements::javascript(MARKDOWN_MODULE_BASE.'/javascript/external/ace/theme-textmate.js'); @@ -30,7 +36,7 @@ public function FieldHolder($properties=array()) { Requirements::javascript(MARKDOWN_MODULE_BASE.'/javascript/MarkdownEditor.js'); return parent::FieldHolder($properties); } - + /** * Generates the attributes to be used on the field * @return {array} Array of attributes to be used on the form field @@ -43,6 +49,5 @@ public function getAttributes() { 'wrap-mode'=>($this->wrap_mode) ? "true" : "false" ) ); - } + } } -?> \ No newline at end of file diff --git a/src/Model/FieldTypes/Markdown.php b/src/Model/FieldTypes/Markdown.php index 6dbfadd..082d7e9 100644 --- a/src/Model/FieldTypes/Markdown.php +++ b/src/Model/FieldTypes/Markdown.php @@ -1,20 +1,28 @@ 'HTMLText', - 'Markdown'=>'Text' + 'Markdown'=>'DBText' ); - - + + public static $escape_type='xml'; - - private static $renderer='GithubMarkdownRenderer'; - + + private static $renderer=GithubMarkdownRenderer::class; + private $renderInst; - + protected $parsedHTML=false; - - + + /** * Checks cache to see if the contents of this field have already been loaded from github, if they haven't then a request is made to the github api to render the markdown * @param {bool} $useGFM Use Github Flavored Markdown or render using plain markdown defaults to false just like how readme files are rendered on github @@ -24,7 +32,7 @@ public function AsHTML($useGFM=false) { if($this->parsedHTML!==false) { return $this->parsedHTML; } - + //Setup renderer $renderer=$this->getRenderer(); $supported=$renderer->isSupported(); @@ -32,47 +40,47 @@ public function AsHTML($useGFM=false) { $class_name=get_class($renderer); user_error("Renderer $class_name is not supported on this system: $supported"); } - + if($renderer instanceof GithubMarkdownRenderer) { $beforeUseGFM=GithubMarkdownRenderer::getUseGFM(); - + GithubMarkdownRenderer::setUseGFM($useGFM); } - + //Init cache stuff - $cacheKey=md5('Markdown_'.$this->tableName.'_'.$this->name.':'.$this->value); - $cache=SS_Cache::factory('Markdown'); + $cacheKey=$this->getCacheKey(); + $cache=Cache::factory('Markdown'); $cachedHTML=$cache->load($cacheKey); - + //Check cache, if it's good use it instead if($cachedHTML!==false) { $this->parsedHTML=$cachedHTML; return $this->parsedHTML; - } - + } + //If empty save time by not attempting to render if(empty($this->value)) { return $this->value; } - + //Get rendered HTML $response=$renderer->getRenderedHTML($this->value); - + //Store response in memory $this->parsedHTML=$response; - + //Cache response to file system $cache->save($this->parsedHTML, $cacheKey); - + //Reset GFM if($renderer instanceof GithubMarkdownRenderer) { GithubMarkdownRenderer::setUseGFM($beforeUseGFM); } - + //Return response return $this->parsedHTML; } - + /** * Renders the field used in the template * @return {string} HTML to be used in the template @@ -88,13 +96,13 @@ public function forTemplate() { * @param {string} $renderer Class Name of an implementation of IMarkdownRenderer */ public static function setRenderer($renderer) { - if(ClassInfo::classImplements($renderer, 'IMarkdownRenderer')) { + if(ClassInfo::classImplements($renderer, 'SilverStripe\\Markdown\\Renderer\\IMarkdownRenderer')) { self::$renderer=$renderer; }else { user_error('The renderer '.$renderer.' does not implement IMarkdownRenderer', E_USER_ERROR); } } - + /** * Gets the active mardown renderer * @return {IMarkdownRenderer} An implementation of IMarkdownRenderer @@ -104,8 +112,15 @@ private function getRenderer() { $class=self::$renderer; $this->renderInst=new $class(); } - + return $this->renderInst; } + + /** + * @return string + */ + public function getCacheKey() + { + return md5('Markdown_' . $this->tableName . '_' . $this->name . '_' . $this->value); + } } -?> \ No newline at end of file diff --git a/src/Renderer/GithubMarkdownRenderer.php b/src/Renderer/GithubMarkdownRenderer.php index 69ce0a0..11322da 100644 --- a/src/Renderer/GithubMarkdownRenderer.php +++ b/src/Renderer/GithubMarkdownRenderer.php @@ -1,11 +1,16 @@ text=$value; $sendObj->mode=(self::$useGFM ? 'gmf':'markdown'); $content=json_encode($sendObj); - + //Build headers $headers=array("Content-type: application/json", "User-Agent: curl"); if(self::$useBasicAuth) { $encoded=base64_encode(self::$username.':'.self::$password); $headers[]="Authorization: Basic $encoded"; } - + //Build curl request to github's api $curl=curl_init('https://api.github.com/markdown'); curl_setopt($curl, CURLOPT_HEADER, false); @@ -46,21 +51,21 @@ public function getRenderedHTML($value) { curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $content); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); - - + + //Send request and verify response $response=curl_exec($curl); $status=curl_getinfo($curl, CURLINFO_HTTP_CODE); if($status!=200) { user_error("Error: Call to api.github.com failed with status $status, response $response, curl_error ".curl_error($curl).", curl_errno ".curl_errno($curl), E_USER_WARNING); } - + //Close curl connection curl_close($curl); - + return $response; } - + /** * Globally enable or disable github flavored markdown * @param {bool} $val Boolean true to enable false otherwise @@ -68,7 +73,7 @@ public function getRenderedHTML($value) { public static function setUseGFM($value=true) { self::$useGFM=$value; } - + /** * Gets if github flavored markdown is enabled or not globally * @return {bool} Returns boolean true if github flavored markdown is enabled false otherwise @@ -76,7 +81,7 @@ public static function setUseGFM($value=true) { public static function getUseGFM() { return self::$useGFM; } - + /** * Sets whether or not to include the Authorization header in GitHub API requests, both parameters are required to enable basic auth * @param {string} $username Github Username @@ -88,4 +93,3 @@ public function useBasicAuth($username=false, $password=false) { self::$password=$password; } } -?> \ No newline at end of file diff --git a/src/Renderer/IMarkdownRenderer.php b/src/Renderer/IMarkdownRenderer.php index 4b42297..ac768ed 100644 --- a/src/Renderer/IMarkdownRenderer.php +++ b/src/Renderer/IMarkdownRenderer.php @@ -1,11 +1,14 @@ \ No newline at end of file diff --git a/src/Renderer/PHPMarkdownMarkdownRenderer.php b/src/Renderer/PHPMarkdownMarkdownRenderer.php index f57d370..ecff341 100644 --- a/src/Renderer/PHPMarkdownMarkdownRenderer.php +++ b/src/Renderer/PHPMarkdownMarkdownRenderer.php @@ -1,4 +1,7 @@ \ No newline at end of file From 416bf6cd1429c9950c00cc347c01bb13d7f71b9a Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Mon, 9 Jan 2017 13:10:46 +1300 Subject: [PATCH 4/7] SS4 compat: Run PSR-2 linter - updates whitespace and brackets mostly --- src/Forms/MarkdownEditor.php | 18 +++--- src/Model/FieldTypes/Markdown.php | 33 ++++++----- src/Renderer/GithubMarkdownRenderer.php | 58 +++++++++++--------- src/Renderer/IMarkdownRenderer.php | 25 +++++---- src/Renderer/PHPMarkdownMarkdownRenderer.php | 43 ++++++++------- 5 files changed, 98 insertions(+), 79 deletions(-) diff --git a/src/Forms/MarkdownEditor.php b/src/Forms/MarkdownEditor.php index 31820a8..62a3ed4 100644 --- a/src/Forms/MarkdownEditor.php +++ b/src/Forms/MarkdownEditor.php @@ -5,7 +5,8 @@ use SilverStripe\Forms\TextareaField; use SilverStripe\View\Requirements; -class MarkdownEditor extends TextareaField { +class MarkdownEditor extends TextareaField +{ protected $rows=30; protected $wrap_mode=false; @@ -14,7 +15,8 @@ class MarkdownEditor extends TextareaField { * Sets the "Wrap Mode" on the ACE editor markdown field. * @param boolean $mode True if word wrap should be enabled, false if not */ - public function setWrapMode($mode = false) { + public function setWrapMode($mode = false) + { $this->wrap_mode=$mode; return $this; } @@ -23,7 +25,8 @@ public function setWrapMode($mode = false) { * Returns the field holder used by templates * @return {string} HTML to be used */ - public function FieldHolder($properties=array()) { + public function FieldHolder($properties = array()) + { $this->extraClasses['stacked']='stacked'; @@ -41,13 +44,14 @@ public function FieldHolder($properties=array()) { * Generates the attributes to be used on the field * @return {array} Array of attributes to be used on the form field */ - public function getAttributes() { + public function getAttributes() + { return array_merge( - parent::getAttributes(), - array( + parent::getAttributes(), + array( 'style'=>'width: 97%; max-width: 100%; height: '.($this->rows * 16).'px; resize: none;', // prevents horizontal scrollbars 'wrap-mode'=>($this->wrap_mode) ? "true" : "false" ) - ); + ); } } diff --git a/src/Model/FieldTypes/Markdown.php b/src/Model/FieldTypes/Markdown.php index 082d7e9..a360563 100644 --- a/src/Model/FieldTypes/Markdown.php +++ b/src/Model/FieldTypes/Markdown.php @@ -7,7 +7,8 @@ use SilverStripe\ORM\FieldType\DBText; use UndefinedOffset\Markdown\Renderer\GithubMarkdownRenderer; -class Markdown extends DBText { +class Markdown extends DBText +{ public static $casting=array( 'AsHTML'=>'HTMLText', 'Markdown'=>'DBText' @@ -28,20 +29,21 @@ class Markdown extends DBText { * @param {bool} $useGFM Use Github Flavored Markdown or render using plain markdown defaults to false just like how readme files are rendered on github * @return {string} Markdown rendered as HTML */ - public function AsHTML($useGFM=false) { - if($this->parsedHTML!==false) { + public function AsHTML($useGFM = false) + { + if ($this->parsedHTML!==false) { return $this->parsedHTML; } //Setup renderer $renderer=$this->getRenderer(); $supported=$renderer->isSupported(); - if($supported!==true) { + if ($supported!==true) { $class_name=get_class($renderer); user_error("Renderer $class_name is not supported on this system: $supported"); } - if($renderer instanceof GithubMarkdownRenderer) { + if ($renderer instanceof GithubMarkdownRenderer) { $beforeUseGFM=GithubMarkdownRenderer::getUseGFM(); GithubMarkdownRenderer::setUseGFM($useGFM); @@ -53,13 +55,13 @@ public function AsHTML($useGFM=false) { $cachedHTML=$cache->load($cacheKey); //Check cache, if it's good use it instead - if($cachedHTML!==false) { + if ($cachedHTML!==false) { $this->parsedHTML=$cachedHTML; return $this->parsedHTML; } //If empty save time by not attempting to render - if(empty($this->value)) { + if (empty($this->value)) { return $this->value; } @@ -73,7 +75,7 @@ public function AsHTML($useGFM=false) { $cache->save($this->parsedHTML, $cacheKey); //Reset GFM - if($renderer instanceof GithubMarkdownRenderer) { + if ($renderer instanceof GithubMarkdownRenderer) { GithubMarkdownRenderer::setUseGFM($beforeUseGFM); } @@ -87,7 +89,8 @@ public function AsHTML($useGFM=false) { * * @see GISMarkdown::AsHTML() */ - public function forTemplate() { + public function forTemplate() + { return $this->AsHTML(); } @@ -95,10 +98,11 @@ public function forTemplate() { * Sets the renderer for markdown fields to use * @param {string} $renderer Class Name of an implementation of IMarkdownRenderer */ - public static function setRenderer($renderer) { - if(ClassInfo::classImplements($renderer, 'SilverStripe\\Markdown\\Renderer\\IMarkdownRenderer')) { + public static function setRenderer($renderer) + { + if (ClassInfo::classImplements($renderer, 'SilverStripe\\Markdown\\Renderer\\IMarkdownRenderer')) { self::$renderer=$renderer; - }else { + } else { user_error('The renderer '.$renderer.' does not implement IMarkdownRenderer', E_USER_ERROR); } } @@ -107,8 +111,9 @@ public static function setRenderer($renderer) { * Gets the active mardown renderer * @return {IMarkdownRenderer} An implementation of IMarkdownRenderer */ - private function getRenderer() { - if(!is_object($this->renderInst)) { + private function getRenderer() + { + if (!is_object($this->renderInst)) { $class=self::$renderer; $this->renderInst=new $class(); } diff --git a/src/Renderer/GithubMarkdownRenderer.php b/src/Renderer/GithubMarkdownRenderer.php index 11322da..a615d40 100644 --- a/src/Renderer/GithubMarkdownRenderer.php +++ b/src/Renderer/GithubMarkdownRenderer.php @@ -4,33 +4,36 @@ use stdClass; -class GithubMarkdownRenderer implements IMarkdownRenderer { - private static $useGFM=false; - private static $useBasicAuth=false; - private static $username=null; - private static $password=null; +class GithubMarkdownRenderer implements IMarkdownRenderer +{ + private static $useGFM=false; + private static $useBasicAuth=false; + private static $username=null; + private static $password=null; /** * Detects if curl is supported which is required for this renderer * @return {bool} Detects if curl is supported */ - public function isSupported() { + public function isSupported() + { $supported=function_exists('curl_version'); - if(!$supported) { - $supported='CURL not found'; - } - - return $supported; - } - - /** - * Returns the supplied Markdown as rendered HTML - * @param {string} $markdown The markdown to render - * @return {string} The rendered HTML - */ - public function getRenderedHTML($value) { - //Build object to send + if (!$supported) { + $supported='CURL not found'; + } + + return $supported; + } + + /** + * Returns the supplied Markdown as rendered HTML + * @param {string} $markdown The markdown to render + * @return {string} The rendered HTML + */ + public function getRenderedHTML($value) + { + //Build object to send $sendObj=new stdClass(); $sendObj->text=$value; $sendObj->mode=(self::$useGFM ? 'gmf':'markdown'); @@ -38,7 +41,7 @@ public function getRenderedHTML($value) { //Build headers $headers=array("Content-type: application/json", "User-Agent: curl"); - if(self::$useBasicAuth) { + if (self::$useBasicAuth) { $encoded=base64_encode(self::$username.':'.self::$password); $headers[]="Authorization: Basic $encoded"; } @@ -56,7 +59,7 @@ public function getRenderedHTML($value) { //Send request and verify response $response=curl_exec($curl); $status=curl_getinfo($curl, CURLINFO_HTTP_CODE); - if($status!=200) { + if ($status!=200) { user_error("Error: Call to api.github.com failed with status $status, response $response, curl_error ".curl_error($curl).", curl_errno ".curl_errno($curl), E_USER_WARNING); } @@ -64,13 +67,14 @@ public function getRenderedHTML($value) { curl_close($curl); return $response; - } + } /** * Globally enable or disable github flavored markdown * @param {bool} $val Boolean true to enable false otherwise */ - public static function setUseGFM($value=true) { + public static function setUseGFM($value = true) + { self::$useGFM=$value; } @@ -78,7 +82,8 @@ public static function setUseGFM($value=true) { * Gets if github flavored markdown is enabled or not globally * @return {bool} Returns boolean true if github flavored markdown is enabled false otherwise */ - public static function getUseGFM() { + public static function getUseGFM() + { return self::$useGFM; } @@ -87,7 +92,8 @@ public static function getUseGFM() { * @param {string} $username Github Username * @param {string} $password Github Password */ - public function useBasicAuth($username=false, $password=false) { + public function useBasicAuth($username = false, $password = false) + { self::$useBasicAuth=($username!==false && $password!==false); self::$username=$username; self::$password=$password; diff --git a/src/Renderer/IMarkdownRenderer.php b/src/Renderer/IMarkdownRenderer.php index ac768ed..0f0c9a5 100644 --- a/src/Renderer/IMarkdownRenderer.php +++ b/src/Renderer/IMarkdownRenderer.php @@ -2,17 +2,18 @@ namespace UndefinedOffset\Markdown\Renderer; -interface IMarkdownRenderer { - /** - * Performs the necessary checks to determine if the markdown renderer is supported on the system (eg, check necessary libraries are available etc) - * @return {mixed} True if the renderer is supported, or string error message detailing why the renderer isnt supported - */ - public function isSupported(); +interface IMarkdownRenderer +{ + /** + * Performs the necessary checks to determine if the markdown renderer is supported on the system (eg, check necessary libraries are available etc) + * @return {mixed} True if the renderer is supported, or string error message detailing why the renderer isnt supported + */ + public function isSupported(); - /** - * Returns the supplied Markdown as rendered HTML - * @param {string} $markdown The markdown to render - * @return {string} The rendered HTML - */ - public function getRenderedHTML($markdown); + /** + * Returns the supplied Markdown as rendered HTML + * @param {string} $markdown The markdown to render + * @return {string} The rendered HTML + */ + public function getRenderedHTML($markdown); } diff --git a/src/Renderer/PHPMarkdownMarkdownRenderer.php b/src/Renderer/PHPMarkdownMarkdownRenderer.php index ecff341..0379c9a 100644 --- a/src/Renderer/PHPMarkdownMarkdownRenderer.php +++ b/src/Renderer/PHPMarkdownMarkdownRenderer.php @@ -2,26 +2,29 @@ namespace UndefinedOffset\Markdown\Renderer; -class PHPMarkdownMarkdownRenderer implements IMarkdownRenderer { +class PHPMarkdownMarkdownRenderer implements IMarkdownRenderer +{ /** - * Returns the supplied Markdown as rendered HTML - * @param {string} $markdown The markdown to render - * @return {string} The rendered HTML - */ - public function isSupported() { - $exists=class_exists("\Michelf\Markdown"); - if(!$exists) { - return "Unable to find the php-markdown class (\Michelf\Markdown) on the classpath."; - } - return $exists; - } + * Returns the supplied Markdown as rendered HTML + * @param {string} $markdown The markdown to render + * @return {string} The rendered HTML + */ + public function isSupported() + { + $exists=class_exists("\Michelf\Markdown"); + if (!$exists) { + return "Unable to find the php-markdown class (\Michelf\Markdown) on the classpath."; + } + return $exists; + } - /** - * Returns the supplied Markdown as rendered HTML - * @param {string} $markdown The markdown to render - * @return {string} The rendered HTML - */ - public function getRenderedHTML($markdown) { - return \Michelf\Markdown::defaultTransform($markdown); - } + /** + * Returns the supplied Markdown as rendered HTML + * @param {string} $markdown The markdown to render + * @return {string} The rendered HTML + */ + public function getRenderedHTML($markdown) + { + return \Michelf\Markdown::defaultTransform($markdown); + } } From 8e58433506281d7643997e6739627d6098af5dbc Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Mon, 9 Jan 2017 13:24:32 +1300 Subject: [PATCH 5/7] SS4 compat: Add/fix PHPDoc blocks and spacing around operators --- src/Forms/MarkdownEditor.php | 41 ++++++---- src/Model/FieldTypes/Markdown.php | 78 ++++++++++++-------- src/Renderer/GithubMarkdownRenderer.php | 75 +++++++++++-------- src/Renderer/IMarkdownRenderer.php | 10 ++- src/Renderer/PHPMarkdownMarkdownRenderer.php | 10 +-- 5 files changed, 128 insertions(+), 86 deletions(-) diff --git a/src/Forms/MarkdownEditor.php b/src/Forms/MarkdownEditor.php index 62a3ed4..aaac7ce 100644 --- a/src/Forms/MarkdownEditor.php +++ b/src/Forms/MarkdownEditor.php @@ -7,51 +7,60 @@ class MarkdownEditor extends TextareaField { - protected $rows=30; + /** + * {@inheritDoc} + */ + protected $rows = 30; - protected $wrap_mode=false; + /** + * {@inheritDoc} + */ + protected $wrap_mode = false; /** * Sets the "Wrap Mode" on the ACE editor markdown field. - * @param boolean $mode True if word wrap should be enabled, false if not + * @param boolean $mode True if word wrap should be enabled, false if not + * @return $this */ public function setWrapMode($mode = false) { - $this->wrap_mode=$mode; + $this->wrap_mode = $mode; return $this; } /** * Returns the field holder used by templates - * @return {string} HTML to be used + * @return string HTML to be used */ public function FieldHolder($properties = array()) { - $this->extraClasses['stacked']='stacked'; + $this->extraClasses['stacked'] = 'stacked'; + + Requirements::css(MARKDOWN_MODULE_BASE . '/css/MarkdownEditor.css'); - Requirements::css(MARKDOWN_MODULE_BASE.'/css/MarkdownEditor.css'); + Requirements::javascript(MARKDOWN_MODULE_BASE . '/javascript/external/ace/ace.js'); + Requirements::javascript(MARKDOWN_MODULE_BASE . '/javascript/external/ace/mode-markdown.js'); + Requirements::javascript(MARKDOWN_MODULE_BASE . '/javascript/external/ace/theme-textmate.js'); + Requirements::javascript(MARKDOWN_MODULE_BASE . '/javascript/external/ace/theme-twilight.js'); + Requirements::javascript(MARKDOWN_MODULE_BASE . '/javascript/MarkdownEditor.js'); - Requirements::javascript(MARKDOWN_MODULE_BASE.'/javascript/external/ace/ace.js'); - Requirements::javascript(MARKDOWN_MODULE_BASE.'/javascript/external/ace/mode-markdown.js'); - Requirements::javascript(MARKDOWN_MODULE_BASE.'/javascript/external/ace/theme-textmate.js'); - Requirements::javascript(MARKDOWN_MODULE_BASE.'/javascript/external/ace/theme-twilight.js'); - Requirements::javascript(MARKDOWN_MODULE_BASE.'/javascript/MarkdownEditor.js'); return parent::FieldHolder($properties); } /** * Generates the attributes to be used on the field - * @return {array} Array of attributes to be used on the form field + * @return array Array of attributes to be used on the form field */ public function getAttributes() { return array_merge( parent::getAttributes(), array( - 'style'=>'width: 97%; max-width: 100%; height: '.($this->rows * 16).'px; resize: none;', // prevents horizontal scrollbars - 'wrap-mode'=>($this->wrap_mode) ? "true" : "false" - ) + // prevents horizontal scrollbars + 'style' => 'width: 97%; max-width: 100%; height: ' . ($this->rows * 16) . 'px; resize: none;', + 'wrap-mode' => ($this->wrap_mode) ? 'true' : 'false' + ) ); } } diff --git a/src/Model/FieldTypes/Markdown.php b/src/Model/FieldTypes/Markdown.php index a360563..b4863ca 100644 --- a/src/Model/FieldTypes/Markdown.php +++ b/src/Model/FieldTypes/Markdown.php @@ -9,54 +9,70 @@ class Markdown extends DBText { - public static $casting=array( - 'AsHTML'=>'HTMLText', - 'Markdown'=>'DBText' - ); - + /** + * {@inheritDoc} + */ + public static $casting = array( + 'AsHTML' => 'HTMLText', + 'Markdown' => 'DBText' + ); - public static $escape_type='xml'; + /** + * @var string + */ + public static $escape_type = 'xml'; - private static $renderer=GithubMarkdownRenderer::class; + /** + * @var string + */ + private static $renderer = 'UndefinedOffset\\Markdown\\Renderer\\GithubMarkdownRenderer'; + /** + * @var \UndefinedOffset\Markdown\Renderer\IMarkdownRenderer + */ private $renderInst; - protected $parsedHTML=false; + /** + * @var string + */ + protected $parsedHTML = false; /** - * Checks cache to see if the contents of this field have already been loaded from github, if they haven't then a request is made to the github api to render the markdown - * @param {bool} $useGFM Use Github Flavored Markdown or render using plain markdown defaults to false just like how readme files are rendered on github - * @return {string} Markdown rendered as HTML + * Checks cache to see if the contents of this field have already been loaded from github, if they haven't + * then a request is made to the github api to render the markdown + * @param bool $useGFM Use Github Flavored Markdown or render using plain markdown defaults to false just like + * how readme files are rendered on github + * @return string Markdown rendered as HTML */ public function AsHTML($useGFM = false) { - if ($this->parsedHTML!==false) { + if ($this->parsedHTML !== false) { return $this->parsedHTML; } //Setup renderer - $renderer=$this->getRenderer(); - $supported=$renderer->isSupported(); - if ($supported!==true) { - $class_name=get_class($renderer); + $renderer = $this->getRenderer(); + $supported = $renderer->isSupported(); + if ($supported !== true) { + $class_name = get_class($renderer); user_error("Renderer $class_name is not supported on this system: $supported"); } if ($renderer instanceof GithubMarkdownRenderer) { - $beforeUseGFM=GithubMarkdownRenderer::getUseGFM(); + $beforeUseGFM = GithubMarkdownRenderer::getUseGFM(); GithubMarkdownRenderer::setUseGFM($useGFM); } //Init cache stuff - $cacheKey=$this->getCacheKey(); - $cache=Cache::factory('Markdown'); - $cachedHTML=$cache->load($cacheKey); + $cacheKey = $this->getCacheKey(); + $cache = Cache::factory('Markdown'); + $cachedHTML = $cache->load($cacheKey); //Check cache, if it's good use it instead - if ($cachedHTML!==false) { - $this->parsedHTML=$cachedHTML; + if ($cachedHTML !== false) { + $this->parsedHTML = $cachedHTML; return $this->parsedHTML; } @@ -66,10 +82,10 @@ public function AsHTML($useGFM = false) } //Get rendered HTML - $response=$renderer->getRenderedHTML($this->value); + $response = $renderer->getRenderedHTML($this->value); //Store response in memory - $this->parsedHTML=$response; + $this->parsedHTML = $response; //Cache response to file system $cache->save($this->parsedHTML, $cacheKey); @@ -85,7 +101,7 @@ public function AsHTML($useGFM = false) /** * Renders the field used in the template - * @return {string} HTML to be used in the template + * @return string HTML to be used in the template * * @see GISMarkdown::AsHTML() */ @@ -96,26 +112,26 @@ public function forTemplate() /** * Sets the renderer for markdown fields to use - * @param {string} $renderer Class Name of an implementation of IMarkdownRenderer + * @param string $renderer Class Name of an implementation of IMarkdownRenderer */ public static function setRenderer($renderer) { if (ClassInfo::classImplements($renderer, 'SilverStripe\\Markdown\\Renderer\\IMarkdownRenderer')) { - self::$renderer=$renderer; + self::$renderer = $renderer; } else { - user_error('The renderer '.$renderer.' does not implement IMarkdownRenderer', E_USER_ERROR); + user_error('The renderer ' . $renderer . ' does not implement IMarkdownRenderer', E_USER_ERROR); } } /** * Gets the active mardown renderer - * @return {IMarkdownRenderer} An implementation of IMarkdownRenderer + * @return IMarkdownRenderer An implementation of IMarkdownRenderer */ private function getRenderer() { if (!is_object($this->renderInst)) { - $class=self::$renderer; - $this->renderInst=new $class(); + $class = self::$renderer; + $this->renderInst = new $class(); } return $this->renderInst; diff --git a/src/Renderer/GithubMarkdownRenderer.php b/src/Renderer/GithubMarkdownRenderer.php index a615d40..341d8b5 100644 --- a/src/Renderer/GithubMarkdownRenderer.php +++ b/src/Renderer/GithubMarkdownRenderer.php @@ -6,21 +6,31 @@ class GithubMarkdownRenderer implements IMarkdownRenderer { - private static $useGFM=false; - private static $useBasicAuth=false; - private static $username=null; - private static $password=null; + /** + * @var bool + */ + private static $useGFM = false; + /** + * @var bool + */ + private static $useBasicAuth = false; + + /** + * @var string + */ + private static $username; + private static $password; /** * Detects if curl is supported which is required for this renderer - * @return {bool} Detects if curl is supported + * @return bool Detects if curl is supported */ public function isSupported() { - $supported=function_exists('curl_version'); + $supported = function_exists('curl_version'); if (!$supported) { - $supported='CURL not found'; + $supported = 'CURL not found'; } return $supported; @@ -28,26 +38,26 @@ public function isSupported() /** * Returns the supplied Markdown as rendered HTML - * @param {string} $markdown The markdown to render - * @return {string} The rendered HTML + * @param string $markdown The markdown to render + * @return string The rendered HTML */ public function getRenderedHTML($value) { //Build object to send - $sendObj=new stdClass(); - $sendObj->text=$value; - $sendObj->mode=(self::$useGFM ? 'gmf':'markdown'); - $content=json_encode($sendObj); + $sendObj = new stdClass(); + $sendObj->text = $value; + $sendObj->mode = (self::$useGFM ? 'gmf' : 'markdown'); + $content = json_encode($sendObj); //Build headers - $headers=array("Content-type: application/json", "User-Agent: curl"); + $headers = array('Content-type: application/json', 'User-Agent: curl'); if (self::$useBasicAuth) { - $encoded=base64_encode(self::$username.':'.self::$password); - $headers[]="Authorization: Basic $encoded"; + $encoded = base64_encode(self::$username . ':' . self::$password); + $headers[] = "Authorization: Basic $encoded"; } //Build curl request to github's api - $curl=curl_init('https://api.github.com/markdown'); + $curl = curl_init('https://api.github.com/markdown'); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); @@ -57,10 +67,14 @@ public function getRenderedHTML($value) //Send request and verify response - $response=curl_exec($curl); - $status=curl_getinfo($curl, CURLINFO_HTTP_CODE); - if ($status!=200) { - user_error("Error: Call to api.github.com failed with status $status, response $response, curl_error ".curl_error($curl).", curl_errno ".curl_errno($curl), E_USER_WARNING); + $response = curl_exec($curl); + $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); + if ($status != 200) { + user_error( + "Error: Call to api.github.com failed with status $status, response $response, curl_error " + . curl_error($curl) . ", curl_errno " . curl_errno($curl), + E_USER_WARNING + ); } //Close curl connection @@ -71,16 +85,16 @@ public function getRenderedHTML($value) /** * Globally enable or disable github flavored markdown - * @param {bool} $val Boolean true to enable false otherwise + * @param bool $val Boolean true to enable false otherwise */ public static function setUseGFM($value = true) { - self::$useGFM=$value; + self::$useGFM = $value; } /** * Gets if github flavored markdown is enabled or not globally - * @return {bool} Returns boolean true if github flavored markdown is enabled false otherwise + * @return bool Returns boolean true if github flavored markdown is enabled false otherwise */ public static function getUseGFM() { @@ -88,14 +102,15 @@ public static function getUseGFM() } /** - * Sets whether or not to include the Authorization header in GitHub API requests, both parameters are required to enable basic auth - * @param {string} $username Github Username - * @param {string} $password Github Password + * Sets whether or not to include the Authorization header in GitHub API requests, both parameters are + * required to enable basic auth + * @param string $username Github Username + * @param string $password Github Password */ public function useBasicAuth($username = false, $password = false) { - self::$useBasicAuth=($username!==false && $password!==false); - self::$username=$username; - self::$password=$password; + self::$useBasicAuth = ($username !== false && $password !== false); + self::$username = $username; + self::$password = $password; } } diff --git a/src/Renderer/IMarkdownRenderer.php b/src/Renderer/IMarkdownRenderer.php index 0f0c9a5..528a3ce 100644 --- a/src/Renderer/IMarkdownRenderer.php +++ b/src/Renderer/IMarkdownRenderer.php @@ -5,15 +5,17 @@ interface IMarkdownRenderer { /** - * Performs the necessary checks to determine if the markdown renderer is supported on the system (eg, check necessary libraries are available etc) - * @return {mixed} True if the renderer is supported, or string error message detailing why the renderer isnt supported + * Performs the necessary checks to determine if the markdown renderer is supported on the system (eg, check + * necessary libraries are available etc) + * @return mixed True if the renderer is supported, or string error message detailing why the renderer + * isnt supported */ public function isSupported(); /** * Returns the supplied Markdown as rendered HTML - * @param {string} $markdown The markdown to render - * @return {string} The rendered HTML + * @param string $markdown The markdown to render + * @return string The rendered HTML */ public function getRenderedHTML($markdown); } diff --git a/src/Renderer/PHPMarkdownMarkdownRenderer.php b/src/Renderer/PHPMarkdownMarkdownRenderer.php index 0379c9a..7518990 100644 --- a/src/Renderer/PHPMarkdownMarkdownRenderer.php +++ b/src/Renderer/PHPMarkdownMarkdownRenderer.php @@ -6,12 +6,12 @@ class PHPMarkdownMarkdownRenderer implements IMarkdownRenderer { /** * Returns the supplied Markdown as rendered HTML - * @param {string} $markdown The markdown to render - * @return {string} The rendered HTML + * @param string $markdown The markdown to render + * @return string The rendered HTML */ public function isSupported() { - $exists=class_exists("\Michelf\Markdown"); + $exist s =class_exists("\Michelf\Markdown"); if (!$exists) { return "Unable to find the php-markdown class (\Michelf\Markdown) on the classpath."; } @@ -20,8 +20,8 @@ public function isSupported() /** * Returns the supplied Markdown as rendered HTML - * @param {string} $markdown The markdown to render - * @return {string} The rendered HTML + * @param string $markdown The markdown to render + * @return string The rendered HTML */ public function getRenderedHTML($markdown) { From 667c1cdea178a834614277e2bdf83710fcebb31d Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Tue, 10 Jan 2017 10:01:48 +1300 Subject: [PATCH 6/7] FIX Composer alias for ss4-compat branch --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 042a7ea..f3749b3 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "extra": { "installer-name": "markdown", "branch-alias": { - "dev-master": "2.x-dev" + "dev-ss4-compat": "2.x-dev" } }, "autoload": { From 87cba2a08ccfc2d3ef8d83440cb4ca174eecce68 Mon Sep 17 00:00:00 2001 From: Nic Horstmeier Date: Thu, 30 May 2019 15:20:59 -0500 Subject: [PATCH 7/7] UPDATE additional SS4 compatability --- _config.php | 2 - _config/injector.yml | 2 +- composer.json | 15 ++++--- src/Forms/MarkdownEditor.php | 28 ++++++------ .../{Markdown.php => DBMarkdown.php} | 43 +++++++++++-------- src/Renderer/GithubMarkdownRenderer.php | 26 +++++++---- src/Renderer/IMarkdownRenderer.php | 6 ++- src/Renderer/PHPMarkdownMarkdownRenderer.php | 15 +++++-- 8 files changed, 87 insertions(+), 50 deletions(-) delete mode 100644 _config.php rename src/Model/FieldTypes/{Markdown.php => DBMarkdown.php} (74%) diff --git a/_config.php b/_config.php deleted file mode 100644 index a3a7032..0000000 --- a/_config.php +++ /dev/null @@ -1,2 +0,0 @@ -wrap_mode = $mode; + return $this; } @@ -32,18 +37,17 @@ public function setWrapMode($mode = false) * Returns the field holder used by templates * @return string HTML to be used */ - public function FieldHolder($properties = array()) + public function FieldHolder($properties = []) { $this->extraClasses['stacked'] = 'stacked'; + Requirements::css('undefinedoffset/silverstripe-markdown: css/MarkdownEditor.css'); - Requirements::css(MARKDOWN_MODULE_BASE . '/css/MarkdownEditor.css'); - - Requirements::javascript(MARKDOWN_MODULE_BASE . '/javascript/external/ace/ace.js'); - Requirements::javascript(MARKDOWN_MODULE_BASE . '/javascript/external/ace/mode-markdown.js'); - Requirements::javascript(MARKDOWN_MODULE_BASE . '/javascript/external/ace/theme-textmate.js'); - Requirements::javascript(MARKDOWN_MODULE_BASE . '/javascript/external/ace/theme-twilight.js'); - Requirements::javascript(MARKDOWN_MODULE_BASE . '/javascript/MarkdownEditor.js'); + Requirements::javascript('undefinedoffset/silverstripe-markdown: javascript/external/ace/ace.js'); + Requirements::javascript('undefinedoffset/silverstripe-markdown: javascript/external/ace/mode-markdown.js'); + Requirements::javascript('undefinedoffset/silverstripe-markdown: javascript/external/ace/theme-textmate.js'); + Requirements::javascript('undefinedoffset/silverstripe-markdown: javascript/external/ace/theme-twilight.js'); + Requirements::javascript('undefinedoffset/silverstripe-markdown: javascript/MarkdownEditor.js'); return parent::FieldHolder($properties); } @@ -56,11 +60,11 @@ public function getAttributes() { return array_merge( parent::getAttributes(), - array( + [ // prevents horizontal scrollbars 'style' => 'width: 97%; max-width: 100%; height: ' . ($this->rows * 16) . 'px; resize: none;', - 'wrap-mode' => ($this->wrap_mode) ? 'true' : 'false' - ) + 'wrap-mode' => ($this->wrap_mode) ? 'true' : 'false', + ] ); } } diff --git a/src/Model/FieldTypes/Markdown.php b/src/Model/FieldTypes/DBMarkdown.php similarity index 74% rename from src/Model/FieldTypes/Markdown.php rename to src/Model/FieldTypes/DBMarkdown.php index b4863ca..aa1f11a 100644 --- a/src/Model/FieldTypes/Markdown.php +++ b/src/Model/FieldTypes/DBMarkdown.php @@ -2,33 +2,39 @@ namespace UndefinedOffset\Markdown\Model\FieldTypes; -use SilverStripe\Core\Cache; +use Psr\SimpleCache\CacheInterface; use SilverStripe\Core\ClassInfo; +use SilverStripe\Core\Injector\Injector; use SilverStripe\ORM\FieldType\DBText; use UndefinedOffset\Markdown\Renderer\GithubMarkdownRenderer; +use UndefinedOffset\Markdown\Renderer\IMarkdownRenderer; -class Markdown extends DBText +/** + * Class Markdown + * @package UndefinedOffset\Markdown\Model\FieldTypes + */ +class DBMarkdown extends DBText { /** * {@inheritDoc} */ - public static $casting = array( + private static $casting = [ 'AsHTML' => 'HTMLText', - 'Markdown' => 'DBText' - ); + 'Markdown' => 'DBText', + ]; /** * @var string */ - public static $escape_type = 'xml'; + private static $escape_type = 'xml'; /** * @var string */ - private static $renderer = 'UndefinedOffset\\Markdown\\Renderer\\GithubMarkdownRenderer'; + private static $renderer = GithubMarkdownRenderer::class; /** - * @var \UndefinedOffset\Markdown\Renderer\IMarkdownRenderer + * @var IMarkdownRenderer */ private $renderInst; @@ -41,7 +47,7 @@ class Markdown extends DBText /** * Checks cache to see if the contents of this field have already been loaded from github, if they haven't * then a request is made to the github api to render the markdown - * @param bool $useGFM Use Github Flavored Markdown or render using plain markdown defaults to false just like + * @param bool $useGFM Use Github Flavored Markdown or render using plain markdown defaults to false just like * how readme files are rendered on github * @return string Markdown rendered as HTML */ @@ -66,13 +72,14 @@ public function AsHTML($useGFM = false) } //Init cache stuff - $cacheKey = $this->getCacheKey(); - $cache = Cache::factory('Markdown'); - $cachedHTML = $cache->load($cacheKey); + /*$cacheKey = $this->getCacheKey(); + $cache = Injector::inst()->get(CacheInterface::class . '.markdown'); + $cachedHTML = $cache->load($cacheKey);//*/ //Check cache, if it's good use it instead - if ($cachedHTML !== false) { + if (isset($cachedHTML) && $cachedHTML !== false) { $this->parsedHTML = $cachedHTML; + return $this->parsedHTML; } @@ -88,10 +95,12 @@ public function AsHTML($useGFM = false) $this->parsedHTML = $response; //Cache response to file system - $cache->save($this->parsedHTML, $cacheKey); + if (isset($cache) && isset($cacheKey)) { + $cache->save($this->parsedHTML, $cacheKey); + } //Reset GFM - if ($renderer instanceof GithubMarkdownRenderer) { + if ($renderer instanceof GithubMarkdownRenderer && isset($beforeUseGFM)) { GithubMarkdownRenderer::setUseGFM($beforeUseGFM); } @@ -116,7 +125,7 @@ public function forTemplate() */ public static function setRenderer($renderer) { - if (ClassInfo::classImplements($renderer, 'SilverStripe\\Markdown\\Renderer\\IMarkdownRenderer')) { + if (ClassInfo::classImplements($renderer, IMarkdownRenderer::class)) { self::$renderer = $renderer; } else { user_error('The renderer ' . $renderer . ' does not implement IMarkdownRenderer', E_USER_ERROR); @@ -124,7 +133,7 @@ public static function setRenderer($renderer) } /** - * Gets the active mardown renderer + * Gets the active markdown renderer * @return IMarkdownRenderer An implementation of IMarkdownRenderer */ private function getRenderer() diff --git a/src/Renderer/GithubMarkdownRenderer.php b/src/Renderer/GithubMarkdownRenderer.php index 341d8b5..c0d12ac 100644 --- a/src/Renderer/GithubMarkdownRenderer.php +++ b/src/Renderer/GithubMarkdownRenderer.php @@ -2,10 +2,17 @@ namespace UndefinedOffset\Markdown\Renderer; +use SilverStripe\Core\Config\Configurable; use stdClass; +/** + * Class GithubMarkdownRenderer + * @package UndefinedOffset\Markdown\Renderer + */ class GithubMarkdownRenderer implements IMarkdownRenderer { + use Configurable; + /** * @var bool */ @@ -20,6 +27,10 @@ class GithubMarkdownRenderer implements IMarkdownRenderer * @var string */ private static $username; + + /** + * @var + */ private static $password; /** @@ -38,7 +49,7 @@ public function isSupported() /** * Returns the supplied Markdown as rendered HTML - * @param string $markdown The markdown to render + * @param string $value The markdown to render * @return string The rendered HTML */ public function getRenderedHTML($value) @@ -46,11 +57,11 @@ public function getRenderedHTML($value) //Build object to send $sendObj = new stdClass(); $sendObj->text = $value; - $sendObj->mode = (self::$useGFM ? 'gmf' : 'markdown'); + $sendObj->mode = (self::$useGFM ? 'gfm' : 'markdown'); $content = json_encode($sendObj); //Build headers - $headers = array('Content-type: application/json', 'User-Agent: curl'); + $headers = ['Content-type: application/json', 'User-Agent: curl']; if (self::$useBasicAuth) { $encoded = base64_encode(self::$username . ':' . self::$password); $headers[] = "Authorization: Basic $encoded"; @@ -65,14 +76,13 @@ public function getRenderedHTML($value) curl_setopt($curl, CURLOPT_POSTFIELDS, $content); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); - //Send request and verify response $response = curl_exec($curl); $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($status != 200) { user_error( "Error: Call to api.github.com failed with status $status, response $response, curl_error " - . curl_error($curl) . ", curl_errno " . curl_errno($curl), + . curl_error($curl) . ", curl_errno " . curl_errno($curl), E_USER_WARNING ); } @@ -85,7 +95,7 @@ public function getRenderedHTML($value) /** * Globally enable or disable github flavored markdown - * @param bool $val Boolean true to enable false otherwise + * @param bool $value Boolean true to enable false otherwise */ public static function setUseGFM($value = true) { @@ -104,8 +114,8 @@ public static function getUseGFM() /** * Sets whether or not to include the Authorization header in GitHub API requests, both parameters are * required to enable basic auth - * @param string $username Github Username - * @param string $password Github Password + * @param bool|string $username Github Username + * @param bool|string $password Github Password */ public function useBasicAuth($username = false, $password = false) { diff --git a/src/Renderer/IMarkdownRenderer.php b/src/Renderer/IMarkdownRenderer.php index 528a3ce..552b110 100644 --- a/src/Renderer/IMarkdownRenderer.php +++ b/src/Renderer/IMarkdownRenderer.php @@ -2,6 +2,10 @@ namespace UndefinedOffset\Markdown\Renderer; +/** + * Interface IMarkdownRenderer + * @package UndefinedOffset\Markdown\Renderer + */ interface IMarkdownRenderer { /** @@ -14,7 +18,7 @@ public function isSupported(); /** * Returns the supplied Markdown as rendered HTML - * @param string $markdown The markdown to render + * @param string $markdown The markdown to render * @return string The rendered HTML */ public function getRenderedHTML($markdown); diff --git a/src/Renderer/PHPMarkdownMarkdownRenderer.php b/src/Renderer/PHPMarkdownMarkdownRenderer.php index 7518990..485155a 100644 --- a/src/Renderer/PHPMarkdownMarkdownRenderer.php +++ b/src/Renderer/PHPMarkdownMarkdownRenderer.php @@ -2,29 +2,36 @@ namespace UndefinedOffset\Markdown\Renderer; +use Michelf\Markdown; + +/** + * Class PHPMarkdownMarkdownRenderer + * @package UndefinedOffset\Markdown\Renderer + */ class PHPMarkdownMarkdownRenderer implements IMarkdownRenderer { /** * Returns the supplied Markdown as rendered HTML - * @param string $markdown The markdown to render + * @param string $markdown The markdown to render * @return string The rendered HTML */ public function isSupported() { - $exist s =class_exists("\Michelf\Markdown"); + $exists = class_exists(Markdown::class); if (!$exists) { return "Unable to find the php-markdown class (\Michelf\Markdown) on the classpath."; } + return $exists; } /** * Returns the supplied Markdown as rendered HTML - * @param string $markdown The markdown to render + * @param string $markdown The markdown to render * @return string The rendered HTML */ public function getRenderedHTML($markdown) { - return \Michelf\Markdown::defaultTransform($markdown); + return Markdown::defaultTransform($markdown); } }