Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support language-specific codefence renderers #534

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ use std::io::{self, Write};

use crate::nodes::Sourcepos;

/// Implement this adapter for custom rendering of codefence blocks.
pub trait CodefenceRendererAdapter: Send + Sync {
/// Render the codefence block.
fn write(
&self,
output: &mut dyn Write,
literal: &String,
sourcepos: Option<Sourcepos>,
) -> io::Result<()>;
}

/// Implement this adapter for creating a plugin for custom syntax highlighting of codefence blocks.
pub trait SyntaxHighlighterAdapter: Send + Sync {
/// Generates a syntax highlighted HTML output.
Expand Down
17 changes: 17 additions & 0 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,23 @@ where
if entering {
if ncb.info.eq("math") {
self.render_math_code_block(node, &ncb.literal)?;
Comment on lines 575 to 576
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably, this could be converted to a language-specific renderer that's registered by default.

} else if let Some(adapter) = self
.plugins
.render
.codefence_renderers
.get(&ncb.info)
{
self.cr()?;

adapter.write(
self.output,
&ncb.literal,
if self.options.render.sourcepos {
Some(node.data.borrow().sourcepos)
} else {
None
},
)?;
} else {
self.cr()?;

Expand Down
6 changes: 5 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod alert;
pub mod math;
pub mod multiline_block_quote;

use crate::adapters::SyntaxHighlighterAdapter;
use crate::adapters::{CodefenceRendererAdapter, SyntaxHighlighterAdapter};
use crate::arena_tree::Node;
use crate::ctype::{isdigit, isspace};
use crate::entity;
Expand Down Expand Up @@ -1098,6 +1098,10 @@ pub struct Plugins<'p> {
#[cfg_attr(feature = "bon", derive(Builder))]
/// Plugins for alternative rendering.
pub struct RenderPlugins<'p> {
/// Provide language-specific renderers for codefence blocks.
#[builder(default)]
pub codefence_renderers: HashMap<String, &'p dyn CodefenceRendererAdapter>,

/// Provide a syntax highlighter adapter implementation for syntax
/// highlighting of codefence blocks.
/// ```
Expand Down