-
Notifications
You must be signed in to change notification settings - Fork 124
feat(tex): support page parameter for includegraphics with multi-page pdf #1922
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| import type { GenericNode } from 'myst-common'; | ||
| import { u } from 'unist-builder'; | ||
| import type { Handler, ITexParser } from './types.js'; | ||
| import { getArguments, texToText } from './utils.js'; | ||
| import { getArguments, extractParams, texToText } from './utils.js'; | ||
| import { group } from 'console'; | ||
|
||
|
|
||
| function renderCaption(node: GenericNode, state: ITexParser) { | ||
| state.closeParagraph(); | ||
|
|
@@ -45,18 +46,33 @@ const FIGURE_HANDLERS: Record<string, Handler> = { | |
| state.closeParagraph(); | ||
| const url = texToText(getArguments(node, 'group')); | ||
| const args = getArguments(node, 'argument')?.[0]?.content ?? []; | ||
| const params = extractParams(args); | ||
|
|
||
| // Only support width and page for now | ||
| for (const key in params) { | ||
| if (key !== 'width' && key !== 'page') { | ||
| delete params[key]; | ||
| } | ||
| } | ||
|
|
||
| // TODO: better width, placement, etc. | ||
| if ( | ||
| args.length === 4 && | ||
| args[0].content === 'width' && | ||
| args[1].content === '=' && | ||
| Number.isFinite(Number.parseFloat(args[2].content)) | ||
| ) { | ||
| const width = `${Math.round(Number.parseFloat(args[2].content) * 100)}%`; | ||
| state.pushNode(u('image', { url, width })); | ||
| } else { | ||
| state.pushNode(u('image', { url })); | ||
|
|
||
| // Convert width to percentage if present | ||
| if (params.width) { | ||
| if (typeof params.width === 'number') { | ||
| params.width = `${Math.round(params.width * 100)}%`; | ||
| } else { | ||
| delete params.width; // If width is a string, we don't know what it is, so we ignore it | ||
| } | ||
| } | ||
| if (params.page) { | ||
| if (typeof params.page === 'number') { | ||
| params.page = Number(params.page) - 1; // Convert to 0-based for imagemagick | ||
|
||
| } else { | ||
| delete params.page; | ||
| } | ||
| } | ||
| state.pushNode(u('image', { url: url, ...params })); | ||
| }, | ||
| macro_caption: renderCaption, | ||
| macro_captionof: renderCaption, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -138,6 +138,23 @@ export function getArguments( | |
| ); | ||
| } | ||
|
|
||
| export function extractParams(args: { content: string }[]): Record<string, string | number> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like it has potential for wider use across other macros/parameter types. 👍 |
||
| const params: Record<string, string | number> = {}; | ||
|
|
||
| for (let i = 0; i < args.length - 2; i++) { | ||
| const param = args[i].content; | ||
| const equalsSign = args[i + 1].content; | ||
| const value = args[i + 2].content; | ||
|
|
||
| if (equalsSign === '=' && (Number.isFinite(Number.parseFloat(value)) || value)) { | ||
| params[param] = Number.isFinite(Number.parseFloat(value)) ? Number.parseFloat(value) : value; | ||
| i += 2; // Skip the processed elements | ||
| } | ||
| } | ||
|
|
||
| return params; | ||
| } | ||
|
|
||
| export function renderInfoIndex(node: GenericNode, name: string): number { | ||
| return node._renderInfo?.namedArguments?.findIndex((a: string) => a === name); | ||
| } | ||
|
|
||
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 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.
done!