[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

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

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


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1