2012-11-29 7 views
1

에 YAML 파일을 구문 분석 나는 자바 스크립트, example.yml로 저장 한 다음 파일을 구문 분석 할 : 나는 JS-yaml.min에서 YAML 파서를 사용하고JS-YAML - 자바 스크립트

images: 
    main : [tester_tester/dic.jpg] 
    red : [tester_tester/red.jpg] 
    blue : [tester_tester/blue.jpg] 
    green : [tester_tester/green.jpg,tester_tester/green2.jpg] 
categories: 
    cat1 : [Yes, No] 
    cat2 : [Aa,Bb,Cc] 
    cat3 : [] 
    cat4 : [1,2,3,4,5] 

.

var fake = "images: {red: boat}\ncategories: {cat1 : []}"; 
var YAMLfile = jsyaml.load(fake); 

을 다음 YAML은 중첩 된 객체로 저장됩니다 : JS,하지만 난 단지 그것을 같은 것들을 사용하여 YAML의 문자열을 구문 분석 할 수 있습니다. YAML 파일을로드하고 파싱하는 대신이 라이브러리를 사용하면 어떻게 될까요? 그들의 웹 사이트에 https://github.com/nodeca/js-yaml, API는 require 기능을 사용한다고 말하지만 작동하지 않습니다. 이견있는 사람?

나는 기본적으로 여기에있는 기능을 찾고 있어요 : http://nodeca.github.com/js-yaml/

답변

1

예는 YAML과 텍스트 영역의 값을 사용합니다. 구문 분석하고 결과 요소 내용을 업데이트하십시오.

/*global window, document, location, CodeMirror, jsyaml, inspect, base64, hasher*/ 


window.runDemo = function runDemo() { 
    'use strict'; 

    var source, result, initial, permalink, timer1, timer2 = null, 
     fallback = document.getElementById('source').value || ''; 

    var SexyYamlType = new jsyaml.Type('!sexy', { 
    kind: 'sequence', // See node kinds in YAML spec: http://www.yaml.org/spec/1.2/spec.html#kind// 
    construct: function (data) { 
     return data.map(function (string) { return 'sexy ' + string; }); 
    } 
    }); 

    var SEXY_SCHEMA = jsyaml.Schema.create([ SexyYamlType ]); 

    function parse() { 
    var str, obj; 

    try { 
     str = source.getValue(); 
     obj = jsyaml.load(str, { schema: SEXY_SCHEMA }); 

     permalink.href = '#yaml=' + base64.encode(str); 

     result.setOption('mode', 'javascript'); 
     result.setValue(inspect(obj, false, 10)); 
    } catch (err) { 
     result.setOption('mode', 'text/plain'); 
     result.setValue(err.stack || err.message || String(err)); 
    } 
    } 

    function updateSource() { 
    var yaml; 

    if (location.hash && '#yaml=' === location.hash.toString().slice(0,6)) { 
     yaml = base64.decode(location.hash.slice(6)); 
    } 

    source.setValue(yaml || fallback); 
    parse(); 
    } 

    permalink = document.getElementById('permalink'); 

    source = CodeMirror.fromTextArea(document.getElementById('source'), { 
    mode: 'yaml', 
    undoDepth: 1, 
    onKeyEvent: function (_, evt) { 
     switch (evt.keyCode) { 
     case 37: 
     case 38: 
     case 39: 
     case 40: 
      return; 
     } 

     if (evt.type === 'keyup') { 
     window.clearTimeout(timer1); 
     timer1 = window.setTimeout(parse, 500); 

     if (null === timer2) { 
      timer2 = setTimeout(function() { 
      window.clearTimeout(timer1); 
      window.clearTimeout(timer2); 
      timer2 = null; 
      parse(); 
      }, 1000); 
     } 
     } 
    } 
    }); 

    result = CodeMirror.fromTextArea(document.getElementById('result'), { 
    readOnly: true 
    }); 

    // initial source 
    updateSource(); 

    // start monitor hash change 
    hasher.prependHash = ''; 
    hasher.changed.add(updateSource); 
    hasher.initialized.add(updateSource); 
    hasher.init(); 
}; 
:

이것은 당신이 찾고있는 실제 코드는