[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Plugins/PipeTables/ -> Parser.js (source)

   1  var pos, table = null, tableTag, tables, _text = text;
   2  
   3  if (config.overwriteMarkdown)
   4  {
   5      overwriteMarkdown();
   6  }
   7  if (config.overwriteEscapes)
   8  {
   9      overwriteEscapes();
  10  }
  11  
  12  captureTables();
  13  processTables();
  14  
  15  /**
  16  * Add current line to a table
  17  *
  18  * @param {string} line Line of text
  19  */
  20  function addLine(line)
  21  {
  22      var ignoreLen = 0;
  23  
  24      if (!table)
  25      {
  26          table = { rows: [] };
  27  
  28          // Make the table start at the first non-space character
  29          ignoreLen = /^ */.exec(line)[0].length;
  30          line      = line.substring(ignoreLen);
  31      }
  32  
  33      // Overwrite the outermost pipes
  34      line = line.replace(/^( *)\|/, '$1 ').replace(/\|( *)$/, ' $1');
  35  
  36      table.rows.push({ line: line, pos: pos + ignoreLen });
  37  }
  38  
  39  /**
  40  * Process current table's body
  41  */
  42  function addTableBody()
  43  {
  44      var i   = 1,
  45          cnt = table.rows.length;
  46      while (++i < cnt)
  47      {
  48          addTableRow('TD', table.rows[i]);
  49      }
  50  
  51      createBodyTags(table.rows[2].pos, pos);
  52  }
  53  
  54  /**
  55  * Add a cell's tags for current table at current position
  56  *
  57  * @param {string} tagName Either TD or TH
  58  * @param {string} align   Either "left", "center", "right" or ""
  59  * @param {string} content Cell's text content
  60  */
  61  function addTableCell(tagName, align, content)
  62  {
  63      var startPos  = pos,
  64          endPos    = startPos + content.length,
  65          ignoreLen;
  66      pos = endPos;
  67  
  68      var m = /^( *).*?( *)$/.exec(content);
  69      if (m[1])
  70      {
  71          ignoreLen = m[1].length;
  72          createIgnoreTag(startPos, ignoreLen);
  73          startPos += ignoreLen;
  74      }
  75      if (m[2])
  76      {
  77          ignoreLen = m[2].length;
  78          createIgnoreTag(endPos - ignoreLen, ignoreLen);
  79          endPos -= ignoreLen;
  80      }
  81  
  82      createCellTags(tagName, startPos, endPos, align);
  83  }
  84  
  85  /**
  86  * Process current table's head
  87  */
  88  function addTableHead()
  89  {
  90      addTableRow('TH', table.rows[0]);
  91      createHeadTags(table.rows[0].pos, pos);
  92  }
  93  
  94  /**
  95  * Process given table row
  96  *
  97  * @param {string}  tagName Either TD or TH
  98  * @param {!Object} row
  99  */
 100  function addTableRow(tagName, row)
 101  {
 102      pos = row.pos;
 103      row.line.split('|').forEach(function(str, i)
 104      {
 105          if (i > 0)
 106          {
 107              createIgnoreTag(pos, 1);
 108              ++pos;
 109          }
 110  
 111          var align = (!table.cols[i]) ? '' : table.cols[i];
 112          addTableCell(tagName, align, str);
 113      });
 114  
 115      createRowTags(row.pos, pos);
 116  }
 117  
 118  /**
 119  * Capture all pipe tables in current text
 120  */
 121  function captureTables()
 122  {
 123      table  = null;
 124      tables = [];
 125  
 126      pos = 0;
 127      _text.split("\n").forEach(function(line)
 128      {
 129          if (line.indexOf('|') < 0)
 130          {
 131              endTable();
 132          }
 133          else
 134          {
 135              addLine(line);
 136          }
 137          pos += 1 + line.length;
 138      });
 139      endTable();
 140  }
 141  
 142  /**
 143  * Create a pair of TBODY tags for given text span
 144  *
 145  * @param {number} startPos
 146  * @param {number} endPos
 147  */
 148  function createBodyTags(startPos, endPos)
 149  {
 150      addTagPair('TBODY', startPos, 0, endPos, 0, -103);
 151  }
 152  
 153  /**
 154  * Create a pair of TD or TH tags for given text span
 155  *
 156  * @param {string} tagName  Either TD or TH
 157  * @param {number} startPos
 158  * @param {number} endPos
 159  * @param {string} align    Either "left", "center", "right" or ""
 160  */
 161  function createCellTags(tagName, startPos, endPos, align)
 162  {
 163      var tag;
 164      if (startPos === endPos)
 165      {
 166          tag = addSelfClosingTag(tagName, startPos, 0, -101);
 167      }
 168      else
 169      {
 170          tag = addTagPair(tagName, startPos, 0, endPos, 0, -101);
 171      }
 172      if (align)
 173      {
 174          tag.setAttribute('align', align);
 175      }
 176  }
 177  
 178  /**
 179  * Create a pair of THEAD tags for given text span
 180  *
 181  * @param {number} startPos
 182  * @param {number} endPos
 183  */
 184  function createHeadTags(startPos, endPos)
 185  {
 186      addTagPair('THEAD', startPos, 0, endPos, 0, -103);
 187  }
 188  
 189  /**
 190  * Create an ignore tag for given text span
 191  *
 192  * @param {number} pos
 193  * @param {number} len
 194  */
 195  function createIgnoreTag(pos, len)
 196  {
 197      tableTag.cascadeInvalidationTo(addIgnoreTag(pos, len, 1000));
 198  }
 199  
 200  /**
 201  * Create a pair of TR tags for given text span
 202  *
 203  * @param {number} startPos
 204  * @param {number} endPos
 205  */
 206  function createRowTags(startPos, endPos)
 207  {
 208      addTagPair('TR', startPos, 0, endPos, 0, -102);
 209  }
 210  
 211  /**
 212  * Create an ignore tag for given separator row
 213  *
 214  * @param {!Object} row
 215  */
 216  function createSeparatorTag(row)
 217  {
 218      createIgnoreTag(row.pos - 1, 1 + row.line.length);
 219  }
 220  
 221  /**
 222  * Create a pair of TABLE tags for given text span
 223  *
 224  * @param {number} startPos
 225  * @param {number} endPos
 226  */
 227  function createTableTags(startPos, endPos)
 228  {
 229      tableTag = addTagPair('TABLE', startPos, 0, endPos, 0, -104);
 230  }
 231  
 232  /**
 233  * End current buffered table
 234  */
 235  function endTable()
 236  {
 237      if (hasValidTable())
 238      {
 239          table.cols = parseColumnAlignments(table.rows[1].line);
 240          tables.push(table);
 241      }
 242      table = null;
 243  }
 244  
 245  /**
 246  * Test whether a valid table is currently buffered
 247  *
 248  * @return {boolean}
 249  */
 250  function hasValidTable()
 251  {
 252      return (table && table.rows.length > 2 && isValidSeparator(table.rows[1].line));
 253  }
 254  
 255  /**
 256  * Test whether given line is a valid separator
 257  *
 258  * @param  {string}  line
 259  * @return {boolean}
 260  */
 261  function isValidSeparator(line)
 262  {
 263      return /^ *:?-+:?(?:(?:\+| *\| *):?-+:?)+ */.test(line);
 264  }
 265  
 266  /**
 267  * Overwrite right angle brackets in given match
 268  *
 269  * @param  {string} str
 270  * @return {string}
 271  */
 272  function overwriteBlockquoteCallback(str)
 273  {
 274      return str.replace(/[!>]/g, ' ');
 275  }
 276  
 277  /**
 278  * Overwrite escape sequences in current text
 279  */
 280  function overwriteEscapes()
 281  {
 282      if (_text.indexOf('\\|') > -1)
 283      {
 284          _text = _text.replace(/\\[\\|]/g, '..');
 285      }
 286  }
 287  
 288  /**
 289  * Overwrite backticks in given match
 290  *
 291  * @param  {string} str
 292  * @return {string}
 293  */
 294  function overwriteInlineCodeCallback(str)
 295  {
 296      return str.replace(/\|/g, '.');
 297  }
 298  
 299  /**
 300  * Overwrite Markdown-style markup in current text
 301  */
 302  function overwriteMarkdown()
 303  {
 304      // Overwrite inline code spans
 305      if (_text.indexOf('`') > -1)
 306      {
 307          _text = _text.replace(/`[^`]*`/g, overwriteInlineCodeCallback);
 308      }
 309  
 310      // Overwrite blockquotes
 311      if (_text.indexOf('>') > -1)
 312      {
 313          _text = _text.replace(/^(?:>!? ?)+/gm, overwriteBlockquoteCallback);
 314      }
 315  }
 316  
 317  /**
 318  * Parse and return column alignments in given separator line
 319  *
 320  * @param  {string} line
 321  * @return {!Array<string>}
 322  */
 323  function parseColumnAlignments(line)
 324  {
 325      // Use a bitfield to represent the colons' presence and map it to the CSS value
 326      var align = [
 327              '',
 328              'right',
 329              'left',
 330              'center'
 331          ],
 332          cols = [],
 333          regexp = /(:?)-+(:?)/g,
 334          m;
 335  
 336      while (m = regexp.exec(line))
 337      {
 338          var key = (m[1] ? 2 : 0) + (m[2] ? 1 : 0);
 339          cols.push(align[key]);
 340      }
 341  
 342      return cols;
 343  }
 344  
 345  /**
 346  * Process current table declaration
 347  */
 348  function processCurrentTable()
 349  {
 350      var firstRow = table.rows[0],
 351          lastRow  = table.rows[table.rows.length - 1];
 352      createTableTags(firstRow.pos, lastRow.pos + lastRow.line.length);
 353  
 354      addTableHead();
 355      createSeparatorTag(table.rows[1]);
 356      addTableBody();
 357  }
 358  
 359  /**
 360  * Process all the captured tables
 361  */
 362  function processTables()
 363  {
 364      var i = -1, cnt = tables.length;
 365      while (++i < cnt)
 366      {
 367          table = tables[i];
 368          processCurrentTable();
 369      }
 370  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1