| 
									
										
										
										
											2015-01-05 19:23:26 +00:00
										 |  |  | /*jshint -W054 */ | 
					
						
							|  |  |  | ;(function (exports) { | 
					
						
							|  |  |  |   'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-13 14:49:11 -07:00
										 |  |  |   var Desi = {} | 
					
						
							|  |  |  |     ; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Desi.YAML           = {}; | 
					
						
							|  |  |  |   Desi.YAML.parse     = (exports.jsyaml || require('js-yaml')).load; | 
					
						
							|  |  |  |   Desi.YAML.stringify = (exports.jsyaml || require('js-yaml')).dump; | 
					
						
							| 
									
										
										
										
											2015-01-05 19:23:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   function readFrontMatter(text) { | 
					
						
							|  |  |  |     var lines | 
					
						
							|  |  |  |       , line | 
					
						
							|  |  |  |       , padIndent = '' | 
					
						
							|  |  |  |       , ymllines = [] | 
					
						
							|  |  |  |       ; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     lines = text.split(/\n/); | 
					
						
							|  |  |  |     line = lines.shift(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!line.match(/^---\s*$/)) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // our yaml parser can't handle objects
 | 
					
						
							|  |  |  |     // that start without indentation, so
 | 
					
						
							|  |  |  |     // we can add it if this is the case
 | 
					
						
							|  |  |  |     if (lines[0] && lines[0].match(/^\S/)) { | 
					
						
							|  |  |  |       padIndent = ''; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (true) { | 
					
						
							|  |  |  |       line = lines.shift(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // premature end-of-file (unsupported yaml)
 | 
					
						
							|  |  |  |       if (!line && '' !== line) { | 
					
						
							|  |  |  |         ymllines = []; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // end-of-yaml front-matter
 | 
					
						
							|  |  |  |       if (line.match(/^---\s*$/)) { | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (line) { | 
					
						
							|  |  |  |         // supported yaml
 | 
					
						
							|  |  |  |         ymllines.push(padIndent + line);  | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // XXX can't be sorted because arrays get messed up
 | 
					
						
							|  |  |  |     //ymllines.sort();
 | 
					
						
							|  |  |  |     if (ymllines) { | 
					
						
							|  |  |  |       return '---\n' + ymllines.join('\n'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function separateText(text, fm) { | 
					
						
							|  |  |  |     var len | 
					
						
							|  |  |  |       , yml | 
					
						
							|  |  |  |       ; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     yml = readFrontMatter(fm); | 
					
						
							|  |  |  |     // strip frontmatter from text, if any
 | 
					
						
							|  |  |  |     // including trailing '---' (which is accounted for by the added '\n')
 | 
					
						
							|  |  |  |     if (yml) { | 
					
						
							| 
									
										
										
										
											2015-01-07 02:20:50 +00:00
										 |  |  |       len = fm.split(/\n/).length + 1; | 
					
						
							| 
									
										
										
										
											2015-01-05 19:23:26 +00:00
										 |  |  |     } else { | 
					
						
							|  |  |  |       len = 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return text.split(/\n/).slice(len).join('\n'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function parseText(text) { | 
					
						
							|  |  |  |     var fm = readFrontMatter(text) | 
					
						
							| 
									
										
										
										
											2015-01-07 02:20:50 +00:00
										 |  |  |       , body = fm && separateText(text, fm) | 
					
						
							| 
									
										
										
										
											2015-01-05 19:23:26 +00:00
										 |  |  |       , yml | 
					
						
							|  |  |  |       ; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-07 02:20:50 +00:00
										 |  |  |     if (fm) { | 
					
						
							|  |  |  |       try { | 
					
						
							| 
									
										
										
										
											2015-01-13 14:49:11 -07:00
										 |  |  |         yml = Desi.YAML.parse(fm); | 
					
						
							| 
									
										
										
										
											2015-01-07 02:20:50 +00:00
										 |  |  |       } catch(e) { | 
					
						
							|  |  |  |         //
 | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-01-05 19:23:26 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       yml: yml | 
					
						
							|  |  |  |     , frontmatter: fm | 
					
						
							|  |  |  |     , body: body | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-13 14:49:11 -07:00
										 |  |  |   exports.Frontmatter = {}; | 
					
						
							| 
									
										
										
										
											2015-01-06 06:52:44 +00:00
										 |  |  |   exports.Frontmatter.Frontmatter = exports.Frontmatter; | 
					
						
							| 
									
										
										
										
											2015-01-05 19:23:26 +00:00
										 |  |  |   exports.Frontmatter.readText = readFrontMatter; | 
					
						
							|  |  |  |   exports.Frontmatter.separateText = separateText; | 
					
						
							|  |  |  |   exports.Frontmatter.parse = parseText; | 
					
						
							| 
									
										
										
										
											2015-01-13 14:49:11 -07:00
										 |  |  |   exports.Frontmatter.YAML = Desi.YAML; | 
					
						
							| 
									
										
										
										
											2015-01-05 19:23:26 +00:00
										 |  |  | }('undefined' !== typeof exports && exports || window)); |