= 3 && substr($line, 0, 3) == "```") { $preformatted = !$preformatted; if($preformatted) { if($line_len > 3) { $body .= "
\n";
} else {
$body .= "\n";
}
} else {
$body .= "\n";
}
continue;
}
if($preformatted) {
$body .= htmlspecialchars($line_untrimmed);
continue;
}
// == LINKS
if(substr($line,0,2) === "=>") {
$line = ltrim($line, "=> \t");
if(strlen($line) == 0) {
continue;
}
$first_space = strpos($line, " ");
$first_tab = strpos($line, "\t");
$link_target = "";
$link_label = "";
if($first_space === false && $first_tab === false) {
$link_target = $line;
$link_label = $line;
} else {
if($first_space === false) $first_space = PHP_INT_MAX;
if($first_tab === false) $first_tab = PHP_INT_MAX;
$parts = [];
if($first_space < $first_tab) {
$parts = explode(" ", $line, 2);
} else {
$parts = explode("\t", $line, 2);
}
$link_target = $parts[0];
$link_label = $parts[1];
}
$extension = strtolower(pathinfo($link_target, PATHINFO_EXTENSION));
if(in_array($extension, IMAGE_EXTENSIONS)) {
$body .= "
\n";
} elseif(in_array($extension, VIDEO_EXTENSIONS)) {
$body .= "";
} elseif(in_array($extension, AUDIO_EXTENSIONS)) {
$body .= "";
} else {
$body .= "".htmlspecialchars($link_label)."\n";
}
continue;
}
// == HEADLINES
$head_level = 0;
if(substr($line,0,2) === "# ") {
$head_level = 1;
} elseif(substr($line,0,3) === "## ") {
$head_level = 2;
} elseif(substr($line,0,4) === "### ") {
$head_level = 3;
}
if($head_level > 0) {
$line = ltrim($line, "# \t");
if($generate_headline_ids) {
$body .= "".htmlspecialchars($line)." \n";
} else {
$body .= "".htmlspecialchars($line)." \n";
}
if($title == "") {
$title = $line;
}
$list_index = 1; # reset
continue;
}
// == BLOCKQUOTES
if(substr($line,0,1) === ">") {
$body .= "".htmlspecialchars(ltrim($line, "> \t"))."
\n";
continue;
}
// == LISTS
if(substr($line,0,2) === "* ") {
if(!$list_open) {
$list_open = true;
$body .= "\n";
}
$line = ltrim($line, "* ");
$body .= "- ".htmlspecialchars($line)."
\n";
$list_index += 1;
continue;
} else if($list_open && substr($line,0,1) !== "*") {
$list_open = false;
$body .= "
\n";
}
// == PARAGRAPHS
if($line_len > 0) {
$body .= "".htmlspecialchars($line)."
\n";
} else {
// == WHITESPACE
// only insert
after 2 newlines
if($last_br == $line_no - 1) {
$body .= "
\n";
}
$last_br = $line_no;
}
}
if($preformatted) {
$preformatted = false;
$body .= "\n";
}
if($list_open) {
$list_open = false;
$body .= "\n";
}
return array("body" => $body, "title" => $title);
}
?>