Html

Struct Html 

Source
pub struct Html { /* private fields */ }

Implementations§

Source§

impl Html

Source

pub fn process( html: &[u8], website_root: &Path, cwd: &Path, ) -> Result<Self, Error>

Pre-processes a slice of bytes.

Source

pub fn get_placeholders(&self) -> Result<Placeholders, Error>

Returns all placeholders in the file. A placeholder is a range in the file which is meant to be replaced server-side each time the file is requested. A placeholder is defined in an html file using a #placeholder comment. This allows for arbitrary insertion of HTML at runtime. The order of replacement does not matter.

§Examples
use std::path::Path;
use std::str;
use htmlprep::*;
 
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let raw_html = r##"
            <!DOCTYPE html><html><body>
                <!-- #placeholder "name" -->
                <!-- #placeholder "visitor-number" -->
            </body></html>"##.as_bytes();
     
    let mut html: Html = raw_html.to_vec().into();
    let mut placeholders = html.get_placeholders()?;
    assert!(placeholders.contains("name"));
    assert!(placeholders.contains("visitor-number"));
     
    let name = "Alice";
    let name_replacement = format!("<p>Welcome to the site, <b>{name}!</b></p>");
    html.replace_placeholder(&mut placeholders, "name", name_replacement.as_bytes());
    let visitor_number = 1234;
    let visitor_num_replacement = format!("<p>You are visitor number: {visitor_number}</p>");
    html.replace_placeholder(&mut placeholders, "visitor-number", visitor_num_replacement.as_bytes());
    // Calling this function again is a no-op
    html.replace_placeholder(&mut placeholders, "visitor-number", visitor_num_replacement.as_bytes());
     
    let html_vec: Vec<u8> = html.into();
    let result = r##"
            <!DOCTYPE html><html><body>
                <p>Welcome to the site, <b>Alice!</b></p>
                <p>You are visitor number: 1234</p>
            </body></html>"##.as_bytes().to_vec();
     
    assert!(result.eq(&html_vec));
    return Ok(());
}
Source

pub fn replace_placeholder( &mut self, placeholders: &mut Placeholders, placeholder_name: &str, replacement: &[u8], )

Replaces the placeholder_name placeholder in the calling Html struct with replacement. Upon completion of this function, the replaced Placeholder will be removed from placeholders.

Trait Implementations§

Source§

impl Debug for Html

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<Html> for Vec<u8>

Source§

fn from(value: Html) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for Html

Source§

fn from(value: Vec<u8>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl Freeze for Html

§

impl RefUnwindSafe for Html

§

impl Send for Html

§

impl Sync for Html

§

impl Unpin for Html

§

impl UnwindSafe for Html

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.