scratch – Diff between revs 75 and 125

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 75 Rev 125
1   1  
2 Parser = require './Parser' 2 Parser = require './Parser'
3 Dumper = require './Dumper' 3 Dumper = require './Dumper'
4 Utils = require './Utils' 4 Utils = require './Utils'
5   5  
6 # Yaml offers convenience methods to load and dump YAML. 6 # Yaml offers convenience methods to load and dump YAML.
7 # 7 #
8 class Yaml 8 class Yaml
9   9  
10 # Parses YAML into a JavaScript object. 10 # Parses YAML into a JavaScript object.
11 # 11 #
12 # The parse method, when supplied with a YAML string, 12 # The parse method, when supplied with a YAML string,
13 # will do its best to convert YAML in a file into a JavaScript object. 13 # will do its best to convert YAML in a file into a JavaScript object.
14 # 14 #
15 # Usage: 15 # Usage:
16 # myObject = Yaml.parse('some: yaml'); 16 # myObject = Yaml.parse('some: yaml');
17 # console.log(myObject); 17 # console.log(myObject);
18 # 18 #
19 # @param [String] input A string containing YAML 19 # @param [String] input A string containing YAML
20 # @param [Boolean] exceptionOnInvalidType true if an exception must be thrown on invalid types, false otherwise 20 # @param [Boolean] exceptionOnInvalidType true if an exception must be thrown on invalid types, false otherwise
21 # @param [Function] objectDecoder A function to deserialize custom objects, null otherwise 21 # @param [Function] objectDecoder A function to deserialize custom objects, null otherwise
22 # 22 #
23 # @return [Object] The YAML converted to a JavaScript object 23 # @return [Object] The YAML converted to a JavaScript object
24 # 24 #
25 # @throw [ParseException] If the YAML is not valid 25 # @throw [ParseException] If the YAML is not valid
26 # 26 #
27 @parse: (input, exceptionOnInvalidType = false, objectDecoder = null) -> 27 @parse: (input, exceptionOnInvalidType = false, objectDecoder = null) ->
28 return new Parser().parse(input, exceptionOnInvalidType, objectDecoder) 28 return new Parser().parse(input, exceptionOnInvalidType, objectDecoder)
29   29  
30   30  
31 # Parses YAML from file path into a JavaScript object. 31 # Parses YAML from file path into a JavaScript object.
32 # 32 #
33 # The parseFile method, when supplied with a YAML file, 33 # The parseFile method, when supplied with a YAML file,
34 # will do its best to convert YAML in a file into a JavaScript object. 34 # will do its best to convert YAML in a file into a JavaScript object.
35 # 35 #
36 # Usage: 36 # Usage:
37 # myObject = Yaml.parseFile('config.yml'); 37 # myObject = Yaml.parseFile('config.yml');
38 # console.log(myObject); 38 # console.log(myObject);
39 # 39 #
40 # @param [String] path A file path pointing to a valid YAML file 40 # @param [String] path A file path pointing to a valid YAML file
41 # @param [Boolean] exceptionOnInvalidType true if an exception must be thrown on invalid types, false otherwise 41 # @param [Boolean] exceptionOnInvalidType true if an exception must be thrown on invalid types, false otherwise
42 # @param [Function] objectDecoder A function to deserialize custom objects, null otherwise 42 # @param [Function] objectDecoder A function to deserialize custom objects, null otherwise
43 # 43 #
44 # @return [Object] The YAML converted to a JavaScript object or null if the file doesn't exist. 44 # @return [Object] The YAML converted to a JavaScript object or null if the file doesn't exist.
45 # 45 #
46 # @throw [ParseException] If the YAML is not valid 46 # @throw [ParseException] If the YAML is not valid
47 # 47 #
48 @parseFile: (path, callback = null, exceptionOnInvalidType = false, objectDecoder = null) -> 48 @parseFile: (path, callback = null, exceptionOnInvalidType = false, objectDecoder = null) ->
49 if callback? 49 if callback?
50 # Async 50 # Async
51 Utils.getStringFromFile path, (input) => 51 Utils.getStringFromFile path, (input) =>
52 result = null 52 result = null
53 if input? 53 if input?
54 result = @parse input, exceptionOnInvalidType, objectDecoder 54 result = @parse input, exceptionOnInvalidType, objectDecoder
55 callback result 55 callback result
56 return 56 return
57 else 57 else
58 # Sync 58 # Sync
59 input = Utils.getStringFromFile path 59 input = Utils.getStringFromFile path
60 if input? 60 if input?
61 return @parse input, exceptionOnInvalidType, objectDecoder 61 return @parse input, exceptionOnInvalidType, objectDecoder
62 return null 62 return null
63   63  
64   64  
65 # Dumps a JavaScript object to a YAML string. 65 # Dumps a JavaScript object to a YAML string.
66 # 66 #
67 # The dump method, when supplied with an object, will do its best 67 # The dump method, when supplied with an object, will do its best
68 # to convert the object into friendly YAML. 68 # to convert the object into friendly YAML.
69 # 69 #
70 # @param [Object] input JavaScript object 70 # @param [Object] input JavaScript object
71 # @param [Integer] inline The level where you switch to inline YAML 71 # @param [Integer] inline The level where you switch to inline YAML
72 # @param [Integer] indent The amount of spaces to use for indentation of nested nodes. 72 # @param [Integer] indent The amount of spaces to use for indentation of nested nodes.
73 # @param [Boolean] exceptionOnInvalidType true if an exception must be thrown on invalid types (a JavaScript resource or object), false otherwise 73 # @param [Boolean] exceptionOnInvalidType true if an exception must be thrown on invalid types (a JavaScript resource or object), false otherwise
74 # @param [Function] objectEncoder A function to serialize custom objects, null otherwise 74 # @param [Function] objectEncoder A function to serialize custom objects, null otherwise
75 # 75 #
76 # @return [String] A YAML string representing the original JavaScript object 76 # @return [String] A YAML string representing the original JavaScript object
77 # 77 #
78 @dump: (input, inline = 2, indent = 4, exceptionOnInvalidType = false, objectEncoder = null) -> 78 @dump: (input, inline = 2, indent = 4, exceptionOnInvalidType = false, objectEncoder = null) ->
79 yaml = new Dumper() 79 yaml = new Dumper()
80 yaml.indentation = indent 80 yaml.indentation = indent
81   81  
82 return yaml.dump(input, inline, 0, exceptionOnInvalidType, objectEncoder) 82 return yaml.dump(input, inline, 0, exceptionOnInvalidType, objectEncoder)
83   83  
84   -  
85 # Registers .yml extension to work with node's require() function. -  
86 # -  
87 @register: -> -  
88 require_handler = (module, filename) -> -  
89 # Fill in result -  
90 module.exports = YAML.parseFile filename -  
91   -  
92 # Register require extensions only if we're on node.js -  
93 # hack for browserify -  
94 if require?.extensions? -  
95 require.extensions['.yml'] = require_handler -  
96 require.extensions['.yaml'] = require_handler -  
97   -  
98   84  
99 # Alias of dump() method for compatibility reasons. 85 # Alias of dump() method for compatibility reasons.
100 # 86 #
101 @stringify: (input, inline, indent, exceptionOnInvalidType, objectEncoder) -> 87 @stringify: (input, inline, indent, exceptionOnInvalidType, objectEncoder) ->
102 return @dump input, inline, indent, exceptionOnInvalidType, objectEncoder 88 return @dump input, inline, indent, exceptionOnInvalidType, objectEncoder
103   89  
104   90  
105 # Alias of parseFile() method for compatibility reasons. 91 # Alias of parseFile() method for compatibility reasons.
106 # 92 #
107 @load: (path, callback, exceptionOnInvalidType, objectDecoder) -> 93 @load: (path, callback, exceptionOnInvalidType, objectDecoder) ->
108 return @parseFile path, callback, exceptionOnInvalidType, objectDecoder 94 return @parseFile path, callback, exceptionOnInvalidType, objectDecoder
109   95  
110   96  
111 # Expose YAML namespace to browser 97 # Expose YAML namespace to browser
112 window?.YAML = Yaml 98 window?.YAML = Yaml
113   99  
114 # Not in the browser? 100 # Not in the browser?
115 unless window? 101 unless window?
116 @YAML = Yaml 102 @YAML = Yaml
117   103  
118 module.exports = Yaml 104 module.exports = Yaml
119   105