Skip to content
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

💬 Support quote environments in latex #922

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/itchy-rules-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tex-to-myst': patch
---

Support quote and displayquote environments as well as epigraph macro
2 changes: 2 additions & 0 deletions packages/tex-to-myst/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { FOOTNOTE_HANDLERS } from './footnotes.js';
import { SIUNITX_HANDLERS } from './siunitx.js';
import { CHEM_HANDLERS } from './chem.js';
import { ALGORITHM_HANDLERS } from './algorithms.js';
import { QUOTE_HANDLERS } from './quotes.js';

const DEFAULT_HANDLERS: Record<string, Handler> = {
...BASIC_TEXT_HANDLERS,
Expand All @@ -36,6 +37,7 @@ const DEFAULT_HANDLERS: Record<string, Handler> = {
...MATH_HANDLERS,
...LIST_HANDLERS,
...LINK_HANDLERS,
...QUOTE_HANDLERS,
...CITATION_HANDLERS,
...CHARACTER_HANDLERS,
...SECTION_HANDLERS,
Expand Down
35 changes: 35 additions & 0 deletions packages/tex-to-myst/src/quotes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { Handler } from './types.js';
import { getArguments } from './utils.js';

export const QUOTE_HANDLERS: Record<string, Handler> = {
env_quote(node, state) {
state.closeParagraph();
state.openNode('blockquote');
state.renderChildren(node);
state.closeNode();
},
env_displayquote(node, state) {
state.closeParagraph();
state.openNode('blockquote');
state.renderChildren(node);
state.closeNode();
},
macro_epigraph(node, state) {
const [quote, author] = getArguments(node, 'group');
if (author) {
state.openNode('container', { kind: 'quote' });
}
state.openNode('blockquote');
state.openParagraph();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These openParagraph/closeParagraph calls aren't in the other handlers - maybe unnecessary? I think the behaviour is the same in each case (looking at test cases), so the differences in the code are a little misleading.

Copy link
Collaborator Author

@rowanc1 rowanc1 Feb 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are actually necessary - and the close node is looking to the paragraph not the blockquote above, which is a bug. Adding the other ones in!

state.renderChildren(quote);
state.closeParagraph();
state.closeNode();
if (!author) return;
state.openNode('caption');
state.openParagraph();
state.renderChildren(author);
state.closeParagraph();
state.closeNode();
state.closeNode(); // the container
},
};
1 change: 1 addition & 0 deletions packages/tex-to-myst/src/tex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const macros: Record<string, number> = {
affil: 1,
affiliation: 1,
framebox: 1,
epigraph: 2,
tnote: 1,
['table*']: 1,
arraystretch: 1,
Expand Down
1 change: 1 addition & 0 deletions packages/tex-to-myst/tests/cases.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const files = [
'algorithm.yml',
'macros.yml',
'input.yml',
'quote.yml',
];

const only = ''; // Can set this to a test title
Expand Down
51 changes: 51 additions & 0 deletions packages/tex-to-myst/tests/quote.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
title: Quotes
cases:
- title: Quote
tex: |-
\begin{quote}
This is a quote
\end{quote}
tree:
type: root
children:
- type: blockquote
children:
- type: paragraph
children:
- type: text
value: This is a quote
- title: displayquote
tex: |-
\begin{displayquote}
Sé que tengo un ego del tamaño de un planeta pequeño, pero incluso yo a veces me equivoco
\end{displayquote}
tree:
type: root
children:
- type: blockquote
children:
- type: paragraph
children:
- type: text
value: Sé que tengo un ego del tamaño de un planeta pequeño, pero incluso yo a veces me equivoco
- title: epigraph
tex: |-
\epigraph{All human things are subject to decay, and when fate summons, Monarchs must obey}{Mac Flecknoe}
tree:
type: root
children:
- type: container
kind: quote
children:
- type: blockquote
children:
- type: paragraph
children:
- type: text
value: All human things are subject to decay, and when fate summons, Monarchs must obey
- type: caption
children:
- type: paragraph
children:
- type: text
value: Mac Flecknoe
Loading