<?php
// This will deal with text formating (Markdown etc.)
// TODO: we need to escape some URLs and some values
// PCRE modifiers: m = work at line level (else at file level)
function rg_format_markdown($a)
{
$k = array();
$v = array();
// 2 spaces at the end of the line
$k[] = '/ {2}$/mu';
$v[] = '<br />';
// blockquote
$k[] = '/^>\s(.*)$/mu';
$v[] = '<blockquote><p>\1</p></blockquote>';
// strikethrough
$k[] = '/~~(.*?)~~/uD';
$v[] = '<strike>\1</strike>';
// hr
$k[] = '/^---$/mu';
$v[] = '<hr />';
// bold
$k[] = '/\*\*(.*?)\*\*/uD'; // note '?' for non-greedy
$v[] = '<strong>\1</strong>';
$k[] = '/__(.*?)__/uD';
$v[] = '<strong>\1</strong>';
// italic
$k[] = '/\*(.*?)\*/uD';
$v[] = '<em>\1</em>';
$k[] = '/_(.*?)_/uD';
$v[] = '<em>\1</em>';
// image
$k[] = '/!\[(.*?)\]\((.*?)\s"(.*?)"\)/uD';
$v[] = '<img alt="\1" title="\3" src="\2" />';
// link
$k[] = '/\[(.*?)\]\((.*?)\)/uD';
$v[] = '<a href="\2" target="_blank">\1</a>';
$k[] = '#(http[s]://.*?)\s#uD';
$v[] = '<a href="\1" target="_blank">\1</a>';
// Headers
$k[] = '/^######\s(.*)/mu';
$v[] = '<h6>\1</h6>';
$k[] = '/^#####\s(.*)/mu';
$v[] = '<h5>\1</h5>';
$k[] = '/^####\s(.*)/mu';
$v[] = '<h4>\1</h4>';
$k[] = '/^###\s(.*)/mu';
$v[] = '<h3>\1</h3>';
$k[] = '/^##\s(.*)/mu';
$v[] = '<h2>\1</h2>';
$k[] = '/^#\s(.*)/mu';
$v[] = '<h1>\1</h1>';
// Syntax highlighting (not there yet)
$k[] = '/`(.*?)`/mu';
$v[] = '<code>\1</code>';
$k[] = '/```(.*?)```/uD';
$v[] = '<code>\1</code>';
$a = preg_replace($k, $v, $a);
// tables
$ret = array(); $j = 0;
$table_started = FALSE;
$f = explode("\n", $a); unset($a);
foreach ($f as $i => $line) {
rg_log('DEBUG: line=' . $line);
if (preg_match('/^=+$/u', $line) === 1) {
rg_log('Found a h1 with = sign');
if (($j > 0) && (strlen($line) == strlen($ret[$j - 1]))) {
$ret[$j - 1] = '<h1>' . $ret[$j - 1] . '</h1>';
continue;
}
}
if (preg_match('/^-+$/u', $line) === 1) {
rg_log('Found a h2 with - sign');
if (($j > 0) && (strlen($line) == strlen($ret[$j - 1]))) {
$ret[$j - 1] = '<h2>' . $ret[$j - 1] . '</h2>';
continue;
}
}
// table line
$cont = FALSE;
while (1) {
$r = preg_split($p = '/\s*\|\s*/u', $line);
if (($r === FALSE) || !isset($r[1]))
break;
rg_log_ml('DEBUG: found a table row: ' . print_r($r, TRUE));
if ($table_started !== TRUE) {
rg_log('DEBUG: But table was not started.');
if ($j == 0) {
rg_log('DEBUG: this is first line; ship');
break;
}
rg_log('DEBUG: Check previous line [' . $ret[$j - 1] . '] to contain \s|\s');
$r2 = preg_split($p = '/\s*\|\s*/u', $ret[$j - 1]);
if (($r2 === FALSE) || !isset($r2[1]))
break;
rg_log_ml('DEBUG: header match: ' . print_r($r2, TRUE));
rg_log(' DEBUG: Found a table in the previous line!');
$ret[$j - 1] = '<table>';
$ret[$j++] = '<tr><th>'
. implode('</th><th>', $r2)
. '</th></tr>';
$table_started = TRUE;
$cont = TRUE;
break;
}
$ret[$j++] = '<tr><td>' . implode('</td><td>', $r)
. '</td></tr>';
$cont = TRUE;
break;
}
if ($cont)
continue;
if ($table_started) {
rg_log('DEBUG: we have a normal line but table is in progress; stop table');
$ret[$j++] = '</table>';
$table_started = FALSE;
}
$ret[$j++] = $line;
}
if ($table_started) {
rg_log('DEBUG: we have a normal line but table is in progress; stop table');
$ret[$j++] = '</table>';
$table_started = FALSE;
}
$a = implode("\n", $ret); unset($ret);
$k = array();
$v = array();
$k[] = '/' . "\n\n" . '(.*)' . '(' . "\n\n" . '|$)/uD';
$v[] = "\n\n" . '<p>' . "\n" . '\1' . "\n" . '</p>' . '\2';
return preg_replace($k, $v, $a);
}
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"
Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit
Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit
You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a
pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master