Skip to content

Commit

Permalink
feat(converter): support figure and tabular environments (#154)
Browse files Browse the repository at this point in the history
* dev: clean code

* dev: more convert functions

* dev: update itemcmd

* dev: minor improvements

* test(convert): refactor tests

* dev: add converter.rs

* fix: make ci happy

* dev: add convert_env

* dev: simplify \label for env

* feat: add quote and abstract envs

* dev: support caption for figure

* fix: fix figure env bug

* test: use assert_snapshot

* test: update tests

* fix: make insta test happy

* fix: make ci happy

* fix: fix bug of commands

* feat: add support for \includegraphics

* feat: add convert_env_figure

* feat: support basic tabular

* fix: make ci happy

* feat: support \toprule,
\midrule, and \bottomrule

* dev: merge main branch

* dev: update

* fix: fix bug of glob env

* fix: fix bug of Glob

* fix: add pattern for Glob

* docs: update README
  • Loading branch information
OrangeX4 authored Apr 25, 2024
1 parent c6c5f68 commit 5db08ce
Show file tree
Hide file tree
Showing 19 changed files with 921 additions and 116 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ https://github.com/mitex-rs/mitex/assets/34951714/0ce77a2c-0a7d-445f-b26d-e139f3
- [x] Inline and block math equations.
- [x] `\ref`, `\eqref` and `\label`.
- [x] `itemize` and `enumerate` environments.
- [x] `figure`, `table` and `tabular` environments.

## Features to Implement

Expand Down
2 changes: 1 addition & 1 deletion assets/artifacts
2 changes: 1 addition & 1 deletion crates/mitex-parser/src/arg_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl ArgMatcherBuilder {
}
}
ArgPattern::Greedy => ArgMatcher::Greedy,
ArgPattern::Glob(re) => ArgMatcher::Glob {
ArgPattern::Glob { pattern: re } => ArgMatcher::Glob {
re: re.clone(),
prefix: String::new(),
},
Expand Down
6 changes: 6 additions & 0 deletions crates/mitex-parser/tests/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ mod ast {
#[cfg(test)]
mod trivia;

#[cfg(test)]
mod figure;

#[cfg(test)]
mod tabular;

/// Convenient function to launch/debug a test case
#[test]
fn bug_playground() {}
Expand Down
180 changes: 180 additions & 0 deletions crates/mitex-parser/tests/ast/figure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
use super::prelude::*;

#[test]
fn figure() {
assert_debug_snapshot!(parse(r###"
\begin{figure}[ht]
\centering
\includegraphics[width=0.5\textwidth]{example-image}
\caption{This is an example image.}
\label{fig:example}
\end{figure}
"###), @r###"
root
|br'("\n")
|space'(" ")
|env
||begin
|||sym'("figure")
|||args
||||bracket
|||||lbracket'("[")
|||||text(word'("ht"))
|||||rbracket'("]")
||br'("\n")
||space'(" ")
||cmd(cmd-name("\\centering"))
||br'("\n")
||space'(" ")
||cmd
|||cmd-name("\\includegraphics")
|||args
||||bracket
|||||lbracket'("[")
|||||text(word'("width=0.5"))
|||||cmd(cmd-name("\\textwidth"))
|||||rbracket'("]")
|||args
||||curly
|||||lbrace'("{")
|||||text(word'("example-image"))
|||||rbrace'("}")
||br'("\n")
||space'(" ")
||cmd
|||cmd-name("\\caption")
|||args
||||curly
|||||lbrace'("{")
|||||text(word'("This"),space'(" "),word'("is"),space'(" "),word'("an"),space'(" "),word'("example"),space'(" "),word'("image."))
|||||rbrace'("}")
||br'("\n")
||space'(" ")
||cmd
|||cmd-name("\\label")
|||args
||||curly
|||||lbrace'("{")
|||||text(word'("fig:example"))
|||||rbrace'("}")
||br'("\n")
||space'(" ")
||end(sym'("figure"))
|br'("\n")
|space'(" ")
"###);
}

#[test]
fn table() {
assert_debug_snapshot!(parse(r###"
\begin{table}[ht]
\centering
\begin{tabular}{|c|c|}
\hline
\textbf{Name} & \textbf{Age} \\
\hline
John & 25 \\
Jane & 22 \\
\hline
\end{tabular}
\caption{This is an example table.}
\label{tab:example}
\end{table}
"###), @r###"
root
|br'("\n")
|space'(" ")
|env
||begin
|||sym'("table")
|||args
||||bracket
|||||lbracket'("[")
|||||text(word'("ht"))
|||||rbracket'("]")
||br'("\n")
||space'(" ")
||cmd(cmd-name("\\centering"))
||br'("\n")
||space'(" ")
||env
|||begin
||||sym'("tabular")
||||args
|||||curly
||||||lbrace'("{")
||||||text(word'("|c|c|"))
||||||rbrace'("}")
|||br'("\n")
|||space'(" ")
|||cmd(cmd-name("\\hline"))
|||br'("\n")
|||space'(" ")
|||cmd
||||cmd-name("\\textbf")
||||args
|||||curly
||||||lbrace'("{")
||||||text(word'("Name"))
||||||rbrace'("}")
|||space'(" ")
|||ampersand'("&")
|||space'(" ")
|||cmd
||||cmd-name("\\textbf")
||||args
|||||curly
||||||lbrace'("{")
||||||text(word'("Age"))
||||||rbrace'("}")
|||space'(" ")
|||newline("\\\\")
|||br'("\n")
|||space'(" ")
|||cmd(cmd-name("\\hline"))
|||br'("\n")
|||space'(" ")
|||text(word'("John"),space'(" "))
|||ampersand'("&")
|||space'(" ")
|||text(word'("25"),space'(" "))
|||newline("\\\\")
|||br'("\n")
|||space'(" ")
|||text(word'("Jane"),space'(" "))
|||ampersand'("&")
|||space'(" ")
|||text(word'("22"),space'(" "))
|||newline("\\\\")
|||br'("\n")
|||space'(" ")
|||cmd(cmd-name("\\hline"))
|||br'("\n")
|||space'(" ")
|||end(sym'("tabular"))
||br'("\n")
||space'(" ")
||cmd
|||cmd-name("\\caption")
|||args
||||curly
|||||lbrace'("{")
|||||text(word'("This"),space'(" "),word'("is"),space'(" "),word'("an"),space'(" "),word'("example"),space'(" "),word'("table."))
|||||rbrace'("}")
||br'("\n")
||space'(" ")
||cmd
|||cmd-name("\\label")
|||args
||||curly
|||||lbrace'("{")
|||||text(word'("tab:example"))
|||||rbrace'("}")
||br'("\n")
||space'(" ")
||end(sym'("table"))
|br'("\n")
|space'(" ")
"###);
}
76 changes: 76 additions & 0 deletions crates/mitex-parser/tests/ast/tabular.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use super::prelude::*;

#[test]
fn tabular() {
assert_debug_snapshot!(parse(r###"
\begin{tabular}{|c|c|}
\hline
\textbf{Name} & \textbf{Age} \\
\hline
John & 25 \\
Jane & 22 \\
\hline
\end{tabular}
"###), @r###"
root
|br'("\n")
|space'(" ")
|env
||begin
|||sym'("tabular")
|||args
||||curly
|||||lbrace'("{")
|||||text(word'("|c|c|"))
|||||rbrace'("}")
||br'("\n")
||space'(" ")
||cmd(cmd-name("\\hline"))
||br'("\n")
||space'(" ")
||cmd
|||cmd-name("\\textbf")
|||args
||||curly
|||||lbrace'("{")
|||||text(word'("Name"))
|||||rbrace'("}")
||space'(" ")
||ampersand'("&")
||space'(" ")
||cmd
|||cmd-name("\\textbf")
|||args
||||curly
|||||lbrace'("{")
|||||text(word'("Age"))
|||||rbrace'("}")
||space'(" ")
||newline("\\\\")
||br'("\n")
||space'(" ")
||cmd(cmd-name("\\hline"))
||br'("\n")
||space'(" ")
||text(word'("John"),space'(" "))
||ampersand'("&")
||space'(" ")
||text(word'("25"),space'(" "))
||newline("\\\\")
||br'("\n")
||space'(" ")
||text(word'("Jane"),space'(" "))
||ampersand'("&")
||space'(" ")
||text(word'("22"),space'(" "))
||newline("\\\\")
||br'("\n")
||space'(" ")
||cmd(cmd-name("\\hline"))
||br'("\n")
||space'(" ")
||end(sym'("tabular"))
|br'("\n")
|space'(" ")
"###);
}
11 changes: 10 additions & 1 deletion crates/mitex-spec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,10 @@ pub enum ArgPattern {
/// - {,b}: first, it matches a bracket option, e.g. `\sqrt[3]`
/// - t: it then matches a single term, e.g. `\sqrt[3]{a}` or `\sqrt{a}`
#[cfg_attr(feature = "serde", serde(rename = "glob"))]
Glob(GlobStr),
Glob {
/// The glob pattern to match the arguments
pattern: GlobStr,
},
}

// struct ArgShape(ArgPattern, Direction);
Expand Down Expand Up @@ -336,6 +339,12 @@ pub enum ContextFeature {
/// Parse content like cases
#[cfg_attr(feature = "serde", serde(rename = "is-cases"))]
IsCases,
/// Parse content like figure
#[cfg_attr(feature = "serde", serde(rename = "is-figure"))]
IsFigure,
/// Parse content like table
#[cfg_attr(feature = "serde", serde(rename = "is-table"))]
IsTable,
/// Parse content like itemize
#[cfg_attr(feature = "serde", serde(rename = "is-itemize"))]
IsItemize,
Expand Down
16 changes: 14 additions & 2 deletions crates/mitex-spec/src/preludes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![allow(missing_docs)]

pub mod command {
use crate::{ArgShape, CommandSpecItem};
use crate::{ArgShape, CommandSpecItem, ContextFeature};

pub fn define_command(len: u8) -> CommandSpecItem {
CommandSpecItem::Cmd(crate::CmdShape {
Expand All @@ -16,12 +16,24 @@ pub mod command {
pub fn define_glob_command(reg: &str, alias: &str) -> CommandSpecItem {
CommandSpecItem::Cmd(crate::CmdShape {
args: crate::ArgShape::Right {
pattern: crate::ArgPattern::Glob(reg.into()),
pattern: crate::ArgPattern::Glob {
pattern: reg.into(),
},
},
alias: Some(alias.to_owned()),
})
}

pub fn define_glob_env(reg: &str, alias: &str, ctx_feature: ContextFeature) -> CommandSpecItem {
CommandSpecItem::Env(crate::EnvShape {
args: crate::ArgPattern::Glob {
pattern: reg.into(),
},
ctx_feature,
alias: Some(alias.to_owned()),
})
}

pub fn define_symbol(alias: &str) -> CommandSpecItem {
CommandSpecItem::Cmd(crate::CmdShape {
args: crate::ArgShape::Right {
Expand Down
Loading

0 comments on commit 5db08ce

Please sign in to comment.