<!DOCTYPE html>  <html> <head>   <title>babel.coffee</title>   <meta http-equiv="content-type" content="text/html; charset=UTF-8">   <link rel="stylesheet" media="all" href="docco.css" /> </head> <body>   <div id="container">     <div id="background"></div>          <table cellpadding="0" cellspacing="0">       <thead>         <tr>           <th class="docs">             <h1>               babel.coffee             </h1>           </th>           <th class="code">           </th>         </tr>       </thead>       <tbody>                               <tr id="section-1">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-1">&#182;</a>               </div>                            </td>             <td class="code">               <div class="highlight"><pre><span class="nv">Fs = </span><span class="nx">require</span><span class="p">(</span><span class="s2">&quot;fs&quot;</span><span class="p">)</span>
<span class="nv">Path = </span><span class="nx">require</span><span class="p">(</span><span class="s2">&quot;path&quot;</span><span class="p">)</span>
<span class="nv">Step = </span><span class="nx">require</span><span class="p">(</span><span class="s2">&quot;step&quot;</span><span class="p">)</span>
<span class="nv">_ = </span><span class="nx">require</span><span class="p">(</span><span class="s2">&quot;underscore&quot;</span><span class="p">)</span>

<span class="nv">module.exports = Babel =</span></pre></div>             </td>           </tr>                               <tr id="section-2">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-2">&#182;</a>               </div>               <h2>Load Typed File</h2>             </td>             <td class="code">               <div class="highlight"><pre></pre></div>             </td>           </tr>                               <tr id="section-3">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-3">&#182;</a>               </div>               <h3>Babel.template( file, callback )</h3>

