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
feat: basic text mode #86
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some comments
crates/mitex/src/lib.rs
Outdated
@@ -476,9 +611,39 @@ static DEFAULT_SPEC: once_cell::sync::Lazy<CommandSpec> = once_cell::sync::Lazy: | |||
#[cfg(test)] | |||
mod tests { | |||
use insta::{assert_debug_snapshot, assert_snapshot}; | |||
use mitex_parser::spec::CommandSpec; | |||
|
|||
static DEFAULT_SPEC: once_cell::sync::Lazy<CommandSpec> = once_cell::sync::Lazy::new(|| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since there is already a DEFAULT_SPEC
in crate
, we can import it by use crate::DEFAULT_SPEC
;
crates/mitex/src/lib.rs
Outdated
self.2.borrow_mut().push_str(&e.to_string()); | ||
let mut ctx = Converter::new(self.1); | ||
if let Err(e) = ctx.convert(f, self.0.clone(), &self.2) { | ||
self.3.borrow_mut().push_str(&e.to_string()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two many fields in a single tuple. I would prefer to refactoring TypstMathRepr
as
struct TypstRepr {
elem: LatexSyntaxElem,
mode: LaTeXMode,
spec: CommandSpec,
error: Rc<RefCell<String>>
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work. 😁
README.md
Outdated
|
||
## Features to Implement | ||
|
||
- [ ] Pass command specification to MiTeX plugin dynamically. With that you can define a typst function `let myop(it) = op(upright(it))` and then use it by `\myop{it}`. | ||
- [ ] Package support, which means that you can change set of commands by telling MiTeX to use a list of packages. | ||
- [ ] Text mode support, enabling the rendering entire LaTeX documents in Typst! | ||
- [ ] More text mode support, enabling the rendering entire LaTeX documents in Typst! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"More text mode support" is not rather descriptive, could we mention at least one direction to improve?
TokenWhiteSpace => { | ||
if self.skip_next_space { | ||
self.skip_next_space = false; | ||
return Ok(()); | ||
} | ||
write!(f, "{}", elem.as_token().unwrap().text())?; | ||
} | ||
TokenLineBreak => { | ||
write!(f, "{}", elem.as_token().unwrap().text())?; | ||
// indent for itemize and enumerate | ||
for _ in 0..self.indent { | ||
f.write_char(' ')?; | ||
} | ||
self.skip_next_space = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A state switch self.skip_next_space
looks easily broken to me. It doesn't quite resist AST changes. We may take a look on how typst processes spaces, which should be inspiring.
And I also think it is okay to have self.skip_next_space
merged into main currently, but may we create a todo for it?
if name == "item" { | ||
if matches!(self.env, LaTeXEnv::Itemize | LaTeXEnv::Enumerate) { | ||
f.write_char('\n')?; | ||
for _ in 0..(self.indent - 2) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can collect indent things into a helper function?
for _ in 0..(self.indent - 2) { | ||
f.write_char(' ')?; | ||
} | ||
if matches!(self.env, LaTeXEnv::Itemize) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
similarly, we can have a helper to make conditional output, which doesn't change performance at all, but improves readability.
packages/mitex/examples/example.typ
Outdated
\section{Title} | ||
|
||
A \textbf{strong} text, a \emph{emph} text and inline equation $x + y$. | ||
|
||
Also block \eqref{eq:pythagoras}. | ||
|
||
\begin{equation} | ||
a^2 + b^2 = c^2 \label{eq:pythagoras} | ||
\end{equation} | ||
|
||
\begin{enumerate} | ||
\item This is the first item | ||
\item This is the second item | ||
\begin{itemize} | ||
\item This is the first subitem | ||
\item This is the second subitem | ||
\end{itemize} | ||
\end{enumerate} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we shorten example of text mode a bit? Just three to five lines would be okay. IMO, the example should be cool and representative and don't have to show all features.
if mode == "math" { | ||
str(mitex-wasm.convert_math(bytes(get-elem-text(it)), spec)) | ||
} else { | ||
str(mitex-wasm.convert_text(bytes(get-elem-text(it)), spec)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
repeated pattern of code
packages/mitex/specs/prelude.typ
Outdated
/// Therefore you need to use `(.. arg) = > {..}` to receive them. | ||
/// | ||
/// Return: A spec item and a scope item (none for no scope item) | ||
#let define-math-env(num, alias: none, handle: none) = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can have a common define-env
and the rest of define-xxx-env
are just alias to define-env.with(ctx_feature: ...)
packages/mitex/specs/prelude.typ
Outdated
@@ -210,7 +253,12 @@ | |||
spec.insert(key, value.at(0)) | |||
if value.at(1) != none { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are many value.at(0)
and value.at(1)
, could be destruct value
into nice names?
\section
,\textbf
,\emph
.\ref
,\eqref
and\label
.itemize
andenumerate
environments.mitex-convert
to convert LaTeX code into Typst code in string.mi
to embed an inline LaTeX equation into Typst.mitex(numbering: none, supplement: auto, ..)
to embed a block LaTeX equation into Typst.mitext
to embed a LaTeX text into Typst.