Regenerated docs.
This commit is contained in:
parent
6ccda33467
commit
41d80096ae
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -302,6 +302,7 @@ such as the color being white.
|
||||||
representer.addRepresenter!Color(&representColor);
|
representer.addRepresenter!Color(&representColor);
|
||||||
|
|
||||||
auto resolver = new Resolver;
|
auto resolver = new Resolver;
|
||||||
|
import std.regex;
|
||||||
resolver.addImplicitResolver("!color", std.regex.regex("[0-9a-fA-F]{6}"),
|
resolver.addImplicitResolver("!color", std.regex.regex("[0-9a-fA-F]{6}"),
|
||||||
"0123456789abcdefABCDEF");
|
"0123456789abcdefABCDEF");
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
===============
|
===============
|
||||||
Getting started
|
Getting started
|
||||||
===============
|
===============
|
||||||
|
|
||||||
Welcome to D:YAML! D:YAML is a `YAML <http://en.wikipedia.org/wiki/YAML>`_
|
Welcome to D:YAML! D:YAML is a `YAML <http://en.wikipedia.org/wiki/YAML>`_ parser
|
||||||
parser library for the `D programming language <http://dlang.org>`_.
|
library for the `D programming language <http://dlang.org>`_. This tutorial will
|
||||||
This tutorial will explain how to set D:YAML up and use it in your projects.
|
explain how to set D:YAML up and use it in your projects.
|
||||||
|
|
||||||
This is meant to be the **simplest possible** introduction to D:YAML. Some of
|
This is meant to be the **simplest possible** introduction to D:YAML. Some of this
|
||||||
this information might already be known to you. Only basic usage is covered.
|
information might already be known to you. Only basic usage is covered.
|
||||||
|
|
||||||
|
|
||||||
----------
|
----------
|
||||||
|
@ -18,59 +18,43 @@ Setting up
|
||||||
Install the DMD compiler
|
Install the DMD compiler
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
Digital Mars D compiler, or DMD, is the most commonly used D compiler. You can
|
Digital Mars D compiler, or DMD, is the most commonly used D compiler. You can find its
|
||||||
find its newest version `here <http://dlang.org/download.html>`_.
|
newest version `here <http://dlang.org/download.html>`_. Download the version of DMD
|
||||||
Download the version of DMD for your operating system and install it.
|
for your operating system and install it.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
Other D compilers exist, such as
|
Other D compilers exist, such as
|
||||||
`GDC <http://gdcproject.org/>`_ and
|
`GDC <http://gdcproject.org/>`_ and
|
||||||
`LDC <http://bitbucket.org/goshawk/gdc/wiki/Home>`_.
|
`LDC <http://bitbucket.org/goshawk/gdc/wiki/Home>`_.
|
||||||
|
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^
|
||||||
Download and compile D:YAML
|
Install dub
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^
|
||||||
|
|
||||||
The newest version of D:YAML can be found
|
`dub <http://code.dlang.org/about>`_ is a build system and package manager for D.
|
||||||
`here <https://github.com/Kiith-Sa/D-YAML>`_. Download a source archive, extract
|
It is the standard way to manage D projects and their dependencies, compilation and so
|
||||||
it, and move to the extracted directory.
|
on.
|
||||||
|
|
||||||
D:YAML uses a modified version of the `CDC <http://dsource.org/projects/cdc/>`_
|
DMD may include DUB in future releases, but at this point we need to install it
|
||||||
script for compilation. To compile D:YAML, you first need to build CDC.
|
separately. See
|
||||||
Do this by typing the following command into the console::
|
`installation instructions <https://github.com/D-Programming-Language/dub#installation>`_.
|
||||||
|
|
||||||
dmd cdc.d
|
|
||||||
|
|
||||||
Now compile D:YAML with CDC.
|
|
||||||
To do this on Unix/Linux, use the following command::
|
|
||||||
|
|
||||||
./cdc
|
|
||||||
|
|
||||||
On Windows::
|
|
||||||
|
|
||||||
cdc.exe
|
|
||||||
|
|
||||||
This will compile the library to a file called ``libdyaml.a`` on Unix/Linux or
|
|
||||||
``libdyaml.lib`` on Windows.
|
|
||||||
|
|
||||||
|
|
||||||
-------------------------
|
-------------------------
|
||||||
Your first D:YAML project
|
Your first D:YAML project
|
||||||
-------------------------
|
-------------------------
|
||||||
|
|
||||||
Create a directory for your project and in that directory, create a file called
|
Create a directory for your project and in that directory, create a new file named
|
||||||
``input.yaml`` with the following contents:
|
``input.yaml`` and paste this code into the file:
|
||||||
|
|
||||||
.. code-block:: yaml
|
.. code-block:: yaml
|
||||||
|
|
||||||
Hello World :
|
Hello World : [Hello, World]
|
||||||
- Hello
|
|
||||||
- World
|
|
||||||
Answer: 42
|
Answer: 42
|
||||||
|
|
||||||
This will serve as input for our example.
|
This will serve as input for our example.
|
||||||
|
|
||||||
Now we need to parse it. Create a file called ``main.d``. Paste following code
|
Now we need to parse it. Create a new file with name ``main.d``. Paste following code
|
||||||
into the file:
|
into the file:
|
||||||
|
|
||||||
.. code-block:: d
|
.. code-block:: d
|
||||||
|
@ -99,83 +83,94 @@ into the file:
|
||||||
Explanation of the code
|
Explanation of the code
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
First, we import the *yaml* module. This is the only D:YAML module you
|
First, we import the *dyaml.all* module. This is the only D:YAML module you need to
|
||||||
need to import - it automatically imports all needed modules.
|
import - it automatically imports all needed modules.
|
||||||
|
|
||||||
Next we load the file using the *Loader.load()* method. *Loader* is a struct
|
Next we load the file using the *Loader.load()* method. *Loader* is a struct used for
|
||||||
used for parsing YAML documents. The *load()* method loads the file as
|
parsing YAML documents. The *load()* method loads the file as **one** YAML document, or
|
||||||
**one** YAML document, or throws *YAMLException*, D:YAML exception type, if the
|
throws *YAMLException*, D:YAML exception type, if the file could not be parsed or
|
||||||
file could not be parsed or does not contain exactly one document. Note that we
|
contains more than one document. Note that we don't do any error checking here in order
|
||||||
don't do any error checking here in order to keep the example as simple as
|
to keep the example as simple as possible.
|
||||||
possible.
|
|
||||||
|
|
||||||
*Node* represents a node in a YAML document. It can be a sequence (array),
|
*Node* represents a node in a YAML document. It can be a sequence (array), mapping
|
||||||
mapping (associative array) or a scalar (value). Here the root node is a
|
(associative array) or a scalar (value). Here the root node is a mapping, and we use the
|
||||||
mapping, and we use the index operator to get subnodes with keys "Hello World"
|
index operator to get subnodes with keys "Hello World" and "Answer". We iterate over the
|
||||||
and "Answer". We iterate over the first, as it is a sequence, and use the
|
former, as it is a sequence, and use the *Node.as()* method on the latter to read its
|
||||||
*Node.as()* method on the second to read its value as an integer.
|
value as an integer.
|
||||||
|
|
||||||
You can iterate over a mapping or sequence as if it was an associative or normal
|
You can iterate over a mapping or sequence as if it was an associative or normal array,
|
||||||
array. If you try to iterate over a scalar, it will throw a *YAMLException*.
|
respectively. If you try to iterate over a scalar, it will throw a *YAMLException*.
|
||||||
|
|
||||||
You can iterate over subnodes using *Node* as the iterated type, or specify
|
You can iterate using *Node* as the iterated type, or specify the type iterated nodes
|
||||||
the type subnodes are expected to have. D:YAML will automatically convert
|
are expected to have. D:YAML will automatically convert to that type if possible. Here
|
||||||
iterated subnodes to that type if possible. Here we specify the *string* type,
|
we specify the *string* type, so we iterate over the "Hello World" sequence as an array
|
||||||
so we iterate over the "Hello World" sequence as an array of strings. If it is
|
of strings. If it is not possible to convert to iterated type, a *YAMLException* is
|
||||||
not possible to convert to iterated type, a *YAMLException* is thrown. For
|
thrown. For instance, if we specified *int* here, we would get an error, as "Hello"
|
||||||
instance, if we specified *int* here, we would get an error, as "Hello"
|
|
||||||
cannot be converted to an integer.
|
cannot be converted to an integer.
|
||||||
|
|
||||||
The *Node.as()* method is used to read value of a scalar node as specified type.
|
The *Node.as()* method is used to read value of a scalar node as specified type. If the
|
||||||
D:YAML will try to return the scalar as this type, converting if needed,
|
scalar does not have the specified type, D:YAML will try to convert it, throwing
|
||||||
throwing *YAMLException* if not possible.
|
*YAMLException* if not possible.
|
||||||
|
|
||||||
Finally we dump the document we just read to ``output.yaml`` with the
|
Finally we dump the document we just read to ``output.yaml`` with the *Dumper.dump()*
|
||||||
*Dumper.dump()* method. *Dumper* is a struct used to dump YAML documents.
|
method. *Dumper* is a struct used to dump YAML documents. The *dump()* method writes
|
||||||
The *dump()* method writes one or more documents to a file, throwing
|
one or more documents to a file, throwing *YAMLException* if the file could not be
|
||||||
*YAMLException* if the file could not be written to.
|
written to.
|
||||||
|
|
||||||
D:YAML doesn't preserve style information in documents, so even though
|
D:YAML tries to preserve style information in documents so e.g. ``[Hello, World]`` is
|
||||||
``output.yaml`` will contain the same data as ``input.yaml``, it might be
|
not turned into:
|
||||||
formatted differently. Comments are not preserved, either.
|
|
||||||
|
| ``- Hello``
|
||||||
|
| ``- World``
|
||||||
|
|
||||||
|
However, comments are not preserved and neither are any extra formatting whitespace that
|
||||||
|
doesn't affect the meaning of YAML contents.
|
||||||
|
|
||||||
|
|
||||||
^^^^^^^^^
|
^^^^^^^^^
|
||||||
Compiling
|
Compiling
|
||||||
^^^^^^^^^
|
^^^^^^^^^
|
||||||
|
|
||||||
To compile your project, DMD needs to know which directories contain the
|
We're going to use dub, which we installed at the beginning, to compile our project.
|
||||||
imported modules and the library. You also need to tell it to link with D:YAML.
|
|
||||||
The import directory should be the ``source`` subdirectory of the D:YAML
|
|
||||||
directory. You can specify it using the ``-I`` option of DMD. The library
|
|
||||||
directory should point to the compiled library. On Unix/Linux you can specify
|
|
||||||
it using the ``-L-L`` option, and link with D:YAML using the ``-L-l`` option.
|
|
||||||
On Windows, the import directory is used as the library directory. To link with
|
|
||||||
the library on Windows, just add the path to it relative to the current
|
|
||||||
directory.
|
|
||||||
|
|
||||||
For example, if you extracted and compiled D:YAML in ``/home/xxx/dyaml``, your
|
Create a file called ``dub.json`` with the following contents:
|
||||||
project is in ``/home/xxx/dyaml-project``, and you are currently in that
|
|
||||||
directory, compile the project with the following command on Unix/Linux::
|
|
||||||
|
|
||||||
dmd -I../dyaml/source -L-L../dyaml -L-ldyaml main.d
|
.. code-block:: json
|
||||||
|
|
||||||
And the following on Windows::
|
{
|
||||||
|
"name": "getting-started",
|
||||||
|
"targetType": "executable",
|
||||||
|
"sourceFiles": ["main.d"],
|
||||||
|
"mainSourceFile": "main.d",
|
||||||
|
"dependencies":
|
||||||
|
{
|
||||||
|
"dyaml": { "version" : "~>0.5.0", "path" : "../../"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
dmd -I../dyaml/source ../dyaml/libdyaml.lib main.d
|
This file tells dub that we're building an executable called ``getting-started`` from
|
||||||
|
a D source file ``main.d``, and that our project depends on D:YAML 0.5.0 or any newer,
|
||||||
|
bugfix release of D:YAML 0.5 . DUB will automatically find and download the correct
|
||||||
|
version of D:YAML when the project is built.
|
||||||
|
|
||||||
This will produce an executable called ``main`` or ``main.exe`` in your
|
Now run the following command in your project's directory::
|
||||||
directory. When you run it, it should produce the following output::
|
|
||||||
|
dub build
|
||||||
|
|
||||||
|
dub will automatically download D:YAML and compile it, and then then it will compile our
|
||||||
|
program. This will generate an executable called ``getting-started`` or
|
||||||
|
``getting-started.exe`` in your directory. When you run it, it should produce the
|
||||||
|
following output::
|
||||||
|
|
||||||
Hello
|
Hello
|
||||||
World
|
World
|
||||||
The answer is 42
|
The answer is 42
|
||||||
|
|
||||||
|
|
||||||
^^^^^^^^^^
|
^^^^^^^^^^
|
||||||
Conclusion
|
Conclusion
|
||||||
^^^^^^^^^^
|
^^^^^^^^^^
|
||||||
|
|
||||||
You should now have a basic idea about how to use D:YAML. To learn more, look at
|
You should now have a basic idea about how to use D:YAML. To learn more, look at the
|
||||||
the `API documentation <../api/index.html>`_ and other tutorials. You can find code for this
|
`API documentation <../api/index.html>`_ and other tutorials. You can find code for this
|
||||||
example in the ``example/getting_started`` directory in the package.
|
example in the ``example/getting_started`` directory in the package.
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -306,6 +306,7 @@ such as the color being white.</p>
|
||||||
<span class="n">representer</span><span class="p">.</span><span class="n">addRepresenter</span><span class="p">!</span><span class="n">Color</span><span class="p">(&</span><span class="n">representColor</span><span class="p">);</span>
|
<span class="n">representer</span><span class="p">.</span><span class="n">addRepresenter</span><span class="p">!</span><span class="n">Color</span><span class="p">(&</span><span class="n">representColor</span><span class="p">);</span>
|
||||||
|
|
||||||
<span class="k">auto</span> <span class="n">resolver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Resolver</span><span class="p">;</span>
|
<span class="k">auto</span> <span class="n">resolver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Resolver</span><span class="p">;</span>
|
||||||
|
<span class="k">import</span> <span class="n">std</span><span class="p">.</span><span class="n">regex</span><span class="p">;</span>
|
||||||
<span class="n">resolver</span><span class="p">.</span><span class="n">addImplicitResolver</span><span class="p">(</span><span class="s">"!color"</span><span class="p">,</span> <span class="n">std</span><span class="p">.</span><span class="n">regex</span><span class="p">.</span><span class="n">regex</span><span class="p">(</span><span class="s">"[0-9a-fA-F]{6}"</span><span class="p">),</span>
|
<span class="n">resolver</span><span class="p">.</span><span class="n">addImplicitResolver</span><span class="p">(</span><span class="s">"!color"</span><span class="p">,</span> <span class="n">std</span><span class="p">.</span><span class="n">regex</span><span class="p">.</span><span class="n">regex</span><span class="p">(</span><span class="s">"[0-9a-fA-F]{6}"</span><span class="p">),</span>
|
||||||
<span class="s">"0123456789abcdefABCDEF"</span><span class="p">);</span>
|
<span class="s">"0123456789abcdefABCDEF"</span><span class="p">);</span>
|
||||||
|
|
||||||
|
|
|
@ -48,18 +48,18 @@
|
||||||
|
|
||||||
<div class="section" id="getting-started">
|
<div class="section" id="getting-started">
|
||||||
<h1>Getting started<a class="headerlink" href="#getting-started" title="Permalink to this headline">¶</a></h1>
|
<h1>Getting started<a class="headerlink" href="#getting-started" title="Permalink to this headline">¶</a></h1>
|
||||||
<p>Welcome to D:YAML! D:YAML is a <a class="reference external" href="http://en.wikipedia.org/wiki/YAML">YAML</a>
|
<p>Welcome to D:YAML! D:YAML is a <a class="reference external" href="http://en.wikipedia.org/wiki/YAML">YAML</a> parser
|
||||||
parser library for the <a class="reference external" href="http://dlang.org">D programming language</a>.
|
library for the <a class="reference external" href="http://dlang.org">D programming language</a>. This tutorial will
|
||||||
This tutorial will explain how to set D:YAML up and use it in your projects.</p>
|
explain how to set D:YAML up and use it in your projects.</p>
|
||||||
<p>This is meant to be the <strong>simplest possible</strong> introduction to D:YAML. Some of
|
<p>This is meant to be the <strong>simplest possible</strong> introduction to D:YAML. Some of this
|
||||||
this information might already be known to you. Only basic usage is covered.</p>
|
information might already be known to you. Only basic usage is covered.</p>
|
||||||
<div class="section" id="setting-up">
|
<div class="section" id="setting-up">
|
||||||
<h2>Setting up<a class="headerlink" href="#setting-up" title="Permalink to this headline">¶</a></h2>
|
<h2>Setting up<a class="headerlink" href="#setting-up" title="Permalink to this headline">¶</a></h2>
|
||||||
<div class="section" id="install-the-dmd-compiler">
|
<div class="section" id="install-the-dmd-compiler">
|
||||||
<h3>Install the DMD compiler<a class="headerlink" href="#install-the-dmd-compiler" title="Permalink to this headline">¶</a></h3>
|
<h3>Install the DMD compiler<a class="headerlink" href="#install-the-dmd-compiler" title="Permalink to this headline">¶</a></h3>
|
||||||
<p>Digital Mars D compiler, or DMD, is the most commonly used D compiler. You can
|
<p>Digital Mars D compiler, or DMD, is the most commonly used D compiler. You can find its
|
||||||
find its newest version <a class="reference external" href="http://dlang.org/download.html">here</a>.
|
newest version <a class="reference external" href="http://dlang.org/download.html">here</a>. Download the version of DMD
|
||||||
Download the version of DMD for your operating system and install it.</p>
|
for your operating system and install it.</p>
|
||||||
<div class="admonition note">
|
<div class="admonition note">
|
||||||
<p class="first admonition-title">Note</p>
|
<p class="first admonition-title">Note</p>
|
||||||
<p class="last">Other D compilers exist, such as
|
<p class="last">Other D compilers exist, such as
|
||||||
|
@ -67,42 +67,26 @@ Download the version of DMD for your operating system and install it.</p>
|
||||||
<a class="reference external" href="http://bitbucket.org/goshawk/gdc/wiki/Home">LDC</a>.</p>
|
<a class="reference external" href="http://bitbucket.org/goshawk/gdc/wiki/Home">LDC</a>.</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" id="download-and-compile-d-yaml">
|
<div class="section" id="install-dub">
|
||||||
<h3>Download and compile D:YAML<a class="headerlink" href="#download-and-compile-d-yaml" title="Permalink to this headline">¶</a></h3>
|
<h3>Install dub<a class="headerlink" href="#install-dub" title="Permalink to this headline">¶</a></h3>
|
||||||
<p>The newest version of D:YAML can be found
|
<p><a class="reference external" href="http://code.dlang.org/about">dub</a> is a build system and package manager for D.
|
||||||
<a class="reference external" href="https://github.com/Kiith-Sa/D-YAML">here</a>. Download a source archive, extract
|
It is the standard way to manage D projects and their dependencies, compilation and so
|
||||||
it, and move to the extracted directory.</p>
|
on.</p>
|
||||||
<p>D:YAML uses a modified version of the <a class="reference external" href="http://dsource.org/projects/cdc/">CDC</a>
|
<p>DMD may include DUB in future releases, but at this point we need to install it
|
||||||
script for compilation. To compile D:YAML, you first need to build CDC.
|
separately. See
|
||||||
Do this by typing the following command into the console:</p>
|
<a class="reference external" href="https://github.com/D-Programming-Language/dub#installation">installation instructions</a>.</p>
|
||||||
<div class="highlight-python"><div class="highlight"><pre>dmd cdc.d
|
|
||||||
</pre></div>
|
|
||||||
</div>
|
|
||||||
<p>Now compile D:YAML with CDC.
|
|
||||||
To do this on Unix/Linux, use the following command:</p>
|
|
||||||
<div class="highlight-python"><div class="highlight"><pre>./cdc
|
|
||||||
</pre></div>
|
|
||||||
</div>
|
|
||||||
<p>On Windows:</p>
|
|
||||||
<div class="highlight-python"><div class="highlight"><pre><span class="n">cdc</span><span class="o">.</span><span class="n">exe</span>
|
|
||||||
</pre></div>
|
|
||||||
</div>
|
|
||||||
<p>This will compile the library to a file called <tt class="docutils literal"><span class="pre">libdyaml.a</span></tt> on Unix/Linux or
|
|
||||||
<tt class="docutils literal"><span class="pre">libdyaml.lib</span></tt> on Windows.</p>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" id="your-first-d-yaml-project">
|
<div class="section" id="your-first-d-yaml-project">
|
||||||
<h2>Your first D:YAML project<a class="headerlink" href="#your-first-d-yaml-project" title="Permalink to this headline">¶</a></h2>
|
<h2>Your first D:YAML project<a class="headerlink" href="#your-first-d-yaml-project" title="Permalink to this headline">¶</a></h2>
|
||||||
<p>Create a directory for your project and in that directory, create a file called
|
<p>Create a directory for your project and in that directory, create a new file named
|
||||||
<tt class="docutils literal"><span class="pre">input.yaml</span></tt> with the following contents:</p>
|
<tt class="docutils literal"><span class="pre">input.yaml</span></tt> and paste this code into the file:</p>
|
||||||
<div class="highlight-yaml"><div class="highlight"><pre><span class="l-Scalar-Plain">Hello World</span> <span class="p-Indicator">:</span>
|
<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>
|
||||||
<span class="p-Indicator">-</span> <span class="l-Scalar-Plain">Hello</span>
|
|
||||||
<span class="p-Indicator">-</span> <span class="l-Scalar-Plain">World</span>
|
|
||||||
<span class="l-Scalar-Plain">Answer</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">42</span>
|
<span class="l-Scalar-Plain">Answer</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">42</span>
|
||||||
</pre></div>
|
</pre></div>
|
||||||
</div>
|
</div>
|
||||||
<p>This will serve as input for our example.</p>
|
<p>This will serve as input for our example.</p>
|
||||||
<p>Now we need to parse it. Create a file called <tt class="docutils literal"><span class="pre">main.d</span></tt>. Paste following code
|
<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
|
||||||
into the file:</p>
|
into the file:</p>
|
||||||
<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>
|
<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>
|
||||||
<span class="k">import</span> <span class="n">yaml</span><span class="p">;</span>
|
<span class="k">import</span> <span class="n">yaml</span><span class="p">;</span>
|
||||||
|
@ -126,62 +110,70 @@ into the file:</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" id="explanation-of-the-code">
|
<div class="section" id="explanation-of-the-code">
|
||||||
<h3>Explanation of the code<a class="headerlink" href="#explanation-of-the-code" title="Permalink to this headline">¶</a></h3>
|
<h3>Explanation of the code<a class="headerlink" href="#explanation-of-the-code" title="Permalink to this headline">¶</a></h3>
|
||||||
<p>First, we import the <em>yaml</em> module. This is the only D:YAML module you
|
<p>First, we import the <em>dyaml.all</em> module. This is the only D:YAML module you need to
|
||||||
need to import - it automatically imports all needed modules.</p>
|
import - it automatically imports all needed modules.</p>
|
||||||
<p>Next we load the file using the <em>Loader.load()</em> method. <em>Loader</em> is a struct
|
<p>Next we load the file using the <em>Loader.load()</em> method. <em>Loader</em> is a struct used for
|
||||||
used for parsing YAML documents. The <em>load()</em> method loads the file as
|
parsing YAML documents. The <em>load()</em> method loads the file as <strong>one</strong> YAML document, or
|
||||||
<strong>one</strong> YAML document, or throws <em>YAMLException</em>, D:YAML exception type, if the
|
throws <em>YAMLException</em>, D:YAML exception type, if the file could not be parsed or
|
||||||
file could not be parsed or does not contain exactly one document. Note that we
|
contains more than one document. Note that we don’t do any error checking here in order
|
||||||
don’t do any error checking here in order to keep the example as simple as
|
to keep the example as simple as possible.</p>
|
||||||
possible.</p>
|
<p><em>Node</em> represents a node in a YAML document. It can be a sequence (array), mapping
|
||||||
<p><em>Node</em> represents a node in a YAML document. It can be a sequence (array),
|
(associative array) or a scalar (value). Here the root node is a mapping, and we use the
|
||||||
mapping (associative array) or a scalar (value). Here the root node is a
|
index operator to get subnodes with keys “Hello World” and “Answer”. We iterate over the
|
||||||
mapping, and we use the index operator to get subnodes with keys “Hello World”
|
former, as it is a sequence, and use the <em>Node.as()</em> method on the latter to read its
|
||||||
and “Answer”. We iterate over the first, as it is a sequence, and use the
|
value as an integer.</p>
|
||||||
<em>Node.as()</em> method on the second to read its value as an integer.</p>
|
<p>You can iterate over a mapping or sequence as if it was an associative or normal array,
|
||||||
<p>You can iterate over a mapping or sequence as if it was an associative or normal
|
respectively. If you try to iterate over a scalar, it will throw a <em>YAMLException</em>.</p>
|
||||||
array. If you try to iterate over a scalar, it will throw a <em>YAMLException</em>.</p>
|
<p>You can iterate using <em>Node</em> as the iterated type, or specify the type iterated nodes
|
||||||
<p>You can iterate over subnodes using <em>Node</em> as the iterated type, or specify
|
are expected to have. D:YAML will automatically convert to that type if possible. Here
|
||||||
the type subnodes are expected to have. D:YAML will automatically convert
|
we specify the <em>string</em> type, so we iterate over the “Hello World” sequence as an array
|
||||||
iterated subnodes to that type if possible. Here we specify the <em>string</em> type,
|
of strings. If it is not possible to convert to iterated type, a <em>YAMLException</em> is
|
||||||
so we iterate over the “Hello World” sequence as an array of strings. If it is
|
thrown. For instance, if we specified <em>int</em> here, we would get an error, as “Hello”
|
||||||
not possible to convert to iterated type, a <em>YAMLException</em> is thrown. For
|
|
||||||
instance, if we specified <em>int</em> here, we would get an error, as “Hello”
|
|
||||||
cannot be converted to an integer.</p>
|
cannot be converted to an integer.</p>
|
||||||
<p>The <em>Node.as()</em> method is used to read value of a scalar node as specified type.
|
<p>The <em>Node.as()</em> method is used to read value of a scalar node as specified type. If the
|
||||||
D:YAML will try to return the scalar as this type, converting if needed,
|
scalar does not have the specified type, D:YAML will try to convert it, throwing
|
||||||
throwing <em>YAMLException</em> if not possible.</p>
|
<em>YAMLException</em> if not possible.</p>
|
||||||
<p>Finally we dump the document we just read to <tt class="docutils literal"><span class="pre">output.yaml</span></tt> with the
|
<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>
|
||||||
<em>Dumper.dump()</em> method. <em>Dumper</em> is a struct used to dump YAML documents.
|
method. <em>Dumper</em> is a struct used to dump YAML documents. The <em>dump()</em> method writes
|
||||||
The <em>dump()</em> method writes one or more documents to a file, throwing
|
one or more documents to a file, throwing <em>YAMLException</em> if the file could not be
|
||||||
<em>YAMLException</em> if the file could not be written to.</p>
|
written to.</p>
|
||||||
<p>D:YAML doesn’t preserve style information in documents, so even though
|
<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
|
||||||
<tt class="docutils literal"><span class="pre">output.yaml</span></tt> will contain the same data as <tt class="docutils literal"><span class="pre">input.yaml</span></tt>, it might be
|
not turned into:</p>
|
||||||
formatted differently. Comments are not preserved, either.</p>
|
<div class="line-block">
|
||||||
|
<div class="line"><tt class="docutils literal"><span class="pre">-</span> <span class="pre">Hello</span></tt></div>
|
||||||
|
<div class="line"><tt class="docutils literal"><span class="pre">-</span> <span class="pre">World</span></tt></div>
|
||||||
|
</div>
|
||||||
|
<p>However, comments are not preserved and neither are any extra formatting whitespace that
|
||||||
|
doesn’t affect the meaning of YAML contents.</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" id="compiling">
|
<div class="section" id="compiling">
|
||||||
<h3>Compiling<a class="headerlink" href="#compiling" title="Permalink to this headline">¶</a></h3>
|
<h3>Compiling<a class="headerlink" href="#compiling" title="Permalink to this headline">¶</a></h3>
|
||||||
<p>To compile your project, DMD needs to know which directories contain the
|
<p>We’re going to use dub, which we installed at the beginning, to compile our project.</p>
|
||||||
imported modules and the library. You also need to tell it to link with D:YAML.
|
<p>Create a file called <tt class="docutils literal"><span class="pre">dub.json</span></tt> with the following contents:</p>
|
||||||
The import directory should be the <tt class="docutils literal"><span class="pre">source</span></tt> subdirectory of the D:YAML
|
<div class="highlight-json"><div class="highlight"><pre><span class="p">{</span>
|
||||||
directory. You can specify it using the <tt class="docutils literal"><span class="pre">-I</span></tt> option of DMD. The library
|
<span class="nt">"name"</span><span class="p">:</span> <span class="s2">"getting-started"</span><span class="p">,</span>
|
||||||
directory should point to the compiled library. On Unix/Linux you can specify
|
<span class="nt">"targetType"</span><span class="p">:</span> <span class="s2">"executable"</span><span class="p">,</span>
|
||||||
it using the <tt class="docutils literal"><span class="pre">-L-L</span></tt> option, and link with D:YAML using the <tt class="docutils literal"><span class="pre">-L-l</span></tt> option.
|
<span class="nt">"sourceFiles"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"main.d"</span><span class="p">],</span>
|
||||||
On Windows, the import directory is used as the library directory. To link with
|
<span class="nt">"mainSourceFile"</span><span class="p">:</span> <span class="s2">"main.d"</span><span class="p">,</span>
|
||||||
the library on Windows, just add the path to it relative to the current
|
<span class="nt">"dependencies"</span><span class="p">:</span>
|
||||||
directory.</p>
|
<span class="p">{</span>
|
||||||
<p>For example, if you extracted and compiled D:YAML in <tt class="docutils literal"><span class="pre">/home/xxx/dyaml</span></tt>, your
|
<span class="nt">"dyaml"</span><span class="p">:</span> <span class="p">{</span> <span class="nt">"version"</span> <span class="p">:</span> <span class="s2">"~>0.5.0"</span><span class="p">,</span> <span class="nt">"path"</span> <span class="p">:</span> <span class="s2">"../../"</span><span class="p">},</span>
|
||||||
project is in <tt class="docutils literal"><span class="pre">/home/xxx/dyaml-project</span></tt>, and you are currently in that
|
<span class="p">},</span>
|
||||||
directory, compile the project with the following command on Unix/Linux:</p>
|
<span class="p">}</span>
|
||||||
<div class="highlight-python"><div class="highlight"><pre>dmd -I../dyaml/source -L-L../dyaml -L-ldyaml main.d
|
|
||||||
</pre></div>
|
</pre></div>
|
||||||
</div>
|
</div>
|
||||||
<p>And the following on Windows:</p>
|
<p>This file tells dub that we’re building an executable called <tt class="docutils literal"><span class="pre">getting-started</span></tt> from
|
||||||
<div class="highlight-python"><div class="highlight"><pre>dmd -I../dyaml/source ../dyaml/libdyaml.lib main.d
|
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,
|
||||||
|
bugfix release of D:YAML 0.5 . DUB will automatically find and download the correct
|
||||||
|
version of D:YAML when the project is built.</p>
|
||||||
|
<p>Now run the following command in your project’s directory:</p>
|
||||||
|
<div class="highlight-python"><div class="highlight"><pre>dub build
|
||||||
</pre></div>
|
</pre></div>
|
||||||
</div>
|
</div>
|
||||||
<p>This will produce an executable called <tt class="docutils literal"><span class="pre">main</span></tt> or <tt class="docutils literal"><span class="pre">main.exe</span></tt> in your
|
<p>dub will automatically download D:YAML and compile it, and then then it will compile our
|
||||||
directory. When you run it, it should produce the following output:</p>
|
program. This will generate an executable called <tt class="docutils literal"><span class="pre">getting-started</span></tt> or
|
||||||
|
<tt class="docutils literal"><span class="pre">getting-started.exe</span></tt> in your directory. When you run it, it should produce the
|
||||||
|
following output:</p>
|
||||||
<div class="highlight-python"><div class="highlight"><pre>Hello
|
<div class="highlight-python"><div class="highlight"><pre>Hello
|
||||||
World
|
World
|
||||||
The answer is 42
|
The answer is 42
|
||||||
|
@ -190,8 +182,8 @@ The answer is 42
|
||||||
</div>
|
</div>
|
||||||
<div class="section" id="conclusion">
|
<div class="section" id="conclusion">
|
||||||
<h3>Conclusion<a class="headerlink" href="#conclusion" title="Permalink to this headline">¶</a></h3>
|
<h3>Conclusion<a class="headerlink" href="#conclusion" title="Permalink to this headline">¶</a></h3>
|
||||||
<p>You should now have a basic idea about how to use D:YAML. To learn more, look at
|
<p>You should now have a basic idea about how to use D:YAML. To learn more, look at the
|
||||||
the <a class="reference external" href="../api/index.html">API documentation</a> and other tutorials. You can find code for this
|
<a class="reference external" href="../api/index.html">API documentation</a> and other tutorials. You can find code for this
|
||||||
example in the <tt class="docutils literal"><span class="pre">example/getting_started</span></tt> directory in the package.</p>
|
example in the <tt class="docutils literal"><span class="pre">example/getting_started</span></tt> directory in the package.</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -211,7 +203,7 @@ example in the <tt class="docutils literal"><span class="pre">example/getting_st
|
||||||
<li><a class="reference internal" href="#">Getting started</a><ul>
|
<li><a class="reference internal" href="#">Getting started</a><ul>
|
||||||
<li><a class="reference internal" href="#setting-up">Setting up</a><ul>
|
<li><a class="reference internal" href="#setting-up">Setting up</a><ul>
|
||||||
<li><a class="reference internal" href="#install-the-dmd-compiler">Install the DMD compiler</a></li>
|
<li><a class="reference internal" href="#install-the-dmd-compiler">Install the DMD compiler</a></li>
|
||||||
<li><a class="reference internal" href="#download-and-compile-d-yaml">Download and compile D:YAML</a></li>
|
<li><a class="reference internal" href="#install-dub">Install dub</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</li>
|
</li>
|
||||||
<li><a class="reference internal" href="#your-first-d-yaml-project">Your first D:YAML project</a><ul>
|
<li><a class="reference internal" href="#your-first-d-yaml-project">Your first D:YAML project</a><ul>
|
||||||
|
|
Loading…
Reference in a new issue