Merge pull request #454 from acrox999/bluditv2

Generate title and description (if empty) from the content.
This commit is contained in:
Diego Najar 2017-08-06 17:10:08 +02:00 committed by GitHub
commit c3cfa54149
2 changed files with 40 additions and 1 deletions

View File

@ -31,7 +31,20 @@ class dbPages extends dbJSON
{
$dataForDb = array(); // This data will be saved in the database
$dataForFile = array(); // This data will be saved in the file
// Generate title if empty
if( empty($args['title']) ) {
$args['title'] = Text::truncate($args['content'], 60);
// Assign the new title to the slug as well.
$args['slug'] = $args['title'];
}
// Generate description if empty
if( empty($args['description']) ) {
$args['description'] = Text::truncate($args['content'], 100);
}
// Generate key
$key = $this->generateKey($args['slug'], $args['parent']);

View File

@ -227,4 +227,30 @@ class Text {
create_function('$input', 'return "<pre><code $input[1]>".htmlentities($input[2])."</code></pre>";'),
$string);
}
// Truncates the string under the limit specified by the limit parameter.
public static function truncate($string, $limit, $end = '...')
{
// Check if over $limit
if(mb_strlen($string) > $limit) {
// Check if string is only one word
if(preg_match('/\s/', $string)) {
// Append the string specified by the end parameter to the end of the string as it is over the limit.
$truncate = trim(mb_substr($string, 0, mb_strpos($string, ' ', $limit, 'UTF-8'), 'UTF-8'));
} else {
$truncate = trim(mb_substr($string, 0, $limit, 'UTF-8'));
}
$truncate = $truncate.$end;
} else {
$truncate = $string;
}
if(empty($truncate)) {
return '';
}
return $truncate;
}
}