Debian Codegraph @

src/adapters.rs

raw
1 //! Adapter traits for plugins.
2 //!
3 //! Each plugin implements one or more of the traits available in this module.
4
5 use std::borrow::Cow;
6 use std::collections::HashMap;
7 use std::fmt;
8
9 use crate::nodes::Sourcepos;
10
11 /// Implement this adapter for custom rendering of codefence blocks.
12 pub trait CodefenceRendererAdapter: Send + Sync {
13 /// Render a codefence block.
14 ///
15 /// lang: Name of the programming language (the first token of the info string).
16 /// meta: The remaining codefence info string after the language token, trimmed.
17 fn write(
18 &self,
19 output: &mut dyn fmt::Write,
20 lang: &str,
21 meta: &str,
22 code: &str,
23 sourcepos: Option<Sourcepos>,
24 ) -> fmt::Result;
25 }
26
27 /// Implement this adapter for creating a plugin for custom syntax highlighting of codefence blocks.
28 pub trait SyntaxHighlighterAdapter: Send + Sync {
29 /// Generates a syntax highlighted HTML output.
30 ///
31 /// lang: Name of the programming language (the info string of the codefence block after the initial "```" part).
32 /// code: The source code to be syntax highlighted.
33 fn write_highlighted(
34 &self,
35 output: &mut dyn fmt::Write,
36 lang: Option<&str>,
37 code: &str,
38 ) -> fmt::Result;
39
40 /// Generates the opening `<pre>` tag. Some syntax highlighter libraries might include their own
41 /// `<pre>` tag possibly with some HTML attribute pre-filled.
42 ///
43 /// `attributes`: A map of HTML attributes provided by Comrak.
44 fn write_pre_tag(
45 &self,
46 output: &mut dyn fmt::Write,
47 attributes: HashMap<&'static str, Cow<'_, str>>,
48 ) -> fmt::Result;
49
50 /// Generates the opening `<code>` tag. Some syntax highlighter libraries might include their own
51 /// `<code>` tag possibly with some HTML attribute pre-filled.
52 ///
53 /// `attributes`: A map of HTML attributes provided by Comrak.
54 fn write_code_tag(
55 &self,
56 output: &mut dyn fmt::Write,
57 attributes: HashMap<&'static str, Cow<'_, str>>,
58 ) -> fmt::Result;
59 }
60
61 /// The struct passed to the [`HeadingAdapter`] for custom heading implementations.
62 #[derive(Clone, Debug)]
63 pub struct HeadingMeta {
64 /// The level of the heading; from 1 to 6 for ATX headings, 1 or 2 for setext headings.
65 pub level: u8,
66
67 /// The content of the heading as a "flattened" string&mdash;flattened in the sense that any
68 /// `<strong>` or other tags are removed. In the Markdown heading `## This is **bold**`, for
69 /// example, the would be the string `"This is bold"`.
70 pub content: String,
71 }
72
73 /// Implement this adapter for creating a plugin for custom headings (`h1`, `h2`, etc.). The `enter`
74 /// method defines what's rendered prior the AST content of the heading while the `exit` method
75 /// defines what's rendered after it. Both methods provide access to a [`HeadingMeta`] struct and
76 /// leave the AST content of the heading unchanged.
77 pub trait HeadingAdapter: Send + Sync {
78 /// Render the opening tag.
79 fn enter(
80 &self,
81 output: &mut dyn fmt::Write,
82 heading: &HeadingMeta,
83 sourcepos: Option<Sourcepos>,
84 ) -> fmt::Result;
85
86 /// Render the closing tag.
87 fn exit(&self, output: &mut dyn fmt::Write, heading: &HeadingMeta) -> fmt::Result;
88 }