Skip to content

Commit

Permalink
Minor code cleanups and removal of duplicate code or unused code.
Browse files Browse the repository at this point in the history
- Removed unneeded/unused code
- Added docblocks to methods
- Added use statements instead of using inline FQN
- Ran PHP-CS-Fixer to make it PSR-2 compliant
- Changed all arrays to shorthand (preferred syntax, and more consistent over all)
  • Loading branch information
Simon Erkelens committed Oct 25, 2017
1 parent 94ec354 commit e6141dd
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 302 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ To use the markdown DB field in your data objects the basic code would look like
class MyDataClass extends DataObject
{
private static $db = array(
private static $db = [
'MarkdownContent' => 'MarkdownText'
);
];
}
Expand All @@ -41,9 +41,9 @@ public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldsToTab('Root.Sidebar', array(
$fields->addFieldsToTab('Root.Sidebar', [
\SilverStripers\markdown\forms\MarkdownEditorField::create('MarkdownContent', 'Content'),
));
]);
return $fields;
}
Expand Down
22 changes: 12 additions & 10 deletions _config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@
* To change this template use File | Settings | File Templates.
*/

use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Core\Config\Config;
use SilverStripers\markdown\db\MarkdownText;
use SilverStripers\markdown\shortcodes\MarkdownImageShortcodeProvider;
use SilverStripe\Assets\Shortcodes\FileShortcodeProvider;
use SilverStripe\Assets\Shortcodes\ImageShortcodeProvider;
use SilverStripe\View\Parsers\ShortcodeParser;

