2012-11-29 47 views
1

解析YAML文件我想解析以下文件,保存爲example.yml,在Javascript:JS-YAML - 在Javascript

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] 

我正在使用JS-yaml.min的YAML解析器。 JS,但我只能得到它的解析,使用像YAML的字符串:

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

然後YAML被存儲爲嵌套對象。我如何使用這個庫來加載和解析一個YAML文件,而不是隻給它一個字符串?在他們的網站https://github.com/nodeca/js-yaml上,API說要使用require函數,但這不起作用。有什麼想法嗎?

我基本上是尋找在這裏找到的功能:http://nodeca.github.com/js-yaml/

回答

1

的範例使用YAML textarea的價值。解析它並更新結果元素內容。

這是你正在尋找真正的代碼:

/*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(); 
};