Advertisement
svetlai

Shaver Parser

Jun 9th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(params) {
  2.     var modelLinesCount = parseInt(params[0]),
  3.         currentLine,
  4.         models = {},
  5.         sections = {},
  6.         result = '',
  7.         i, j, k, l;
  8.  
  9.     for (i = 0; i < modelLinesCount; i += 1) {
  10.         currentLine = params[i + 1].trim();
  11.         var modelName = '',
  12.             modelValue = '',
  13.             isModelName = true;
  14.  
  15.         for (j = 0; j < currentLine.length; j += 1) {
  16.             if (currentLine[j] === ':') {
  17.                 isModelName = false;
  18.                 continue;
  19.             }
  20.  
  21.             if (isModelName) {
  22.                 modelName += currentLine[j];
  23.             } else {
  24.                 modelValue += currentLine[j];
  25.             }
  26.         }
  27.  
  28.         models[modelName.trim()] = modelValue.trim();
  29.     }
  30.  
  31.     var isKeyWord = false,
  32.         inSection = false,
  33.         inRenderSection = false,
  34.         inIf = false,
  35.         inForeach = false,
  36.         isSectionName = false,
  37.         inCondition = false,
  38.         inIfBody = false,
  39.         inForBody = false,
  40.         inEscaped = false,
  41.         inForCondition = false,
  42.         keyWord = '',
  43.         sectionName = '',
  44.         sectionContent = '',
  45.         toBeRendered = '',
  46.         conditionModel = '',
  47.         condition = '',
  48.         forCondition = '',
  49.         forConditionArray = [],
  50.         forBody = '';
  51.  
  52.  
  53.     for (i = modelLinesCount + 2; i < params.length; i += 1) {
  54.         currentLine = params[i].trim();
  55.  
  56.         if (currentLine === '') {
  57.             result += '\n';
  58.             continue;
  59.         }
  60.  
  61.         for (j = 0; j < currentLine.length; j += 1) {
  62.             if (currentLine[j] === '@' && currentLine[j + 1] === '@') {
  63.                 inEscaped = true;
  64.                 continue;
  65.             }
  66.  
  67.             if (inEscaped) {
  68.                 if (currentLine[j] === ' ' || currentLine[j] === '<') {
  69.                     inEscaped = false;
  70.                 }
  71.  
  72.                 result += currentLine[j];
  73.                 if (j === currentLine.length - 1) {
  74.                     result += '\n';
  75.                 }
  76.  
  77.                 continue;
  78.             }
  79.  
  80.             if (currentLine[j] === '@' && !inEscaped) {
  81.                 isKeyWord = true;
  82.                 keyWord = '@';
  83.                 continue;
  84.             }
  85.  
  86.             if (isKeyWord) {
  87.                 if (currentLine[j] === ' ' || currentLine[j] === '(' || currentLine[j] === '<' || j === currentLine.length - 1) {
  88.                     isKeyWord = false;
  89.  
  90.                     switch (keyWord) {
  91.                         case '@section':    // TODO: same as if
  92.                             inSection = true;
  93.                             isSectionName = true;
  94.                             break;
  95.                         case '@renderSection':
  96.                             inRenderSection = true;
  97.                             break;
  98.                         case '@if':
  99.                             if (currentLine[currentLine.length - 1] === '{') {
  100.                                 inIf = true;
  101.                                 break;
  102.                             }
  103.                         case '@foreach':
  104.                             if (currentLine[currentLine.length - 1] === '{') {
  105.                                 inForeach = true;
  106.                                 break;
  107.                             }
  108.                         default:
  109.                             keyWord = keyWord.slice(1);
  110.                             if (inIf && condition === 'false') {
  111.                                 continue;
  112.                             }
  113.  
  114.                             if (inForBody) {
  115.                                 if (j === currentLine.length - 1) {
  116.                                     if (models[keyWord]) {
  117.                                         forBody += models[keyWord] + '\n';
  118.                                     } else {
  119.                                         forBody += '##' + keyWord + '##' + '\n';
  120.                                     }
  121.                                 } else {
  122.                                     if (models[keyWord]) {
  123.                                         forBody += models[keyWord] + currentLine[j];
  124.                                     } else {
  125.                                         forBody += '##' + keyWord + '##' + currentLine[j];
  126.                                     }
  127.                                 }
  128.  
  129.                                 continue;
  130.                             }
  131.  
  132.                             if (j === currentLine.length - 1) {
  133.                                 keyWord += currentLine[j];
  134.                                 result += models[keyWord] + '\n';
  135.                             } else {
  136.                                 result += models[keyWord] + currentLine[j];
  137.                             }
  138.                             break;
  139.                     }
  140.  
  141.                     continue;
  142.                 }
  143.  
  144.                 keyWord += currentLine[j];
  145.                 continue;
  146.             }
  147.  
  148.             if (inSection) {
  149.                 if (currentLine[j] === '}') {
  150.                     inSection = false;
  151.                     sections[sectionName] = sectionContent;
  152.                     sectionName = '';
  153.                     sectionContent = '';
  154.                     continue;
  155.                 }
  156.  
  157.                 if (isSectionName && (currentLine[j] === ' ' || currentLine[j] === '{')) {
  158.                     isSectionName = false;
  159.                     continue;
  160.                 }
  161.  
  162.                 if (isSectionName) {
  163.                     sectionName += currentLine[j];
  164.                 } else {
  165.                     if (currentLine[j] === '{') {
  166.                         continue;
  167.                     }
  168.  
  169.                     sectionContent += currentLine[j];
  170.                     if (j === currentLine.length - 1) {
  171.                         sectionContent += '\n';
  172.                     }
  173.                 }
  174.  
  175.                 continue;
  176.             }
  177.  
  178.             if (inRenderSection) {
  179.                 if (currentLine[j] === ')') {
  180.                     inRenderSection = false;
  181.                     result += sections[toBeRendered];
  182.                     toBeRendered = '';
  183.                     continue;
  184.                 }
  185.                 if (currentLine[j] === '(' || currentLine[j] === '"' || currentLine[j] === "'") {
  186.                     continue;
  187.                 }
  188.  
  189.                 toBeRendered += currentLine[j];
  190.                 continue;
  191.             }
  192.  
  193.             if (inIf) {
  194.                 if (currentLine[j] === '}') {
  195.                     inIf = false;
  196.                     inIfBody = false;
  197.                     condition = '';
  198.                     continue;
  199.                 }
  200.  
  201.                 if (currentLine[j] === '{') {
  202.                     inIfBody = true;
  203.                     continue;
  204.                 }
  205.  
  206.                 if (currentLine[j] === '(' && !inIfBody) {
  207.                     inCondition = true;
  208.                     continue;
  209.                 }
  210.  
  211.                 if (currentLine[j] === ')' && !inIfBody) {
  212.                     inCondition = false;
  213.                     condition = models[conditionModel];
  214.                     conditionModel = '';
  215.                     continue;
  216.                 }
  217.  
  218.                 if (inCondition) {
  219.                     conditionModel += currentLine[j];
  220.                     continue;
  221.                 }
  222.  
  223.                 if (condition === 'true' && inIfBody) {
  224.                     result += currentLine[j];
  225.                     if (j === currentLine.length - 1) {
  226.                         result += '\n';
  227.                     }
  228.                     continue;
  229.                 } else {
  230.                     continue;
  231.                 }
  232.             }
  233.  
  234.             if (inForeach) {
  235.                 if (currentLine[j] === '}') {
  236.                     inForeach = false;
  237.  
  238.                     for (k = 0; k < models[forConditionArray[3]].length; k += 1) {
  239.                         var arr = forBody.split('##');
  240.                         var prop = arr[1];
  241.                         for (l in arr) {
  242.                             if (arr[l] === prop) {
  243.                                 arr[l] = models[forConditionArray[3]][k];
  244.                             }
  245.                         }
  246.  
  247.                         var resultBody = arr.join('');
  248.                         result += resultBody;
  249.                     }
  250.  
  251.                     inForBody = false;
  252.                     forBody = '';
  253.                     forCondition = '';
  254.                     continue;
  255.                 }
  256.  
  257.                 if (currentLine[j] === '(' && !inForBody) {
  258.                     inForCondition = true;
  259.                     continue;
  260.                 }
  261.  
  262.                 if (currentLine[j] === ')' && !inForBody) {
  263.                     inForCondition = false;
  264.                     forConditionArray = forCondition.split(' '); // var, prop, in, model
  265.                     models[forConditionArray[3]] = models[forConditionArray[3]].split(',');
  266.                     continue;
  267.                 }
  268.  
  269.                 if (inForCondition) {
  270.                     forCondition += currentLine[j];
  271.                     continue;
  272.                 }
  273.  
  274.                 if (currentLine[j] === '{') {
  275.                     inForBody = true;
  276.                     continue;
  277.                 }
  278.  
  279.                 if (inForBody) {
  280.                     forBody += currentLine[j];
  281.                     if (j === currentLine.length - 1) {
  282.                         forBody += '\n';
  283.                     }
  284.                     continue;
  285.                 }
  286.             }
  287.  
  288.             result += currentLine[j];
  289.  
  290.             if (j === currentLine.length - 1) {
  291.                 result += '\n';
  292.             }
  293.         }
  294.     }
  295.     // console.log(models);
  296.     return result.trim();
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement