summaryrefslogtreecommitdiff
path: root/src/BBCodeParser.php
blob: 344b5e6dc020893aa8f5147c5d6c79dbbf3dd3e9 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php

class BBCodeParser {

  const TEXT = 0;
  const CTAG = 1;
  const OTAG = 2;
  const DONE = 3;

  function parse($in, $uid) {
    # decode HTML entities before parsing
    $in = html_entity_decode($in, ENT_QUOTES, 'UTF-8');

    $text_stack = array();
    $arg_stack = array();

    $fn_number = 1;
    $fn = array();

    $i = 0;
    $len = strlen($in);

    $list_counter_stack = array();

    $out = '';

    $state = self::TEXT;

    while ($state != self::DONE) {
      switch ($state) {
      case self::TEXT:
        # find next occurance of uid
        $ustart = strpos($in, ":$uid", $i);
        if ($ustart === false) {
          #  no more tags, all done
          $out .= substr($in, $i);
          $state = self::DONE;
        }
        else {
          # locate next tag
          $tstart = strrpos($in, '[', $ustart-$len);
          $tag = substr($in, $tstart+1, $ustart-$tstart-1);

          # determine whether it's an open or close tag
          if ($tag[0] == '/') {
            $state = self::CTAG;
            $tag = substr($tag, 1); 
          }
          else {
            $state = self::OTAG;
          }
         
          # copy leading text to output 
          $out .= substr($in, $i, $tstart-$i);

          # advance past ":$uid]" to next unparsed character
          $i = $ustart + strlen($uid) + 2;
        }
        break;

      case self::OTAG:
        # split tag into tag name and argument, if any
        $arg = false;
        $epos = strpos($tag, '=');
        if ($epos !== false) {
          $tag = substr($tag, 0, $epos);
          $arg = substr($tag, $epos+1);
        }

        $arg_stack[] = $arg;

        switch ($tag) {
        case 'b':
          $out .= '__';
          break;
        case 'u':
        case 'i':
          $out .= '_';
          break;
        case 'url':
        case 'email':
          # nothing to do on opening
          break;
        case 'quote':
          $text_stack[] = $out . "\n";
          $out = '';
          break;
        case 'code':
          $out .= "\n";
          break;
        case 'list':
          $out .= "\n";
          
          switch ($arg) {
          case '1': $list_counter_stack[] = 1;   break;
          case 'a': $list_counter_stack[] = 'a'; break;
          default:  $list_counter_stack[] = '*'; break; 
          }

          break;
        case '*':
          $out .= "\n" . str_repeat(' ', count($list_counter_stack));

          $c = array_pop($list_counter_stack);
          if ($c == '*') {
          }
          else if (is_int($c)) {
            $out .= $c . '. ';
            $list_counter_stack[] = $c + 1;
          }
          else if ($c == '*') {
            $out .= $c . ' ';
            $list_counter_stack[] = '*';
          }
          else {
            $out .= $c . '. ';
            $list_counter_stack[] = chr(ord($c)+1);
          }
          break;
        case 'img':
          $text_stack[] = $out;
          $out = '';
          break;
        case 'attachment':
# TODO: unimplemented
          break;
        case 'color':
        case 'size':
          # ignored
          break;
        default:
          throw new Exception('Unrecognized open tag: ' . $tag);
        }

        $state = self::TEXT;
        break;

      case self::CTAG:
        $arg = array_pop($arg_stack);

        switch ($tag) {
        case 'b':
          $out .= '__';
          break;
        case 'u':
        case 'i':
          $out .= '_';
          break;
        case 'url':
        case 'email':
# TODO: untested
          if ($arg !== false) {
            # built footnotes for links with text
            $out .= '[' . $fn_number++ .']';
            $fn[] = $arg;
          }
          break;
        case 'quote':
          $level = count($text_stack);
          $out = wordwrap($out, 72 - 2*$level);
          $out = str_replace("\n", "\n> ", $out);
          $out = '> ' . $out;
          $out = array_pop($text_stack) . $out . "\n";
          break;
        case 'code':
# TODO: untested
# FIXME: don't wordwrap code!
          $out .= "\n";
          break;
        case 'list':
        case 'list:o':
        case 'list:u':
# TODO: untested
          $out .= "\n";
          array_pop($list_counter_stack);
          break;
        case '*':
        case '*:m':
# TODO: untested
          if ($in[$i] != "\n") {
            $out .= "\n";
          }
          break;
        case 'img':
# TODO: untested
          $fn[] = $out; 
          $out = array_pop($text_stack) . '[' . $fn_number++ . ']';
          break;
        case 'attachment':
          break;
        case 'color':
        case 'size':
          # ignored
          break;
        default:
          throw new Exception('Unrecognized close tag: ' . $tag);
        }

        $state = self::TEXT;
        break;
      }
    }

    $out = wordwrap($out, 72);

    if (!empty($fn)) {
      # build footnotes
      $out .= "\n";

      for ($i = 0; $i < count($fn); ++$i) {
        $out .= "\n[" . ($i+1) . '] ' . $fn[$i];
      }
    }

    return $out;
  }
}

?>