use crate::html::{self, Anchorizer};
use crate::{options::Plugins, Options};
use std::cell::Cell;
use std::fmt::{self, Write};
pub struct Context<'o, 'c, T = ()> {
output: &'o mut dyn Write,
last_was_lf: Cell<bool>,
pub options: &'o Options<'c>,
pub plugins: &'o Plugins<'o>,
pub anchorizer: Anchorizer,
pub user: T,
pub(super) footnote_ix: u32,
pub(super) written_footnote_ix: u32,
}
impl<'o, 'c, T> Context<'o, 'c, T> {
pub(super) fn new(
output: &'o mut dyn Write,
options: &'o Options<'c>,
plugins: &'o Plugins<'o>,
user: T,
) -> Self {
Context {
output,
last_was_lf: Cell::new(true),
options,
plugins,
anchorizer: Anchorizer::new(),
user,
footnote_ix: 0,
written_footnote_ix: 0,
}
}
pub(super) fn finish(mut self) -> Result<T, fmt::Error> {
if self.footnote_ix > 0 {
self.write_str("</ol>\n</section>\n")?;
}
Ok(self.user)
}
pub fn cr(&mut self) -> fmt::Result {
if !self.last_was_lf.get() {
self.write_str("\n")?;
}
Ok(())
}
pub fn escape(&mut self, buffer: &str) -> fmt::Result {
html::escape(self, buffer)
}
pub fn escape_href(&mut self, buffer: &str) -> fmt::Result {
let relaxed_autolinks = self.options.parse.relaxed_autolinks;
html::escape_href(self, buffer, relaxed_autolinks)
}
}
impl<'o, 'c, T> Write for Context<'o, 'c, T> {
fn write_str(&mut self, s: &str) -> fmt::Result {
let l = s.len();
if l > 0 {
self.last_was_lf.set(s.as_bytes()[l - 1] == 10);
}
self.output.write_str(s)
}
}
impl<'o, 'c, T> fmt::Debug for Context<'o, 'c, T> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
formatter.write_str("<comrak::html::Context>")
}
}