| 1 |
<?php |
| 2 |
function process_http_refs($text) |
| 3 |
{ |
| 4 |
$match = array(); |
| 5 |
|
| 6 |
if (preg_match('/^(.*)\[([^\]]+)\]\s*\(([^\)]+)\)(.*)$/', $text, $match)) |
| 7 |
{ |
| 8 |
$text = process_http_refs($match[1]) . |
| 9 |
'<a href="' . htmlspecialchars($match[3]) . '">' . |
| 10 |
htmlspecialchars($match[2]) . |
| 11 |
'</a>' . |
| 12 |
process_http_refs($match[4]); |
| 13 |
} |
| 14 |
else { |
| 15 |
$text = htmlspecialchars($text); |
| 16 |
} |
| 17 |
return $text; |
| 18 |
} |
| 19 |
|
| 20 |
function file_emit_line(&$state, $line = '', $tag = null) |
| 21 |
{ |
| 22 |
$s = &$state['state']; |
| 23 |
if (count($s) <= 0) |
| 24 |
return; |
| 25 |
|
| 26 |
if (!isset($tag)) |
| 27 |
$tag = end($s); |
| 28 |
|
| 29 |
print('<' . $tag . '>'); |
| 30 |
print(process_http_refs($state['line'] . $line)); |
| 31 |
$state['line'] = ''; |
| 32 |
print('</' . $tag . ">\n"); |
| 33 |
|
| 34 |
array_pop($s); |
| 35 |
} |
| 36 |
|
| 37 |
function file_append_line(&$state, $line = '', $tag = null) |
| 38 |
{ |
| 39 |
$s = &$state['state']; |
| 40 |
if (isset($tag)) |
| 41 |
array_push($s, $tag); |
| 42 |
$state['line'] .= $line; |
| 43 |
} |
| 44 |
|
| 45 |
function file_process_string(&$state, $line) |
| 46 |
{ |
| 47 |
$match = array(); |
| 48 |
$s = &$state['state']; |
| 49 |
$last = end($s); |
| 50 |
|
| 51 |
if (($last == 'ul') || ($last == 'li')) |
| 52 |
{ |
| 53 |
if (preg_match('/^\s*$/', $line)) |
| 54 |
{ |
| 55 |
file_emit_line($state); |
| 56 |
$last = end($s); |
| 57 |
if ($last == 'ul') |
| 58 |
{ |
| 59 |
array_pop($s); |
| 60 |
print("</ul>\n"); |
| 61 |
} |
| 62 |
} |
| 63 |
elseif (preg_match('/^\s*\*\s(.*)\s*$/', $line, $match)) |
| 64 |
{ |
| 65 |
file_emit_line($state); |
| 66 |
file_append_line($state, $match[1], 'li'); |
| 67 |
} |
| 68 |
else |
| 69 |
file_append_line($state, $line); |
| 70 |
} |
| 71 |
elseif ($last == 'p') |
| 72 |
{ |
| 73 |
if (preg_match('/^\s*$/', $line)) |
| 74 |
file_emit_line($state); |
| 75 |
elseif (preg_match('/^\s*\*\s(.*)\s*$/', $line, $match)) |
| 76 |
{ |
| 77 |
file_emit_line($state); |
| 78 |
$state['unget'] = $line; |
| 79 |
} |
| 80 |
elseif (preg_match('/^ {2}(.*)$/', $line, $match)) |
| 81 |
{ |
| 82 |
file_emit_line($state); |
| 83 |
$state['unget'] = $line; |
| 84 |
} |
| 85 |
else |
| 86 |
file_append_line($state, $line); |
| 87 |
} |
| 88 |
elseif ($last == 'pre') |
| 89 |
{ |
| 90 |
if (!preg_match('/^ {2}(.*)$/', $line, $match)) |
| 91 |
{ |
| 92 |
file_emit_line($state); |
| 93 |
$state['unget'] = $line; |
| 94 |
} |
| 95 |
else |
| 96 |
file_append_line($state, rtrim($match[1]) . "\n"); |
| 97 |
} |
| 98 |
elseif ($last == 'pre-q') |
| 99 |
{ |
| 100 |
if (preg_match('/^```.*$/', $line, $match)) |
| 101 |
{ |
| 102 |
file_emit_line($state, "", "pre"); |
| 103 |
} |
| 104 |
else |
| 105 |
file_append_line($state, rtrim($line) . "\n"); |
| 106 |
} |
| 107 |
else |
| 108 |
{ |
| 109 |
if (preg_match('/^\s*\*\s(.*)\s*$/', $line, $match)) |
| 110 |
{ |
| 111 |
array_push($s, 'ul'); |
| 112 |
print("<ul>\n"); |
| 113 |
file_append_line($state, $match[1], 'li'); |
| 114 |
} |
| 115 |
elseif (preg_match('/^ {2}(.*)$/', $line, $match)) |
| 116 |
file_append_line($state, $match[1] . "\n", 'pre'); |
| 117 |
elseif (preg_match('/^```.*$/', $line, $match)) |
| 118 |
file_append_line($state, "\n", 'pre-q'); |
| 119 |
else |
| 120 |
file_append_line($state, $line, 'p'); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
function file_content($file, $section = null) |
| 125 |
{ |
| 126 |
// Open file |
| 127 |
$fd = fopen($file, 'r'); |
| 128 |
if (!$fd) |
| 129 |
return; |
| 130 |
|
| 131 |
if (!isset($section)) |
| 132 |
{ |
| 133 |
while (!feof($fd)) |
| 134 |
{ |
| 135 |
$line = fgets($fd); |
| 136 |
print htmlspecialchars($line); |
| 137 |
} |
| 138 |
} |
| 139 |
else |
| 140 |
{ |
| 141 |
$pattern = '/^\={1,}\s*' . $section . '\s*\={1,}\s*$/i'; |
| 142 |
$pattern2 = '/^\#{1,}\s*' . $section . '\s*$/i'; |
| 143 |
$search = true; |
| 144 |
$state = array( |
| 145 |
'line' => '', |
| 146 |
'state' => array(''), |
| 147 |
'unget' => null |
| 148 |
); |
| 149 |
|
| 150 |
while ((isset($state['unget'])) || (!feof($fd))) |
| 151 |
{ |
| 152 |
$line = isset($state['unget']) ? $state['unget'] : fgets($fd); |
| 153 |
$state['unget'] = null; |
| 154 |
|
| 155 |
if ($search) |
| 156 |
{ |
| 157 |
if (preg_match($pattern, $line)) |
| 158 |
$search = false; |
| 159 |
elseif (preg_match($pattern2, $line)) |
| 160 |
$search = false; |
| 161 |
} |
| 162 |
else |
| 163 |
{ |
| 164 |
if (preg_match('/^\={1,}\s*.+\s*\={1,}\s*$/', $line)) |
| 165 |
break; |
| 166 |
if (preg_match('/^\#{1,}\s*.+$/', $line)) |
| 167 |
break; |
| 168 |
|
| 169 |
file_process_string($state, $line); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
// Close file |
| 175 |
fclose($fd); |
| 176 |
} |
| 177 |
|
| 178 |
?> |