#[non_exhaustive]pub struct Document {
pub lang: Option<String>,
pub head: Element,
pub body: Element,
}Expand description
A complete HTML document: a language, a <head> and a <body>.
The Document owns the doctype — Document::render prepends <!DOCTYPE html>\n,
while Document::root gives you the bare <html> element without it.
§Rendering default
Document::render defaults to pretty, while Element::render defaults to
compact. The asymmetry is deliberate and the golden fixtures depend on it, so it is not
tidied up.
§Examples
use molt::prelude::*;
use molt::Document;
let page = Document::new(Some("pt-BR"))
.head_children([title().text("RideKeeper")])
.body_children([h1().text("Track every service")]);
assert!(page.render().starts_with("<!DOCTYPE html>\n<html lang=\"pt-BR\">"));Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.lang: Option<String>The document language, rendered as <html lang="…">. Omitted when None.
head: ElementThe <head> element.
body: ElementThe <body> element.
Implementations§
Source§impl Document
impl Document
Sourcepub fn with_parts(lang: Option<&str>, head: Element, body: Element) -> Self
pub fn with_parts(lang: Option<&str>, head: Element, body: Element) -> Self
Creates a document from an existing <head> and <body>.
Sourcepub fn head_children<N: Into<Node>>(
self,
children: impl IntoIterator<Item = N>,
) -> Self
pub fn head_children<N: Into<Node>>( self, children: impl IntoIterator<Item = N>, ) -> Self
Appends nodes to the <head>.
Sourcepub fn body_children<N: Into<Node>>(
self,
children: impl IntoIterator<Item = N>,
) -> Self
pub fn body_children<N: Into<Node>>( self, children: impl IntoIterator<Item = N>, ) -> Self
Appends nodes to the <body>.
Sourcepub fn root(&self) -> Element
pub fn root(&self) -> Element
The <html> element, without the doctype.
Kept separate from render because the static site generator and
several tests want the tree rather than the serialized page.
Sourcepub fn render_with(&self, options: &RenderOptions) -> String
pub fn render_with(&self, options: &RenderOptions) -> String
Renders the document with the given options, prefixed by the doctype.
Sourcepub fn render(&self) -> String
pub fn render(&self) -> String
Renders the document pretty-printed, which is the default for a document.
Sourcepub fn render_compact(&self) -> String
pub fn render_compact(&self) -> String
Renders the document on a single line.
Sourcepub fn render_to(&self, sink: impl Write, options: &RenderOptions) -> Result<()>
pub fn render_to(&self, sink: impl Write, options: &RenderOptions) -> Result<()>
Writes the same bytes as render_with to sink.
When <body> is only a list of children, each child is rendered with
Render::write_into into its own buffer and then written out, so memory stays
bounded by the largest section rather than the whole page. A body with text
content, no children, or a self-closing flag is written in one piece — still the
same bytes.
§Errors
Returns an error if sink does.
§Examples
use molt::prelude::*;
use molt::{Document, RenderOptions};
let page = Document::new(Some("en")).body_children([h1().text("Hi")]);
let mut buf = Vec::new();
page.render_to(&mut buf, &RenderOptions::compact())?;
assert_eq!(buf, page.render_compact().into_bytes());Trait Implementations§
impl Eq for Document
impl StructuralPartialEq for Document
Auto Trait Implementations§
impl Freeze for Document
impl RefUnwindSafe for Document
impl Send for Document
impl Sync for Document
impl Unpin for Document
impl UnsafeUnpin for Document
impl UnwindSafe for Document
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more