[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

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

   1  var attrName       = config.attrName,
   2      hasSingleQuote = (text.indexOf("'") >= 0),
   3      hasDoubleQuote = (text.indexOf('"') >= 0),
   4      tagName        = config.tagName;
   5  
   6  if (typeof config.disableQuotes === 'undefined')
   7  {
   8      parseSingleQuotes();
   9      parseSingleQuotePairs();
  10      parseDoubleQuotePairs();
  11  }
  12  if (typeof config.disableGuillemets === 'undefined')
  13  {
  14      parseGuillemets();
  15  }
  16  if (typeof config.disableMathSymbols === 'undefined')
  17  {
  18      parseNotEqualSign();
  19      parseSymbolsAfterDigits();
  20      parseFractions();
  21  }
  22  if (typeof config.disablePunctuation === 'undefined')
  23  {
  24      parseDashesAndEllipses();
  25  }
  26  if (typeof config.disableSymbols === 'undefined')
  27  {
  28      parseSymbolsInParentheses();
  29  }
  30  
  31  /**
  32  * Add a fancy replacement tag
  33  *
  34  * @param  {number} tagPos Position of the tag in the text
  35  * @param  {number} tagLen Length of text consumed by the tag
  36  * @param  {string} chr    Replacement character
  37  * @param  {number=} prio   Tag's priority
  38  * @return {!Tag}
  39  */
  40  function addTag(tagPos, tagLen, chr, prio)
  41  {
  42      var tag = addSelfClosingTag(tagName, tagPos, tagLen, prio || 0);
  43      tag.setAttribute(attrName, chr);
  44  
  45      return tag;
  46  }
  47  
  48  /**
  49  * Parse dashes and ellipses
  50  *
  51  * Does en dash –, em dash — and ellipsis …
  52  */
  53  function parseDashesAndEllipses()
  54  {
  55      if (text.indexOf('...') < 0 && text.indexOf('--') < 0)
  56      {
  57          return;
  58      }
  59  
  60      var chrs = {
  61              '--'  : "\u2013",
  62              '---' : "\u2014",
  63              '...' : "\u2026"
  64          },
  65          regexp = /---?|\.\.\./g,
  66          m;
  67      while (m = regexp.exec(text))
  68      {
  69          addTag(m.index, m[0].length, chrs[m[0]]);
  70      }
  71  }
  72  
  73  /**
  74  * Parse pairs of double quotes
  75  *
  76  * Does quote pairs “” -- must be done separately to handle nesting
  77  */
  78  function parseDoubleQuotePairs()
  79  {
  80      if (hasDoubleQuote)
  81      {
  82          parseQuotePairs('"', /(?:^|\W)".+?"(?!\w)/g, "\u201c", "\u201d");
  83      }
  84  }
  85  
  86  /**
  87  * Parse vulgar fractions
  88  */
  89  function parseFractions()
  90  {
  91      if (text.indexOf('/') < 0)
  92      {
  93          return;
  94      }
  95  
  96      /** @const */
  97      var map = {
  98          '0/3'  : "\u2189",
  99          '1/10' : "\u2152",
 100          '1/2'  : "\u00BD",
 101          '1/3'  : "\u2153",
 102          '1/4'  : "\u00BC",
 103          '1/5'  : "\u2155",
 104          '1/6'  : "\u2159",
 105          '1/7'  : "\u2150",
 106          '1/8'  : "\u215B",
 107          '1/9'  : "\u2151",
 108          '2/3'  : "\u2154",
 109          '2/5'  : "\u2156",
 110          '3/4'  : "\u00BE",
 111          '3/5'  : "\u2157",
 112          '3/8'  : "\u215C",
 113          '4/5'  : "\u2158",
 114          '5/6'  : "\u215A",
 115          '5/8'  : "\u215D",
 116          '7/8'  : "\u215E"
 117      };
 118  
 119      var m, regexp = /\b(?:0\/3|1\/(?:[2-9]|10)|2\/[35]|3\/[458]|4\/5|5\/[68]|7\/8)\b/g;
 120      while (m = regexp.exec(text))
 121      {
 122          addTag(m.index, m[0].length, map[m[0]]);
 123      }
 124  }
 125  
 126  /**
 127  * Parse guillemets-style quotation marks
 128  */
 129  function parseGuillemets()
 130  {
 131      if (text.indexOf('<<') < 0)
 132      {
 133          return;
 134      }
 135  
 136      var m, regexp = /<<( ?)(?! )[^\n<>]*?[^\n <>]\1>>(?!>)/g;
 137      while (m = regexp.exec(text))
 138      {
 139          var left  = addTag(m.index,                   2, "\u00AB"),
 140              right = addTag(m.index + m[0].length - 2, 2, "\u00BB");
 141  
 142          left.cascadeInvalidationTo(right);
 143      }
 144  }
 145  
 146  /**
 147  * Parse the not equal sign
 148  *
 149  * Supports != and =/=
 150  */
 151  function parseNotEqualSign()
 152  {
 153      if (text.indexOf('!=') < 0 && text.indexOf('=/=') < 0)
 154      {
 155          return;
 156      }
 157  
 158      var m, regexp = /\b (?:!|=\/)=(?= \b)/g;
 159      while (m = regexp.exec(text))
 160      {
 161          addTag(m.index + 1, m[0].length - 1, "\u2260");
 162      }
 163  }
 164  
 165  /**
 166  * Parse pairs of quotes
 167  *
 168  * @param {string}  q          ASCII quote character
 169  * @param {!RegExp} regexp     Regexp used to identify quote pairs
 170  * @param {string}  leftQuote  Fancy replacement for left quote
 171  * @param {string}  rightQuote Fancy replacement for right quote
 172  */
 173  function parseQuotePairs(q, regexp, leftQuote, rightQuote)
 174  {
 175      var m;
 176      while (m = regexp.exec(text))
 177      {
 178          var left  = addTag(m.index + m[0].indexOf(q), 1, leftQuote),
 179              right = addTag(m.index + m[0].length - 1, 1, rightQuote);
 180  
 181          // Cascade left tag's invalidation to the right so that if we skip the left quote,
 182          // the right quote remains untouched
 183          left.cascadeInvalidationTo(right);
 184      }
 185  }
 186  
 187  /**
 188  * Parse pairs of single quotes
 189  *
 190  * Does quote pairs ‘’ must be done separately to handle nesting
 191  */
 192  function parseSingleQuotePairs()
 193  {
 194      if (hasSingleQuote)
 195      {
 196          parseQuotePairs("'", /(?:^|\W)'.+?'(?!\w)/g, "\u2018", "\u2019");
 197      }
 198  }
 199  
 200  /**
 201  * Parse single quotes in general
 202  *
 203  * Does apostrophes ’ after a letter or at the beginning of a word or a couple of digits
 204  */
 205  function parseSingleQuotes()
 206  {
 207      if (!hasSingleQuote)
 208      {
 209          return;
 210      }
 211  
 212      var m, regexp = /[a-z]'|(?:^|\s)'(?=[a-z]|[0-9]{2})/gi;
 213      while (m = regexp.exec(text))
 214      {
 215          // Give this tag a worse priority than default so that quote pairs take precedence
 216          addTag(m.index + m[0].indexOf("'"), 1, "\u2019", 10);
 217      }
 218  }
 219  
 220  /**
 221  * Parse symbols found after digits
 222  *
 223  * Does symbols found after a digit:
 224  *  - apostrophe ’ if it's followed by an "s" as in 80's
 225  *  - prime ′ and double prime ″
 226  *  - multiply sign × if it's followed by an optional space and another digit
 227  */
 228  function parseSymbolsAfterDigits()
 229  {
 230      if (!hasSingleQuote && !hasDoubleQuote && text.indexOf('x') < 0)
 231      {
 232          return;
 233      }
 234  
 235      /** @const */
 236      var map = {
 237          // 80's -- use an apostrophe
 238          "'s" : "\u2019",
 239          // 12' or 12" -- use a prime
 240          "'"  : "\u2032",
 241          "' " : "\u2032",
 242          "'x" : "\u2032",
 243          '"'  : "\u2033",
 244          '" ' : "\u2033",
 245          '"x' : "\u2033"
 246      };
 247  
 248      var m, regexp = /[0-9](?:'s|["']? ?x(?= ?[0-9])|["'])/g;
 249      while (m = regexp.exec(text))
 250      {
 251          // Test for a multiply sign at the end
 252          if (m[0][m[0].length - 1] === 'x')
 253          {
 254              addTag(m.index + m[0].length - 1, 1, "\u00d7");
 255          }
 256  
 257          // Test for an apostrophe/prime right after the digit
 258          var str = m[0].substring(1, 3);
 259          if (map[str])
 260          {
 261              addTag(m.index + 1, 1, map[str]);
 262          }
 263      }
 264  }
 265  
 266  /**
 267  * Parse symbols found in parentheses such as (c)
 268  *
 269  * Does symbols ©, ® and ™
 270  */
 271  function parseSymbolsInParentheses()
 272  {
 273      if (text.indexOf('(') < 0)
 274      {
 275          return;
 276      }
 277  
 278      var chrs = {
 279              '(c)'  : "\u00A9",
 280              '(r)'  : "\u00AE",
 281              '(tm)' : "\u2122"
 282          },
 283          regexp = /\((?:c|r|tm)\)/gi,
 284          m;
 285      while (m = regexp.exec(text))
 286      {
 287          addTag(m.index, m[0].length, chrs[m[0].toLowerCase()]);
 288      }
 289  }


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