$asBase = \SilverStripe\Core\Config\Config::inst()->get(\SilverStripers\markdown\db\MarkdownText::class, 'markdown_as_base');
if($asBase) {
$siteTreeDB = \SilverStripe\Core\Config\Config::inst()->get(\SilverStripe\CMS\Model\SiteTree::class, 'db');
$siteTreeDB['Content'] = \SilverStripers\markdown\db\MarkdownText::class;
\SilverStripe\Core\Config\Config::modify()
->set(\SilverStripe\CMS\Model\SiteTree::class, 'db', $siteTreeDB);
$asBase = Config::inst()->get(MarkdownText::class, 'markdown_as_base');
if ($asBase) {
$siteTreeDB = Config::inst()->get(SiteTree::class, 'db');
$siteTreeDB['Content'] = MarkdownText::class;
Config::modify()
->set(SiteTree::class, 'db', $siteTreeDB);
}


ShortcodeParser::get('default')
->register('image', [ImageShortcodeProvider::class, 'handle_shortcode'])
->register('image_link', [MarkdownImageShortcodeProvider::class, 'handle_shortcode'])
->register('file_link', [FileShortcodeProvider::class, 'handle_shortcode']);

->register('image', [ImageShortcodeProvider::class, 'handle_shortcode'])
->register('image_link', [MarkdownImageShortcodeProvider::class, 'handle_shortcode'])
->register('file_link', [FileShortcodeProvider::class, 'handle_shortcode']);
2 changes: 1 addition & 1 deletion _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ SilverStripe\Admin\LeftAndMain:
- SilverStripers\markdown\extensions\MarkdownFieldExtension
SilverStripe\CMS\Model\SiteTree:
extensions:
- SilverStripers\markdown\extensions\MarkdownSiteTreeExtension
- SilverStripers\markdown\extensions\MarkdownSiteTreeExtension
90 changes: 43 additions & 47 deletions src/db/MarkdownText.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,51 @@
* Time: 11:11 AM
* To change this template use File | Settings | File Templates.
*/
namespace SilverStripers\markdown\db;


namespace SilverStripers\markdown\db;

use cebe\markdown\GithubMarkdown;
use SilverStripe\Forms\FormField;
use SilverStripe\Forms\NullableField;
use SilverStripe\Forms\TextareaField;
use SilverStripe\Forms\TextField;
use SilverStripe\ORM\FieldType\DBText;
use SilverStripe\View\Parsers\ShortcodeParser;
use cebe\markdown\GithubMarkdown;
use SilverStripers\markdown\MarkdownEditorField;
use SilverStripers\markdown\forms\MarkdownEditorField;

class MarkdownText extends \SilverStripe\ORM\FieldType\DBText
class MarkdownText extends DBText
{

private static $escape_type = 'xml';

private static $casting = array(
"AbsoluteLinks" => "HTMLText",
"BigSummary" => "HTMLText",
"ContextSummary" => "HTMLText",
"FirstParagraph" => "HTMLText",
"FirstSentence" => "HTMLText",
"LimitCharacters" => "HTMLText",
"LimitSentences" => "HTMLText",
"Lower" => "HTMLText",
"LowerCase" => "HTMLText",
"Summary" => "HTMLText",
"Upper" => "HTMLText",
"UpperCase" => "HTMLText",
'EscapeXML' => 'HTMLText',
'LimitWordCount' => 'HTMLText',
private static $casting = [
'AbsoluteLinks' => 'HTMLText',
'BigSummary' => 'HTMLText',
'ContextSummary' => 'HTMLText',
'FirstParagraph' => 'HTMLText',
'FirstSentence' => 'HTMLText',
'LimitCharacters' => 'HTMLText',
'LimitSentences' => 'HTMLText',
'Lower' => 'HTMLText',
'LowerCase' => 'HTMLText',
'Summary' => 'HTMLText',
'Upper' => 'HTMLText',
'UpperCase' => 'HTMLText',
'EscapeXML' => 'HTMLText',
'LimitWordCount' => 'HTMLText',
'LimitWordCountXML' => 'HTMLText',
'NoHTML' => 'Text',
);
'NoHTML' => 'Text',
];

private static $markdown_as_base = false;

private $parsedContent;
private $shortcodes = array();
private $shortcodes = [];


/**
* @return string
* @param bool $bCache
* @param string $strValue
* @return string parse contents of the markdown field to tempates
* parse contents of the markdown field to tempates
*/
public function ParseMarkdown($bCache = true, $strValue = '')
Expand All @@ -57,24 +61,26 @@ public function ParseMarkdown($bCache = true, $strValue = '')

$parsed = !empty($strValue) ? $strValue : $this->value;

$this->shortcodes = array();
$this->shortcodes = [];

// shortcodes
$regexes = array(
$regexes = [
'/\[image_link*\s[a-z|A-Z|0-9\s\=]*\]/',
'/\[file_link\,[a-z|A-Z|0-9\s\=]*\]/'
);
];

foreach ($regexes as $pattern) {
preg_match_all($pattern, $parsed, $matches);
if(!empty($matches[0])) foreach ($matches[0] as $attachment) {
$this->shortcodes[md5($attachment)] = $attachment;
$parsed = str_replace($attachment, md5($attachment), $parsed);
if (!empty($matches[0])) {
foreach ($matches[0] as $attachment) {
$this->shortcodes[md5($attachment)] = $attachment;
$parsed = str_replace($attachment, md5($attachment), $parsed);
}
}
}

$parseDown = new GithubMarkdown();
$parsed = $parseDown->parse($parsed);
$parsed = $parseDown->parse($parsed);

foreach ($this->shortcodes as $key => $shortcode) {
$parsed = str_replace($key, $shortcode, $parsed);
Expand All @@ -85,11 +91,11 @@ public function ParseMarkdown($bCache = true, $strValue = '')
$parsed = $shortCodeParser->parse($parsed);

$this->parsedContent = $parsed;

return $parsed;
}



/**
* @return string
*/
Expand Down Expand Up @@ -117,18 +123,6 @@ public function scaffoldFormField($title = null, $params = null)
return new MarkdownEditorField($this->name, $title);
}


/**
* @param null $title
* @param null $params
* @return FormField|TextField
*/
public function scaffoldSearchField($title = null, $params = null)
{
return new TextField($this->name, $title);
}


/**
* @return string
*/
Expand All @@ -143,6 +137,7 @@ public function NoHTML()
public function Upper()
{
$strValue = strtoupper($this->__toString());

return $this->ParseMarkdown(false, $strValue);
}

Expand All @@ -161,6 +156,7 @@ public function UpperCase()
public function Lower()
{
$strValue = strtolower($this->__toString());

return $this->ParseMarkdown(false, $strValue);
}

Expand Down
16 changes: 6 additions & 10 deletions src/extensions/MarkdownFieldExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@

namespace SilverStripers\markdown\extensions;


use SilverStripe\Core\Extension;
use SilverStripe\View\Requirements;

class MarkdownFieldExtension extends Extension
{

public function init()
{
Requirements::javascript('silverstripers/silverstripe-markdown:client/dist/bundle.min.js');
Requirements::css('silverstripers/silverstripe-markdown:client/dist/bundle.min.css');

}

}
public function init()
{
Requirements::javascript('silverstripers/silverstripe-markdown:client/dist/bundle.min.js');
Requirements::css('silverstripers/silverstripe-markdown:client/dist/bundle.min.css');
}
}
10 changes: 5 additions & 5 deletions src/extensions/MarkdownSiteTreeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace SilverStripers\markdown\extensions;


use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\DataExtension;
Expand All @@ -17,12 +16,13 @@

class MarkdownSiteTreeExtension extends DataExtension
{

/**
* @param FieldList $fields
*/
public function updateCMSFields(FieldList $fields)
{
if(Config::inst()->get(MarkdownText::class, 'markdown_as_base')) {
if (Config::inst()->get(MarkdownText::class, 'markdown_as_base')) {
$fields->replaceField('Content', MarkdownEditorField::create('Content'));
}
}

}
}
Loading

0 comments on commit e6141dd

Please sign in to comment.