PageRenderTime 76ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/doc/html/tutorials/getting_started.html

http://github.com/kiith-sa/D-YAML
HTML | 241 lines | 228 code | 13 blank | 0 comment | 0 complexity | aa139915f18dfcbc8b25360465150aaa MD5 | raw file
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Getting started &mdash; D:YAML 0.5 documentation</title>
  7. <link rel="stylesheet" href="../_static/default.css" type="text/css" />
  8. <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  9. <script type="text/javascript">
  10. var DOCUMENTATION_OPTIONS = {
  11. URL_ROOT: '../',
  12. VERSION: '0.5',
  13. COLLAPSE_INDEX: false,
  14. FILE_SUFFIX: '.html',
  15. HAS_SOURCE: true
  16. };
  17. </script>
  18. <script type="text/javascript" src="../_static/jquery.js"></script>
  19. <script type="text/javascript" src="../_static/underscore.js"></script>
  20. <script type="text/javascript" src="../_static/doctools.js"></script>
  21. <link rel="top" title="D:YAML 0.5 documentation" href="../index.html" />
  22. <link rel="next" title="Custom YAML data types" href="custom_types.html" />
  23. <link rel="prev" title="Welcome to D:YAML documentation!" href="../index.html" />
  24. </head>
  25. <body>
  26. <div class="related">
  27. <h3>Navigation</h3>
  28. <ul>
  29. <li class="right" style="margin-right: 10px">
  30. <a href="custom_types.html" title="Custom YAML data types"
  31. accesskey="N">next</a></li>
  32. <li class="right" >
  33. <a href="../index.html" title="Welcome to D:YAML documentation!"
  34. accesskey="P">previous</a> |</li>
  35. <li><a href="../index.html">D:YAML 0.5 documentation</a> &raquo;</li>
  36. </ul>
  37. </div>
  38. <div class="document">
  39. <div class="documentwrapper">
  40. <div class="bodywrapper">
  41. <div class="body">
  42. <div class="section" id="getting-started">
  43. <h1>Getting started<a class="headerlink" href="#getting-started" title="Permalink to this headline"></a></h1>
  44. <p>Welcome to D:YAML! D:YAML is a <a class="reference external" href="http://en.wikipedia.org/wiki/YAML">YAML</a> parser
  45. library for the <a class="reference external" href="http://dlang.org">D programming language</a>. This tutorial will
  46. explain how to set D:YAML up and use it in your projects.</p>
  47. <p>This is meant to be the <strong>simplest possible</strong> introduction to D:YAML. Some of this
  48. information might already be known to you. Only basic usage is covered.</p>
  49. <div class="section" id="setting-up">
  50. <h2>Setting up<a class="headerlink" href="#setting-up" title="Permalink to this headline"></a></h2>
  51. <div class="section" id="install-the-dmd-compiler">
  52. <h3>Install the DMD compiler<a class="headerlink" href="#install-the-dmd-compiler" title="Permalink to this headline"></a></h3>
  53. <p>Digital Mars D compiler, or DMD, is the most commonly used D compiler. You can find its
  54. newest version <a class="reference external" href="http://dlang.org/download.html">here</a>. Download the version of DMD
  55. for your operating system and install it.</p>
  56. <div class="admonition note">
  57. <p class="first admonition-title">Note</p>
  58. <p class="last">Other D compilers exist, such as
  59. <a class="reference external" href="http://gdcproject.org/">GDC</a> and
  60. <a class="reference external" href="http://bitbucket.org/goshawk/gdc/wiki/Home">LDC</a>.</p>
  61. </div>
  62. </div>
  63. <div class="section" id="install-dub">
  64. <h3>Install dub<a class="headerlink" href="#install-dub" title="Permalink to this headline"></a></h3>
  65. <p><a class="reference external" href="http://code.dlang.org/about">dub</a> is a build system and package manager for D.
  66. It is the standard way to manage D projects and their dependencies, compilation and so
  67. on.</p>
  68. <p>DMD may include DUB in future releases, but at this point we need to install it
  69. separately. See
  70. <a class="reference external" href="https://github.com/D-Programming-Language/dub#installation">installation instructions</a>.</p>
  71. </div>
  72. </div>
  73. <div class="section" id="your-first-d-yaml-project">
  74. <h2>Your first D:YAML project<a class="headerlink" href="#your-first-d-yaml-project" title="Permalink to this headline"></a></h2>
  75. <p>Create a directory for your project and in that directory, create a new file named
  76. <tt class="docutils literal"><span class="pre">input.yaml</span></tt> and paste this code into the file:</p>
  77. <div class="highlight-yaml"><div class="highlight"><pre><span class="l-Scalar-Plain">Hello World</span> <span class="p-Indicator">:</span> <span class="p-Indicator">[</span><span class="nv">Hello</span><span class="p-Indicator">,</span> <span class="nv">World</span><span class="p-Indicator">]</span>
  78. <span class="l-Scalar-Plain">Answer</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">42</span>
  79. </pre></div>
  80. </div>
  81. <p>This will serve as input for our example.</p>
  82. <p>Now we need to parse it. Create a new file with name <tt class="docutils literal"><span class="pre">main.d</span></tt>. Paste following code
  83. into the file:</p>
  84. <div class="highlight-d"><div class="highlight"><pre><span class="k">import</span> <span class="n">std</span><span class="p">.</span><span class="n">stdio</span><span class="p">;</span>
  85. <span class="k">import</span> <span class="n">yaml</span><span class="p">;</span>
  86. <span class="kt">void</span> <span class="n">main</span><span class="p">()</span>
  87. <span class="p">{</span>
  88. <span class="c1">//Read the input.</span>
  89. <span class="n">Node</span> <span class="n">root</span> <span class="p">=</span> <span class="n">Loader</span><span class="p">(</span><span class="s">&quot;input.yaml&quot;</span><span class="p">).</span><span class="n">load</span><span class="p">();</span>
  90. <span class="c1">//Display the data read.</span>
  91. <span class="k">foreach</span><span class="p">(</span><span class="nb">string</span> <span class="n">word</span><span class="p">;</span> <span class="n">root</span><span class="p">[</span><span class="s">&quot;Hello World&quot;</span><span class="p">])</span>
  92. <span class="p">{</span>
  93. <span class="n">writeln</span><span class="p">(</span><span class="n">word</span><span class="p">);</span>
  94. <span class="p">}</span>
  95. <span class="n">writeln</span><span class="p">(</span><span class="s">&quot;The answer is &quot;</span><span class="p">,</span> <span class="n">root</span><span class="p">[</span><span class="s">&quot;Answer&quot;</span><span class="p">].</span><span class="n">as</span><span class="p">!</span><span class="kt">int</span><span class="p">);</span>
  96. <span class="c1">//Dump the loaded document to output.yaml.</span>
  97. <span class="n">Dumper</span><span class="p">(</span><span class="s">&quot;output.yaml&quot;</span><span class="p">).</span><span class="n">dump</span><span class="p">(</span><span class="n">root</span><span class="p">);</span>
  98. <span class="p">}</span>
  99. </pre></div>
  100. </div>
  101. <div class="section" id="explanation-of-the-code">
  102. <h3>Explanation of the code<a class="headerlink" href="#explanation-of-the-code" title="Permalink to this headline"></a></h3>
  103. <p>First, we import the <em>dyaml.all</em> module. This is the only D:YAML module you need to
  104. import - it automatically imports all needed modules.</p>
  105. <p>Next we load the file using the <em>Loader.load()</em> method. <em>Loader</em> is a struct used for
  106. parsing YAML documents. The <em>load()</em> method loads the file as <strong>one</strong> YAML document, or
  107. throws <em>YAMLException</em>, D:YAML exception type, if the file could not be parsed or
  108. contains more than one document. Note that we don&#8217;t do any error checking here in order
  109. to keep the example as simple as possible.</p>
  110. <p><em>Node</em> represents a node in a YAML document. It can be a sequence (array), mapping
  111. (associative array) or a scalar (value). Here the root node is a mapping, and we use the
  112. index operator to get subnodes with keys &#8220;Hello World&#8221; and &#8220;Answer&#8221;. We iterate over the
  113. former, as it is a sequence, and use the <em>Node.as()</em> method on the latter to read its
  114. value as an integer.</p>
  115. <p>You can iterate over a mapping or sequence as if it was an associative or normal array,
  116. respectively. If you try to iterate over a scalar, it will throw a <em>YAMLException</em>.</p>
  117. <p>You can iterate using <em>Node</em> as the iterated type, or specify the type iterated nodes
  118. are expected to have. D:YAML will automatically convert to that type if possible. Here
  119. we specify the <em>string</em> type, so we iterate over the &#8220;Hello World&#8221; sequence as an array
  120. of strings. If it is not possible to convert to iterated type, a <em>YAMLException</em> is
  121. thrown. For instance, if we specified <em>int</em> here, we would get an error, as &#8220;Hello&#8221;
  122. cannot be converted to an integer.</p>
  123. <p>The <em>Node.as()</em> method is used to read value of a scalar node as specified type. If the
  124. scalar does not have the specified type, D:YAML will try to convert it, throwing
  125. <em>YAMLException</em> if not possible.</p>
  126. <p>Finally we dump the document we just read to <tt class="docutils literal"><span class="pre">output.yaml</span></tt> with the <em>Dumper.dump()</em>
  127. method. <em>Dumper</em> is a struct used to dump YAML documents. The <em>dump()</em> method writes
  128. one or more documents to a file, throwing <em>YAMLException</em> if the file could not be
  129. written to.</p>
  130. <p>D:YAML tries to preserve style information in documents so e.g. <tt class="docutils literal"><span class="pre">[Hello,</span> <span class="pre">World]</span></tt> is
  131. not turned into:</p>
  132. <div class="line-block">
  133. <div class="line"><tt class="docutils literal"><span class="pre">-</span> <span class="pre">Hello</span></tt></div>
  134. <div class="line"><tt class="docutils literal"><span class="pre">-</span> <span class="pre">World</span></tt></div>
  135. </div>
  136. <p>However, comments are not preserved and neither are any extra formatting whitespace that
  137. doesn&#8217;t affect the meaning of YAML contents.</p>
  138. </div>
  139. <div class="section" id="compiling">
  140. <h3>Compiling<a class="headerlink" href="#compiling" title="Permalink to this headline"></a></h3>
  141. <p>We&#8217;re going to use dub, which we installed at the beginning, to compile our project.</p>
  142. <p>Create a file called <tt class="docutils literal"><span class="pre">dub.json</span></tt> with the following contents:</p>
  143. <div class="highlight-json"><div class="highlight"><pre><span class="p">{</span>
  144. <span class="nt">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;getting-started&quot;</span><span class="p">,</span>
  145. <span class="nt">&quot;targetType&quot;</span><span class="p">:</span> <span class="s2">&quot;executable&quot;</span><span class="p">,</span>
  146. <span class="nt">&quot;sourceFiles&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;main.d&quot;</span><span class="p">],</span>
  147. <span class="nt">&quot;mainSourceFile&quot;</span><span class="p">:</span> <span class="s2">&quot;main.d&quot;</span><span class="p">,</span>
  148. <span class="nt">&quot;dependencies&quot;</span><span class="p">:</span>
  149. <span class="p">{</span>
  150. <span class="nt">&quot;dyaml&quot;</span><span class="p">:</span> <span class="p">{</span> <span class="nt">&quot;version&quot;</span> <span class="p">:</span> <span class="s2">&quot;~&gt;0.5.0&quot;</span> <span class="p">},</span>
  151. <span class="p">},</span>
  152. <span class="p">}</span>
  153. </pre></div>
  154. </div>
  155. <p>This file tells dub that we&#8217;re building an executable called <tt class="docutils literal"><span class="pre">getting-started</span></tt> from
  156. a D source file <tt class="docutils literal"><span class="pre">main.d</span></tt>, and that our project depends on D:YAML 0.5.0 or any newer,
  157. bugfix release of D:YAML 0.5 . DUB will automatically find and download the correct
  158. version of D:YAML when the project is built.</p>
  159. <p>Now run the following command in your project&#8217;s directory:</p>
  160. <div class="highlight-python"><div class="highlight"><pre>dub build
  161. </pre></div>
  162. </div>
  163. <p>dub will automatically download D:YAML and compile it, and then then it will compile our
  164. program. This will generate an executable called <tt class="docutils literal"><span class="pre">getting-started</span></tt> or
  165. <tt class="docutils literal"><span class="pre">getting-started.exe</span></tt> in your directory. When you run it, it should produce the
  166. following output:</p>
  167. <div class="highlight-python"><div class="highlight"><pre>Hello
  168. World
  169. The answer is 42
  170. </pre></div>
  171. </div>
  172. </div>
  173. <div class="section" id="conclusion">
  174. <h3>Conclusion<a class="headerlink" href="#conclusion" title="Permalink to this headline"></a></h3>
  175. <p>You should now have a basic idea about how to use D:YAML. To learn more, look at the
  176. <a class="reference external" href="../api/index.html">API documentation</a> and other tutorials. You can find code for this
  177. example in the <tt class="docutils literal"><span class="pre">example/getting_started</span></tt> directory in the package.</p>
  178. </div>
  179. </div>
  180. </div>
  181. </div>
  182. </div>
  183. </div>
  184. <div class="sphinxsidebar">
  185. <div class="sphinxsidebarwrapper">
  186. <p class="logo"><a href="../index.html">
  187. <img class="logo" src="../_static/logo210.png" alt="Logo"/>
  188. </a></p>
  189. <h3><a href="../index.html">Table Of Contents</a></h3>
  190. <ul>
  191. <li><a class="reference internal" href="#">Getting started</a><ul>
  192. <li><a class="reference internal" href="#setting-up">Setting up</a><ul>
  193. <li><a class="reference internal" href="#install-the-dmd-compiler">Install the DMD compiler</a></li>
  194. <li><a class="reference internal" href="#install-dub">Install dub</a></li>
  195. </ul>
  196. </li>
  197. <li><a class="reference internal" href="#your-first-d-yaml-project">Your first D:YAML project</a><ul>
  198. <li><a class="reference internal" href="#explanation-of-the-code">Explanation of the code</a></li>
  199. <li><a class="reference internal" href="#compiling">Compiling</a></li>
  200. <li><a class="reference internal" href="#conclusion">Conclusion</a></li>
  201. </ul>
  202. </li>
  203. </ul>
  204. </li>
  205. </ul>
  206. </div>
  207. </div>
  208. <div class="clearer"></div>
  209. </div>
  210. <div class="related">
  211. <h3>Navigation</h3>
  212. <ul>
  213. <li class="right" style="margin-right: 10px">
  214. <a href="custom_types.html" title="Custom YAML data types"
  215. >next</a></li>
  216. <li class="right" >
  217. <a href="../index.html" title="Welcome to D:YAML documentation!"
  218. >previous</a> |</li>
  219. <li><a href="../index.html">D:YAML 0.5 documentation</a> &raquo;</li>
  220. </ul>
  221. </div>
  222. <div class="footer">
  223. &copy; Copyright 2011-2014, Ferdinand Majerech. Based on PyYAML http://www.pyyaml.org by Kirill Simonov.
  224. Last updated on Aug 06, 2014.
  225. Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.2.
  226. </div>
  227. </body>
  228. </html>