Git Product home page Git Product logo

rust-htmlescape's Introduction

A HTML entity encoding library for Rust

Build Status

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.

let title = "Cats & dogs";
let tag = format!("<title>{}</title>", encode_minimal(title));
assert_eq!(tag.as_slice(), "<title>Cats &amp; dogs</title>");

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.

let encoded = "Cats&#x20;&amp;&#32;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.

rust-htmlescape's People

Contributors

gilescope avatar torhve avatar veddan avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.