Skip to content

Commit

Permalink
Merge pull request #210 from leomoon/master
Browse files Browse the repository at this point in the history
multiple improvements
  • Loading branch information
leomoon authored Nov 12, 2024
2 parents 95ba31b + 57b0d18 commit 6593200
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 57 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.63
1.0.64
2 changes: 1 addition & 1 deletion bootstrap.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
require_once(BASE."libraries/parsedown-extra-0.8.1/ParsedownExtra.php");
require_once(BASE."libraries/parsedown-extended-1.1.2-modified/ParsedownExtended.php");
require_once(BASE."libraries/parsedown-filter-0.0.1/ParsedownFilter.php");
require_once(BASE."libraries/parsedown-plus-0.0.7/ParsedownPlus.php");
require_once(BASE."libraries/parsedown-plus-0.0.8/ParsedownPlus.php");

// if behind https reverse proxy, set HTTPS property correctly
if(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO']=='https'){$_SERVER['HTTPS']='on';}
Expand Down
4 changes: 3 additions & 1 deletion libraries/parsedown-extra-0.8.1/ParsedownExtra.php
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,9 @@ protected function processTag($elementMarkup) # recursive
$DOMDocument = new DOMDocument;

# http://stackoverflow.com/q/11309194/200145
$elementMarkup = mb_convert_encoding($elementMarkup, 'HTML-ENTITIES', 'UTF-8');
// $elementMarkup = mb_convert_encoding($elementMarkup, 'HTML-ENTITIES', 'UTF-8');
$convmap = [0x80, 0x10FFFF, 0, 0xFFFFFF];
$elementMarkup = mb_encode_numericentity($elementMarkup, $convmap, 'UTF-8');

# http://stackoverflow.com/q/4879946/200145
$DOMDocument->loadHTML($elementMarkup);
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ protected function loadConfig()

public function text($text)
{
if (!$this->cssAdded) {
$text = $this->addCss($text);
$this->cssAdded = true;
}
// process special quotes first (which now handles collapsible sections within them)
$text = $this->processSpecialQuotesOutsideCode($text);
// then process top-level collapsible sections
Expand All @@ -56,6 +52,10 @@ public function text($text)
$text = $this->processCustomTagsOutsideCode($text, false);
$text = parent::text($text);
$text = $this->processColorTags($text);
if (!$this->cssAdded) {
$text = $this->addCss($text);
$this->cssAdded = true;
}
return $text;
}

Expand Down Expand Up @@ -300,47 +300,35 @@ protected function processDetailsTags($text)

protected function processCollapsibleSections($text)
{
$parts = preg_split(self::CODE_BLOCK_PATTERN, $text, -1, PREG_SPLIT_DELIM_CAPTURE);
$result = '';
$lines = explode("\n", $text);
$resultLines = [];
$inCollapsible = false;
$currentSummary = '';
$currentContent = '';
foreach ($parts as $part) {
// if this is a code block, preserve it as-is
if (preg_match(self::CODE_BLOCK_PATTERN, $part)) {
foreach ($lines as $line) {
// preserve the original line indentation
$originalLine = $line;
if (preg_match('/^(\s*)\+\+\+(.*)$/', $line, $matches)) {
$indentation = $matches[1];
$summary = trim($matches[2]);
if ($inCollapsible) {
$currentContent .= $part;
} else {
$result .= $part;
}
continue;
}
$lines = explode("\n", $part);
foreach ($lines as $line) {
if (preg_match('/^\+\+\+(.*)$/', $line, $matches)) {
if ($inCollapsible) {
// close previous collapsible section
$processedContent = $this->processIndentedContent($currentContent);
$result .= "<details><summary>" . ($currentSummary ?: "Click to expand") . "</summary>{$processedContent}</details>";
$inCollapsible = false;
$currentContent = '';
} else {
$currentSummary = trim($matches[1]);
$inCollapsible = true;
}
} else if ($inCollapsible) {
$currentContent .= $line . "\n";
// end of collapsible section
$resultLines[] = "{$indentation}</details>";
$inCollapsible = false;
} else {
$result .= $line . "\n";
// start of collapsible section
$currentSummary = $summary ?: 'Click to expand';
$resultLines[] = "{$indentation}<details><summary>{$currentSummary}</summary>";
$inCollapsible = true;
}
} else {
$resultLines[] = $originalLine;
}
}
// handle any remaining collapsible section
// close any remaining open collapsible section
if ($inCollapsible) {
$processedContent = $this->processIndentedContent($currentContent);
$result .= "<details><summary>" . ($currentSummary ?: "Click to expand") . "</summary>{$processedContent}</details>";
$resultLines[] = "</details>";
}
return $result;
return implode("\n", $resultLines);
}

protected function processIndentedContent($content)
Expand Down Expand Up @@ -396,7 +384,8 @@ protected function processIndentedContent($content)
// process special quotes first
$specialQuotesProcessed = $this->processSpecialQuotesOutsideCode($processedContent);
// finally process as markdown
return parent::text($specialQuotesProcessed);
$result = parent::text($specialQuotesProcessed);
return $result;
}

protected function getSpecialQuoteIconClass($tag)
Expand All @@ -413,13 +402,8 @@ protected function getSpecialQuoteIconClass($tag)

protected function processSpecialQuotesOutsideCode($text)
{
$parts = preg_split(self::CODE_BLOCK_PATTERN, $text, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($parts as &$part) {
if (!preg_match(self::CODE_BLOCK_PATTERN, $part)) {
$part = $this->processSpecialQuotes($part);
}
}
return implode('', $parts);
$text = $this->processSpecialQuotes($text);
return $text;
}

protected function processSpecialQuotes($text)
Expand All @@ -431,56 +415,117 @@ protected function processSpecialQuotes($text)
$specialQuoteHeader = '';
$specialQuoteContent = [];
$inCollapsible = false;
$collapsibleContent = '';
$collapsibleContent = [];
$currentSummary = '';
$inCodeBlock = false;
$codeBlockContent = '';
foreach ($lines as $line) {
if (preg_match('/^> \[!(CAUTION|IMPORTANT|WARNING|TIP|QUESTION)\](.*)/i', $line, $matches)) {
// start of a special blockquote
if ($inSpecialQuote) {
// close any previously opened blockquote
if ($inCollapsible) {
// close collapsible if open
if ($inCodeBlock) {
// close code block if open
$collapsibleContent[] = $codeBlockContent;
$codeBlockContent = '';
$inCodeBlock = false;
}
$processedCollapsible = sprintf(
'<details><summary>%s</summary>%s</details>',
$currentSummary ?: 'Click to expand',
parent::text(implode("\n", $collapsibleContent))
);
$specialQuoteContent[] = $processedCollapsible;
$inCollapsible = false;
$collapsibleContent = [];
}
$outputLines[] = $this->generateSpecialBlockquote($specialQuoteTag, $specialQuoteHeader, $specialQuoteContent);
$specialQuoteContent = []; // reset content
}
$inSpecialQuote = true;
$specialQuoteTag = strtolower($matches[1]);
$specialQuoteHeader = trim($matches[2]);
} elseif ($inSpecialQuote && preg_match('/^> ?(.*)/', $line, $matches)) {
$content = trim($matches[1]);
$content = $matches[1];
// handle code blocks inside special quote
if (preg_match('/^(```|~~~)/', trim($content))) {
if ($inCodeBlock) {
// end of code block
$inCodeBlock = false;
$codeBlockContent .= $content;
if ($inCollapsible) {
$collapsibleContent[] = $codeBlockContent;
} else {
$specialQuoteContent[] = $codeBlockContent;
}
$codeBlockContent = '';
} else {
// start of code block
$inCodeBlock = true;
$codeBlockContent = $content . "\n";
}
continue;
}
if ($inCodeBlock) {
// inside code block, append line
$codeBlockContent .= $content . "\n";
continue;
}
// handle collapsible section markers within special quotes
if (preg_match('/^\+\+\+(.*)$/', $content, $collapsibleMatches)) {
if ($inCollapsible) {
// close current collapsible section
if ($inCodeBlock) {
// close code block if open
$collapsibleContent[] = $codeBlockContent;
$codeBlockContent = '';
$inCodeBlock = false;
}
$processedCollapsible = sprintf(
'<details><summary>%s</summary>%s</details>',
$currentSummary ?: 'Click to expand',
parent::text($collapsibleContent)
parent::text(implode("\n", $collapsibleContent))
);
$specialQuoteContent[] = $processedCollapsible;
$inCollapsible = false;
$collapsibleContent = '';
$collapsibleContent = [];
} else {
// start new collapsible section
$currentSummary = trim($collapsibleMatches[1]);
$inCollapsible = true;
}
} elseif ($inCollapsible) {
// add content to current collapsible section
$collapsibleContent .= $content . "\n";
$collapsibleContent[] = $content;
} else {
// regular special quote content
$specialQuoteContent[] = $content;
}
} else {
// line is not part of the special quote
if ($inSpecialQuote) {
// close code block if open
if ($inCodeBlock) {
if ($inCollapsible) {
$collapsibleContent[] = $codeBlockContent;
} else {
$specialQuoteContent[] = $codeBlockContent;
}
$codeBlockContent = '';
$inCodeBlock = false;
}
// close collapsible if open
if ($inCollapsible) {
// close current collapsible section if any
$processedCollapsible = sprintf(
'<details><summary>%s</summary>%s</details>',
$currentSummary ?: 'Click to expand',
parent::text($collapsibleContent)
parent::text(implode("\n", $collapsibleContent))
);
$specialQuoteContent[] = $processedCollapsible;
$inCollapsible = false;
$collapsibleContent = [];
}
// close the special quote
$outputLines[] = $this->generateSpecialBlockquote($specialQuoteTag, $specialQuoteHeader, $specialQuoteContent);
Expand All @@ -492,17 +537,30 @@ protected function processSpecialQuotes($text)
$outputLines[] = $line;
}
}
// handle any unclosed blockquote at the end
// handle any unclosed structures at the end
if ($inSpecialQuote) {
// close code block if open
if ($inCodeBlock) {
if ($inCollapsible) {
$collapsibleContent[] = $codeBlockContent;
} else {
$specialQuoteContent[] = $codeBlockContent;
}
$codeBlockContent = '';
$inCodeBlock = false;
}
// close collapsible if open
if ($inCollapsible) {
// close any remaining collapsible section
$processedCollapsible = sprintf(
'<details><summary>%s</summary>%s</details>',
$currentSummary ?: 'Click to expand',
parent::text($collapsibleContent)
parent::text(implode("\n", $collapsibleContent))
);
$specialQuoteContent[] = $processedCollapsible;
$inCollapsible = false;
$collapsibleContent = [];
}
// close special quote
$outputLines[] = $this->generateSpecialBlockquote($specialQuoteTag, $specialQuoteHeader, $specialQuoteContent);
}
return implode("\n", $outputLines);
Expand Down
File renamed without changes.

0 comments on commit 6593200

Please sign in to comment.