[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Plugins/Litedown/Parser/Passes/ -> Emphasis.js (source)

   1  /**
   2  * @type {boolean} Whether current EM span is being closed by current emphasis mark
   3  */
   4  var closeEm;
   5  
   6  /**
   7  * @type {boolean} Whether current EM span is being closed by current emphasis mark
   8  */
   9  var closeStrong;
  10  
  11  /**
  12  * @type {number} Starting position of the current EM span in the text
  13  */
  14  var emPos;
  15  
  16  /**
  17  * @type {number} Ending position of the current EM span in the text
  18  */
  19  var emEndPos;
  20  
  21  /**
  22  * @type {number} Number of emphasis characters unused in current span
  23  */
  24  var remaining;
  25  
  26  /**
  27  * @type {number} Starting position of the current STRONG span in the text
  28  */
  29  var strongPos;
  30  
  31  /**
  32  * @type {number} Ending position of the current STRONG span in the text
  33  */
  34  var strongEndPos;
  35  
  36  function parse()
  37  {
  38      parseEmphasisByCharacter('*', /\*+/g);
  39      parseEmphasisByCharacter('_', /_+/g);
  40  }
  41  
  42  /**
  43  * Adjust the ending position of current EM and STRONG spans
  44  */
  45  function adjustEndingPositions()
  46  {
  47      if (closeEm && closeStrong)
  48      {
  49          if (emPos < strongPos)
  50          {
  51              emEndPos += 2;
  52          }
  53          else
  54          {
  55              ++strongEndPos;
  56          }
  57      }
  58  }
  59  
  60  /**
  61  * Adjust the starting position of current EM and STRONG spans
  62  *
  63  * If both EM and STRONG are set to start at the same position, we adjust their position
  64  * to match the order they are closed. If they start and end at the same position, STRONG
  65  * starts before EM to match Markdown's behaviour
  66  */
  67  function adjustStartingPositions()
  68  {
  69      if (emPos >= 0 && emPos === strongPos)
  70      {
  71          if (closeEm)
  72          {
  73              emPos += 2;
  74          }
  75          else
  76          {
  77              ++strongPos;
  78          }
  79      }
  80  }
  81  
  82  /**
  83  * End current valid EM and STRONG spans
  84  */
  85  function closeSpans()
  86  {
  87      if (closeEm)
  88      {
  89          --remaining;
  90          addTagPair('EM', emPos, 1, emEndPos, 1);
  91          emPos = -1;
  92      }
  93      if (closeStrong)
  94      {
  95          remaining -= 2;
  96          addTagPair('STRONG', strongPos, 2, strongEndPos, 2);
  97          strongPos = -1;
  98      }
  99  }
 100  
 101  /**
 102  * Get emphasis markup split by block
 103  *
 104  * @param  {!RegExp} regexp Regexp used to match emphasis
 105  * @param  {number}  pos    Position in the text of the first emphasis character
 106  * @return {!Array}         Each array contains a list of [matchPos, matchLen] pairs
 107  */
 108  function getEmphasisByBlock(regexp, pos)
 109  {
 110      var block    = [],
 111          blocks   = [],
 112          breakPos = text.indexOf("\x17", pos),
 113          m;
 114  
 115      regexp.lastIndex = pos;
 116      while (m = regexp.exec(text))
 117      {
 118          var matchPos = m.index,
 119              matchLen = m[0].length;
 120  
 121          // Test whether we've just passed the limits of a block
 122          if (matchPos > breakPos)
 123          {
 124              blocks.push(block);
 125              block    = [];
 126              breakPos = text.indexOf("\x17", matchPos);
 127          }
 128  
 129          // Test whether we should ignore this markup
 130          if (!ignoreEmphasis(matchPos, matchLen))
 131          {
 132              block.push([matchPos, matchLen]);
 133          }
 134      }
 135      blocks.push(block);
 136  
 137      return blocks;
 138  }
 139  
 140  
 141  /**
 142  * Test whether emphasis should be ignored at the given position in the text
 143  *
 144  * @param  {number}  pos Position of the emphasis in the text
 145  * @param  {number}  len Length of the emphasis
 146  * @return {boolean}
 147  */
 148  function ignoreEmphasis(pos, len)
 149  {
 150      // Ignore single underscores between alphanumeric characters
 151      return (text.charAt(pos) === '_' && len === 1 && isSurroundedByAlnum(pos, len));
 152  }
 153  
 154  /**
 155  * Open EM and STRONG spans whose content starts at given position
 156  *
 157  * @param {number} pos
 158  */
 159  function openSpans(pos)
 160  {
 161      if (remaining & 1)
 162      {
 163          emPos     = pos - remaining;
 164      }
 165      if (remaining & 2)
 166      {
 167          strongPos = pos - remaining;
 168      }
 169  }
 170  
 171  /**
 172  * Parse emphasis and strong applied using given character
 173  *
 174  * @param  {string} character Markup character, either * or _
 175  * @param  {!RegExp} regexp    Regexp used to match the series of emphasis character
 176  */
 177  function parseEmphasisByCharacter(character, regexp)
 178  {
 179      var pos = text.indexOf(character);
 180      if (pos === -1)
 181      {
 182          return;
 183      }
 184  
 185      getEmphasisByBlock(regexp, pos).forEach(processEmphasisBlock);
 186  }
 187  
 188  
 189  /**
 190  * Process a list of emphasis markup strings
 191  *
 192  * @param {!Array<!Array<number>>} block List of [matchPos, matchLen] pairs
 193  */
 194  function processEmphasisBlock(block)
 195  {
 196      emPos     = -1,
 197      strongPos = -1;
 198  
 199      block.forEach(function(pair)
 200      {
 201          processEmphasisMatch(pair[0], pair[1]);
 202      });
 203  }
 204  
 205  /**
 206  * Process an emphasis mark
 207  *
 208  * @param {number} matchPos
 209  * @param {number} matchLen
 210  */
 211  function processEmphasisMatch(matchPos, matchLen)
 212  {
 213      var canOpen  = !isBeforeWhitespace(matchPos + matchLen - 1),
 214          canClose = !isAfterWhitespace(matchPos),
 215          closeLen = (canClose) ? Math.min(matchLen, 3) : 0;
 216  
 217      closeEm      = !!(closeLen & 1) && emPos     >= 0;
 218      closeStrong  = !!(closeLen & 2) && strongPos >= 0;
 219      emEndPos     = matchPos;
 220      strongEndPos = matchPos;
 221      remaining    = matchLen;
 222  
 223      adjustStartingPositions();
 224      adjustEndingPositions();
 225      closeSpans();
 226  
 227      // Adjust the length of unused markup remaining in current match
 228      remaining = (canOpen) ? Math.min(remaining, 3) : 0;
 229      openSpans(matchPos + matchLen);
 230  }


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