Ejemplo n.º 1
0
 /**
  * Create a table object from a block of text.
  *
  * @param string $text  table definition
  * @return T_Text_Table  table object
  */
 protected function populateTable($text, T_Text_Table $table)
 {
     $lines = preg_split('/(?:\\r\\n|\\n|\\x0b|\\r|\\f|\\x85)\\s*/u', $text);
     // also removes starting spaces
     // starting delimiter has already been stripped by the parsing regex, so we just need
     // to check whether the auto header 2nd line exists, and then parse text into table data array.
     $is_headers = count($lines) > 1 && strncmp($lines[1], '|', 1) !== 0;
     $data = array();
     foreach ($lines as $row) {
         if (strncmp($row, '|', 1) !== 0) {
             continue;
         }
         // skip delimiters
         $data[] = explode('|', mb_trim(mb_trim($row), '|'));
     }
     // now we need to actually flesh out the data into a table. Here we need to account for the
     // fact that trailing empty elements in the array are merged via span into the previous cell.
     $per_row = max(array_map('count', $data));
     foreach ($data as $num => $src) {
         $table->addChild($row = new T_Text_TableRow());
         if (count($src) != $per_row) {
             // add extra cols at end which will be merged by span into previous cell
             for ($i = 0, $max = $per_row - count($src); $i < $max; $i++) {
                 $src[] = null;
             }
         }
         foreach ($src as $n => $cell) {
             if ($n > 0 && strlen($cell) == 0) {
                 continue;
             }
             // skip cells already created into spans
             $type = $is_headers || strncmp($cell, '^', 1) === 0 ? T_Text_TableCell::HEADER : T_Text_TableCell::PLAIN;
             $cell = mb_trim(mb_ltrim($cell, '^'));
             $span = 1;
             while (++$n < $per_row && strlen($src[$n]) == 0) {
                 ++$span;
             }
             $row->addChild(new T_Text_TableCell($cell, $type, $span));
         }
         $is_headers = false;
         // turn off auto headers after first line
     }
     return $table;
 }
Ejemplo n.º 2
0
 function testParsesInsideAQuoteBlock()
 {
     $text = new T_Text_Quote('some citation', '| 1 | 2 |');
     $expect = new T_Text_Quote('some citation', null);
     $expect->addChild($table = new T_Text_Table());
     $table->addChild($row = new T_Text_TableRow());
     $row->addChild(new T_Text_TableCell('1'))->addChild(new T_Text_TableCell('2'));
     $text->accept(new T_Text_TableLexer());
     $this->assertEquals($expect, $text);
 }