scratch – Diff between revs 75 and 125
?pathlinks?
Rev 75 | Rev 125 | |||
---|---|---|---|---|
1 | yaml.js |
1 | yaml.js |
|
2 | ======= |
2 | ======= |
|
3 | |
3 | |
|
4 | ![Build status](https://travis-ci.org/jeremyfa/yaml.js.svg?branch=develop) |
4 | ![Build status](https://travis-ci.org/jeremyfa/yaml.js.svg?branch=develop) |
|
5 | |
5 | |
|
6 | Standalone JavaScript YAML 1.2 Parser & Encoder. Works under node.js and all major browsers. Also brings command line YAML/JSON conversion tools. |
6 | Standalone JavaScript YAML 1.2 Parser & Encoder. Works under node.js and all major browsers. Also brings command line YAML/JSON conversion tools. |
|
7 | |
7 | |
|
8 | Mainly inspired from [Symfony Yaml Component](https://github.com/symfony/Yaml). |
8 | Mainly inspired from [Symfony Yaml Component](https://github.com/symfony/Yaml). |
|
9 | |
9 | |
|
10 | How to use |
10 | How to use |
|
11 | ---------- |
11 | ---------- |
|
12 | |
12 | |
|
13 | Import yaml.js in your html page: |
13 | Import yaml.js in your html page: |
|
14 | |
14 | |
|
15 | ``` html |
15 | ``` html |
|
16 | <script type="text/javascript" src="yaml.js"></script> |
16 | <script type="text/javascript" src="yaml.js"></script> |
|
17 | ``` |
17 | ``` |
|
18 | |
18 | |
|
19 | Parse yaml string: |
19 | Parse yaml string: |
|
20 | |
20 | |
|
21 | ``` js |
21 | ``` js |
|
22 | nativeObject = YAML.parse(yamlString); |
22 | nativeObject = YAML.parse(yamlString); |
|
23 | ``` |
23 | ``` |
|
24 | |
24 | |
|
25 | Dump native object into yaml string: |
25 | Dump native object into yaml string: |
|
26 | |
26 | |
|
27 | ``` js |
27 | ``` js |
|
28 | yamlString = YAML.stringify(nativeObject[, inline /* @integer depth to start using inline notation at */[, spaces /* @integer number of spaces to use for indentation */] ]); |
28 | yamlString = YAML.stringify(nativeObject[, inline /* @integer depth to start using inline notation at */[, spaces /* @integer number of spaces to use for indentation */] ]); |
|
29 | ``` |
29 | ``` |
|
30 | |
30 | |
|
31 | Load yaml file: |
31 | Load yaml file: |
|
32 | |
32 | |
|
33 | ``` js |
33 | ``` js |
|
34 | nativeObject = YAML.load('file.yml'); |
34 | nativeObject = YAML.load('file.yml'); |
|
35 | ``` |
35 | ``` |
|
36 | |
36 | |
|
37 | Load yaml file: |
37 | Load yaml file: |
|
38 | |
38 | |
|
39 | ``` js |
39 | ``` js |
|
40 | YAML.load('file.yml', function(result) |
40 | YAML.load('file.yml', function(result) |
|
41 | { |
41 | { |
|
42 | nativeObject = result; |
42 | nativeObject = result; |
|
43 | }); |
43 | }); |
|
44 | ``` |
44 | ``` |
|
45 | |
45 | |
|
46 | Use with node.js |
46 | Use with node.js |
|
47 | ---------------- |
47 | ---------------- |
|
48 | |
48 | |
|
49 | Install module: |
49 | Install module: |
|
50 | |
50 | |
|
51 | ``` bash |
51 | ``` bash |
|
52 | npm install yamljs |
52 | npm install yamljs |
|
53 | ``` |
53 | ``` |
|
54 | |
54 | |
|
55 | Use it: |
55 | Use it: |
|
56 | |
56 | |
|
57 | ``` js |
57 | ``` js |
|
58 | YAML = require('yamljs'); |
58 | YAML = require('yamljs'); |
|
59 | |
59 | |
|
60 | // parse YAML string |
60 | // parse YAML string |
|
61 | nativeObject = YAML.parse(yamlString); |
61 | nativeObject = YAML.parse(yamlString); |
|
62 | |
62 | |
|
63 | // Generate YAML |
63 | // Generate YAML |
|
64 | yamlString = YAML.stringify(nativeObject, 4); |
64 | yamlString = YAML.stringify(nativeObject, 4); |
|
65 | |
65 | |
|
66 | // Load yaml file using YAML.load |
66 | // Load yaml file using YAML.load |
|
67 | nativeObject = YAML.load('myfile.yml'); |
67 | nativeObject = YAML.load('myfile.yml'); |
|
68 | ``` |
68 | ``` |
|
69 | |
69 | |
|
70 | Command line tools |
70 | Command line tools |
|
71 | ------------------ |
71 | ------------------ |
|
72 | |
72 | |
|
73 | You can enable the command line tools by installing yamljs as a global module: |
73 | You can enable the command line tools by installing yamljs as a global module: |
|
74 | |
74 | |
|
75 | ``` bash |
75 | ``` bash |
|
76 | npm install -g yamljs |
76 | npm install -g yamljs |
|
77 | ``` |
77 | ``` |
|
78 | |
78 | |
|
79 | Then, two cli commands should become available: **yaml2json** and **json2yaml**. They let you convert YAML to JSON and JSON to YAML very easily. |
79 | Then, two cli commands should become available: **yaml2json** and **json2yaml**. They let you convert YAML to JSON and JSON to YAML very easily. |
|
80 | |
80 | |
|
81 | **yaml2json** |
81 | **yaml2json** |
|
82 | |
82 | |
|
83 | ``` |
83 | ``` |
|
84 | usage: yaml2json [-h] [-v] [-p] [-i INDENTATION] [-s] [-r] [-w] input |
84 | usage: yaml2json [-h] [-v] [-p] [-i INDENTATION] [-s] [-r] [-w] input |
|
85 | |
85 | |
|
86 | Positional arguments: |
86 | Positional arguments: |
|
87 | input YAML file or directory containing YAML files. |
87 | input YAML file or directory containing YAML files. |
|
88 | |
88 | |
|
89 | Optional arguments: |
89 | Optional arguments: |
|
90 | -h, --help Show this help message and exit. |
90 | -h, --help Show this help message and exit. |
|
91 | -v, --version Show program's version number and exit. |
91 | -v, --version Show program's version number and exit. |
|
92 | -p, --pretty Output pretty (indented) JSON. |
92 | -p, --pretty Output pretty (indented) JSON. |
|
93 | -i INDENTATION, --indentation INDENTATION |
93 | -i INDENTATION, --indentation INDENTATION |
|
94 | Number of space characters used to indent code (use |
94 | Number of space characters used to indent code (use |
|
95 | with --pretty, default: 2). |
95 | with --pretty, default: 2). |
|
96 | -s, --save Save output inside JSON file(s) with the same name. |
96 | -s, --save Save output inside JSON file(s) with the same name. |
|
97 | -r, --recursive If the input is a directory, also find YAML files in |
97 | -r, --recursive If the input is a directory, also find YAML files in |
|
98 | sub-directories recursively. |
98 | sub-directories recursively. |
|
99 | -w, --watch Watch for changes. |
99 | -w, --watch Watch for changes. |
|
100 | ``` |
100 | ``` |
|
101 | |
101 | |
|
102 | **json2yaml** |
102 | **json2yaml** |
|
103 | |
103 | |
|
104 | ``` |
104 | ``` |
|
105 | usage: json2yaml [-h] [-v] [-d DEPTH] [-i INDENTATION] [-s] [-r] [-w] input |
105 | usage: json2yaml [-h] [-v] [-d DEPTH] [-i INDENTATION] [-s] [-r] [-w] input |
|
106 | |
106 | |
|
107 | Positional arguments: |
107 | Positional arguments: |
|
108 | input JSON file or directory containing JSON files. |
108 | input JSON file or directory containing JSON files. |
|
109 | |
109 | |
|
110 | Optional arguments: |
110 | Optional arguments: |
|
111 | -h, --help Show this help message and exit. |
111 | -h, --help Show this help message and exit. |
|
112 | -v, --version Show program's version number and exit. |
112 | -v, --version Show program's version number and exit. |
|
113 | -d DEPTH, --depth DEPTH |
113 | -d DEPTH, --depth DEPTH |
|
114 | Set minimum level of depth before generating inline |
114 | Set minimum level of depth before generating inline |
|
115 | YAML (default: 2). |
115 | YAML (default: 2). |
|
116 | -i INDENTATION, --indentation INDENTATION |
116 | -i INDENTATION, --indentation INDENTATION |
|
117 | Number of space characters used to indent code |
117 | Number of space characters used to indent code |
|
118 | (default: 2). |
118 | (default: 2). |
|
119 | -s, --save Save output inside YML file(s) with the same name. |
119 | -s, --save Save output inside YML file(s) with the same name. |
|
120 | -r, --recursive If the input is a directory, also find JSON files in |
120 | -r, --recursive If the input is a directory, also find JSON files in |
|
121 | sub-directories recursively. |
121 | sub-directories recursively. |
|
122 | -w, --watch Watch for changes. |
122 | -w, --watch Watch for changes. |
|
123 | ``` |
123 | ``` |
|
124 | |
124 | |
|
125 | **examples** |
125 | **examples** |
|
126 | |
126 | |
|
127 | ``` bash |
127 | ``` bash |
|
128 | # Convert YAML to JSON and output resulting JSON on the console |
128 | # Convert YAML to JSON and output resulting JSON on the console |
|
129 | yaml2json myfile.yml |
129 | yaml2json myfile.yml |
|
130 | |
130 | |
|
131 | # Store output inside a JSON file |
131 | # Store output inside a JSON file |
|
132 | yaml2json myfile.yml > ouput.json |
132 | yaml2json myfile.yml > output.json |
|
133 | |
133 | |
|
134 | # Output "pretty" (indented) JSON |
134 | # Output "pretty" (indented) JSON |
|
135 | yaml2json myfile.yml --pretty |
135 | yaml2json myfile.yml --pretty |
|
136 | |
136 | |
|
137 | # Save the output inside a file called myfile.json |
137 | # Save the output inside a file called myfile.json |
|
138 | yaml2json myfile.yml --pretty --save |
138 | yaml2json myfile.yml --pretty --save |
|
139 | |
139 | |
|
140 | # Watch a full directory and convert any YAML file into its JSON equivalent |
140 | # Watch a full directory and convert any YAML file into its JSON equivalent |
|
141 | yaml2json mydirectory --pretty --save --recursive |
141 | yaml2json mydirectory --pretty --save --recursive |
|
142 | |
142 | |
|
143 | # Convert JSON to YAML and store output inside a JSON file |
143 | # Convert JSON to YAML and store output inside a JSON file |
|
144 | json2yaml myfile.json > ouput.yml |
144 | json2yaml myfile.json > output.yml |
|
145 | |
145 | |
|
146 | # Output YAML that will be inlined only after 8 levels of indentation |
146 | # Output YAML that will be inlined only after 8 levels of indentation |
|
147 | json2yaml myfile.json --depth 8 |
147 | json2yaml myfile.json --depth 8 |
|
148 | |
148 | |
|
149 | # Save the output inside a file called myfile.json with 4 spaces for each indentation |
149 | # Save the output inside a file called myfile.json with 4 spaces for each indentation |
|
150 | json2yaml myfile.json --indentation 4 |
150 | json2yaml myfile.json --indentation 4 |
|
151 | |
151 | |
|
152 | # Watch a full directory and convert any JSON file into its YAML equivalent |
152 | # Watch a full directory and convert any JSON file into its YAML equivalent |
|
153 | json2yaml mydirectory --pretty --save --recursive |
153 | json2yaml mydirectory --pretty --save --recursive |
|
154 | |
154 | |
|
155 | |
155 | |