Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/veddan/rust-htmlescape
A HTML entity encoding library for Rust
https://github.com/veddan/rust-htmlescape
Last synced: 3 months ago
JSON representation
A HTML entity encoding library for Rust
- Host: GitHub
- URL: https://github.com/veddan/rust-htmlescape
- Owner: veddan
- Created: 2013-07-30T22:17:27.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2023-05-31T17:48:13.000Z (over 1 year ago)
- Last Synced: 2024-09-19T20:38:40.261Z (4 months ago)
- Language: Rust
- Homepage:
- Size: 101 KB
- Stars: 41
- Watchers: 5
- Forks: 13
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-rust-cn - veddan/rust-htmlescape - ci.org/veddan/rust-htmlescape.svg?branch=master">](https://travis-ci.org/veddan/rust-htmlescape) (Libraries / Encoding)
- awesome-rust - veddan/rust-htmlescape - ci.org/veddan/rust-htmlescape.svg?branch=master">](https://travis-ci.org/veddan/rust-htmlescape) (Libraries / Encoding)
- awesome-rust-zh - veddan/rust-htmlescape - 编码/解码 HTML 条目[<img src="https://api.travis-ci.org/veddan/rust-htmlescape.svg?branch=master">](https://travis-ci.org/veddan/rust-htmlescape) (库 / 编码(Encoding))
- awesome-rust - veddan/rust-htmlescape - ci.org/veddan/rust-htmlescape.svg?branch=master">](https://travis-ci.org/veddan/rust-htmlescape) (库 Libraries / 加密 Encoding)
README
# A HTML entity encoding library for Rust
[![Build Status](https://travis-ci.org/veddan/rust-htmlescape.png?branch=master)](https://travis-ci.org/veddan/rust-htmlescape)## Example usage
All example assume a `extern crate htmlescape;` and `use htmlescape::{relevant functions here};` is present.###Encoding
`htmlescape::encode_minimal()` encodes an input string using a minimal set of HTML entities.```rust
let title = "Cats & dogs";
let tag = format!("{}", encode_minimal(title));
assert_eq!(tag.as_slice(), "Cats & dogs");
```There is also a `htmlescape::encode_attribute()` function for encoding strings that are to be used
as html attribute values.###Decoding
`htmlescape::decode_html()` decodes an encoded string, replacing HTML entities with the
corresponding characters. Named, hex, and decimal entities are supported. A `Result` value is
returned, with either the decoded string in `Ok`, or an error in `Err`.```rust
let encoded = "Cats & dogs";
let decoded = match decode_html(encoded) {
Err(reason) => panic!("Error {:?} at character {}", reason.kind, reason.position),
Ok(s) => s
};
assert_eq!(decoded.as_slice(), "Cats & dogs");
```###Avoiding allocations
Both the encoding and decoding functions are available in forms that take a `Writer` for output rather
than returning an `String`. These version can be used to avoid allocation and copying if the returned
`String` was just going to be written to a `Writer` anyway.