1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
/*!
# Map to JavaScript in HTML

This is a library for serializing a map to JavaScript code in HTML, usually for dynamically generating strings on web pages.

## Usage

In your HTML or templates to generate HTML, such as Handlebars, for instance,

```html
<script>
var _text = {};
{{{text}}}
</script>
```

Then, you can use the `MapToJavaScriptHTML` trait to insert your text from a map,

```rust
extern crate map_to_javascript_html;

use std::collections::BTreeMap;

use map_to_javascript_html::MapToJavaScriptHTML;

let mut map = BTreeMap::new();

map.insert("hello", "Hello world!");
map.insert("welcome", "Welcome to my website.");
map.insert("other keys", "Hello world!");

let text = map.to_javascript_html("_text");

assert_eq!("_text['hello']='Hello world!';_text['other keys']='Hello world!';_text['welcome']='Welcome to my website.';", text);
```

After Handlebars replaces **{{{text}}}** with your text, the HTML will be,

```html
<script>
var _text = {};
_text['hello']='Hello world!';_text['other keys']='Hello world!';_text['welcome']='Welcome to my website.';
</script>
```

The key and the value used in a map must implement the `Display` trait.

Methods suffixed with `_to_string`, `_to_vec`, `_to_writer` can be used to generate HTML.

There are also methods prefixed with `to_javascript_html_with_keys` which can be used with keys to filter the output.

```rust
extern crate map_to_javascript_html;

use std::collections::BTreeMap;

use map_to_javascript_html::MapToJavaScriptHTML;

let mut map = BTreeMap::new();

map.insert("hello", "Hello world!");
map.insert("welcome", "Welcome to my website.");
map.insert("other keys", "Hello world!");

let text = map.to_javascript_html_with_keys("_text", &["hello", "welcome"]);

assert_eq!("_text['hello']='Hello world!';_text['welcome']='Welcome to my website.';", text);
```

## No Std

Disable the default features to compile this crate without std.

```toml
[dependencies.map-to-javascript-html]
version = "*"
default-features = false
```

## Serde Support

To support the maps from the `serde` framework, enable the `serde` feature.

```toml
[dependencies.map-to-javascript-html]
version = "*"
features = ["serde"]
```
*/

#![cfg_attr(not(feature = "std"), no_std)]

#[macro_use]
extern crate alloc;

extern crate html_escape;

mod maps;
mod traits;

pub use maps::*;
pub use traits::*;