-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPostLexer.class.php
More file actions
128 lines (120 loc) · 4 KB
/
PostLexer.class.php
File metadata and controls
128 lines (120 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
class PostLexer {
protected static $_terminals = array(
"/^(\(code\))/" => "T_CODE",
"/^(\(bold)/" => "T_BOLD",
"/^(\(cite)/" => "T_CITE",
"/^(\(italic)/" => "T_ITALIC",
"/^(\(quote)/" => "T_QUOTE",
"/^(\".+\")/" => "T_STRING",
"/^('.+')/" => "T_STRING",
"/^(\".+")/" => "T_STRING",
"/^(\d+)/" => "T_NUMBER",
"/^(\()/" => "T_PAREN_START",
"/^(\))/" => "T_PAREN_END",
"/^(\s+)/" => "T_WHITESPACE",
"/^([^\(\)]+)/" => "T_JUNK",
);
public static function get_terms() { return static::$_terminals; }
public static function toHTML($tokens) {
$code_scope = false;
$open_tags = array();
$res = "";
for($i = 0; $i < count($tokens); $i++) {
$t = $tokens[$i];
switch ($t['token']) {
case "T_CODE":
$res .= $code_scope ? '</pre>' : '<pre>';
$code_scope = !$code_scope;
break;
case "T_BOLD":
$res .= '<b>';
$open_tags[] = 'b';
$i++;
break;
case "T_CITE":
$cit = $tokens[$i + 2];
if ($cit['token'] != "T_STRING") {
break;
}
$src = $tokens[$i + 4];
if ($src['token'] != "T_JUNK") {
break;
}
$end = $tokens[$i + 5];
if ($end['token'] != "T_PAREN_END") {
break;
}
$res .= '<blockquote cite="' . htmlentities($src['match']) .
'">' . htmlentities($cit['match']) . '</blockquote>';
$i += 5;
break;
case "T_ITALIC":
$res .= '<i>';
$open_tags[] = 'i';
$i++;
break;
case "T_QUOTE":
$src = $tokens[$i + 2];
if ($src['token'] != "T_NUMBER") {
break;
}
$res .= '<a href="#' . $src['match'] . '">' . '#' .
$src['match'] . '</a>';
$i += 2;
break;
case "T_JUNK":
$res .= $t['match'];
break;
case "T_PAREN_END":
$tag = array_pop($open_tags);
if ($tag !== null) {
$res .= '</' . $tag . '>';
}
break;
case "T_WHITESPACE":
$res .= $t['match'];
break;
}
}
foreach($open_tags as $tag) {
$res .= '</' . $tag . '>';
}
if ($code_scope == true) {
$res .= "</pre>";
}
return $res;
}
public static function run($source) {
$tokens = array();
foreach($source as $number => $line) {
$offset = 0;
while($offset < strlen($line)) {
$result = static::_match($line, $number, $offset);
if($result === false) {
throw new Exception("Unable to parse line " . ($line+1) .
".");
}
$tokens[] = $result;
$offset += strlen($result['match']);
}
}
return $tokens;
}
protected static function _match($line, $number, $offset) {
$string = substr($line, $offset);
foreach(static::$_terminals as $pattern => $name) {
if(preg_match($pattern, $string, $matches)) {
return array(
'match' => $matches[1],
'token' => $name
);
}
}
return false;
}
public static function parseString($string) {
$string=self::run(array($string));
return self::toHTML($string);}
}
?>