Trait write_html::HtmlEnv

source ·
pub trait HtmlEnv: Write + Sized {
    // Provided methods
    fn with(&mut self, html: impl Html) -> Result<&mut Self, Error> { ... }
    fn doctype(&mut self) { ... }
    fn text<'s>(&'s mut self) -> HtmlEscaper<'s, Self> { ... }
    fn tag<'s, 't>(
        &'s mut self,
        tag: &'t str,
        compactability: Compactability
    ) -> Result<TagOpening<'s, 't, Self>, Error> { ... }
    fn html_root<'s>(
        &'s mut self,
        lang: &str
    ) -> Result<InsideTagHtml<'s, 'static, Self>, Error> { ... }
    fn html5_default_meta(&mut self) -> Result<&mut Self, Error> { ... }
}
Expand description

Represents an environment that can write HTML.

This trait is implemented for everything that implements Write, for example String.

Provided Methods§

source

fn with(&mut self, html: impl Html) -> Result<&mut Self, Error>

Writes an Html into the environment.

Example
use write_html::{HtmlEnv, Html, AsHtml};
 
let mut s = String::new();
s.with("Hello, world!".as_html()).unwrap();
assert_eq!(s, "Hello, world!");
source

fn doctype(&mut self)

Writes the HTML5 doctype.

Example
use write_html::HtmlEnv;
 
let mut s = String::new();
s.doctype();
assert_eq!(s, "<!DOCTYPE html>");
source

fn text<'s>(&'s mut self) -> HtmlEscaper<'s, Self>

Lets you write text into the HTML document, escaping it as necessary.

Example
use write_html::HtmlEnv;
use std::fmt::Write;
 
let mut s = String::new();
s.text().write_str("Hello, <world>").unwrap();
assert_eq!(s, "Hello, &lt;world&gt;");
source

fn tag<'s, 't>( &'s mut self, tag: &'t str, compactability: Compactability ) -> Result<TagOpening<'s, 't, Self>, Error>

Returns a tag opening, which lets you write attributes and inner HTML.

Arguments
  • tag - The tag name.
  • compactability - Whether the tag can be compacted.
Example
use write_html::{HtmlEnv, Compactability};
use std::fmt::Write;
 
let mut s = String::new();
s.tag("h1", Compactability::No).unwrap()
    .with_attr("id", "my-id").unwrap()
    .inner_html().unwrap()
    .write_str("Hello, world!").unwrap();
assert_eq!(s, "<h1 id=\"my-id\">Hello, world!</h1>");
source

fn html_root<'s>( &'s mut self, lang: &str ) -> Result<InsideTagHtml<'s, 'static, Self>, Error>

Adds a new root <html> node to the HTML document.

See HtmlEnv::tag for more information.

source

fn html5_default_meta(&mut self) -> Result<&mut Self, Error>

Adds default <meta> tags to the HTML document.

Example
use write_html::HtmlEnv;
 
let mut s = String::new();
s.html5_default_meta().unwrap();
assert_eq!(s, "<meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\"><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");

Implementors§

source§

impl<W: Write> HtmlEnv for W