| 1 |
//! HTML rendering infrastructure for the CommonMark AST, as well as helper |
| 2 |
//! functions. |
| 3 |
//! |
| 4 |
//! [`format_document`] and [`format_document_with_plugins`] use the standard |
| 5 |
//! formatter. The [`create_formatter!`][super::create_formatter] macro allows |
| 6 |
//! specialisation of formatting for specific node types. |
| 7 |
|
| 8 |
mod anchorizer; |
| 9 |
mod context; |
| 10 |
|
| 11 |
use std::borrow::Cow; |
| 12 |
use std::collections::HashMap; |
| 13 |
use std::fmt::{self, Write}; |
| 14 |
use std::str; |
| 15 |
|
| 16 |
use crate::adapters::HeadingMeta; |
| 17 |
use crate::character_set::character_set; |
| 18 |
use crate::ctype::isspace; |
| 19 |
#[cfg(feature = "shortcodes")] |
| 20 |
use crate::nodes::NodeShortCode; |
| 21 |
use crate::nodes::{ |
| 22 |
ListType, Node, NodeAlert, NodeBlockDirective, NodeCode, NodeCodeBlock, NodeFootnoteDefinition, |
| 23 |
NodeFootnoteReference, NodeHeading, NodeHtmlBlock, NodeLink, NodeList, NodeMath, NodeTaskItem, |
| 24 |
NodeValue, NodeWikiLink, TableAlignment, |
| 25 |
}; |
| 26 |
use crate::parser::options::{AlertStyleType, Options, Plugins}; |
| 27 |
use crate::{node_matches, scanners}; |
| 28 |
|
| 29 |
#[doc(hidden)] |
| 30 |
pub use anchorizer::Anchorizer; |
| 31 |
pub use context::Context; |
| 32 |
|
| 33 |
/// Formats an AST as HTML, modified by the given options. |
| 34 |
pub fn format_document(root: Node<'_>, options: &Options, output: &mut dyn Write) -> fmt::Result { |
| 35 |
// Validate the AST as part of the debug build. See https://github.com/kivikakk/comrak/issues/371. |
| 36 |
#[cfg(debug_assertions)] |
| 37 |
root.validate().unwrap_or_else(|e| { |
| 38 |
panic!("The document to format is ill-formed: {:?}", e); |
| 39 |
}); |
| 40 |
|
| 41 |
format_document_with_formatter( |
| 42 |
root, |
| 43 |
options, |
| 44 |
output, |
| 45 |
&Plugins::default(), |
| 46 |
format_node_default, |
| 47 |
(), |
| 48 |
) |
| 49 |
} |
| 50 |
|
| 51 |
/// Formats an AST as HTML, modified by the given options. Accepts custom plugins. |
| 52 |
pub fn format_document_with_plugins( |
| 53 |
root: Node<'_>, |
| 54 |
options: &Options, |
| 55 |
output: &mut dyn fmt::Write, |
| 56 |
plugins: &Plugins, |
| 57 |
) -> fmt::Result { |
| 58 |
format_document_with_formatter(root, options, output, plugins, format_node_default, ()) |
| 59 |
} |
| 60 |
|
| 61 |
/// Returned by the [`format_document_with_formatter`] callback to indicate |
| 62 |
/// whether children of a node should be rendered with full HTML as usual, in |
| 63 |
/// "plain" mode, such as for the `title` attribute of an image (which is a full |
| 64 |
/// subtree in CommonMark — there are probably few other use cases for "plain" |
| 65 |
/// mode), or whether they should be skipped. |
| 66 |
#[derive(Debug, Clone, Copy)] |
| 67 |
pub enum ChildRendering { |
| 68 |
/// Indicates children should be rendered in full HTML as usual. |
| 69 |
HTML, |
| 70 |
/// Indicates children should be rendered in "plain" mode; see the source of |
| 71 |
/// [`format_document_with_formatter`] for details. |
| 72 |
Plain, |
| 73 |
/// Indicates children should be skipped. |
| 74 |
Skip, |
| 75 |
} |
| 76 |
|
| 77 |
/// Create a formatter with specialised rules for certain node types. |
| 78 |
/// |
| 79 |
/// Give the name of the newly created struct, and then a list of [`NodeValue`] |
| 80 |
/// match cases within curly braces. The left-hand side are regular patterns and |
| 81 |
/// can include captures. The right-hand side starts with a mandatory list of |
| 82 |
/// contextual captures, similar to lambdas. The following contextual captures |
| 83 |
/// are available: |
| 84 |
/// |
| 85 |
/// * `context`: the <code>[&mut] [Context]</code>, giving access to rendering |
| 86 |
/// options, plugins, and output appending via its <code>[Write]</code> |
| 87 |
/// implementation. |
| 88 |
/// * `node`: the <code>[&][&][Node]</code> being formatted, when the |
| 89 |
/// [`NodeValue`]'s contents aren't enough. |
| 90 |
/// * `entering`: [`true`] when the node is being first descended into, |
| 91 |
/// [`false`] when being exited. |
| 92 |
/// |
| 93 |
/// By default, an overridden formatter will return [`ChildRendering::HTML`], |
| 94 |
/// causing children of the node to be rendered as HTML as usual. You can |
| 95 |
/// return one of these enum values (wrapped in [`Ok`]) from within your |
| 96 |
/// override to change this behaviour, in some or all cases. These values are |
| 97 |
/// only noted when `entering` a node. |
| 98 |
/// |
| 99 |
/// If you supply a type parameter after the name of your formatter, it will be |
| 100 |
/// taken as an additional argument on the generated `format_document` method, |
| 101 |
/// is available on the [`Context`] as the `user` property, and becomes the |
| 102 |
/// return value of `format_document`. |
| 103 |
/// |
| 104 |
/// ```rust |
| 105 |
/// # use comrak::{create_formatter, parse_document, Arena, Options, nodes::NodeValue, html::ChildRendering}; |
| 106 |
/// # use std::fmt::Write; |
| 107 |
/// create_formatter!(CustomFormatter<usize>, { |
| 108 |
/// NodeValue::Emph => |context, entering| { |
| 109 |
/// context.user += 1; |
| 110 |
/// if entering { |
| 111 |
/// context.write_str("<i>")?; |
| 112 |
/// } else { |
| 113 |
/// context.write_str("</i>")?; |
| 114 |
/// } |
| 115 |
/// }, |
| 116 |
/// NodeValue::Strong => |context, entering| { |
| 117 |
/// context.user += 1; |
| 118 |
/// context.write_str(if entering { "<b>" } else { "</b>" })?; |
| 119 |
/// }, |
| 120 |
/// NodeValue::Image(ref nl) => |context, node, entering| { |
| 121 |
/// assert!(node.data().sourcepos == (3, 1, 3, 18).into()); |
| 122 |
/// if entering { |
| 123 |
/// context.write_str(&nl.url.to_uppercase())?; |
| 124 |
/// return Ok(ChildRendering::Skip); |
| 125 |
/// } |
| 126 |
/// }, |
| 127 |
/// }); |
| 128 |
/// |
| 129 |
/// let options = Options::default(); |
| 130 |
/// let arena = Arena::new(); |
| 131 |
/// let doc = parse_document( |
| 132 |
/// &arena, |
| 133 |
/// "_Hello_, **world**.\n\n", |
| 134 |
/// &options, |
| 135 |
/// ); |
| 136 |
/// |
| 137 |
/// let mut result: String = String::new(); |
| 138 |
/// let converted_count = CustomFormatter::format_document(doc, &options, &mut result, 0).unwrap(); |
| 139 |
/// |
| 140 |
/// assert_eq!( |
| 141 |
/// result, |
| 142 |
/// "<p><i>Hello</i>, <b>world</b>.</p>\n<p>/IMG.PNG</p>\n" |
| 143 |
/// ); |
| 144 |
/// |
| 145 |
/// assert_eq!(converted_count, 4); |
| 146 |
/// ``` |
| 147 |
#[macro_export] |
| 148 |
macro_rules! create_formatter { |
| 149 |
($name:ident, { $( $pat:pat => | $( $capture:ident ),* | $case:tt ),* $(,)? }) => { |
| 150 |
$crate::create_formatter!(@inner $name<()>, { $( $pat => | $( $capture ),* | $case ),*, }); |
| 151 |
}; |
| 152 |
|
| 153 |
($name:ident<$type:ty>, { $( $pat:pat => | $( $capture:ident ),* | $case:tt ),* $(,)? }) => { |
| 154 |
$crate::create_formatter!(@inner $name<$type, $type>, { $( $pat => | $( $capture ),* | $case ),*, }); |
| 155 |
}; |
| 156 |
|
| 157 |
// Actual implementation |
| 158 |
(@inner $name:ident<$type:ty $(, $user_type:ty)?>, { $( $pat:pat => | $( $capture:ident ),* | $case:tt ),* $(,)? }) => { |
| 159 |
#[allow(missing_copy_implementations)] |
| 160 |
#[allow(missing_debug_implementations)] |
| 161 |
/// Created by [`comrak::create_formatter!`][crate::create_formatter]. |
| 162 |
pub struct $name; |
| 163 |
|
| 164 |
impl $name { |
| 165 |
/// Formats an AST as HTML, modified by the given options. |
| 166 |
#[inline] |
| 167 |
pub fn format_document<'a>( |
| 168 |
root: &'a $crate::nodes::AstNode<'a>, |
| 169 |
options: &$crate::Options, |
| 170 |
output: &mut dyn ::std::fmt::Write, |
| 171 |
$(user: $user_type,)? |
| 172 |
) -> ::std::result::Result<$type, ::std::fmt::Error> { |
| 173 |
// Validate the AST as part of the debug build. See https://github.com/kivikakk/comrak/issues/371. |
| 174 |
#[cfg(debug_assertions)] |
| 175 |
root.validate().unwrap_or_else(|e| { |
| 176 |
panic!("The document to format is ill-formed: {:?}", e); |
| 177 |
}); |
| 178 |
|
| 179 |
#[allow(unused_mut)] |
| 180 |
let mut maybe_user = None$(::<$user_type>)?; |
| 181 |
$(maybe_user = Some::<$user_type>(user);)? |
| 182 |
$crate::html::format_document_with_formatter( |
| 183 |
root, |
| 184 |
options, |
| 185 |
output, |
| 186 |
&$crate::options::Plugins::default(), |
| 187 |
Self::formatter, |
| 188 |
maybe_user.unwrap_or(<$type>::default()), |
| 189 |
) |
| 190 |
} |
| 191 |
|
| 192 |
/// Formats an AST as HTML, modified by the given options. Accepts custom plugins. |
| 193 |
#[inline] |
| 194 |
pub fn format_document_with_plugins<'a, 'o, 'c: 'o>( |
| 195 |
root: &'a $crate::nodes::AstNode<'a>, |
| 196 |
options: &'o $crate::Options<'c>, |
| 197 |
output: &'o mut dyn ::std::fmt::Write, |
| 198 |
plugins: &'o $crate::options::Plugins<'o>, |
| 199 |
$(user: $user_type,)? |
| 200 |
) -> ::std::result::Result<$type, ::std::fmt::Error> { |
| 201 |
#[allow(unused_mut)] |
| 202 |
let mut maybe_user = None$(::<$user_type>)?; |
| 203 |
$(maybe_user = Some::<$user_type>(user);)? |
| 204 |
$crate::html::format_document_with_formatter( |
| 205 |
root, |
| 206 |
options, |
| 207 |
output, |
| 208 |
plugins, |
| 209 |
Self::formatter, |
| 210 |
maybe_user.unwrap_or(<$type>::default()), |
| 211 |
) |
| 212 |
} |
| 213 |
|
| 214 |
fn formatter<'a>( |
| 215 |
context: &mut $crate::html::Context<$type>, |
| 216 |
node: &'a $crate::nodes::AstNode<'a>, |
| 217 |
entering: bool, |
| 218 |
) -> ::std::result::Result<$crate::html::ChildRendering, ::std::fmt::Error> { |
| 219 |
match node.data().value { |
| 220 |
$( |
| 221 |
$pat => { |
| 222 |
$crate::formatter_captures!((context, node, entering), ($( $capture ),*)); |
| 223 |
$case |
| 224 |
// Don't warn on unconditional return in user code. |
| 225 |
#[allow(unreachable_code)] |
| 226 |
::std::result::Result::Ok($crate::html::ChildRendering::HTML) |
| 227 |
} |
| 228 |
),* |
| 229 |
_ => $crate::html::format_node_default(context, node, entering), |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
}; |
| 234 |
} |
| 235 |
|
| 236 |
/// This must be exported so its uses in [`create_formatter!`] can be expanded, |
| 237 |
/// but it's not intended for direct use. |
| 238 |
#[doc(hidden)] |
| 239 |
#[macro_export] |
| 240 |
macro_rules! formatter_captures { |
| 241 |
(($context:ident, $node:ident, $entering:ident), context, $bind:ident) => { |
| 242 |
let $bind = $context; |
| 243 |
}; |
| 244 |
(($context:ident, $node:ident, $entering:ident), node, $bind:ident) => { |
| 245 |
let $bind = $node; |
| 246 |
}; |
| 247 |
(($context:ident, $node:ident, $entering:ident), entering, $bind:ident) => { |
| 248 |
let $bind = $entering; |
| 249 |
}; |
| 250 |
(($context:ident, $node:ident, $entering:ident), $unknown:ident, $bind:ident) => { |
| 251 |
compile_error!(concat!("unknown capture '", stringify!($unknown), "'; available are 'context', 'node', 'entering'")); |
| 252 |
}; |
| 253 |
(($context:ident, $node:ident, $entering:ident), ($capture:ident)) => { |
| 254 |
$crate::formatter_captures!(($context, $node, $entering), $capture, $capture); |
| 255 |
}; |
| 256 |
(($context:ident, $node:ident, $entering:ident), ($capture:ident, $( $rest:ident ),*)) => { |
| 257 |
$crate::formatter_captures!(($context, $node, $entering), $capture, $capture); |
| 258 |
$crate::formatter_captures!(($context, $node, $entering), ($( $rest ),*)); |
| 259 |
}; |
| 260 |
} |
| 261 |
|
| 262 |
/// Formats the given AST with all options and formatter function specified. |
| 263 |
/// |
| 264 |
/// The default formatter as used by [`format_document`] is |
| 265 |
/// [`format_node_default`]. It is given the [`Context`], [`Node`], and a |
| 266 |
/// boolean indicating whether the node is being entered into or exited. The |
| 267 |
/// returned [`ChildRendering`] is used to inform whether and how the node's |
| 268 |
/// children are recursed into automatically. |
| 269 |
pub fn format_document_with_formatter<'a, 'o, 'c: 'o, T>( |
| 270 |
root: Node<'a>, |
| 271 |
options: &'o Options<'c>, |
| 272 |
output: &'o mut dyn Write, |
| 273 |
plugins: &'o Plugins<'o>, |
| 274 |
formatter: fn( |
| 275 |
context: &mut Context<T>, |
| 276 |
node: Node<'a>, |
| 277 |
entering: bool, |
| 278 |
) -> Result<ChildRendering, fmt::Error>, |
| 279 |
user: T, |
| 280 |
) -> Result<T, fmt::Error> { |
| 281 |
// Traverse the AST iteratively using a work stack, with pre- and |
| 282 |
// post-child-traversal phases. During pre-order traversal render the |
| 283 |
// opening tags, then push the node back onto the stack for the |
| 284 |
// post-order traversal phase, then push the children in reverse order |
| 285 |
// onto the stack and begin rendering first child. |
| 286 |
|
| 287 |
let mut context = Context::new(output, options, plugins, user); |
| 288 |
|
| 289 |
enum Phase { |
| 290 |
Pre, |
| 291 |
Post, |
| 292 |
} |
| 293 |
let mut stack = vec![(root, ChildRendering::HTML, Phase::Pre)]; |
| 294 |
|
| 295 |
while let Some((node, child_rendering, phase)) = stack.pop() { |
| 296 |
match phase { |
| 297 |
Phase::Pre => { |
| 298 |
let new_cr = match child_rendering { |
| 299 |
ChildRendering::Plain => { |
| 300 |
match node.data().value { |
| 301 |
NodeValue::Text(ref literal) => { |
| 302 |
context.escape(literal)?; |
| 303 |
} |
| 304 |
NodeValue::Code(NodeCode { ref literal, .. }) |
| 305 |
| NodeValue::HtmlInline(ref literal) => { |
| 306 |
context.escape(literal)?; |
| 307 |
} |
| 308 |
NodeValue::LineBreak | NodeValue::SoftBreak => { |
| 309 |
fmt::Write::write_str(&mut context, " ")?; |
| 310 |
} |
| 311 |
NodeValue::Math(NodeMath { ref literal, .. }) => { |
| 312 |
context.escape(literal)?; |
| 313 |
} |
| 314 |
_ => (), |
| 315 |
} |
| 316 |
ChildRendering::Plain |
| 317 |
} |
| 318 |
ChildRendering::HTML => { |
| 319 |
stack.push((node, ChildRendering::HTML, Phase::Post)); |
| 320 |
formatter(&mut context, node, true)? |
| 321 |
} |
| 322 |
ChildRendering::Skip => { |
| 323 |
// We never push a node with ChildRendering::Skip. |
| 324 |
unreachable!() |
| 325 |
} |
| 326 |
}; |
| 327 |
|
| 328 |
if !matches!(new_cr, ChildRendering::Skip) { |
| 329 |
for ch in node.reverse_children() { |
| 330 |
stack.push((ch, new_cr, Phase::Pre)); |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
Phase::Post => { |
| 335 |
debug_assert!(matches!(child_rendering, ChildRendering::HTML)); |
| 336 |
formatter(&mut context, node, false)?; |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
context.finish() |
| 342 |
} |
| 343 |
|
| 344 |
/// Default node formatting function, used by [`format_document`], |
| 345 |
/// [`format_document_with_plugins`] and as the fallback for any node types not |
| 346 |
/// handled in custom formatters created by [`create_formatter!`]. |
| 347 |
#[inline] |
| 348 |
pub fn format_node_default<T>( |
| 349 |
context: &mut Context<T>, |
| 350 |
node: Node<'_>, |
| 351 |
entering: bool, |
| 352 |
) -> Result<ChildRendering, fmt::Error> { |
| 353 |
match node.data().value { |
| 354 |
// Commonmark |
| 355 |
NodeValue::BlockQuote => render_block_quote(context, node, entering), |
| 356 |
NodeValue::Code(ref nc) => render_code(context, node, entering, nc), |
| 357 |
NodeValue::CodeBlock(ref ncb) => render_code_block(context, node, entering, ncb), |
| 358 |
NodeValue::Document => Ok(ChildRendering::HTML), |
| 359 |
NodeValue::Emph => render_emph(context, node, entering), |
| 360 |
NodeValue::Heading(ref nh) => render_heading(context, node, entering, nh), |
| 361 |
NodeValue::HtmlBlock(ref nhb) => render_html_block(context, entering, nhb), |
| 362 |
#[cfg(feature = "phoenix_heex")] |
| 363 |
NodeValue::HeexBlock(ref nhb) => render_heex_block(context, entering, nhb), |
| 364 |
NodeValue::HtmlInline(ref literal) => render_html_inline(context, entering, literal), |
| 365 |
#[cfg(feature = "phoenix_heex")] |
| 366 |
NodeValue::HeexInline(ref literal) => render_heex_inline(context, entering, literal), |
| 367 |
NodeValue::Image(ref nl) => render_image(context, node, entering, nl), |
| 368 |
NodeValue::Item(_) => render_item(context, node, entering), |
| 369 |
NodeValue::LineBreak => render_line_break(context, node, entering), |
| 370 |
NodeValue::Link(ref nl) => render_link(context, node, entering, nl), |
| 371 |
NodeValue::List(ref nl) => render_list(context, node, entering, nl), |
| 372 |
NodeValue::Paragraph => render_paragraph(context, node, entering), |
| 373 |
NodeValue::SoftBreak => render_soft_break(context, node, entering), |
| 374 |
NodeValue::Strong => render_strong(context, node, entering), |
| 375 |
NodeValue::Text(ref literal) => render_text(context, entering, literal), |
| 376 |
NodeValue::ThematicBreak => render_thematic_break(context, node, entering), |
| 377 |
|
| 378 |
// GFM |
| 379 |
NodeValue::FootnoteDefinition(ref nfd) => { |
| 380 |
render_footnote_definition(context, node, entering, nfd) |
| 381 |
} |
| 382 |
NodeValue::FootnoteReference(ref nfr) => { |
| 383 |
render_footnote_reference(context, node, entering, nfr) |
| 384 |
} |
| 385 |
NodeValue::Strikethrough => render_strikethrough(context, node, entering), |
| 386 |
NodeValue::Highlight => render_highlight(context, node, entering), |
| 387 |
NodeValue::Insert => render_insert(context, node, entering), |
| 388 |
NodeValue::Table(_) => render_table(context, node, entering), |
| 389 |
NodeValue::TableCell => render_table_cell(context, node, entering), |
| 390 |
NodeValue::TableRow(thead) => render_table_row(context, node, entering, thead), |
| 391 |
NodeValue::TaskItem(ref nti) => render_task_item(context, node, entering, nti), |
| 392 |
|
| 393 |
// Extensions |
| 394 |
NodeValue::Alert(ref alert) => render_alert(context, node, entering, alert), |
| 395 |
NodeValue::DescriptionDetails => render_description_details(context, node, entering), |
| 396 |
NodeValue::DescriptionItem(_) => Ok(ChildRendering::HTML), |
| 397 |
NodeValue::DescriptionList => render_description_list(context, node, entering), |
| 398 |
NodeValue::DescriptionTerm => render_description_term(context, node, entering), |
| 399 |
NodeValue::Escaped => render_escaped(context, node, entering), |
| 400 |
NodeValue::EscapedTag(net) => render_escaped_tag(context, net), |
| 401 |
NodeValue::FrontMatter(_) => Ok(ChildRendering::HTML), |
| 402 |
NodeValue::Math(ref nm) => render_math(context, node, entering, nm), |
| 403 |
NodeValue::MultilineBlockQuote(_) => render_multiline_block_quote(context, node, entering), |
| 404 |
NodeValue::Raw(ref literal) => render_raw(context, entering, literal), |
| 405 |
#[cfg(feature = "shortcodes")] |
| 406 |
NodeValue::ShortCode(ref nsc) => render_short_code(context, entering, nsc), |
| 407 |
NodeValue::SpoileredText => render_spoiler_text(context, node, entering), |
| 408 |
NodeValue::Subscript => render_subscript(context, node, entering), |
| 409 |
NodeValue::Superscript => render_superscript(context, node, entering), |
| 410 |
NodeValue::Underline => render_underline(context, node, entering), |
| 411 |
NodeValue::WikiLink(ref nwl) => render_wiki_link(context, node, entering, nwl), |
| 412 |
NodeValue::Subtext => render_subtext(context, node, entering), |
| 413 |
NodeValue::BlockDirective(ref nbd) => render_block_directive(context, node, entering, nbd), |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
// Commonmark |
| 418 |
|
| 419 |
/// Renders sourcepos data for the given node to the supplied [`Context`]. |
| 420 |
/// |
| 421 |
/// This function renders anything iff `context.options.render.sourcepos` is |
| 422 |
/// true, and includes a leading space if so, so you can use it unconditionally |
| 423 |
/// immediately before writing a closing `>` in your opening HTML tag. |
| 424 |
pub fn render_sourcepos<T>(context: &mut Context<T>, node: Node<'_>) -> fmt::Result { |
| 425 |
if context.options.render.sourcepos { |
| 426 |
let ast = node.data(); |
| 427 |
if ast.sourcepos.start.line > 0 { |
| 428 |
write!(context, " data-sourcepos=\"{}\"", ast.sourcepos)?; |
| 429 |
} |
| 430 |
} |
| 431 |
Ok(()) |
| 432 |
} |
| 433 |
|
| 434 |
fn render_block_directive<T>( |
| 435 |
context: &mut Context<T>, |
| 436 |
node: Node<'_>, |
| 437 |
entering: bool, |
| 438 |
nbd: &NodeBlockDirective, |
| 439 |
) -> Result<ChildRendering, fmt::Error> { |
| 440 |
if entering { |
| 441 |
context.cr()?; |
| 442 |
context.write_str("<div class=\"")?; |
| 443 |
context.escape(&nbd.info)?; |
| 444 |
context.write_str("\"")?; |
| 445 |
render_sourcepos(context, node)?; |
| 446 |
context.write_str(">")?; |
| 447 |
} else { |
| 448 |
context.cr()?; |
| 449 |
context.write_str("</div>")?; |
| 450 |
context.lf()?; |
| 451 |
} |
| 452 |
|
| 453 |
Ok(ChildRendering::HTML) |
| 454 |
} |
| 455 |
|
| 456 |
fn render_block_quote<T>( |
| 457 |
context: &mut Context<T>, |
| 458 |
node: Node<'_>, |
| 459 |
entering: bool, |
| 460 |
) -> Result<ChildRendering, fmt::Error> { |
| 461 |
if entering { |
| 462 |
context.cr()?; |
| 463 |
context.write_str("<blockquote")?; |
| 464 |
render_sourcepos(context, node)?; |
| 465 |
context.write_str(">")?; |
| 466 |
context.lf()?; |
| 467 |
} else { |
| 468 |
context.cr()?; |
| 469 |
context.write_str("</blockquote>")?; |
| 470 |
context.lf()?; |
| 471 |
} |
| 472 |
Ok(ChildRendering::HTML) |
| 473 |
} |
| 474 |
|
| 475 |
fn render_code<T>( |
| 476 |
context: &mut Context<T>, |
| 477 |
node: Node<'_>, |
| 478 |
entering: bool, |
| 479 |
nc: &NodeCode, |
| 480 |
) -> Result<ChildRendering, fmt::Error> { |
| 481 |
if entering { |
| 482 |
context.write_str("<code")?; |
| 483 |
render_sourcepos(context, node)?; |
| 484 |
context.write_str(">")?; |
| 485 |
context.escape(&nc.literal)?; |
| 486 |
context.write_str("</code>")?; |
| 487 |
} |
| 488 |
|
| 489 |
Ok(ChildRendering::HTML) |
| 490 |
} |
| 491 |
|
| 492 |
fn render_code_block<T>( |
| 493 |
context: &mut Context<T>, |
| 494 |
node: Node<'_>, |
| 495 |
entering: bool, |
| 496 |
ncb: &NodeCodeBlock, |
| 497 |
) -> Result<ChildRendering, fmt::Error> { |
| 498 |
if entering { |
| 499 |
let info = &ncb.info; |
| 500 |
let info_bytes = info.as_bytes(); |
| 501 |
let mut first_tag = 0; |
| 502 |
|
| 503 |
while first_tag < info.len() && !isspace(info_bytes[first_tag]) { |
| 504 |
first_tag += 1; |
| 505 |
} |
| 506 |
|
| 507 |
let lang = &info[..first_tag]; |
| 508 |
let meta = info[first_tag..].trim(); |
| 509 |
|
| 510 |
if lang.eq("math") { |
| 511 |
render_math_code_block(context, node, &ncb.literal)?; |
| 512 |
} else if !lang.is_empty() { |
| 513 |
if let Some(adapter) = context.plugins.render.codefence_renderers.get(lang) { |
| 514 |
context.cr()?; |
| 515 |
let sourcepos = if context.options.render.sourcepos { |
| 516 |
Some(node.data().sourcepos) |
| 517 |
} else { |
| 518 |
None |
| 519 |
}; |
| 520 |
|
| 521 |
adapter.write(context, lang, meta, &ncb.literal, sourcepos)?; |
| 522 |
return Ok(ChildRendering::HTML); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
if !lang.eq("math") { |
| 527 |
context.cr()?; |
| 528 |
|
| 529 |
let mut pre_attributes: HashMap<&str, Cow<str>> = HashMap::new(); |
| 530 |
let mut code_attributes: HashMap<&str, Cow<str>> = HashMap::new(); |
| 531 |
let code_attr: String; |
| 532 |
|
| 533 |
let literal = &ncb.literal; |
| 534 |
|
| 535 |
if !info.is_empty() { |
| 536 |
if context.options.render.github_pre_lang { |
| 537 |
pre_attributes.insert("lang", lang.into()); |
| 538 |
|
| 539 |
if context.options.render.full_info_string && !meta.is_empty() { |
| 540 |
pre_attributes.insert("data-meta", meta.trim().into()); |
| 541 |
} |
| 542 |
} else { |
| 543 |
code_attr = format!("language-{}", lang); |
| 544 |
code_attributes.insert("class", code_attr.into()); |
| 545 |
|
| 546 |
if context.options.render.full_info_string && !meta.is_empty() { |
| 547 |
code_attributes.insert("data-meta", meta.into()); |
| 548 |
} |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
if context.options.render.sourcepos { |
| 553 |
let ast = node.data(); |
| 554 |
pre_attributes.insert("data-sourcepos", ast.sourcepos.to_string().into()); |
| 555 |
} |
| 556 |
|
| 557 |
match context.plugins.render.codefence_syntax_highlighter { |
| 558 |
None => { |
| 559 |
write_opening_tag(context, "pre", pre_attributes.into_iter())?; |
| 560 |
write_opening_tag(context, "code", code_attributes.into_iter())?; |
| 561 |
|
| 562 |
context.escape(literal)?; |
| 563 |
|
| 564 |
context.write_str("</code></pre>")?; |
| 565 |
context.lf()? |
| 566 |
} |
| 567 |
Some(highlighter) => { |
| 568 |
highlighter.write_pre_tag(context, pre_attributes)?; |
| 569 |
highlighter.write_code_tag(context, code_attributes)?; |
| 570 |
|
| 571 |
highlighter.write_highlighted(context, Some(lang), &ncb.literal)?; |
| 572 |
|
| 573 |
context.write_str("</code></pre>")?; |
| 574 |
context.lf()? |
| 575 |
} |
| 576 |
} |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
Ok(ChildRendering::HTML) |
| 581 |
} |
| 582 |
|
| 583 |
fn render_emph<T>( |
| 584 |
context: &mut Context<T>, |
| 585 |
node: Node<'_>, |
| 586 |
entering: bool, |
| 587 |
) -> Result<ChildRendering, fmt::Error> { |
| 588 |
if entering { |
| 589 |
context.write_str("<em")?; |
| 590 |
render_sourcepos(context, node)?; |
| 591 |
context.write_str(">")?; |
| 592 |
} else { |
| 593 |
context.write_str("</em>")?; |
| 594 |
} |
| 595 |
|
| 596 |
Ok(ChildRendering::HTML) |
| 597 |
} |
| 598 |
|
| 599 |
fn render_heading<T>( |
| 600 |
context: &mut Context<T>, |
| 601 |
node: Node<'_>, |
| 602 |
entering: bool, |
| 603 |
nh: &NodeHeading, |
| 604 |
) -> Result<ChildRendering, fmt::Error> { |
| 605 |
match context.plugins.render.heading_adapter { |
| 606 |
None => { |
| 607 |
if entering { |
| 608 |
context.cr()?; |
| 609 |
write!(context, "<h{}", nh.level)?; |
| 610 |
|
| 611 |
if let Some(prefix) = &context.options.extension.header_id_prefix { |
| 612 |
let text_content = node.collect_text(); |
| 613 |
let id = context.anchorizer.anchorize(&text_content); |
| 614 |
|
| 615 |
write!(context, r##" id="{prefix}{id}""##)?; |
| 616 |
context.current_anchorized_id = Some(id); |
| 617 |
}; |
| 618 |
|
| 619 |
render_sourcepos(context, node)?; |
| 620 |
context.write_str(">")?; |
| 621 |
} else { |
| 622 |
if let Some(prefix) = &context.options.extension.header_id_prefix { |
| 623 |
let text_content = node.collect_text(); |
| 624 |
let id = context.current_anchorized_id.take().unwrap(); |
| 625 |
let href_prefix = if context.options.extension.header_id_prefix_in_href { |
| 626 |
prefix.as_str() |
| 627 |
} else { |
| 628 |
"" |
| 629 |
}; |
| 630 |
write!( |
| 631 |
context, |
| 632 |
r##"<a href="#{href_prefix}{id}" aria-label="Link to heading '"## |
| 633 |
)?; |
| 634 |
context.escape(&text_content)?; |
| 635 |
context.write_str(r#"'" data-heading-content=""#)?; |
| 636 |
context.escape(&text_content)?; |
| 637 |
context.write_str(r#"" class="anchor"></a>"#)?; |
| 638 |
} |
| 639 |
write!(context, "</h{}>", nh.level)?; |
| 640 |
context.lf()?; |
| 641 |
} |
| 642 |
} |
| 643 |
Some(adapter) => { |
| 644 |
let text_content = node.collect_text(); |
| 645 |
let heading = HeadingMeta { |
| 646 |
level: nh.level, |
| 647 |
content: text_content, |
| 648 |
}; |
| 649 |
|
| 650 |
if entering { |
| 651 |
context.cr()?; |
| 652 |
let sp = if context.options.render.sourcepos { |
| 653 |
Some(node.data().sourcepos) |
| 654 |
} else { |
| 655 |
None |
| 656 |
}; |
| 657 |
adapter.enter(context, &heading, sp)?; |
| 658 |
} else { |
| 659 |
adapter.exit(context, &heading)?; |
| 660 |
} |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
Ok(ChildRendering::HTML) |
| 665 |
} |
| 666 |
|
| 667 |
fn render_html_block<T>( |
| 668 |
context: &mut Context<T>, |
| 669 |
entering: bool, |
| 670 |
nhb: &NodeHtmlBlock, |
| 671 |
) -> Result<ChildRendering, fmt::Error> { |
| 672 |
// No sourcepos. |
| 673 |
if entering { |
| 674 |
context.cr()?; |
| 675 |
let literal = &nhb.literal; |
| 676 |
if context.options.render.escape { |
| 677 |
context.escape(literal)?; |
| 678 |
} else if !context.options.render.r#unsafe { |
| 679 |
context.write_str("<!-- raw HTML omitted -->")?; |
| 680 |
} else if context.options.extension.tagfilter { |
| 681 |
tagfilter_block(context, literal)?; |
| 682 |
} else { |
| 683 |
context.write_str(literal)?; |
| 684 |
} |
| 685 |
context.cr()?; |
| 686 |
} |
| 687 |
|
| 688 |
Ok(ChildRendering::HTML) |
| 689 |
} |
| 690 |
|
| 691 |
#[cfg(feature = "phoenix_heex")] |
| 692 |
fn render_heex_block<T>( |
| 693 |
context: &mut Context<T>, |
| 694 |
entering: bool, |
| 695 |
nhb: &crate::nodes::NodeHeexBlock, |
| 696 |
) -> Result<ChildRendering, fmt::Error> { |
| 697 |
if entering { |
| 698 |
context.cr()?; |
| 699 |
context.write_str(&nhb.literal)?; |
| 700 |
context.cr()?; |
| 701 |
} |
| 702 |
|
| 703 |
Ok(ChildRendering::HTML) |
| 704 |
} |
| 705 |
|
| 706 |
#[cfg(feature = "phoenix_heex")] |
| 707 |
fn render_heex_inline<T>( |
| 708 |
context: &mut Context<T>, |
| 709 |
entering: bool, |
| 710 |
literal: &str, |
| 711 |
) -> Result<ChildRendering, fmt::Error> { |
| 712 |
if entering { |
| 713 |
context.write_str(literal)?; |
| 714 |
} |
| 715 |
|
| 716 |
Ok(ChildRendering::HTML) |
| 717 |
} |
| 718 |
|
| 719 |
fn render_html_inline<T>( |
| 720 |
context: &mut Context<T>, |
| 721 |
entering: bool, |
| 722 |
literal: &str, |
| 723 |
) -> Result<ChildRendering, fmt::Error> { |
| 724 |
// No sourcepos. |
| 725 |
if entering { |
| 726 |
if context.options.render.escape { |
| 727 |
context.escape(literal)?; |
| 728 |
} else if !context.options.render.r#unsafe { |
| 729 |
context.write_str("<!-- raw HTML omitted -->")?; |
| 730 |
} else if context.options.extension.tagfilter && tagfilter(literal) { |
| 731 |
context.write_str("<")?; |
| 732 |
context.write_str(&literal[1..])?; |
| 733 |
} else { |
| 734 |
context.write_str(literal)?; |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
Ok(ChildRendering::HTML) |
| 739 |
} |
| 740 |
|
| 741 |
fn render_image<T>( |
| 742 |
context: &mut Context<T>, |
| 743 |
node: Node<'_>, |
| 744 |
entering: bool, |
| 745 |
nl: &NodeLink, |
| 746 |
) -> Result<ChildRendering, fmt::Error> { |
| 747 |
if entering { |
| 748 |
if context.options.render.figure_with_caption { |
| 749 |
context.write_str("<figure>")?; |
| 750 |
} |
| 751 |
context.write_str("<img")?; |
| 752 |
render_sourcepos(context, node)?; |
| 753 |
context.write_str(" src=\"")?; |
| 754 |
let url = &nl.url; |
| 755 |
if context.options.render.r#unsafe || !dangerous_url(url) { |
| 756 |
if let Some(rewriter) = &context.options.extension.image_url_rewriter { |
| 757 |
context.escape_href(&rewriter.to_html(&nl.url))?; |
| 758 |
} else { |
| 759 |
context.escape_href(url)?; |
| 760 |
} |
| 761 |
} |
| 762 |
context.write_str("\" alt=\"")?; |
| 763 |
return Ok(ChildRendering::Plain); |
| 764 |
} else { |
| 765 |
if !nl.title.is_empty() { |
| 766 |
context.write_str("\" title=\"")?; |
| 767 |
context.escape(&nl.title)?; |
| 768 |
} |
| 769 |
context.write_str("\" />")?; |
| 770 |
if context.options.render.figure_with_caption { |
| 771 |
if !nl.title.is_empty() { |
| 772 |
context.write_str("<figcaption>")?; |
| 773 |
context.escape(&nl.title)?; |
| 774 |
context.write_str("</figcaption>")?; |
| 775 |
} |
| 776 |
context.write_str("</figure>")?; |
| 777 |
}; |
| 778 |
} |
| 779 |
|
| 780 |
Ok(ChildRendering::HTML) |
| 781 |
} |
| 782 |
|
| 783 |
fn render_item<T>( |
| 784 |
context: &mut Context<T>, |
| 785 |
node: Node<'_>, |
| 786 |
entering: bool, |
| 787 |
) -> Result<ChildRendering, fmt::Error> { |
| 788 |
if entering { |
| 789 |
context.cr()?; |
| 790 |
context.write_str("<li")?; |
| 791 |
render_sourcepos(context, node)?; |
| 792 |
context.write_str(">")?; |
| 793 |
} else { |
| 794 |
context.write_str("</li>")?; |
| 795 |
context.lf()?; |
| 796 |
} |
| 797 |
|
| 798 |
Ok(ChildRendering::HTML) |
| 799 |
} |
| 800 |
|
| 801 |
fn render_line_break<T>( |
| 802 |
context: &mut Context<T>, |
| 803 |
node: Node<'_>, |
| 804 |
entering: bool, |
| 805 |
) -> Result<ChildRendering, fmt::Error> { |
| 806 |
if entering { |
| 807 |
context.write_str("<br")?; |
| 808 |
render_sourcepos(context, node)?; |
| 809 |
context.write_str(" />")?; |
| 810 |
context.lf()?; |
| 811 |
} |
| 812 |
|
| 813 |
Ok(ChildRendering::HTML) |
| 814 |
} |
| 815 |
|
| 816 |
fn render_link<T>( |
| 817 |
context: &mut Context<T>, |
| 818 |
node: Node<'_>, |
| 819 |
entering: bool, |
| 820 |
nl: &NodeLink, |
| 821 |
) -> Result<ChildRendering, fmt::Error> { |
| 822 |
let parent_node = node.parent(); |
| 823 |
|
| 824 |
if !context.options.parse.relaxed_autolinks |
| 825 |
|| (parent_node.is_none() || !node_matches!(parent_node.unwrap(), NodeValue::Link(..))) |
| 826 |
{ |
| 827 |
if entering { |
| 828 |
context.write_str("<a")?; |
| 829 |
render_sourcepos(context, node)?; |
| 830 |
context.write_str(" href=\"")?; |
| 831 |
let url = &nl.url; |
| 832 |
if context.options.render.r#unsafe || !dangerous_url(url) { |
| 833 |
if let Some(rewriter) = &context.options.extension.link_url_rewriter { |
| 834 |
context.escape_href(&rewriter.to_html(&nl.url))?; |
| 835 |
} else { |
| 836 |
context.escape_href(url)?; |
| 837 |
} |
| 838 |
} |
| 839 |
if !nl.title.is_empty() { |
| 840 |
context.write_str("\" title=\"")?; |
| 841 |
context.escape(&nl.title)?; |
| 842 |
} |
| 843 |
context.write_str("\">")?; |
| 844 |
} else { |
| 845 |
context.write_str("</a>")?; |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
Ok(ChildRendering::HTML) |
| 850 |
} |
| 851 |
|
| 852 |
fn render_list<T>( |
| 853 |
context: &mut Context<T>, |
| 854 |
node: Node<'_>, |
| 855 |
entering: bool, |
| 856 |
nl: &NodeList, |
| 857 |
) -> Result<ChildRendering, fmt::Error> { |
| 858 |
if entering { |
| 859 |
context.cr()?; |
| 860 |
match nl.list_type { |
| 861 |
ListType::Bullet => { |
| 862 |
context.write_str("<ul")?; |
| 863 |
if nl.is_task_list && context.options.render.tasklist_classes { |
| 864 |
context.write_str(" class=\"contains-task-list\"")?; |
| 865 |
} |
| 866 |
render_sourcepos(context, node)?; |
| 867 |
context.write_str(">")?; |
| 868 |
context.lf()?; |
| 869 |
} |
| 870 |
ListType::Ordered => { |
| 871 |
context.write_str("<ol")?; |
| 872 |
if nl.is_task_list && context.options.render.tasklist_classes { |
| 873 |
context.write_str(" class=\"contains-task-list\"")?; |
| 874 |
} |
| 875 |
render_sourcepos(context, node)?; |
| 876 |
if nl.start == 1 { |
| 877 |
context.write_str(">")?; |
| 878 |
context.lf()?; |
| 879 |
} else { |
| 880 |
write!(context, " start=\"{}\">", nl.start)?; |
| 881 |
context.lf()?; |
| 882 |
} |
| 883 |
} |
| 884 |
} |
| 885 |
} else if nl.list_type == ListType::Bullet { |
| 886 |
context.write_str("</ul>")?; |
| 887 |
context.lf()?; |
| 888 |
} else { |
| 889 |
context.write_str("</ol>")?; |
| 890 |
context.lf()?; |
| 891 |
} |
| 892 |
|
| 893 |
Ok(ChildRendering::HTML) |
| 894 |
} |
| 895 |
|
| 896 |
fn render_paragraph<T>( |
| 897 |
context: &mut Context<T>, |
| 898 |
node: Node<'_>, |
| 899 |
entering: bool, |
| 900 |
) -> Result<ChildRendering, fmt::Error> { |
| 901 |
let tight = node |
| 902 |
.parent() |
| 903 |
.and_then(|n| n.parent()) |
| 904 |
.is_some_and(|n| match n.data().value { |
| 905 |
NodeValue::List(nl) => nl.tight, |
| 906 |
NodeValue::DescriptionItem(ndi) => ndi.tight, |
| 907 |
_ => false, |
| 908 |
}) |
| 909 |
|| node |
| 910 |
.parent() |
| 911 |
.is_some_and(|n| node_matches!(n, NodeValue::DescriptionTerm)); |
| 912 |
|
| 913 |
if !tight { |
| 914 |
if entering { |
| 915 |
context.cr()?; |
| 916 |
context.write_str("<p")?; |
| 917 |
render_sourcepos(context, node)?; |
| 918 |
context.write_str(">")?; |
| 919 |
} else { |
| 920 |
if let Some(parent) = node.parent() { |
| 921 |
if let NodeValue::FootnoteDefinition(ref nfd) = parent.data().value { |
| 922 |
if node.next_sibling().is_none() { |
| 923 |
context.write_str(" ")?; |
| 924 |
put_footnote_backref(context, nfd)?; |
| 925 |
} |
| 926 |
} |
| 927 |
} |
| 928 |
context.write_str("</p>")?; |
| 929 |
context.lf()?; |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
Ok(ChildRendering::HTML) |
| 934 |
} |
| 935 |
|
| 936 |
fn render_soft_break<T>( |
| 937 |
context: &mut Context<T>, |
| 938 |
node: Node<'_>, |
| 939 |
entering: bool, |
| 940 |
) -> Result<ChildRendering, fmt::Error> { |
| 941 |
if entering { |
| 942 |
if context.options.render.hardbreaks { |
| 943 |
context.write_str("<br")?; |
| 944 |
render_sourcepos(context, node)?; |
| 945 |
context.write_str(" />")?; |
| 946 |
context.lf()?; |
| 947 |
} else { |
| 948 |
context.write_str("\n")?; |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
Ok(ChildRendering::HTML) |
| 953 |
} |
| 954 |
|
| 955 |
fn render_strong<T>( |
| 956 |
context: &mut Context<T>, |
| 957 |
node: Node<'_>, |
| 958 |
entering: bool, |
| 959 |
) -> Result<ChildRendering, fmt::Error> { |
| 960 |
let parent_node = node.parent(); |
| 961 |
if !context.options.render.gfm_quirks |
| 962 |
|| (parent_node.is_none() || !node_matches!(parent_node.unwrap(), NodeValue::Strong)) |
| 963 |
{ |
| 964 |
if entering { |
| 965 |
context.write_str("<strong")?; |
| 966 |
render_sourcepos(context, node)?; |
| 967 |
context.write_str(">")?; |
| 968 |
} else { |
| 969 |
context.write_str("</strong>")?; |
| 970 |
} |
| 971 |
} |
| 972 |
|
| 973 |
Ok(ChildRendering::HTML) |
| 974 |
} |
| 975 |
|
| 976 |
fn render_text<T>( |
| 977 |
context: &mut Context<T>, |
| 978 |
entering: bool, |
| 979 |
literal: &str, |
| 980 |
) -> Result<ChildRendering, fmt::Error> { |
| 981 |
// Nowhere to put sourcepos. |
| 982 |
if entering { |
| 983 |
context.escape(literal)?; |
| 984 |
} |
| 985 |
|
| 986 |
Ok(ChildRendering::HTML) |
| 987 |
} |
| 988 |
|
| 989 |
fn render_thematic_break<T>( |
| 990 |
context: &mut Context<T>, |
| 991 |
node: Node<'_>, |
| 992 |
entering: bool, |
| 993 |
) -> Result<ChildRendering, fmt::Error> { |
| 994 |
if entering { |
| 995 |
context.cr()?; |
| 996 |
context.write_str("<hr")?; |
| 997 |
render_sourcepos(context, node)?; |
| 998 |
context.write_str(" />")?; |
| 999 |
context.lf()?; |
| 1000 |
} |
| 1001 |
|
| 1002 |
Ok(ChildRendering::HTML) |
| 1003 |
} |
| 1004 |
|
| 1005 |
// GFM |
| 1006 |
|
| 1007 |
fn render_footnote_definition<T>( |
| 1008 |
context: &mut Context<T>, |
| 1009 |
node: Node<'_>, |
| 1010 |
entering: bool, |
| 1011 |
nfd: &NodeFootnoteDefinition, |
| 1012 |
) -> Result<ChildRendering, fmt::Error> { |
| 1013 |
if entering { |
| 1014 |
if context.footnote_ix == 0 { |
| 1015 |
context.write_str("<section")?; |
| 1016 |
render_sourcepos(context, node)?; |
| 1017 |
context.write_str(" class=\"footnotes\" data-footnotes>")?; |
| 1018 |
context.lf()?; |
| 1019 |
context.write_str("<ol>")?; |
| 1020 |
context.lf()?; |
| 1021 |
} |
| 1022 |
context.footnote_ix += 1; |
| 1023 |
context.write_str("<li")?; |
| 1024 |
render_sourcepos(context, node)?; |
| 1025 |
context.write_str(" id=\"fn-")?; |
| 1026 |
context.escape_href(&nfd.name)?; |
| 1027 |
context.write_str("\">")?; |
| 1028 |
} else { |
| 1029 |
if put_footnote_backref(context, nfd)? { |
| 1030 |
context.lf()?; |
| 1031 |
} |
| 1032 |
context.write_str("</li>")?; |
| 1033 |
context.lf()?; |
| 1034 |
} |
| 1035 |
|
| 1036 |
Ok(ChildRendering::HTML) |
| 1037 |
} |
| 1038 |
|
| 1039 |
fn render_footnote_reference<T>( |
| 1040 |
context: &mut Context<T>, |
| 1041 |
node: Node<'_>, |
| 1042 |
entering: bool, |
| 1043 |
nfr: &NodeFootnoteReference, |
| 1044 |
) -> Result<ChildRendering, fmt::Error> { |
| 1045 |
if entering { |
| 1046 |
let mut ref_id = format!("fnref-{}", nfr.name); |
| 1047 |
if nfr.ref_num > 1 { |
| 1048 |
ref_id = format!("{}-{}", ref_id, nfr.ref_num); |
| 1049 |
} |
| 1050 |
|
| 1051 |
context.write_str("<sup")?; |
| 1052 |
render_sourcepos(context, node)?; |
| 1053 |
context.write_str(" class=\"footnote-ref\"><a href=\"#fn-")?; |
| 1054 |
context.escape_href(&nfr.name)?; |
| 1055 |
context.write_str("\" id=\"")?; |
| 1056 |
context.escape_href(&ref_id)?; |
| 1057 |
write!(context, "\" data-footnote-ref>{}</a></sup>", nfr.ix)?; |
| 1058 |
} |
| 1059 |
|
| 1060 |
Ok(ChildRendering::HTML) |
| 1061 |
} |
| 1062 |
|
| 1063 |
fn render_strikethrough<T>( |
| 1064 |
context: &mut Context<T>, |
| 1065 |
node: Node<'_>, |
| 1066 |
entering: bool, |
| 1067 |
) -> Result<ChildRendering, fmt::Error> { |
| 1068 |
if entering { |
| 1069 |
context.write_str("<del")?; |
| 1070 |
render_sourcepos(context, node)?; |
| 1071 |
context.write_str(">")?; |
| 1072 |
} else { |
| 1073 |
context.write_str("</del>")?; |
| 1074 |
} |
| 1075 |
|
| 1076 |
Ok(ChildRendering::HTML) |
| 1077 |
} |
| 1078 |
|
| 1079 |
fn render_highlight<T>( |
| 1080 |
context: &mut Context<T>, |
| 1081 |
node: Node<'_>, |
| 1082 |
entering: bool, |
| 1083 |
) -> Result<ChildRendering, fmt::Error> { |
| 1084 |
if entering { |
| 1085 |
context.write_str("<mark")?; |
| 1086 |
render_sourcepos(context, node)?; |
| 1087 |
context.write_str(">")?; |
| 1088 |
} else { |
| 1089 |
context.write_str("</mark>")?; |
| 1090 |
} |
| 1091 |
|
| 1092 |
Ok(ChildRendering::HTML) |
| 1093 |
} |
| 1094 |
|
| 1095 |
fn render_insert<T>( |
| 1096 |
context: &mut Context<T>, |
| 1097 |
node: Node<'_>, |
| 1098 |
entering: bool, |
| 1099 |
) -> Result<ChildRendering, fmt::Error> { |
| 1100 |
if entering { |
| 1101 |
context.write_str("<ins")?; |
| 1102 |
render_sourcepos(context, node)?; |
| 1103 |
context.write_str(">")?; |
| 1104 |
} else { |
| 1105 |
context.write_str("</ins>")?; |
| 1106 |
} |
| 1107 |
|
| 1108 |
Ok(ChildRendering::HTML) |
| 1109 |
} |
| 1110 |
|
| 1111 |
fn render_table<T>( |
| 1112 |
context: &mut Context<T>, |
| 1113 |
node: Node<'_>, |
| 1114 |
entering: bool, |
| 1115 |
) -> Result<ChildRendering, fmt::Error> { |
| 1116 |
if entering { |
| 1117 |
context.cr()?; |
| 1118 |
context.write_str("<table")?; |
| 1119 |
render_sourcepos(context, node)?; |
| 1120 |
context.write_str(">")?; |
| 1121 |
context.lf()?; |
| 1122 |
} else { |
| 1123 |
if let Some(true) = node |
| 1124 |
.last_child() |
| 1125 |
.map(|n| !n.same_node(node.first_child().unwrap())) |
| 1126 |
// node.first_child() guaranteed to exist in block since last_child does! |
| 1127 |
{ |
| 1128 |
context.cr()?; |
| 1129 |
context.write_str("</tbody>")?; |
| 1130 |
context.lf()?; |
| 1131 |
} |
| 1132 |
context.cr()?; |
| 1133 |
context.write_str("</table>")?; |
| 1134 |
context.lf()?; |
| 1135 |
} |
| 1136 |
|
| 1137 |
Ok(ChildRendering::HTML) |
| 1138 |
} |
| 1139 |
|
| 1140 |
fn render_table_cell<T>( |
| 1141 |
context: &mut Context<T>, |
| 1142 |
node: Node<'_>, |
| 1143 |
entering: bool, |
| 1144 |
) -> Result<ChildRendering, fmt::Error> { |
| 1145 |
let Some(row_node) = node.parent() else { |
| 1146 |
panic!("rendered a table cell without a containing table row"); |
| 1147 |
}; |
| 1148 |
let row = &row_node.data().value; |
| 1149 |
let in_header = match *row { |
| 1150 |
NodeValue::TableRow(header) => header, |
| 1151 |
_ => panic!("rendered a table cell contained by something other than a table row"), |
| 1152 |
}; |
| 1153 |
|
| 1154 |
let Some(table_node) = row_node.parent() else { |
| 1155 |
panic!("rendered a table cell without a containing table"); |
| 1156 |
}; |
| 1157 |
let table = &table_node.data().value; |
| 1158 |
let alignments = match table { |
| 1159 |
NodeValue::Table(nt) => &nt.alignments, |
| 1160 |
_ => { |
| 1161 |
panic!("rendered a table cell in a table row contained by something other than a table") |
| 1162 |
} |
| 1163 |
}; |
| 1164 |
|
| 1165 |
if entering { |
| 1166 |
context.cr()?; |
| 1167 |
if in_header { |
| 1168 |
context.write_str("<th")?; |
| 1169 |
render_sourcepos(context, node)?; |
| 1170 |
} else { |
| 1171 |
context.write_str("<td")?; |
| 1172 |
render_sourcepos(context, node)?; |
| 1173 |
} |
| 1174 |
|
| 1175 |
let mut start = row_node.first_child().unwrap(); // guaranteed to exist because `node' itself does! |
| 1176 |
let mut i = 0; |
| 1177 |
while !start.same_node(node) { |
| 1178 |
i += 1; |
| 1179 |
start = start.next_sibling().unwrap(); |
| 1180 |
} |
| 1181 |
|
| 1182 |
match alignments[i] { |
| 1183 |
TableAlignment::Left => { |
| 1184 |
context.write_str(" align=\"left\"")?; |
| 1185 |
} |
| 1186 |
TableAlignment::Right => { |
| 1187 |
context.write_str(" align=\"right\"")?; |
| 1188 |
} |
| 1189 |
TableAlignment::Center => { |
| 1190 |
context.write_str(" align=\"center\"")?; |
| 1191 |
} |
| 1192 |
TableAlignment::None => (), |
| 1193 |
} |
| 1194 |
|
| 1195 |
context.write_str(">")?; |
| 1196 |
} else if in_header { |
| 1197 |
context.write_str("</th>")?; |
| 1198 |
} else { |
| 1199 |
context.write_str("</td>")?; |
| 1200 |
} |
| 1201 |
|
| 1202 |
Ok(ChildRendering::HTML) |
| 1203 |
} |
| 1204 |
|
| 1205 |
fn render_table_row<T>( |
| 1206 |
context: &mut Context<T>, |
| 1207 |
node: Node<'_>, |
| 1208 |
entering: bool, |
| 1209 |
thead: bool, |
| 1210 |
) -> Result<ChildRendering, fmt::Error> { |
| 1211 |
if entering { |
| 1212 |
context.cr()?; |
| 1213 |
if thead { |
| 1214 |
context.write_str("<thead>")?; |
| 1215 |
context.lf()?; |
| 1216 |
} else if let Some(n) = node.previous_sibling() { |
| 1217 |
if let NodeValue::TableRow(true) = n.data().value { |
| 1218 |
context.write_str("<tbody>")?; |
| 1219 |
context.lf()?; |
| 1220 |
} |
| 1221 |
} |
| 1222 |
context.write_str("<tr")?; |
| 1223 |
render_sourcepos(context, node)?; |
| 1224 |
context.write_str(">")?; |
| 1225 |
} else { |
| 1226 |
context.cr()?; |
| 1227 |
context.write_str("</tr>")?; |
| 1228 |
if thead { |
| 1229 |
context.cr()?; |
| 1230 |
context.write_str("</thead>")?; |
| 1231 |
} |
| 1232 |
} |
| 1233 |
|
| 1234 |
Ok(ChildRendering::HTML) |
| 1235 |
} |
| 1236 |
|
| 1237 |
fn render_task_item<T>( |
| 1238 |
context: &mut Context<T>, |
| 1239 |
node: Node<'_>, |
| 1240 |
entering: bool, |
| 1241 |
nti: &NodeTaskItem, |
| 1242 |
) -> Result<ChildRendering, fmt::Error> { |
| 1243 |
let write_li = node |
| 1244 |
.parent() |
| 1245 |
.map(|p| node_matches!(p, NodeValue::List(_))) |
| 1246 |
.unwrap_or_default(); |
| 1247 |
|
| 1248 |
if entering { |
| 1249 |
context.cr()?; |
| 1250 |
if write_li { |
| 1251 |
context.write_str("<li")?; |
| 1252 |
if context.options.render.tasklist_classes { |
| 1253 |
context.write_str(" class=\"task-list-item\"")?; |
| 1254 |
} |
| 1255 |
render_sourcepos(context, node)?; |
| 1256 |
context.write_str(">")?; |
| 1257 |
} |
| 1258 |
context.write_str("<input type=\"checkbox\"")?; |
| 1259 |
if !write_li { |
| 1260 |
render_sourcepos(context, node)?; |
| 1261 |
} |
| 1262 |
if context.options.render.tasklist_classes { |
| 1263 |
context.write_str(" class=\"task-list-item-checkbox\"")?; |
| 1264 |
} |
| 1265 |
if nti.symbol.is_some() { |
| 1266 |
context.write_str(" checked=\"\"")?; |
| 1267 |
} |
| 1268 |
context.write_str(" disabled=\"\" /> ")?; |
| 1269 |
} else if write_li { |
| 1270 |
context.write_str("</li>")?; |
| 1271 |
context.lf()?; |
| 1272 |
} |
| 1273 |
|
| 1274 |
Ok(ChildRendering::HTML) |
| 1275 |
} |
| 1276 |
|
| 1277 |
// Extensions |
| 1278 |
|
| 1279 |
fn render_alert<T>( |
| 1280 |
context: &mut Context<T>, |
| 1281 |
node: Node<'_>, |
| 1282 |
entering: bool, |
| 1283 |
alert: &NodeAlert, |
| 1284 |
) -> Result<ChildRendering, fmt::Error> { |
| 1285 |
if entering { |
| 1286 |
context.cr()?; |
| 1287 |
context.write_str(match context.options.render.alert_style { |
| 1288 |
AlertStyleType::Specific => "<div class=\"markdown-alert ", |
| 1289 |
AlertStyleType::Semantic => "<aside class=\"admonition ", |
| 1290 |
})?; |
| 1291 |
context.write_str( |
| 1292 |
alert |
| 1293 |
.alert_type |
| 1294 |
.css_class(context.options.render.alert_style), |
| 1295 |
)?; |
| 1296 |
context.write_str("\"")?; |
| 1297 |
render_sourcepos(context, node)?; |
| 1298 |
context.write_str(">")?; |
| 1299 |
context.lf()?; |
| 1300 |
context.write_str("<p class=\"")?; |
| 1301 |
context.write_str(match context.options.render.alert_style { |
| 1302 |
AlertStyleType::Specific => "markdown-alert-title", |
| 1303 |
AlertStyleType::Semantic => "admonition-title", |
| 1304 |
})?; |
| 1305 |
context.write_str("\">")?; |
| 1306 |
match alert.title { |
| 1307 |
Some(ref title) => context.escape(title)?, |
| 1308 |
None => { |
| 1309 |
context.write_str(alert.alert_type.default_title())?; |
| 1310 |
} |
| 1311 |
} |
| 1312 |
context.write_str("</p>")?; |
| 1313 |
context.lf()?; |
| 1314 |
} else { |
| 1315 |
context.cr()?; |
| 1316 |
context.write_str(match context.options.render.alert_style { |
| 1317 |
AlertStyleType::Specific => "</div>", |
| 1318 |
AlertStyleType::Semantic => "</aside>", |
| 1319 |
})?; |
| 1320 |
context.lf()?; |
| 1321 |
} |
| 1322 |
|
| 1323 |
Ok(ChildRendering::HTML) |
| 1324 |
} |
| 1325 |
|
| 1326 |
fn render_description_details<T>( |
| 1327 |
context: &mut Context<T>, |
| 1328 |
node: Node<'_>, |
| 1329 |
entering: bool, |
| 1330 |
) -> Result<ChildRendering, fmt::Error> { |
| 1331 |
if entering { |
| 1332 |
context.write_str("<dd")?; |
| 1333 |
render_sourcepos(context, node)?; |
| 1334 |
context.write_str(">")?; |
| 1335 |
} else { |
| 1336 |
context.write_str("</dd>")?; |
| 1337 |
context.lf()?; |
| 1338 |
} |
| 1339 |
|
| 1340 |
Ok(ChildRendering::HTML) |
| 1341 |
} |
| 1342 |
|
| 1343 |
fn render_description_list<T>( |
| 1344 |
context: &mut Context<T>, |
| 1345 |
node: Node<'_>, |
| 1346 |
entering: bool, |
| 1347 |
) -> Result<ChildRendering, fmt::Error> { |
| 1348 |
if entering { |
| 1349 |
context.cr()?; |
| 1350 |
context.write_str("<dl")?; |
| 1351 |
render_sourcepos(context, node)?; |
| 1352 |
context.write_str(">")?; |
| 1353 |
context.lf()?; |
| 1354 |
} else { |
| 1355 |
context.write_str("</dl>")?; |
| 1356 |
context.lf()?; |
| 1357 |
} |
| 1358 |
|
| 1359 |
Ok(ChildRendering::HTML) |
| 1360 |
} |
| 1361 |
|
| 1362 |
fn render_description_term<T>( |
| 1363 |
context: &mut Context<T>, |
| 1364 |
node: Node<'_>, |
| 1365 |
entering: bool, |
| 1366 |
) -> Result<ChildRendering, fmt::Error> { |
| 1367 |
if entering { |
| 1368 |
context.write_str("<dt")?; |
| 1369 |
render_sourcepos(context, node)?; |
| 1370 |
context.write_str(">")?; |
| 1371 |
} else { |
| 1372 |
context.write_str("</dt>")?; |
| 1373 |
context.lf()?; |
| 1374 |
} |
| 1375 |
|
| 1376 |
Ok(ChildRendering::HTML) |
| 1377 |
} |
| 1378 |
|
| 1379 |
fn render_escaped<T>( |
| 1380 |
context: &mut Context<T>, |
| 1381 |
node: Node<'_>, |
| 1382 |
entering: bool, |
| 1383 |
) -> Result<ChildRendering, fmt::Error> { |
| 1384 |
if context.options.render.escaped_char_spans { |
| 1385 |
if entering { |
| 1386 |
context.write_str("<span data-escaped-char")?; |
| 1387 |
render_sourcepos(context, node)?; |
| 1388 |
context.write_str(">")?; |
| 1389 |
} else { |
| 1390 |
context.write_str("</span>")?; |
| 1391 |
} |
| 1392 |
} |
| 1393 |
|
| 1394 |
Ok(ChildRendering::HTML) |
| 1395 |
} |
| 1396 |
|
| 1397 |
fn render_escaped_tag<T>( |
| 1398 |
context: &mut Context<T>, |
| 1399 |
net: &str, |
| 1400 |
) -> Result<ChildRendering, fmt::Error> { |
| 1401 |
// Nowhere to put sourcepos. |
| 1402 |
context.write_str(net)?; |
| 1403 |
|
| 1404 |
Ok(ChildRendering::HTML) |
| 1405 |
} |
| 1406 |
|
| 1407 |
/// Renders a math dollar inline, `$...$` and `$$...$$` using `<span>` to be |
| 1408 |
/// similar to other renderers. |
| 1409 |
pub fn render_math<T>( |
| 1410 |
context: &mut Context<T>, |
| 1411 |
node: Node<'_>, |
| 1412 |
entering: bool, |
| 1413 |
nm: &NodeMath, |
| 1414 |
) -> Result<ChildRendering, fmt::Error> { |
| 1415 |
if entering { |
| 1416 |
let mut tag_attributes: Vec<(&str, Cow<str>)> = Vec::new(); |
| 1417 |
let style_attr = if nm.display_math { "display" } else { "inline" }; |
| 1418 |
let tag: &str = if nm.dollar_math { "span" } else { "code" }; |
| 1419 |
|
| 1420 |
tag_attributes.push(("data-math-style", style_attr.into())); |
| 1421 |
|
| 1422 |
if context.options.render.sourcepos { |
| 1423 |
let ast = node.data(); |
| 1424 |
tag_attributes.push(("data-sourcepos", ast.sourcepos.to_string().into())); |
| 1425 |
} |
| 1426 |
|
| 1427 |
write_opening_tag(context, tag, tag_attributes.into_iter())?; |
| 1428 |
context.escape(&nm.literal)?; |
| 1429 |
write!(context, "</{tag}>")?; |
| 1430 |
} |
| 1431 |
|
| 1432 |
Ok(ChildRendering::HTML) |
| 1433 |
} |
| 1434 |
|
| 1435 |
/// Renders a math code block, ```` ```math ```` using `<pre><code>`. |
| 1436 |
pub fn render_math_code_block<T>( |
| 1437 |
context: &mut Context<T>, |
| 1438 |
node: Node<'_>, |
| 1439 |
literal: &str, |
| 1440 |
) -> Result<ChildRendering, fmt::Error> { |
| 1441 |
context.cr()?; |
| 1442 |
|
| 1443 |
// use vectors to ensure attributes always written in the same order, |
| 1444 |
// for testing stability |
| 1445 |
let mut pre_attributes: Vec<(&str, Cow<str>)> = Vec::new(); |
| 1446 |
let mut code_attributes: Vec<(&str, Cow<str>)> = Vec::new(); |
| 1447 |
let lang_str = "math"; |
| 1448 |
|
| 1449 |
if context.options.render.github_pre_lang { |
| 1450 |
pre_attributes.push(("lang", lang_str.into())); |
| 1451 |
pre_attributes.push(("data-math-style", "display".into())); |
| 1452 |
} else { |
| 1453 |
let code_attr = format!("language-{}", lang_str); |
| 1454 |
code_attributes.push(("class", code_attr.into())); |
| 1455 |
code_attributes.push(("data-math-style", "display".into())); |
| 1456 |
} |
| 1457 |
|
| 1458 |
if context.options.render.sourcepos { |
| 1459 |
let ast = node.data(); |
| 1460 |
pre_attributes.push(("data-sourcepos", ast.sourcepos.to_string().into())); |
| 1461 |
} |
| 1462 |
|
| 1463 |
write_opening_tag(context, "pre", pre_attributes.into_iter())?; |
| 1464 |
write_opening_tag(context, "code", code_attributes.into_iter())?; |
| 1465 |
|
| 1466 |
context.escape(literal)?; |
| 1467 |
context.write_str("</code></pre>")?; |
| 1468 |
context.lf()?; |
| 1469 |
|
| 1470 |
Ok(ChildRendering::HTML) |
| 1471 |
} |
| 1472 |
|
| 1473 |
fn render_multiline_block_quote<T>( |
| 1474 |
context: &mut Context<T>, |
| 1475 |
node: Node<'_>, |
| 1476 |
entering: bool, |
| 1477 |
) -> Result<ChildRendering, fmt::Error> { |
| 1478 |
if entering { |
| 1479 |
context.cr()?; |
| 1480 |
context.write_str("<blockquote")?; |
| 1481 |
render_sourcepos(context, node)?; |
| 1482 |
context.write_str(">")?; |
| 1483 |
context.lf()?; |
| 1484 |
} else { |
| 1485 |
context.cr()?; |
| 1486 |
context.write_str("</blockquote>")?; |
| 1487 |
context.lf()?; |
| 1488 |
} |
| 1489 |
|
| 1490 |
Ok(ChildRendering::HTML) |
| 1491 |
} |
| 1492 |
|
| 1493 |
fn render_raw<T>( |
| 1494 |
context: &mut Context<T>, |
| 1495 |
entering: bool, |
| 1496 |
literal: &str, |
| 1497 |
) -> Result<ChildRendering, fmt::Error> { |
| 1498 |
// No sourcepos. |
| 1499 |
if entering { |
| 1500 |
context.write_str(literal)?; |
| 1501 |
} |
| 1502 |
|
| 1503 |
Ok(ChildRendering::HTML) |
| 1504 |
} |
| 1505 |
|
| 1506 |
#[cfg(feature = "shortcodes")] |
| 1507 |
fn render_short_code<T>( |
| 1508 |
context: &mut Context<T>, |
| 1509 |
entering: bool, |
| 1510 |
nsc: &NodeShortCode, |
| 1511 |
) -> Result<ChildRendering, fmt::Error> { |
| 1512 |
// Nowhere to put sourcepos. |
| 1513 |
if entering { |
| 1514 |
context.write_str(&nsc.emoji)?; |
| 1515 |
} |
| 1516 |
|
| 1517 |
Ok(ChildRendering::HTML) |
| 1518 |
} |
| 1519 |
|
| 1520 |
fn render_spoiler_text<T>( |
| 1521 |
context: &mut Context<T>, |
| 1522 |
node: Node<'_>, |
| 1523 |
entering: bool, |
| 1524 |
) -> Result<ChildRendering, fmt::Error> { |
| 1525 |
if entering { |
| 1526 |
context.write_str("<span")?; |
| 1527 |
render_sourcepos(context, node)?; |
| 1528 |
context.write_str(" class=\"spoiler\">")?; |
| 1529 |
} else { |
| 1530 |
context.write_str("</span>")?; |
| 1531 |
} |
| 1532 |
|
| 1533 |
Ok(ChildRendering::HTML) |
| 1534 |
} |
| 1535 |
|
| 1536 |
fn render_subscript<T>( |
| 1537 |
context: &mut Context<T>, |
| 1538 |
node: Node<'_>, |
| 1539 |
entering: bool, |
| 1540 |
) -> Result<ChildRendering, fmt::Error> { |
| 1541 |
if entering { |
| 1542 |
context.write_str("<sub")?; |
| 1543 |
render_sourcepos(context, node)?; |
| 1544 |
context.write_str(">")?; |
| 1545 |
} else { |
| 1546 |
context.write_str("</sub>")?; |
| 1547 |
} |
| 1548 |
|
| 1549 |
Ok(ChildRendering::HTML) |
| 1550 |
} |
| 1551 |
|
| 1552 |
fn render_subtext<T>( |
| 1553 |
context: &mut Context<T>, |
| 1554 |
node: Node<'_>, |
| 1555 |
entering: bool, |
| 1556 |
) -> Result<ChildRendering, fmt::Error> { |
| 1557 |
if entering { |
| 1558 |
context.cr()?; |
| 1559 |
context.write_str("<p><sub")?; |
| 1560 |
render_sourcepos(context, node)?; |
| 1561 |
context.write_str(">")?; |
| 1562 |
} else { |
| 1563 |
write!(context, "</sub></p>")?; |
| 1564 |
context.lf()?; |
| 1565 |
} |
| 1566 |
|
| 1567 |
Ok(ChildRendering::HTML) |
| 1568 |
} |
| 1569 |
|
| 1570 |
fn render_superscript<T>( |
| 1571 |
context: &mut Context<T>, |
| 1572 |
node: Node<'_>, |
| 1573 |
entering: bool, |
| 1574 |
) -> Result<ChildRendering, fmt::Error> { |
| 1575 |
if entering { |
| 1576 |
context.write_str("<sup")?; |
| 1577 |
render_sourcepos(context, node)?; |
| 1578 |
context.write_str(">")?; |
| 1579 |
} else { |
| 1580 |
context.write_str("</sup>")?; |
| 1581 |
} |
| 1582 |
|
| 1583 |
Ok(ChildRendering::HTML) |
| 1584 |
} |
| 1585 |
|
| 1586 |
fn render_underline<T>( |
| 1587 |
context: &mut Context<T>, |
| 1588 |
node: Node<'_>, |
| 1589 |
entering: bool, |
| 1590 |
) -> Result<ChildRendering, fmt::Error> { |
| 1591 |
if entering { |
| 1592 |
context.write_str("<u")?; |
| 1593 |
render_sourcepos(context, node)?; |
| 1594 |
context.write_str(">")?; |
| 1595 |
} else { |
| 1596 |
context.write_str("</u>")?; |
| 1597 |
} |
| 1598 |
|
| 1599 |
Ok(ChildRendering::HTML) |
| 1600 |
} |
| 1601 |
|
| 1602 |
fn render_wiki_link<T>( |
| 1603 |
context: &mut Context<T>, |
| 1604 |
node: Node<'_>, |
| 1605 |
entering: bool, |
| 1606 |
nwl: &NodeWikiLink, |
| 1607 |
) -> Result<ChildRendering, fmt::Error> { |
| 1608 |
if entering { |
| 1609 |
context.write_str("<a")?; |
| 1610 |
render_sourcepos(context, node)?; |
| 1611 |
context.write_str(" href=\"")?; |
| 1612 |
let url = &nwl.url; |
| 1613 |
if context.options.render.r#unsafe || !dangerous_url(url) { |
| 1614 |
context.escape_href(url)?; |
| 1615 |
} |
| 1616 |
context.write_str("\" data-wikilink=\"true")?; |
| 1617 |
context.write_str("\">")?; |
| 1618 |
} else { |
| 1619 |
context.write_str("</a>")?; |
| 1620 |
} |
| 1621 |
|
| 1622 |
Ok(ChildRendering::HTML) |
| 1623 |
} |
| 1624 |
|
| 1625 |
fn put_footnote_backref<T>( |
| 1626 |
context: &mut Context<T>, |
| 1627 |
nfd: &NodeFootnoteDefinition, |
| 1628 |
) -> Result<bool, fmt::Error> { |
| 1629 |
if context.written_footnote_ix >= context.footnote_ix { |
| 1630 |
return Ok(false); |
| 1631 |
} |
| 1632 |
|
| 1633 |
context.written_footnote_ix = context.footnote_ix; |
| 1634 |
|
| 1635 |
let mut ref_suffix = String::new(); |
| 1636 |
let mut superscript = String::new(); |
| 1637 |
|
| 1638 |
for ref_num in 1..=nfd.total_references { |
| 1639 |
if ref_num > 1 { |
| 1640 |
ref_suffix = format!("-{ref_num}"); |
| 1641 |
superscript = format!("<sup class=\"footnote-ref\">{ref_num}</sup>"); |
| 1642 |
write!(context, " ")?; |
| 1643 |
} |
| 1644 |
|
| 1645 |
context.write_str("<a href=\"#fnref-")?; |
| 1646 |
context.escape_href(&nfd.name)?; |
| 1647 |
let fnix = context.footnote_ix; |
| 1648 |
write!( |
| 1649 |
context, |
| 1650 |
"{ref_suffix}\" class=\"footnote-backref\" data-footnote-backref data-footnote-backref-idx=\"{fnix}{ref_suffix}\" aria-label=\"Back to reference {fnix}{ref_suffix}\">↩{superscript}</a>", |
| 1651 |
)?; |
| 1652 |
} |
| 1653 |
Ok(true) |
| 1654 |
} |
| 1655 |
|
| 1656 |
fn tagfilter(literal: &str) -> bool { |
| 1657 |
let bytes = literal.as_bytes(); |
| 1658 |
|
| 1659 |
static TAGFILTER_BLACKLIST: [&str; 9] = [ |
| 1660 |
"title", |
| 1661 |
"textarea", |
| 1662 |
"style", |
| 1663 |
"xmp", |
| 1664 |
"iframe", |
| 1665 |
"noembed", |
| 1666 |
"noframes", |
| 1667 |
"script", |
| 1668 |
"plaintext", |
| 1669 |
]; |
| 1670 |
|
| 1671 |
if bytes.len() < 3 || bytes[0] != b'<' { |
| 1672 |
return false; |
| 1673 |
} |
| 1674 |
|
| 1675 |
let mut i = 1; |
| 1676 |
if bytes[i] == b'/' { |
| 1677 |
i += 1; |
| 1678 |
} |
| 1679 |
|
| 1680 |
let lc = literal[i..].to_lowercase(); |
| 1681 |
for t in TAGFILTER_BLACKLIST.iter() { |
| 1682 |
if lc.starts_with(t) { |
| 1683 |
let j = i + t.len(); |
| 1684 |
let Some(&b) = bytes.get(j) else { |
| 1685 |
return false; |
| 1686 |
}; |
| 1687 |
return isspace(b) || b == b'>' || (b == b'/' && bytes.get(j + 1) == Some(&b'>')); |
| 1688 |
} |
| 1689 |
} |
| 1690 |
|
| 1691 |
false |
| 1692 |
} |
| 1693 |
|
| 1694 |
fn tagfilter_block(output: &mut dyn Write, buffer: &str) -> fmt::Result { |
| 1695 |
let bytes = buffer.as_bytes(); |
| 1696 |
let matcher = jetscii::bytes!(b'<'); |
| 1697 |
|
| 1698 |
let mut offset = 0; |
| 1699 |
while let Some(i) = matcher.find(&bytes[offset..]) { |
| 1700 |
output.write_str(&buffer[offset..offset + i])?; |
| 1701 |
if tagfilter(&buffer[offset + i..]) { |
| 1702 |
output.write_str("<")?; |
| 1703 |
} else { |
| 1704 |
output.write_str("<")?; |
| 1705 |
} |
| 1706 |
offset += i + 1; |
| 1707 |
} |
| 1708 |
output.write_str(&buffer[offset..])?; |
| 1709 |
Ok(()) |
| 1710 |
} |
| 1711 |
|
| 1712 |
/// Check if the input would be considered a dangerous url |
| 1713 |
pub fn dangerous_url(input: &str) -> bool { |
| 1714 |
scanners::dangerous_url(input).is_some() |
| 1715 |
} |
| 1716 |
|
| 1717 |
/// Writes buffer to output, escaping anything that could be interpreted as an |
| 1718 |
/// HTML tag. |
| 1719 |
/// |
| 1720 |
/// Namely: |
| 1721 |
/// |
| 1722 |
/// * U+0022 QUOTATION MARK " is rendered as " |
| 1723 |
/// * U+0026 AMPERSAND & is rendered as & |
| 1724 |
/// * U+003C LESS-THAN SIGN < is rendered as < |
| 1725 |
/// * U+003E GREATER-THAN SIGN > is rendered as > |
| 1726 |
/// * Everything else is passed through unchanged. |
| 1727 |
/// |
| 1728 |
/// Note that this is appropriate and sufficient for free text, but not for |
| 1729 |
/// URLs in attributes. See escape_href. |
| 1730 |
pub fn escape(output: &mut dyn Write, buffer: &str) -> fmt::Result { |
| 1731 |
let bytes = buffer.as_bytes(); |
| 1732 |
let matcher = jetscii::bytes!(b'"', b'&', b'<', b'>', b'\0'); |
| 1733 |
|
| 1734 |
let mut offset = 0; |
| 1735 |
while let Some(i) = matcher.find(&bytes[offset..]) { |
| 1736 |
let esc: &str = match bytes[offset + i] { |
| 1737 |
b'"' => """, |
| 1738 |
b'&' => "&", |
| 1739 |
b'<' => "<", |
| 1740 |
b'>' => ">", |
| 1741 |
b'\0' => "\u{fffd}", |
| 1742 |
_ => unreachable!(), |
| 1743 |
}; |
| 1744 |
output.write_str(&buffer[offset..offset + i])?; |
| 1745 |
output.write_str(esc)?; |
| 1746 |
offset += i + 1; |
| 1747 |
} |
| 1748 |
output.write_str(&buffer[offset..])?; |
| 1749 |
Ok(()) |
| 1750 |
} |
| 1751 |
|
| 1752 |
/// Writes buffer to output, escaping in a manner appropriate for URLs in HTML |
| 1753 |
/// attributes. |
| 1754 |
/// |
| 1755 |
/// Namely: |
| 1756 |
/// |
| 1757 |
/// * U+0026 AMPERSAND & is rendered as & |
| 1758 |
/// * U+0027 APOSTROPHE ' is rendered as ' |
| 1759 |
/// * Alphanumeric and a range of non-URL safe characters. |
| 1760 |
/// |
| 1761 |
/// Note that we leave "%" alone if it is followed by two hexdigits. |
| 1762 |
/// See: |
| 1763 |
/// |
| 1764 |
/// <https://github.com/github/cmark-gfm/blob/c32ef78bae851cb83b7ad52d0fbff880acdcd44a/src/houdini_href_e.c#L7-L31> |
| 1765 |
/// |
| 1766 |
/// In other words, if a CommonMark user enters: |
| 1767 |
/// |
| 1768 |
/// ```markdown |
| 1769 |
/// [hi](https://ddg.gg/?q=a%20b) |
| 1770 |
/// ``` |
| 1771 |
/// |
| 1772 |
/// We assume they actually want the query string "?q=a%20b", a search for |
| 1773 |
/// the string "a b", rather than "?q=a%2520b", a search for the literal |
| 1774 |
/// string "a%20b". |
| 1775 |
/// |
| 1776 |
/// We take some care with the square bracket characters `[` and `]`. We permit |
| 1777 |
/// them to be unescaped in the output iff they surround what reasonably appears |
| 1778 |
/// to be an IPv6 address in the host part of a URI. If `relaxed_ipv6` is |
| 1779 |
/// `true`, any scheme is permitted for such an address. Otherwise, only `http` |
| 1780 |
/// or `https` are permitted. |
| 1781 |
pub fn escape_href(output: &mut dyn Write, buffer: &str, relaxed_ipv6: bool) -> fmt::Result { |
| 1782 |
const HREF_SAFE: [bool; 256] = character_set!( |
| 1783 |
b"-_.+!*(),#@?=;:/,+$~", |
| 1784 |
b"abcdefghijklmnopqrstuvwxyz", |
| 1785 |
b"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" |
| 1786 |
); |
| 1787 |
|
| 1788 |
let bytes = buffer.as_bytes(); |
| 1789 |
let size = buffer.len(); |
| 1790 |
let mut i = 0; |
| 1791 |
|
| 1792 |
let possible_ipv6_url_end = if relaxed_ipv6 { |
| 1793 |
scanners::ipv6_relaxed_url_start(buffer) |
| 1794 |
} else { |
| 1795 |
scanners::ipv6_url_start(buffer) |
| 1796 |
}; |
| 1797 |
if let Some(ipv6_url_end) = possible_ipv6_url_end { |
| 1798 |
output.write_str(&buffer[0..ipv6_url_end])?; |
| 1799 |
i = ipv6_url_end; |
| 1800 |
} |
| 1801 |
|
| 1802 |
while i < size { |
| 1803 |
let org = i; |
| 1804 |
while i < size && HREF_SAFE[bytes[i] as usize] { |
| 1805 |
i += 1; |
| 1806 |
} |
| 1807 |
|
| 1808 |
if i > org { |
| 1809 |
output.write_str(&buffer[org..i])?; |
| 1810 |
} |
| 1811 |
|
| 1812 |
if i >= size { |
| 1813 |
break; |
| 1814 |
} |
| 1815 |
|
| 1816 |
match bytes[i] { |
| 1817 |
b'&' => { |
| 1818 |
output.write_str("&")?; |
| 1819 |
} |
| 1820 |
b'\'' => { |
| 1821 |
output.write_str("'")?; |
| 1822 |
} |
| 1823 |
b'%' => { |
| 1824 |
if bytes.get(i + 1).is_some_and(|b| b.is_ascii_hexdigit()) |
| 1825 |
&& bytes.get(i + 2).is_some_and(|b| b.is_ascii_hexdigit()) |
| 1826 |
{ |
| 1827 |
output.write_str(&buffer[i..=i + 2])?; |
| 1828 |
i += 2; |
| 1829 |
} else { |
| 1830 |
output.write_str("%25")?; |
| 1831 |
} |
| 1832 |
} |
| 1833 |
0 => { |
| 1834 |
// U+FFFD REPLACEMENT CHARACTER |
| 1835 |
output.write_str("%EF%BF%BD")?; |
| 1836 |
} |
| 1837 |
_ => write!(output, "%{:02X}", bytes[i])?, |
| 1838 |
} |
| 1839 |
|
| 1840 |
i += 1; |
| 1841 |
} |
| 1842 |
|
| 1843 |
Ok(()) |
| 1844 |
} |
| 1845 |
|
| 1846 |
/// Writes an opening HTML tag, using an iterator to enumerate the attributes. |
| 1847 |
/// Note that attribute values are automatically escaped. |
| 1848 |
pub fn write_opening_tag<K: AsRef<str>, V: AsRef<str>>( |
| 1849 |
output: &mut dyn Write, |
| 1850 |
tag: &str, |
| 1851 |
attributes: impl IntoIterator<Item = (K, V)>, |
| 1852 |
) -> fmt::Result { |
| 1853 |
write!(output, "<{tag}")?; |
| 1854 |
for (attr, val) in attributes { |
| 1855 |
write!(output, " {}=\"", attr.as_ref())?; |
| 1856 |
escape(output, val.as_ref())?; |
| 1857 |
output.write_str("\"")?; |
| 1858 |
} |
| 1859 |
output.write_str(">")?; |
| 1860 |
Ok(()) |
| 1861 |
} |