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

Add ability to not show latex attributes, and reorganize settings menu #1017

Merged
merged 3 commits into from
Nov 28, 2023
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
26 changes: 16 additions & 10 deletions ts/ui/menu/Menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ const XMLDECLARATION = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>';
* The various values that are stored in the menu
*/
export interface MenuSettings {
filterSRE: boolean;
showSRE: boolean;
showTex: boolean;
texHints: boolean;
semantics: boolean;
zoom: string;
Expand Down Expand Up @@ -123,7 +124,8 @@ export class Menu {
*/
public static OPTIONS: OptionList = {
settings: {
filterSRE: true,
showSRE: false,
showTex: false,
texHints: true,
semantics: false,
zoom: 'NoZoom',
Expand Down Expand Up @@ -462,7 +464,8 @@ export class Menu {
type: 'contextMenu',
id: 'MathJax_Menu',
pool: [
this.variable<boolean>('filterSRE'),
this.variable<boolean>('showSRE'),
this.variable<boolean>('showTex'),
this.variable<boolean>('texHints'),
this.variable<boolean>('semantics'),
this.variable<string> ('zoom'),
Expand Down Expand Up @@ -537,6 +540,13 @@ export class Menu {
this.checkbox('BreakInline', 'Allow In-line Breaks', 'breakInline'),
]),
this.rule(),
this.submenu('MathmlIncludes', 'MathML Includes', [
this.checkbox('showSRE', 'Semantic attributes', 'showSRE'),
this.checkbox('showTex', 'LaTeX attributes', 'showTex'),
this.checkbox('texHints', 'TeX hints', 'texHints'),
this.checkbox('semantics', 'Original as annotation', 'semantics')
]),
this.rule(),
this.submenu('ZoomTrigger', 'Zoom Trigger', [
this.command('ZoomNow', 'Zoom Once Now', () => this.zoom(null, '', this.menu.mathItem)),
this.rule(),
Expand All @@ -556,10 +566,6 @@ export class Menu {
this.rule(),
this.command('Scale', 'Scale All Math...', () => this.scaleAllMath()),
this.rule(),
this.checkbox('filterSRE', 'Filter semantic annotations', 'filterSRE'),
this.checkbox('texHints', 'Add TeX hints to MathML', 'texHints'),
this.checkbox('semantics', 'Add original as annotation', 'semantics'),
this.rule(),
this.command('Reset', 'Reset to defaults', () => this.resetDefaults())
]),
this.submenu('Accessibility', 'Accessibility', [
Expand Down Expand Up @@ -1047,12 +1053,12 @@ export class Menu {
* @returns {string} The serialized version of the internal MathML
*/
protected toMML(math: HTMLMATHITEM): string {
const mml = this.MmlVisitor.visitTree(math.root, math, {
return this.MmlVisitor.visitTree(math.root, math, {
filterSRE: !this.settings.showSRE,
filterTex: !this.settings.showTex,
texHints: this.settings.texHints,
semantics: (this.settings.semantics && math.inputJax.name !== 'MathML')
});
return (!this.settings.filterSRE ? mml :
mml.replace(/ (?:data-semantic-.*?|role|aria-(?:level|posinset|setsize))=".*?"/g, ''));
}

/**
Expand Down
21 changes: 21 additions & 0 deletions ts/ui/menu/MmlVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import {MathItem} from '../../core/MathItem.js';
import {MmlNode} from '../../core/MmlTree/MmlNode.js';
import {PropertyList} from '../../core/Tree/Node.js';
import {SerializedMmlVisitor} from '../../core/MmlTree/SerializedMmlVisitor.js';
import {OptionList, userOptions} from '../../util/Options.js';

Expand All @@ -41,6 +42,8 @@ export class MmlVisitor<N, T, D> extends SerializedMmlVisitor {
* The options controlling the serialization
*/
public options: OptionList = {
filterSRE: true, // True means remove data-semantic, role, and aria attributes
filterTex: true, // True means remove data-latex and data-latexItem attributes
texHints: true, // True means include classes for TeXAtom elements
semantics: false, // True means include original form as annotation in a semantics element
};
Expand Down Expand Up @@ -97,4 +100,22 @@ export class MmlVisitor<N, T, D> extends SerializedMmlVisitor {
+ space + '</math>';
}

/**
* @override
*/
protected getAttributeList(node: MmlNode): PropertyList {
const list = super.getAttributeList(node);
if (this.options.filterTex) {
delete list['data-latex'], list['data-latexItem'];
}
if (this.options.filterSRE) {
const keys = Object.keys(list).filter(
id => id.match(/^(?:data-semantic-.*?|role|aria-(?:level|posinset|setsize))$/)
Copy link
Member

Choose a reason for hiding this comment

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

What if we parse from MathML and some of these aria attributes are already in the incoming element?
(I suppose we would have overwritten them at this point anyway.)

Copy link
Member Author

@dpvc dpvc Nov 20, 2023

Choose a reason for hiding this comment

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

This filtering only occurs in the Show Math As or Copy Math As menus. Nothing is changed internally. This filtering is controlled by menu options, so the user can turn off the filtering in the case where retaining those values is desired. Does that put you more at ease?

Copy link
Member Author

Choose a reason for hiding this comment

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

Note that this MmlVisitor is in the ts/ui/menu directory, not the one in ts/core/MmlTree, so it only affects the menu output.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks. Sounds good.

);
for (const key of keys) {
delete list[key];
}
}
return list;
}
}