pub mod prelude {
pub use super::{
HtmlFormatter, LatexFormatter, PygmentizeFormatter, SvgFormatter, Terminal256Formatter,
TerminalFormatter, TerminalTrueColorFormatter,
};
}
use std::borrow::Cow;
use crate::{highlight, PygmentizeError};
pub trait PygmentizeFormatter: Sized {
const SHORT_NAME: &'static str;
fn options_str(&self) -> Option<Cow<'_, str>>;
fn highlight(
&self,
code: impl AsRef<str>,
lang: Option<&str>,
) -> Result<String, PygmentizeError> {
highlight(code, lang, self)
}
}
#[derive(Clone, Debug)]
pub struct HtmlFormatter {
pub line_numbers: bool,
}
impl Default for HtmlFormatter {
fn default() -> Self {
Self {
line_numbers: false,
}
}
}
impl HtmlFormatter {
pub fn new() -> Self {
Self::default()
}
}
impl PygmentizeFormatter for HtmlFormatter {
const SHORT_NAME: &'static str = "html";
fn options_str(&self) -> Option<Cow<'_, str>> {
if self.line_numbers {
Some(Cow::Borrowed("linenos=true"))
} else {
None
}
}
}
#[derive(Clone, Debug)]
pub struct SvgFormatter {
pub line_numbers: bool,
}
impl Default for SvgFormatter {
fn default() -> Self {
Self {
line_numbers: false,
}
}
}
impl SvgFormatter {
pub fn new() -> Self {
Self::default()
}
}
impl PygmentizeFormatter for SvgFormatter {
const SHORT_NAME: &'static str = "svg";
fn options_str(&self) -> Option<Cow<'_, str>> {
if self.line_numbers {
Some(Cow::Borrowed("linenos=true"))
} else {
None
}
}
}
#[derive(Clone, Debug)]
pub struct LatexFormatter {
pub line_numbers: bool,
}
impl Default for LatexFormatter {
fn default() -> Self {
Self {
line_numbers: false,
}
}
}
impl LatexFormatter {
pub fn new() -> Self {
Self::default()
}
}
impl PygmentizeFormatter for LatexFormatter {
const SHORT_NAME: &'static str = "latex";
fn options_str(&self) -> Option<Cow<'_, str>> {
if self.line_numbers {
Some(Cow::Borrowed("linenos=true"))
} else {
None
}
}
}
#[derive(Clone, Debug)]
pub struct TerminalFormatter {
pub line_numbers: bool,
}
impl Default for TerminalFormatter {
fn default() -> Self {
Self {
line_numbers: false,
}
}
}
impl TerminalFormatter {
pub fn new() -> Self {
Self::default()
}
}
impl PygmentizeFormatter for TerminalFormatter {
const SHORT_NAME: &'static str = "terminal";
fn options_str(&self) -> Option<Cow<'_, str>> {
if self.line_numbers {
Some(Cow::Borrowed("linenos=true"))
} else {
None
}
}
}
#[derive(Clone, Debug)]
pub struct TerminalTrueColorFormatter {
pub line_numbers: bool,
}
impl Default for TerminalTrueColorFormatter {
fn default() -> Self {
Self {
line_numbers: false,
}
}
}
impl TerminalTrueColorFormatter {
pub fn new() -> Self {
Self::default()
}
}
impl PygmentizeFormatter for TerminalTrueColorFormatter {
const SHORT_NAME: &'static str = "terminal16m";
fn options_str(&self) -> Option<Cow<'_, str>> {
if self.line_numbers {
Some(Cow::Borrowed("linenos=true"))
} else {
None
}
}
}
#[derive(Clone, Debug)]
pub struct Terminal256Formatter {
pub line_numbers: bool,
}
impl Default for Terminal256Formatter {
fn default() -> Self {
Self {
line_numbers: false,
}
}
}
impl Terminal256Formatter {
pub fn new() -> Self {
Self::default()
}
}
impl PygmentizeFormatter for Terminal256Formatter {
const SHORT_NAME: &'static str = "terminal256";
fn options_str(&self) -> Option<Cow<'_, str>> {
if self.line_numbers {
Some(Cow::Borrowed("linenos=true"))
} else {
None
}
}
}