markdown fix

This commit is contained in:
stitz 2020-04-23 11:38:11 +02:00
parent b2edf6bef5
commit 39100e09a3
2 changed files with 88 additions and 66 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
/.project
/secret.php
/config.php
/test.php

View File

@ -1,33 +1,43 @@
<?php
// TODO markdown imple
function md($str) {
return nl2br ( $str ); // TODO md problem
$text = '';
// return nl2br ( $str ); // TODO md problem
$text = '<p>';
$lv = 0;
foreach ( explode ( "\n", $str ) as $t ) {
$str = explode ( "\n", str_replace ( "\r\n", "\n", $str ) );
// var_dump ( $str );
foreach ( $str as $t ) {
// echo '<pre>' . $t . '</pre>';
$t = preg_replace_callback ( '/\[\[([^\]]*)\]\]/m', '_md_link_replacer', $t );
$nlv = 0;
if (startsWith ( '**** ', $t )) {
if (startsWith ( $t, '**** ' )) {
// echo - 1;
$t = substr ( $t, 5 );
$nlv = 4;
}
if (startsWith ( '*** ', $t )) {
if (startsWith ( $t, '*** ' )) {
// echo - 2;
$t = substr ( $t, 4 );
$nlv = 3;
}
if (startsWith ( '** ', $t )) {
if (startsWith ( $t, '** ' )) {
// echo - 3;
$t = substr ( $t, 3 );
$nlv = 2;
}
if (startsWith ( '* ', $t )) {
if (startsWith ( $t, '* ' )) {
// echo - 4;
$t = substr ( $t, 2 );
$nlv = 1;
}
if ($lv != $nlv) {
while ( $lv < $nlv ) {
// echo '-5 (' . $lv . '-' . $nlv . ')';
$text .= '<ul>';
$lv ++;
}
while ( $lv > $nlv ) {
// echo '-6 (' . $lv . '-' . $nlv . ')';
$text .= '</ul>';
$lv --;
}
@ -48,7 +58,11 @@ function md($str) {
$t = '<h1>' . substr ( $t, 2 ) . '</h1>';
}
if ($lv == 0) {
if ($t == '') {
$text .= '</p><p>';
} else {
$text .= $t;
}
} else {
$text .= '<li>' . $t . '</li>';
}
@ -58,6 +72,13 @@ function md($str) {
$text .= '</ul>';
$lv --;
}
$text .= '</p>';
return $text;
}
function _md_link_replacer($in) {
// var_dump ( $in );
$in = explode ( '|', $in [1], 2 );
return '<a href="' . $in [0] . '" target="_blank">' . (isset ( $in [1] ) ? $in [1] : $in [0]) . '</a>';
}
?>