Struct HtmlRenderer

Source
pub struct HtmlRenderer;
Expand description

High level API to render a note providing its content and some context.

Implementations§

Source§

impl HtmlRenderer

Source

pub fn viewer_page<T: Content>( context: Context, content: T, ) -> Result<String, NoteError>

Returns the HTML rendition of a ContentString. The markup rendition engine is determined, by the file extension of the variable context.path. The resulting HTML and other HTML template variables originating from context are inserted into the TMPL_HTML_VIEWER template (which can be replaced at runtime) before being returned. This function is stateless.

use tpnote_lib::config::TMPL_HTML_VAR_VIEWER_DOC_JS;
use tpnote_lib::content::Content;
use tpnote_lib::content::ContentString;
use tpnote_lib::context::Context;
use tpnote_lib::html_renderer::HtmlRenderer;
use std::env::temp_dir;
use std::fs;
use std::path::Path;

// Prepare test: create existing note file.
let raw = String::from(r#"---
title: "My day"
subtitle: "Note"
---
Body text
"#);

// Start test
let mut context = Context::from(Path::new("/path/to/note.md"));
// We do not inject any JavaScript.
context.insert(TMPL_HTML_VAR_VIEWER_DOC_JS, &"".to_string());
// Render.
let html = HtmlRenderer::viewer_page::<ContentString>(context, raw.into())
           .unwrap();
// Check the HTML rendition.
assert!(html.starts_with("<!DOCTYPE html>\n<html"))

A more elaborated example that reads from disk:

use tpnote_lib::config::LIB_CFG;
use tpnote_lib::config::TMPL_HTML_VAR_VIEWER_DOC_JS;
use tpnote_lib::content::Content;
use tpnote_lib::content::ContentString;
use tpnote_lib::context::Context;
use tpnote_lib::html_renderer::HtmlRenderer;
use std::env::temp_dir;
use std::fs;

// Prepare test: create existing note file.
let raw = r#"---
title: "My day2"
subtitle: "Note"
---
Body text
"#;
let notefile = temp_dir().join("20221030-My day2--Note.md");
fs::write(&notefile, raw.as_bytes()).unwrap();

// Start test
let mut context = Context::from(&notefile);
// We do not inject any JavaScript.
context.insert(TMPL_HTML_VAR_VIEWER_DOC_JS, &"".to_string());
// Render.
let content = ContentString::open(&context.path).unwrap();
// You can plug in your own type (must impl. `Content`).
let html = HtmlRenderer::viewer_page(context, content).unwrap();
// Check the HTML rendition.
assert!(html.starts_with("<!DOCTYPE html>\n<html"))
Source

pub fn exporter_page<T: Content>( context: Context, content: T, ) -> Result<String, NoteError>

Returns the HTML rendition of a ContentString. The markup rendition engine is determined, by the file extension of the variable context.path. The resulting HTML and other HTML template variables originating from context are inserted into the TMPL_HTML_EXPORTER template (which can be replaced at runtime) before being returned. This function is stateless.

use tpnote_lib::config::TMPL_HTML_VAR_VIEWER_DOC_JS;
use tpnote_lib::content::Content;
use tpnote_lib::content::ContentString;
use tpnote_lib::context::Context;
use tpnote_lib::html_renderer::HtmlRenderer;
use std::env::temp_dir;
use std::fs;
use std::path::Path;

// Prepare test: create existing note file.
let raw = String::from(r#"---
title: "My day"
subtitle: "Note"
---
Body text
"#);

// Start test
let mut context = Context::from(Path::new("/path/to/note.md"));
// The exporter template does not insert any JavaScript.
// Render.
let html = HtmlRenderer::exporter_page::<ContentString>(context, raw.into())
           .unwrap();
// Check the HTML rendition.
assert!(html.starts_with("<!DOCTYPE html>\n<html"))
Source

pub fn error_page<T: Content>( context: Context, note_erroneous_content: T, error_message: &str, ) -> Result<String, NoteError>

When the header can not be deserialized, the file located in context.path is rendered as “Error HTML page”. The erroneous content is rendered to html with parse_hyperlinks::renderer::text_rawlinks2html and inserted in the TMPL_HTML_VIEWER_ERROR template (can be replace at runtime). This template expects the template variables TMPL_VAR_PATH and TMPL_HTML_VAR_VIEWER_DOC_JS in context to be set. NB: The value of TMPL_VAR_PATH equals context.path.

use tpnote_lib::config::LIB_CFG;
use tpnote_lib::config::TMPL_HTML_VAR_DOC_ERROR;
use tpnote_lib::config::TMPL_HTML_VAR_VIEWER_DOC_JS;
use tpnote_lib::content::Content;
use tpnote_lib::content::ContentString;
use tpnote_lib::context::Context;
use tpnote_lib::error::NoteError;
use tpnote_lib::html_renderer::HtmlRenderer;
use std::env::temp_dir;
use std::fs;

// Prepare test: create existing erroneous note file.
let raw_error = r#"---
title: "My day3"
subtitle: "Note"
--
Body text
"#;
let notefile = temp_dir().join("20221030-My day3--Note.md");
fs::write(&notefile, raw_error.as_bytes()).unwrap();
let mut context = Context::from(&notefile);
let e = NoteError::FrontMatterFieldMissing { field_name: "title".to_string() };

// Start test
let mut context = Context::from(&notefile);
// We do not inject any JavaScript.
context.insert(TMPL_HTML_VAR_VIEWER_DOC_JS, "");
// Render.
// Read from file.
// You can plug in your own type (must impl. `Content`).
let content = ContentString::open(&context.path).unwrap();
let html = HtmlRenderer::error_page(
              context, content, &e.to_string()).unwrap();
// Check the HTML rendition.
assert!(html.starts_with("<!DOCTYPE html>\n<html"))
Source

pub fn save_exporter_page<T: Content>( doc_path: &Path, content: T, export_dir: &Path, local_link_kind: LocalLinkKind, ) -> Result<(), NoteError>

Renders doc_path with content into HTML and saves the result in export_dir. If export_dir is the empty string, the directory of doc_path is used. - dumps the rendition to STDOUT. The filename of the html rendition is the same as in doc_path, but with .html appended.

use tpnote_lib::config::LIB_CFG;
use tpnote_lib::config::TMPL_HTML_VAR_VIEWER_DOC_JS;
use tpnote_lib::config::LocalLinkKind;
use tpnote_lib::content::Content;
use tpnote_lib::content::ContentString;
use tpnote_lib::context::Context;
use tpnote_lib::html_renderer::HtmlRenderer;
use std::env::temp_dir;
use std::fs;
use std::path::Path;

// Prepare test: create existing note file.
let raw = r#"---
title: "My day3"
subtitle: "Note"
---
Body text
"#;
let notefile = temp_dir().join("20221030-My day3--Note.md");
fs::write(&notefile, raw.as_bytes()).unwrap();

// Start test
let content = ContentString::open(&notefile).unwrap();
// You can plug in your own type (must impl. `Content`).
HtmlRenderer::save_exporter_page(
       &notefile, content, Path::new(""), LocalLinkKind::Long).unwrap();
// Check the HTML rendition.
let expected_file = temp_dir().join("20221030-My day3--Note.md.html");
let html = fs::read_to_string(expected_file).unwrap();
assert!(html.starts_with("<!DOCTYPE html>\n<html"))

Auto Trait Implementations§

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T