https://github.com/fefit/htmlentity
A library for encoding and decoding html entities.
https://github.com/fefit/htmlentity
Last synced: 6 months ago
JSON representation
A library for encoding and decoding html entities.
- Host: GitHub
- URL: https://github.com/fefit/htmlentity
- Owner: fefit
- License: mit
- Created: 2020-11-18T14:10:33.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-06-07T07:17:40.000Z (about 2 years ago)
- Last Synced: 2025-10-21T02:51:21.446Z (8 months ago)
- Language: Rust
- Homepage:
- Size: 218 KB
- Stars: 6
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# htmlentity
A library for encoding and decoding HTML entities.
[](https://docs.rs/htmlentity)
[](https://github.com/fefit/htmlentity/actions)
[](https://codecov.io/gh/fefit/htmlentity)
## How to use
```rust
use htmlentity::entity::{ encode, decode, EncodeType, CharacterSet, ICodedDataTrait };
use htmlentity::types::{ AnyhowResult, Byte };
fn main() -> AnyhowResult<()> {
let html = "
Hello!世界!";
let html_after_encoded = "<div name='htmlentity'>Hello!世界!</div>";
// encode
let encoded_data = encode(
html.as_bytes(),
// encode format
&EncodeType::NamedOrHex,
// charcters need to be encoded
&CharacterSet::HtmlAndNonASCII
);
assert_eq!(encoded_data.to_bytes(), html_after_encoded.as_bytes());
assert_eq!(encoded_data.to_string()?, String::from(html_after_encoded));
let char_list = String::from(html_after_encoded).chars().collect::>();
assert_eq!(encoded_data.to_chars()?, char_list);
// decode
let bytes = encoded_data.into_iter().map(|(byte, _)| *byte).collect::>();
let decoded_data = decode(&bytes);
assert_eq!(decoded_data.to_bytes(), html.as_bytes());
assert_eq!(decoded_data.to_string()?, String::from(html));
let char_list = String::from(html).chars().collect::>();
assert_eq!(decoded_data.to_chars()?, char_list);
// shortcut usage
assert_eq!(
encode(
html.as_bytes(),
&EncodeType::NamedOrHex,
&CharacterSet::HtmlAndNonASCII,
).to_string()?,
String::from(html)
);
Ok(())
}
```
For more details, please see the document in [Docs.rs](https://docs.rs/htmlentity)
## License
[MIT License](./LICENSE).