pub struct Html { /* private fields */ }Implementations§
Source§impl Html
impl Html
Sourcepub fn process(
html: &[u8],
website_root: &Path,
cwd: &Path,
) -> Result<Self, Error>
pub fn process( html: &[u8], website_root: &Path, cwd: &Path, ) -> Result<Self, Error>
Pre-processes a slice of bytes.
Sourcepub fn get_placeholders(&self) -> Result<Placeholders, Error>
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(());
}Sourcepub fn replace_placeholder(
&mut self,
placeholders: &mut Placeholders,
placeholder_name: &str,
replacement: &[u8],
)
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§
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> 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
Mutably borrows from an owned value. Read more