Skip to content

Commit

Permalink
Fix DBHTMLText summaries
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Mooyman committed Jun 15, 2016
1 parent fa70697 commit cdfcffb
Show file tree
Hide file tree
Showing 6 changed files with 363 additions and 109 deletions.
6 changes: 3 additions & 3 deletions model/FieldType/DBHTMLText.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,12 @@ public function Plain() {
// Convert paragraph breaks to multi-lines
$text = preg_replace('/\<\/p\>/i', "\n\n", $text);

// Implode >3 consecutive linebreaks into 2
$text = preg_replace('/(\n){3,}/', "\n\n", $text);

// Strip out HTML tags
$text = strip_tags($text);

// Implode >3 consecutive linebreaks into 2
$text = preg_replace('~(\R){2,}~', "\n\n", $text);

// Decode HTML entities back to plain text
return trim(\Convert::xml2raw($text));
}
Expand Down
13 changes: 8 additions & 5 deletions model/FieldType/DBText.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,29 +114,32 @@ public function FirstSentence() {
* Builds a basic summary, up to a maximum number of words
*
* @param int $maxWords
* @param string $add
* @return string
*/
public function Summary($maxWords = 50) {
public function Summary($maxWords = 50, $add = '...') {
// Get plain-text version
$value = $this->Plain();
if(!$value) {
return '';
}

// Find sentences
$sentences = explode('.', $value);
// Split on sentences (don't remove period)
$sentences = array_filter(array_map(function($str) {
return trim($str);
}, preg_split('@(?<=\.)@', $value)));
$wordCount = count(preg_split('#\s+#', $sentences[0]));

// if the first sentence is too long, show only the first $maxWords words
if($wordCount > $maxWords) {
return implode( ' ', array_slice(explode( ' ', $sentences[0] ), 0, $maxWords)) . '...';
return implode( ' ', array_slice(explode( ' ', $sentences[0] ), 0, $maxWords)) . $add;
}

// add each sentence while there are enough words to do so
$result = '';
do {
// Add next sentence
$result .= ' ' . trim(array_shift( $sentences )).'.';
$result .= ' ' . array_shift( $sentences );

// If more sentences to process, count number of words
if($sentences) {
Expand Down
13 changes: 3 additions & 10 deletions parsers/BBCodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static function usable_tags() {
);
}

public function useable_tagsHTML(){
public function useable_tagsHTML() {
$useabletags = "<ul class='bbcodeExamples'>";
foreach($this->usable_tags()->toArray() as $tag){
$useabletags = $useabletags."<li><span>".$tag->Example."</span></li>";
Expand All @@ -118,20 +118,13 @@ public function useable_tagsHTML(){
* @return DBField
*/
public function parse() {
$this->content = str_replace(array('&', '<', '>'), array('&amp;', '&lt;', '&gt;'), $this->content);
// Convert content to plain text
$this->content = DBField::create_field('HTMLFragment', $this->content)->Plain();

$p = new SSHTMLBBCodeParser();
$this->content = $p->qparse($this->content);
unset($p);

$this->content = "<p>".$this->content."</p>";

$this->content = preg_replace('/(<p[^>]*>)\s+/i', '\\1', $this->content);
$this->content = preg_replace('/\s+(<\/p[^>]*>)/i', '\\1', $this->content);

$this->content = preg_replace("/\n\s*\n/", "</p><p>", $this->content);
$this->content = str_replace("\n", "<br />", $this->content);

if($this->config()->allow_smilies) {
$smilies = array(
'#(?<!\w):D(?!\w)#i' => " <img src='".BBCodeParser::smilies_location(). "/grin.gif'> ", // :D
Expand Down
2 changes: 2 additions & 0 deletions parsers/TextParser.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
use SilverStripe\Model\FieldType\DBField;

/**
* Parses text in a variety of ways.
*
Expand Down
Loading

0 comments on commit cdfcffb

Please sign in to comment.