<p>Load and convert a template file</p>             </td>             <td class="code">               <div class="highlight"><pre>	
	<span class="nv">template: </span><span class="nf">(file, callback) -&gt;</span>
		<span class="nx">Babel</span><span class="p">.</span><span class="nx">typedFile</span><span class="p">(</span><span class="s2">&quot;template&quot;</span><span class="p">,</span> <span class="nx">file</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span>
	</pre></div>             </td>           </tr>                               <tr id="section-4">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-4">&#182;</a>               </div>               <h3>Babel.data( file, callback )</h3>

<p>Load and convert a data file</p>             </td>             <td class="code">               <div class="highlight"><pre>	
	<span class="nv">data: </span><span class="nf">(file, callback) -&gt;</span>
		<span class="nx">Babel</span><span class="p">.</span><span class="nx">typedFile</span><span class="p">(</span><span class="s2">&quot;data&quot;</span><span class="p">,</span> <span class="nx">file</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span>
	</pre></div>             </td>           </tr>                               <tr id="section-5">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-5">&#182;</a>               </div>               <h3>Babel.data( file, callback )</h3>

<p>Load and convert a script file</p>             </td>             <td class="code">               <div class="highlight"><pre>	
	<span class="nv">script: </span><span class="nf">(file, callback) -&gt;</span>
		<span class="nx">Babel</span><span class="p">.</span><span class="nx">typedFile</span><span class="p">(</span><span class="s2">&quot;script&quot;</span><span class="p">,</span> <span class="nx">file</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span>
	</pre></div>             </td>           </tr>                               <tr id="section-6">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-6">&#182;</a>               </div>               <h3>Babel.stylesheet( file, callback )</h3>

<p>Load and convert a stylesheet</p>             </td>             <td class="code">               <div class="highlight"><pre>	
	<span class="nv">stylesheet: </span><span class="nf">(file, callback) -&gt;</span>
		<span class="nx">Babel</span><span class="p">.</span><span class="nx">typedFile</span><span class="p">(</span><span class="s2">&quot;stylesheet&quot;</span><span class="p">,</span> <span class="nx">file</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span>
	</pre></div>             </td>           </tr>                               <tr id="section-7">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-7">&#182;</a>               </div>               <h3>Babel.typedFile( type, file, callback )</h3>

<p>Load a file of a specific type (determined by the file extension)
and load the appropriate translator. Run the file's contents through the
translator and return the result.</p>

<p>If the file does not exist, pass an empty string into the translator to
get a "default" value</p>

<p>If the translator doesn't exist, return null</p>             </td>             <td class="code">               <div class="highlight"><pre>	<span class="nv">typedFile: </span><span class="nf">(type, file, callback) -&gt;</span>
		
		<span class="nv">extension = </span><span class="nx">Path</span><span class="p">.</span><span class="nx">extname</span><span class="p">(</span><span class="nx">file</span><span class="p">).</span><span class="nx">substr</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
				
		<span class="nx">Step</span> <span class="o">-&gt;</span>
			<span class="nx">Babel</span><span class="p">.</span><span class="nx">translator</span><span class="p">(</span><span class="nx">type</span><span class="p">,</span> <span class="nx">extension</span><span class="p">,</span> <span class="nx">@parallel</span><span class="p">())</span>
			<span class="nx">Babel</span><span class="p">.</span><span class="nx">plain</span><span class="p">(</span><span class="nx">file</span><span class="p">,</span> <span class="nx">@parallel</span><span class="p">())</span>
			
		<span class="p">,</span> <span class="nf">(err, translator, source) -&gt;</span>
			<span class="nx">translator</span><span class="p">(</span><span class="nx">source</span><span class="p">,</span> <span class="err">@</span><span class="p">)</span>
			
		<span class="p">,</span> <span class="nx">callback</span>
			</pre></div>             </td>           </tr>                               <tr id="section-8">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-8">&#182;</a>               </div>               <h2>Load Plain Text</h2>

<p>Load the plain text of a file and return the result. If the file
does not exist, return an empty string.</p>             </td>             <td class="code">               <div class="highlight"><pre>	
	<span class="nv">plain: </span><span class="nf">(file, callback) -&gt;</span>				
		<span class="nx">Fs</span><span class="p">.</span><span class="nx">readFile</span> <span class="nx">file</span><span class="p">,</span> <span class="s2">&quot;utf8&quot;</span><span class="p">,</span> <span class="nf">(err, source) -&gt;</span>
			<span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="k">if</span> <span class="o">!</span><span class="nx">err</span> <span class="k">then</span> <span class="nx">source</span> <span class="k">else</span> <span class="s2">&quot;&quot;</span><span class="p">)</span>
	</pre></div>             </td>           </tr>                               <tr id="section-9">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-9">&#182;</a>               </div>               <h2>Load Translator</h2>             </td>             <td class="code">               <div class="highlight"><pre>	
	<span class="nv">translators: </span><span class="p">{}</span>
	
	<span class="nv">translatorsDir: </span><span class="nx">Path</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="nx">__dirname</span><span class="p">,</span> <span class="s2">&quot;../translators&quot;</span><span class="p">)</span>
	</pre></div>             </td>           </tr>                               <tr id="section-10">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-10">&#182;</a>               </div>               <h3>Babel.translator( type, extension, callback )</h3>

<p>Lazyload translators as they are needed. If a translator
for a requested file type does not exist, use a function
that always returns null in its place.</p>             </td>             <td class="code">               <div class="highlight"><pre>	
	<span class="nv">translator: </span><span class="nf">(type, extension, callback) -&gt;</span>
		
		<span class="nx">@translators</span><span class="p">[</span><span class="nx">type</span><span class="p">]</span> <span class="o">?=</span> <span class="p">{}</span>
		<span class="nv">translator = </span><span class="nx">@translators</span><span class="p">[</span><span class="nx">type</span><span class="p">][</span><span class="nx">extension</span><span class="p">]</span>

		<span class="k">if</span> <span class="nx">translator</span> <span class="o">!=</span> <span class="kc">undefined</span>
			<span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">translator</span><span class="p">)</span>

		<span class="k">else</span>
			<span class="nv">file = </span><span class="nx">Path</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="nx">@translatorsDir</span><span class="p">,</span> <span class="nx">type</span><span class="p">,</span> <span class="s2">&quot;#{extension}.coffee&quot;</span><span class="p">)</span>
			<span class="nv">translator = </span><span class="nx">require</span><span class="p">(</span><span class="nx">file</span><span class="p">)</span>
			<span class="nx">@translators</span><span class="p">[</span><span class="nx">type</span><span class="p">][</span><span class="nx">extension</span><span class="p">]</span> <span class="o">=</span> <span class="nx">translator</span>
			<span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">translator</span><span class="p">)</span>
			
	</pre></div>             </td>           </tr>                               <tr id="section-11">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-11">&#182;</a>               </div>               <h2>Load Directory</h2>             </td>             <td class="code">               <div class="highlight"><pre>	<span class="nv">ignoreFiles: </span><span class="p">[</span> <span class="s2">&quot;.DS_Store&quot;</span><span class="p">,</span> <span class="s2">&quot;.git&quot;</span> <span class="p">]</span>
	</pre></div>             </td>           </tr>                               <tr id="section-12">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-12">&#182;</a>               </div>               <h3>Babel.dir( file, callback )</h3>

<p>Run an iterator function for each file in a directory, allowing each
iterator to run its own async loader. The results of these loaders are
placed into an object by the file's basname</p>             </td>             <td class="code">               <div class="highlight"><pre>	
	<span class="nv">dir: </span><span class="nf">(dir, iterator, callback = -&gt;) -&gt;</span>
			
		<span class="nx">Step</span> <span class="o">-&gt;</span></pre></div>             </td>           </tr>                               <tr id="section-13">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-13">&#182;</a>               </div>               <p>Get a list of files in the directory</p>             </td>             <td class="code">               <div class="highlight"><pre>			<span class="nx">Fs</span><span class="p">.</span><span class="nx">readdir</span><span class="p">(</span><span class="nx">dir</span><span class="p">,</span> <span class="err">@</span><span class="p">)</span>
					
		<span class="p">,</span> <span class="nf">(err, files = []) -&gt;</span></pre></div>             </td>           </tr>                               <tr id="section-14">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-14">&#182;</a>               </div>               <p>If the directory doesn't exist, set <code>files</code> to an empty array</p>             </td>             <td class="code">               <div class="highlight"><pre>			<span class="k">if</span> <span class="nx">err</span> <span class="k">then</span> <span class="nv">files = </span><span class="p">[]</span>
			
			<span class="nx">@parallel</span><span class="p">()(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">files</span><span class="p">)</span>
		</pre></div>             </td>           </tr>                               <tr id="section-15">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-15">&#182;</a>               </div>               <p>Run the iterator on each (non-ignored) file</p>             </td>             <td class="code">               <div class="highlight"><pre>			<span class="nv">group = </span><span class="nx">@group</span><span class="p">()</span> <span class="k">if</span> <span class="p">(</span><span class="nx">files</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span>
			<span class="k">for</span> <span class="nx">file</span> <span class="k">in</span> <span class="nx">files</span>
				<span class="k">if</span> <span class="o">!</span><span class="nx">_</span><span class="p">.</span><span class="nx">include</span><span class="p">(</span><span class="nx">Babel</span><span class="p">.</span><span class="nx">ignoreFiles</span><span class="p">,</span> <span class="nx">file</span><span class="p">)</span>
					<span class="nx">iterator</span><span class="p">(</span><span class="nx">Path</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="nx">dir</span><span class="p">,</span> <span class="nx">file</span><span class="p">),</span> <span class="nx">group</span><span class="p">())</span>
			
			<span class="kc">undefined</span>
			
		<span class="p">,</span> <span class="nf">(err, files = [], results = []) -&gt;</span>
			<span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="k">if</span> <span class="nx">err</span>
			</pre></div>             </td>           </tr>                               <tr id="section-16">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-16">&#182;</a>               </div>               <p>Match each result with its file path and build the final object</p>             </td>             <td class="code">               <div class="highlight"><pre>			<span class="nv">object = </span><span class="p">{}</span>
			<span class="k">for</span> <span class="nx">touple</span> <span class="k">in</span> <span class="nx">_</span><span class="p">.</span><span class="nx">zip</span><span class="p">(</span><span class="nx">files</span><span class="p">,</span> <span class="nx">results</span><span class="p">)</span>
				<span class="p">[</span><span class="nx">file</span><span class="p">,</span> <span class="nx">result</span><span class="p">]</span> <span class="o">=</span> <span class="nx">touple</span>
				<span class="nv">name = </span><span class="nx">Path</span><span class="p">.</span><span class="nx">basename</span><span class="p">(</span><span class="nx">file</span><span class="p">).</span><span class="nx">split</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
				<span class="nx">object</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">result</span>
		
			<span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">object</span><span class="p">)</span>

</pre></div>             </td>           </tr>                </tbody>     </table>   </div> </body> </html> 