| 1 |
use crate::html::{self, Anchorizer}; |
| 2 |
use crate::{Options, options::Plugins}; |
| 3 |
|
| 4 |
use std::cell::Cell; |
| 5 |
use std::fmt::{self, Write}; |
| 6 |
|
| 7 |
/// Context struct given to formatter functions as taken by |
| 8 |
/// [`html::format_document_with_formatter`]. Output can be appended to through |
| 9 |
/// this struct's [`Write`] interface. |
| 10 |
pub struct Context<'o, 'c, T = ()> { |
| 11 |
output: &'o mut dyn Write, |
| 12 |
last_was_lf: Cell<bool>, |
| 13 |
|
| 14 |
/// [`Options`] in use in this render. |
| 15 |
pub options: &'o Options<'c>, |
| 16 |
/// [`Plugins`] in use in this render. |
| 17 |
pub plugins: &'o Plugins<'o>, |
| 18 |
/// [`Anchorizer`] instance used in this render. |
| 19 |
pub anchorizer: Anchorizer, |
| 20 |
/// Any user data used by the [`Context`]. |
| 21 |
pub user: T, |
| 22 |
/// Current anchorized id being rendered. |
| 23 |
pub current_anchorized_id: Option<String>, |
| 24 |
|
| 25 |
pub(super) footnote_ix: u32, |
| 26 |
pub(super) written_footnote_ix: u32, |
| 27 |
} |
| 28 |
|
| 29 |
impl<'o, 'c, T> Context<'o, 'c, T> { |
| 30 |
pub(super) fn new( |
| 31 |
output: &'o mut dyn Write, |
| 32 |
options: &'o Options<'c>, |
| 33 |
plugins: &'o Plugins<'o>, |
| 34 |
user: T, |
| 35 |
) -> Self { |
| 36 |
Context { |
| 37 |
output, |
| 38 |
last_was_lf: Cell::new(true), |
| 39 |
options, |
| 40 |
plugins, |
| 41 |
anchorizer: Anchorizer::new(), |
| 42 |
user, |
| 43 |
current_anchorized_id: None, |
| 44 |
footnote_ix: 0, |
| 45 |
written_footnote_ix: 0, |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
pub(super) fn finish(mut self) -> Result<T, fmt::Error> { |
| 50 |
if self.footnote_ix > 0 { |
| 51 |
self.write_str("</ol>")?; |
| 52 |
self.lf()?; |
| 53 |
self.write_str("</section>")?; |
| 54 |
self.lf()?; |
| 55 |
} |
| 56 |
Ok(self.user) |
| 57 |
} |
| 58 |
|
| 59 |
/// If the last byte written to ts [`Write`] interface was **not** a U+000A |
| 60 |
/// LINE FEED, writes one. Otherwise, does nothing. |
| 61 |
/// |
| 62 |
/// (In other words, ensures the output is at a new line.) |
| 63 |
/// |
| 64 |
/// No-op when [`compact_html`](crate::options::Render::compact_html) is on. |
| 65 |
pub fn cr(&mut self) -> fmt::Result { |
| 66 |
if self.options.render.compact_html { |
| 67 |
return Ok(()); |
| 68 |
} |
| 69 |
if !self.last_was_lf.get() { |
| 70 |
self.write_str("\n")?; |
| 71 |
} |
| 72 |
Ok(()) |
| 73 |
} |
| 74 |
|
| 75 |
/// Writes `\n` unless [`compact_html`](crate::options::Render::compact_html) is on. |
| 76 |
pub fn lf(&mut self) -> fmt::Result { |
| 77 |
if !self.options.render.compact_html { |
| 78 |
self.write_str("\n")?; |
| 79 |
} |
| 80 |
Ok(()) |
| 81 |
} |
| 82 |
|
| 83 |
/// Convenience wrapper for [`html::escape`]. |
| 84 |
pub fn escape(&mut self, buffer: &str) -> fmt::Result { |
| 85 |
html::escape(self, buffer) |
| 86 |
} |
| 87 |
|
| 88 |
/// Convenience wrapper for [`html::escape_href`]. |
| 89 |
pub fn escape_href(&mut self, buffer: &str) -> fmt::Result { |
| 90 |
let relaxed_autolinks = self.options.parse.relaxed_autolinks; |
| 91 |
html::escape_href(self, buffer, relaxed_autolinks) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
impl<T> Write for Context<'_, '_, T> { |
| 96 |
fn write_str(&mut self, s: &str) -> fmt::Result { |
| 97 |
let l = s.len(); |
| 98 |
if l > 0 { |
| 99 |
self.last_was_lf.set(s.as_bytes()[l - 1] == 10); |
| 100 |
} |
| 101 |
self.output.write_str(s) |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
impl<T> fmt::Debug for Context<'_, '_, T> { |
| 106 |
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { |
| 107 |
formatter.write_str("<comrak::html::Context>") |
| 108 |
} |
| 109 |
} |