Skip to content

Commit

Permalink
to_binary => to_iodata, update README.md, doc
Browse files Browse the repository at this point in the history
  • Loading branch information
soranoba committed May 8, 2015
1 parent fd41a09 commit 0d90a65
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ Associative array
<<"hoge">>
```

### More information
You want more information, see the [doc](doc).

## Attention
- The number of new line.
- New line in the template has left all.
Expand Down
54 changes: 45 additions & 9 deletions doc/mustache.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Copyright (c) 2015 Hinagiku Soranoba All Rights Reserved.


<pre><code>
assoc_data() = [{<a href="#type-data_key">data_key()</a>, <a href="#type-data_value">data_value()</a>}]
assoc_data() = [{atom(), <a href="#type-data_value">data_value()</a>}] | [{binary(), <a href="#type-data_value">data_value()</a>}] | [{string(), <a href="#type-data_value">data_value()</a>}]
</code></pre>


Expand All @@ -42,24 +42,38 @@ data() = <a href="#type-assoc_data">assoc_data()</a>



### <a name="type-data_key">data_key()</a> ###
### <a name="type-data_value">data_value()</a> ###



<pre><code>
data_key() = string()
data_value() = <a href="#type-data">data()</a> | iodata() | number() | atom() | fun((<a href="#type-data">data()</a>, function()) -&gt; iodata())
</code></pre>





### <a name="type-data_value">data_value()</a> ###
### <a name="type-option">option()</a> ###



<pre><code>
option() = {key_type, atom | binary | string}
</code></pre>



- key_type: Specify the type of the key in [`data/0`](#data-0). Default value is `string`.



### <a name="type-options">options()</a> ###



<pre><code>
data_value() = <a href="#type-data">data()</a> | iodata() | fun((<a href="#type-data">data()</a>, function()) -&gt; iodata())
options() = [<a href="#type-option">option()</a>]
</code></pre>


Expand All @@ -76,7 +90,7 @@ __abstract datatype__: `template()`
## Function Index ##


<table width="100%" border="1" cellspacing="0" cellpadding="2" summary="function index"><tr><td valign="top"><a href="#compile-2">compile/2</a></td><td>Embed the data in the template.</td></tr><tr><td valign="top"><a href="#parse_binary-1">parse_binary/1</a></td><td>Create a <a href="#template-0"><code>template/0</code></a> from a binary.</td></tr><tr><td valign="top"><a href="#parse_file-1">parse_file/1</a></td><td>Create a <a href="#template-0"><code>template/0</code></a> from a file.</td></tr><tr><td valign="top"><a href="#render-2">render/2</a></td><td>Equivalent to <a href="#compile-2"><tt>compile(parse_binary(Bin), Data)</tt></a>.</td></tr></table>
<table width="100%" border="1" cellspacing="0" cellpadding="2" summary="function index"><tr><td valign="top"><a href="#compile-2">compile/2</a></td><td>Equivalent to <a href="#compile-3"><tt>compile(Template, Data, [])</tt></a>.</td></tr><tr><td valign="top"><a href="#compile-3">compile/3</a></td><td>Embed the data in the template.</td></tr><tr><td valign="top"><a href="#parse_binary-1">parse_binary/1</a></td><td>Create a <a href="#template-0"><code>template/0</code></a> from a binary.</td></tr><tr><td valign="top"><a href="#parse_file-1">parse_file/1</a></td><td>Create a <a href="#template-0"><code>template/0</code></a> from a file.</td></tr><tr><td valign="top"><a href="#render-2">render/2</a></td><td>Equivalent to <a href="#render-3"><tt>render(Bin, Data, [])</tt></a>.</td></tr><tr><td valign="top"><a href="#render-3">render/3</a></td><td>Equivalent to <a href="#compile-3"><tt>compile(parse_binary(Bin), Data, Options)</tt></a>.</td></tr></table>


<a name="functions"></a>
Expand All @@ -89,7 +103,18 @@ __abstract datatype__: `template()`


<pre><code>
compile(Mustache::<a href="#type-template">template()</a>, Data::<a href="#type-data">data()</a>) -&gt; binary()
compile(Template::<a href="#type-template">template()</a>, Data::<a href="#type-data">data()</a>) -&gt; binary()
</code></pre>
<br />

Equivalent to [`compile(Template, Data, [])`](#compile-3).
<a name="compile-3"></a>

### compile/3 ###


<pre><code>
compile(Mustache::<a href="#type-template">template()</a>, Data::<a href="#type-data">data()</a>, Options::<a href="#type-options">options()</a>) -&gt; binary()
</code></pre>
<br />

Expand All @@ -111,7 +136,7 @@ Create a [`template/0`](#template-0) from a binary.


<pre><code>
parse_file(Filename::<a href="file.md#type-filename">file:filename()</a>) -&gt; <a href="#type-template">template()</a>
parse_file(Filename::<a href="file.md#type-filename_all">file:filename_all()</a>) -&gt; <a href="#type-template">template()</a>
</code></pre>
<br />

Expand All @@ -126,6 +151,17 @@ render(Bin::binary(), Data::<a href="#type-data">data()</a>) -&gt; binary()
</code></pre>
<br />

Equivalent to [`compile(parse_binary(Bin), Data)`](#compile-2).
Equivalent to [`render(Bin, Data, [])`](#render-3).

__See also:__ [compile/2](#compile-2), [parse_binary/1](#parse_binary-1), [parse_file/1](#parse_file-1), [render/2](#render-2).
<a name="render-3"></a>

### render/3 ###


<pre><code>
render(Bin::binary(), Data::<a href="#type-data">data()</a>, Options::<a href="#type-options">options()</a>) -&gt; binary()
</code></pre>
<br />

Equivalent to [`compile(parse_binary(Bin), Data, Options)`](#compile-3).
16 changes: 8 additions & 8 deletions src/mustache.erl
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ compile(#?MODULE{data = Tags} = T, Data, Options) ->
compile_impl([], _, Result, _) ->
Result;
compile_impl([{n, Key} | T], Map, Result, Options) ->
compile_impl(T, Map, [escape(to_binary(data_get(convert_keytype(Key, Options), Map, <<>>))) | Result], Options);
compile_impl(T, Map, [escape(to_iodata(data_get(convert_keytype(Key, Options), Map, <<>>))) | Result], Options);
compile_impl([{'&', Key} | T], Map, Result, Options) ->
compile_impl(T, Map, [to_binary(data_get(convert_keytype(Key, Options), Map, <<>>)) | Result], Options);
compile_impl(T, Map, [to_iodata(data_get(convert_keytype(Key, Options), Map, <<>>)) | Result], Options);
compile_impl([{'#', Key, Tags, Source} | T], Map, Result, Options) ->
Value = data_get(convert_keytype(Key, Options), Map, undefined),
case check_data_type(Value) of
Expand Down Expand Up @@ -279,15 +279,15 @@ remove_space_from_tail_impl([{X, Y} | T], Size) when Size =:= X + Y ->
remove_space_from_tail_impl(_, Size) ->
Size.

%% @doc Number to binary
-spec to_binary(number() | binary() | string() | atom()) -> binary() | string().
to_binary(Integer) when is_integer(Integer) ->
%% @doc term to iodata
-spec to_iodata(number() | binary() | string() | atom()) -> binary() | string().
to_iodata(Integer) when is_integer(Integer) ->
list_to_binary(integer_to_list(Integer));
to_binary(Float) when is_float(Float) ->
to_iodata(Float) when is_float(Float) ->
io_lib:format("~p", [Float]);
to_binary(Atom) when is_atom(Atom) ->
to_iodata(Atom) when is_atom(Atom) ->
list_to_binary(atom_to_list(Atom));
to_binary(X) ->
to_iodata(X) ->
X.

%% @doc HTML Escape
Expand Down

0 comments on commit 0d90a65

Please sign in to comment.