From 9d079e5d2b3dad4e9d0ce962ba2cd8a815a297db Mon Sep 17 00:00:00 2001 From: Treer Date: Thu, 28 Apr 2016 23:48:50 +1000 Subject: Add definition lists to bbcode --- include/bbcode.php | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'include/bbcode.php') diff --git a/include/bbcode.php b/include/bbcode.php index b8cd23f59..1640307f8 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -345,6 +345,46 @@ function bb_spoilertag($match) { return ''; } +function bb_definitionList($match) { + // $match[1] is the markup styles for the "terms" in the definition list. + // $match[2] is the content between the [dl]...[/dl] tags + + $classes = ''; + if (stripos($match[1], "b") !== false) $classes .= 'dl-terms-bold '; + if (stripos($match[1], "i") !== false) $classes .= 'dl-terms-italic '; + if (stripos($match[1], "u") !== false) $classes .= 'dl-terms-underline '; + if (stripos($match[1], "l") !== false) $classes .= 'dl-terms-large '; + if (stripos($match[1], "m") !== false) $classes .= 'dl-terms-monospace '; + if (stripos($match[1], "h") !== false) $classes .= 'dl-horizontal '; // dl-horizontal is already provided by bootstrap + if (strlen($classes) === 0) $classes = "dl-terms-plain"; + + // The bbcode transformation will be: + // [*=term-text] description-text =>
term-text
description-text + // then after all replacements have been made, the
remaining the start of the + // string can be removed. HTML5 allows the missing end tag. + // Using '(?\n"; + $listElements = preg_replace( + '/\[\*=([[:print:]]*?)(?$1
', + $match[2] + ); + // Unescape any \] inside the
tags + $listElements = preg_replace_callback('/
(.*?)<\/dt>/ism', 'bb_definitionList_unescapeBraces', $listElements); + + // Remove the extra at the start of the string, if there is one. + $firstOpenTag = strpos($listElements, '
'); + $firstCloseTag = strpos($listElements, $closeDescriptionTag); + if ($firstCloseTag !== false && ($firstOpenTag === false || ($firstCloseTag < $firstOpenTag))) { + $listElements = preg_replace( '/<\/dd>/ism', '', $listElements, 1); + } + + return '
' . $listElements . '
';; +} +function bb_definitionList_unescapeBraces($match) { + return '
' . str_replace('\]', ']', $match[1]) . '
'; +} + /** * @brief Sanitize style properties from BBCode to HTML. * @@ -713,6 +753,7 @@ function bbcode($Text, $preserve_nl = false, $tryoembed = true, $cache = false) while ((((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false)) || ((strpos($Text, "[/ol]") !== false) && (strpos($Text, "[ol]") !== false)) || ((strpos($Text, "[/ul]") !== false) && (strpos($Text, "[ul]") !== false)) || + ((strpos($Text, "[/dl]") !== false) && (strpos($Text, "[dl") !== false)) || ((strpos($Text, "[/li]") !== false) && (strpos($Text, "[li]") !== false))) && (++$endlessloop < 20)) { $Text = preg_replace("/\[list\](.*?)\[\/list\]/ism", '', $Text); $Text = preg_replace("/\[list=\](.*?)\[\/list\]/ism", '', $Text); @@ -724,6 +765,13 @@ function bbcode($Text, $preserve_nl = false, $tryoembed = true, $cache = false) $Text = preg_replace("/\[ul\](.*?)\[\/ul\]/ism", '', $Text); $Text = preg_replace("/\[ol\](.*?)\[\/ol\]/ism", '', $Text); $Text = preg_replace("/\[li\](.*?)\[\/li\]/ism", '
  • $1
  • ', $Text); + + // [dl] tags have an optional [dl terms="bi"] form where bold/italic/underline/mono/large + // etc. style may be specified for the "terms" in the definition list. The quotation marks + // are also optional. The regex looks intimidating, but breaks down as: + // "[dl" "]" "[/dl]" + // where optional-termStyles are: "terms=" + $Text = preg_replace_callback('/\[dl[[:space:]]*(?:terms=(?:"|")?([a-zA-Z]+)(?:"|")?)?\](.*?)\[\/dl\]/ism', 'bb_definitionList', $Text); } if (strpos($Text,'[th]') !== false) { $Text = preg_replace("/\[th\](.*?)\[\/th\]/sm", '$1', $Text); -- cgit v1.2.3