Advertisement
anilak

FakeTextMarkupLanguage

May 10th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(args) {
  2.     var texts = [],
  3.         tags = [],
  4.         head = 0,
  5.         i,
  6.         j,
  7.         input,
  8.         result = '',
  9.         isOpenTag = false,
  10.         tag = '',
  11.         text = '',
  12.         currentText;
  13.  
  14.         function parseFTML(text, tag) {
  15.  
  16.             function reverse(s) {
  17.                 var r = '',
  18.                     ii;
  19.                 for (ii = s.length - 1; ii >= 0; ii--) {
  20.                     r += s[ii];
  21.                 }
  22.  
  23.                 return r;
  24.             }
  25.  
  26.             function toggle(s) {
  27.                 var t = '',
  28.                     ii;
  29.                 for (ii = 0; ii < s.length; ii++) {
  30.                     if (s.charCodeAt(ii) > 96 && s.charCodeAt(ii) < 123) {
  31.                         t += s.charAt(ii).toUpperCase();
  32.                     }
  33.                     else {
  34.                         t += s.charAt(ii).toLowerCase();
  35.                     }
  36.                 }
  37.  
  38.                 return t;
  39.             }
  40.  
  41.             switch (tag) {
  42.                 case 'rev': return reverse(text);
  43.                 case 'del': return '';
  44.                 case 'upper': return text.toUpperCase();
  45.                 case 'lower': return text.toLowerCase();
  46.                 case 'toggle': return toggle(text);
  47.  
  48.             }
  49.         }
  50.  
  51.     for (i = 1; i < args.length; i++) {
  52.         input = args[i] + '\n';
  53.         for (j = 0; j < input.length; j++) {
  54.             if (input.charAt(j) === '<' && tags.length == 0) {
  55.                 result += text;
  56.                 text = '';
  57.                 isOpenTag = true;
  58.             } else if (input.charAt(j) === '<' && input.charAt(j + 1) !== '/') {
  59.                 if (text === '') {
  60.                     text = '#';
  61.                 }
  62.                 texts.push(text);
  63.                 text = '';
  64.                 isOpenTag = true;
  65.             } else if (input.charAt(j) === '<' && input.charAt(j + 1) === '/') {
  66.                 while (input.charAt(j) !== '>') {
  67.                     j++;
  68.                 }
  69.                 isOpenTag = false;
  70.                 if (tags.length > 1) {
  71.                     currentText = texts.pop();
  72.                     text = (currentText === '#' ? parseFTML(text, tags.pop()) : currentText + parseFTML(text, tags.pop()));
  73.                 } else {
  74.                     text = parseFTML(text, tags.pop());
  75.                 }
  76.             } else if (input.charAt(j) === '>') {
  77.                 tags.push(tag);
  78.                 tag = '';
  79.                 isOpenTag = false;
  80.             } else if (isOpenTag) {
  81.                 tag += input.charAt(j);
  82.             } else {
  83.                 text += input.charAt(j);
  84.             }
  85.         }
  86.     }
  87.  
  88.     if (text !== '' && tags.length === 0) {
  89.         result += text;
  90.     }
  91.  
  92.     return result;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement