0) {
$result[] = self::formatOrderedList($listItems);
$listItems = array();
$inOrderedList = false;
}
$result[] = '';
continue;
}
// Check if this is an ordered list item (starts with "1. ", "2. ", etc.)
if (self::isOrderedListItem($trimmed)) {
if (!$inOrderedList) {
$inOrderedList = true;
$listItems = array();
}
// Extract text after the number
$listItems[] = self::extractListItemText($trimmed);
} else {
// Flush any pending list before processing non-list line
if ($inOrderedList && count($listItems) > 0) {
$result[] = self::formatOrderedList($listItems);
$listItems = array();
$inOrderedList = false;
}
// Process regular line
$formatted = self::formatLine($trimmed);
$result[] = $formatted;
}
}
// Flush any remaining list
if ($inOrderedList && count($listItems) > 0) {
$result[] = self::formatOrderedList($listItems);
}
// Join with line breaks
$html = implode('
', $result);
// Clean up excessive line breaks
$html = preg_replace('/
+/', '
', $html);
return $html;
}
/**
* Check if a line is an ordered list item (e.g., "1. text")
*/
private static function isOrderedListItem($line) {
return preg_match('/^\d+\.\s+/', $line) === 1;
}
/**
* Extract the text part of a list item, removing the number
*/
private static function extractListItemText($line) {
$matches = array();
if (preg_match('/^\d+\.\s+(.*)$/', $line, $matches)) {
return isset($matches[1]) ? $matches[1] : $line;
}
return $line;
}
/**
* Convert an array of list items to HTML ordered list
*/
private static function formatOrderedList($items) {
if (count($items) === 0) {
return '';
}
$html = '