2022-05-15 21:09:33 +02:00
|
|
|
use crate::export::markup::MarkupExporter;
|
2022-02-06 17:44:28 +01:00
|
|
|
|
2022-05-15 21:28:35 +02:00
|
|
|
use super::markup::Alignment;
|
|
|
|
|
|
2022-03-24 07:38:18 +01:00
|
|
|
#[derive(Default)]
|
2022-05-15 21:09:33 +02:00
|
|
|
pub struct MarkdownExporter {}
|
2022-03-24 07:38:18 +01:00
|
|
|
|
2022-05-15 21:09:33 +02:00
|
|
|
impl MarkupExporter for MarkdownExporter {
|
2022-05-15 21:28:35 +02:00
|
|
|
fn table_row(&self, cells: &[&str]) -> String {
|
|
|
|
|
format!("| {} |\n", cells.join(" | "))
|
2022-03-24 07:38:18 +01:00
|
|
|
}
|
|
|
|
|
|
2022-05-15 21:28:35 +02:00
|
|
|
fn table_divider(&self, cell_aligmnents: &[Alignment]) -> String {
|
|
|
|
|
format!(
|
|
|
|
|
"|{}\n",
|
|
|
|
|
cell_aligmnents
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|a| match a {
|
|
|
|
|
Alignment::Left => ":---|",
|
|
|
|
|
Alignment::Right => "---:|",
|
|
|
|
|
})
|
|
|
|
|
.collect::<String>()
|
|
|
|
|
)
|
2022-03-24 07:38:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn command(&self, cmd: &str) -> String {
|
2024-05-31 13:26:35 -07:00
|
|
|
format!("`{cmd}`")
|
2022-03-24 07:38:18 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Check Markdown-based data row formatting
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_markdown_formatter_table_data() {
|
2022-05-15 21:09:33 +02:00
|
|
|
let formatter = MarkdownExporter::default();
|
2022-03-24 07:38:18 +01:00
|
|
|
|
2022-05-15 21:28:35 +02:00
|
|
|
assert_eq!(formatter.table_row(&["a", "b", "c"]), "| a | b | c |\n");
|
2022-03-24 07:38:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Check Markdown-based horizontal line formatting
|
|
|
|
|
#[test]
|
2022-05-15 21:28:35 +02:00
|
|
|
fn test_markdown_formatter_table_divider() {
|
2022-05-15 21:09:33 +02:00
|
|
|
let formatter = MarkdownExporter::default();
|
2022-03-24 07:38:18 +01:00
|
|
|
|
2022-05-15 21:29:36 +02:00
|
|
|
let divider = formatter.table_divider(&[Alignment::Left, Alignment::Right, Alignment::Left]);
|
|
|
|
|
assert_eq!(divider, "|:---|---:|:---|\n");
|
2022-03-24 07:38:18 +01:00
|
|
|
}
|