MINOR Formatting

This commit is contained in:
Ingo Schommer 2012-01-30 18:28:15 +01:00
parent 008ecf750a
commit 0b0d42c24e

View File

@ -135,47 +135,39 @@ class Text extends StringField {
* Caution: Not XML/HTML-safe - does not respect closing tags. * Caution: Not XML/HTML-safe - does not respect closing tags.
*/ */
function Summary($maxWords = 50) { function Summary($maxWords = 50) {
// get first sentence? // get first sentence?
// this needs to be more robust // this needs to be more robust
$data = Convert::xml2raw( $this->value /*, true*/ ); $value = Convert::xml2raw( $this->value /*, true*/ );
if(!$value) return '';
if( !$data )
return "";
// grab the first paragraph, or, failing that, the whole content // grab the first paragraph, or, failing that, the whole content
$pos = strpos( $data, "\n\n" ); if(strpos($value, "\n\n")) $value = substr($value, 0, strpos($value, "\n\n"));
if( $pos ) $sentences = explode('.', $value);
$data = substr( $data, 0, $pos ); $count = count(explode(' ', $sentences[0]));
$sentences = explode( '.', $data );
$count = count( explode( ' ', $sentences[0] ) );
// if the first sentence is too long, show only the first $maxWords words // if the first sentence is too long, show only the first $maxWords words
if( $count > $maxWords ) { if($count > $maxWords) {
return implode( ' ', array_slice( explode( ' ', $sentences[0] ), 0, $maxWords ) ).'...'; return implode( ' ', array_slice(explode( ' ', $sentences[0] ), 0, $maxWords)) . '...';
} }
// add each sentence while there are enough words to do so // add each sentence while there are enough words to do so
$result = ''; $result = '';
do { do {
$result .= trim(array_shift( $sentences )).'.'; $result .= trim(array_shift( $sentences )).'.';
if(count($sentences) > 0) { if(count($sentences) > 0) {
$count += count( explode( ' ', $sentences[0] ) ); $count += count(explode(' ', $sentences[0]));
} }
// Ensure that we don't trim half way through a tag or a link // Ensure that we don't trim half way through a tag or a link
$brokenLink = (substr_count($result,'<') != substr_count($result,'>')) || $brokenLink = (
(substr_count($result,'<a') != substr_count($result,'</a')); substr_count($result,'<') != substr_count($result,'>')) ||
(substr_count($result,'<a') != substr_count($result,'</a')
} while( ($count < $maxWords || $brokenLink) && $sentences && trim( $sentences[0] ) ); );
} while(($count < $maxWords || $brokenLink) && $sentences && trim( $sentences[0]));
if( preg_match( '/<a[^>]*>/', $result ) && !preg_match( '/<\/a>/', $result ) ) if(preg_match('/<a[^>]*>/', $result) && !preg_match( '/<\/a>/', $result)) $result .= '</a>';
$result .= '</a>';
$result = Convert::raw2xml( $result ); return Convert::raw2xml($result);
return $result;
} }
/** /**
@ -184,20 +176,21 @@ class Text extends StringField {
*/ */
function BigSummary($maxWords = 50, $plain = 1) { function BigSummary($maxWords = 50, $plain = 1) {
$result = ""; $result = "";
// get first sentence? // get first sentence?
// this needs to be more robust // this needs to be more robust
if($plain) $data = Convert::xml2raw( $this->value, true ); if($plain) $data = Convert::xml2raw($this->value, true);
if( !$data ) if(!$data) return "";
return "";
$sentences = explode( '.', $data ); $sentences = explode('.', $data);
$count = count( explode( ' ', $sentences[0] ) ); $count = count(explode(' ', $sentences[0]));
// if the first sentence is too long, show only the first $maxWords words // if the first sentence is too long, show only the first $maxWords words
if( $count > $maxWords ) { if($count > $maxWords) {
return implode( ' ', array_slice( explode( ' ', $sentences[0] ), 0, $maxWords ) ).'...'; return implode(' ', array_slice(explode( ' ', $sentences[0] ), 0, $maxWords)) . '...';
} }
// add each sentence while there are enough words to do so // add each sentence while there are enough words to do so
do { do {
$result .= trim(array_shift($sentences)); $result .= trim(array_shift($sentences));
@ -207,12 +200,15 @@ class Text extends StringField {
} }
// Ensure that we don't trim half way through a tag or a link // Ensure that we don't trim half way through a tag or a link
$brokenLink = (substr_count($result,'<') != substr_count($result,'>')) || $brokenLink = (
(substr_count($result,'<a') != substr_count($result,'</a')); substr_count($result,'<') != substr_count($result,'>')) ||
} while( ($count < $maxWords || $brokenLink) && $sentences && trim( $sentences[0] ) ); (substr_count($result,'<a') != substr_count($result,'</a')
);
} while(($count < $maxWords || $brokenLink) && $sentences && trim($sentences[0]));
if( preg_match( '/<a[^>]*>/', $result ) && !preg_match( '/<\/a>/', $result ) ) if(preg_match( '/<a[^>]*>/', $result) && !preg_match( '/<\/a>/', $result)) {
$result .= '</a>'; $result .= '</a>';
}
return $result; return $result;
} }
@ -224,23 +220,22 @@ class Text extends StringField {
// get first sentence? // get first sentence?
// this needs to be more robust // this needs to be more robust
if($plain && $plain != 'html') { if($plain && $plain != 'html') {
$data = Convert::xml2raw( $this->value, true ); $data = Convert::xml2raw($this->value, true);
if( !$data ) return ""; if(!$data) return "";
// grab the first paragraph, or, failing that, the whole content // grab the first paragraph, or, failing that, the whole content
$pos = strpos( $data, "\n\n" ); $pos = strpos($data, "\n\n");
if( $pos ) if($pos) $data = substr($data, 0, $pos);
$data = substr( $data, 0, $pos );
return $data; return $data;
} else { } else {
if(strpos( $this->value, "</p>" ) === false) return $this->value; if(strpos($this->value, "</p>") === false) return $this->value;
$data = substr( $this->value, 0, strpos( $this->value, "</p>" ) + 4 ); $data = substr($this->value, 0, strpos($this->value, "</p>") + 4);
if(strlen($data) < 20 && strpos($this->value, "</p>", strlen($data))) {
if(strlen($data) < 20 && strpos( $this->value, "</p>", strlen($data) )) $data = substr( $this->value, 0, strpos( $this->value, "</p>", strlen($data) ) + 4 ); $data = substr($this->value, 0, strpos( $this->value, "</p>", strlen($data)) + 4 );
}
return $data; return $data;
} }