Archived
1
0

Changed behavior between body and extended part of article.

The body part now has never a whiteline at the bottom, the extended part has one at the top if it begins with a newline or the body ends with a newline.
This commit is contained in:
Stephan Brunker
2017-11-26 01:19:59 +01:00
committed by onli
parent 0ee37818f2
commit b837adde3c
@@ -269,7 +269,16 @@ class serendipity_event_nl2br extends serendipity_event
$element = $temp['element']; $element = $temp['element'];
if ($p_tags) { if ($p_tags) {
$eventData[$element] = $this->nl2p($eventData[$element]); $text = $eventData[$element];
if (!empty($text)) {
//Standardize line endings to Unix
$text = str_replace(array("\r\n", "\r"), "\n", $text);
if ($element == 'body' && isset($eventData['extended'])) {
//move newlines from body to extended
$eventData['extended'] = str_repeat("\n",strspn($text,"\n",-1)) . $eventData['extended'];
}
$eventData[$element] = $this->nl2p($text, $element);
}
} else if ($isolate) { } else if ($isolate) {
$eventData[$element] = $this->isolate($eventData[$element], '~[<\[](' . implode('|', $isolate) . ').*?[>\]].*?[<\[]/\1[>\]]~si'); $eventData[$element] = $this->isolate($eventData[$element], '~[<\[](' . implode('|', $isolate) . ').*?[>\]].*?[<\[]/\1[>\]]~si');
$eventData[$element] = nl2br($eventData[$element]); $eventData[$element] = nl2br($eventData[$element]);
@@ -316,11 +325,21 @@ class serendipity_event_nl2br extends serendipity_event
/* nl2br plugin start */ /* nl2br plugin start */
p.whiteline { p.whitelinebottom {
margin-top: 0em; margin-top: 0em;
margin-bottom: 1em; margin-bottom: 1em;
} }
p.whitelinetop {
margin-top: 1em;
margin-bottom: 0em;
}
p.whitelinetopbottom {
margin-top: 1em;
margin-bottom: 1em;
}
p.break { p.break {
margin-top: 0em; margin-top: 0em;
margin-bottom: 0em; margin-bottom: 0em;
@@ -371,56 +390,54 @@ p.break {
/** /**
* Insert <p class="whiteline" at paragraphs ending with two newlines * Insert <p class="whiteline" at paragraphs ending with two newlines
* Insert <p class="break" at paragraphs ending with one nl * Insert <p class="break" at paragraphs ending with one or no nl
* Insert <p class="whitelinetop" at the first paragraph if starting with a nl
* Insert <p class="whitelinetopbottom" if the first paragraph is ending with two newlines
* @param string text * @param string text
* @param boolean complex operations (not necessary when text is flat) * @param boolean complex operations (not necessary when text is flat)
* @return string * @return string
*/ */
function nl2p(&$text, $complex=true) function nl2p(&$text, $element='any', $complex=true)
{ {
if (empty($text)) { //check if string starts with a newline (extended only)
return $text; $startnl = ($element =='extended' && strspn($text,"\n")) ? true : false;
}
//Standardize line endings: //trim whitespaces and line breaks
//DOS to Unix and Mac to Unix $text = trim($text);
$text = str_replace(array("\r\n", "\r"), "\n", $text);
//split into array
$text = str_split($text); $text = str_split($text);
$big_p = '<p class="whiteline">'; $big_p = '<p class="whiteline">';
$small_p = '<p class="break">'; $small_p = '<p class="break">';
$insert = true; $i = count($text) - 1;
$i = count($text);
$whiteline = false; $whiteline = false;
if ($text[$i-1] == "\n") {
//prevent unnexessary p-tag at the end
unset($text[$i-1]);
}
//main operation: convert \n to big_p and small_p //main operation: convert \n to big_p and small_p
while ($i > 0) { while ($i > 0) {
if ($insert) { //search next /n enclosing text, starting at $i-1
$i = $this->next_nl_block($i, $text); $i = $this->next_nl_block($i, $text);
if ($i == 0) { if ($i == 0) { //no newlines left
//prevent replacing of first character break;
break; } elseif ($whiteline == true) {
} $text[$i] = '</p>' . $big_p;
if ($whiteline == true) {
$text[$i] = '</p>' . $big_p;
} else {
$text[$i] = '</p>' . $small_p;
}
$whiteline = false;
$insert = false;
} else { } else {
if ($text[$i-1] === "\n") { $text[$i] = '</p>' . $small_p;
//newline is follower of a newline }
$whiteline = true; //look ahead for next paragraph class
} if ($text[$i-1] === "\n") {
$insert = true; $whiteline = true;
$i--;
} else {
$whiteline = false;
} }
} }
if ($whiteline) { if ($whiteline && $startnl) {
$start_tag = '<p class="whitelinetopbottom">';
} elseif ($startnl) {
$start_tag = '<p class="whitelinetop">';
} elseif ($whiteline) {
$start_tag = $big_p; $start_tag = $big_p;
} else { } else {
$start_tag = $small_p; $start_tag = $small_p;
@@ -447,7 +464,7 @@ p.break {
if (is_array($text)) { if (is_array($text)) {
$text = implode($text); $text = implode($text);
} }
return str_replace(array('<p class="whiteline"></p>','<p class="break"></p>', '<p></p>'),"", $text); return str_replace(array('<p class="whiteline"></p>','<p class="break"></p>','<p class="whitelinetop"></p>','<p class="whitelinetopbottom"></p>','<p></p>'),"", $text);
} }
function purge_p($text) function purge_p($text)
@@ -583,4 +600,4 @@ p.break {
} }
/* vim: set sts=4 ts=4 expandtab : */ /* vim: set sts=4 ts=4 expandtab : */
?> ?>