From 97058daa1201cceb7a26492ed5290312c1daf72b Mon Sep 17 00:00:00 2001 From: Hakim Zulkufli Date: Sun, 6 Aug 2017 00:49:52 +0800 Subject: [PATCH 1/2] Generate title (if empty) from the content. A new function is created to achieve this, Text::truncate which is based on Text::cut from the text.class.php helper. Limit is set to 60 characters. Example A: 1. Content has over 60 characters. 2. Title = First 60 characters + remaining characters of the last word + '...' Example B: 1. Content has less than or equal to 60 characters 2. Title = All. Example C: 1. Content only has one word but is over 60 characters. 2. Title = First 60 characters + '...' Why B and C differ from each other? Because we do not want the whole that one weird word with over 60 characters to make its way to the title. It's probably unrealistic, the code can be much cleaner with B and C combined. Feel free to discuss. --- bl-kernel/dbpages.class.php | 12 ++++++++++-- bl-kernel/dbposts.class.php | 10 +++++++++- bl-kernel/helpers/text.class.php | 27 +++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/bl-kernel/dbpages.class.php b/bl-kernel/dbpages.class.php index 74736edd..f5eec294 100644 --- a/bl-kernel/dbpages.class.php +++ b/bl-kernel/dbpages.class.php @@ -28,9 +28,17 @@ 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($args['title']) ) { + $args['title'] = Text::truncate($args['content'], 60); + + // Assign the new title to the slug as well. + $args['slug'] = $args['title']; + } + $key = $this->generateKey($args['slug'], $args['parent']); - + // Generate UUID $args['uuid'] = md5(time().DOMAIN); diff --git a/bl-kernel/dbposts.class.php b/bl-kernel/dbposts.class.php index d14f7945..37cba8a8 100644 --- a/bl-kernel/dbposts.class.php +++ b/bl-kernel/dbposts.class.php @@ -111,7 +111,15 @@ class dbPosts extends dbJSON // Current date, format of DB_DATE_FORMAT $currentDate = Date::current(DB_DATE_FORMAT); - + + // Generate title + 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 the database key / index $key = $this->generateKey($args['slug']); diff --git a/bl-kernel/helpers/text.class.php b/bl-kernel/helpers/text.class.php index da532f43..a165ad6b 100644 --- a/bl-kernel/helpers/text.class.php +++ b/bl-kernel/helpers/text.class.php @@ -212,6 +212,33 @@ class Text { return $cut; } + + // Truncates the string under the limit specified by the limit parameter. + public static function truncate($string, $limit, $end = '...') + { + + // 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')).$end; + + } else { + $truncate = trim(mb_substr($string, 0, $limit, 'UTF-8')); + + // Check if string is more than limit + if(mb_strlen($string) > $limit) { + // Append $end + $truncate = $truncate.$end; + } + } + + if(empty($truncate)) { + return ''; + } + + return $truncate; + } // Return string length public static function length($string) From 3e869fd60d2fd47ca224c169cca4095f20babc27 Mon Sep 17 00:00:00 2001 From: Hakim Zulkufli Date: Sun, 6 Aug 2017 02:34:32 +0800 Subject: [PATCH 2/2] More safety checks for Text::truncate --- bl-kernel/helpers/text.class.php | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/bl-kernel/helpers/text.class.php b/bl-kernel/helpers/text.class.php index a165ad6b..f2e6de04 100644 --- a/bl-kernel/helpers/text.class.php +++ b/bl-kernel/helpers/text.class.php @@ -216,23 +216,22 @@ class Text { // Truncates the string under the limit specified by the limit parameter. public static function truncate($string, $limit, $end = '...') { - - // Check if string is only one word - if(preg_match('/\s/', $string)) { + // Check if over $limit + if(mb_strlen($string) > $limit) { - // 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')).$end; - - } else { - $truncate = trim(mb_substr($string, 0, $limit, 'UTF-8')); - - // Check if string is more than limit - if(mb_strlen($string) > $limit) { - // Append $end - $truncate = $truncate.$end; + // 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 ''; }