Skip to content

Commit 15e4d13

Browse files
committed
Add \noscript command
1 parent dfb85bc commit 15e4d13

4 files changed

Lines changed: 94 additions & 10 deletions

File tree

ts/input/tex/base/BaseConfiguration.ts

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import * as bitem from './BaseItems.js';
3232
import {AbstractTags} from '../Tags.js';
3333
import './BaseMappings.js';
3434
import {getRange} from '../../../core/MmlTree/OperatorDictionary.js';
35+
import {MmlNode} from '../../../core/MmlTree/MmlNode.js';
36+
import ParseOptions from '../ParseOptions.js';
3537

3638
/**
3739
* Remapping some ASCII characters to their Unicode operator equivalent.
@@ -89,6 +91,36 @@ function envUndefined(_parser: TexParser, env: string) {
8991
throw new TexError('UnknownEnv', 'Unknown environment \'%1\'', env);
9092
}
9193

94+
/**
95+
* Filter for removing spacing following \nonscript
96+
* @param{ParseOptions} data The active tex parser.
97+
*/
98+
function filterNonscript({data}: {data: ParseOptions}) {
99+
for (const mml of data.getList('nonscript')) {
100+
//
101+
// If we are in script or script-script style
102+
// remove the space (either mspace or mrow containing it)
103+
// and remove it (and its contents) from the other lists.
104+
// Otherwise, if it is an mrow (which we added),
105+
// replace the mrow with its contents
106+
// and remove it from its list
107+
//
108+
if (mml.attributes.get('scriptlevel') > 0) {
109+
const parent = mml.parent;
110+
parent.childNodes.splice(parent.childIndex(mml), 1);
111+
data.removeFromList(mml.kind, [mml]);
112+
if (mml.isKind('mrow')) {
113+
const mstyle = mml.childNodes[0] as MmlNode;
114+
data.removeFromList('mstyle', [mstyle]);
115+
data.removeFromList('mspace', mstyle.childNodes[0].childNodes as MmlNode[]);
116+
}
117+
} else if (mml.isKind('mrow')) {
118+
mml.parent.replaceChild(mml.childNodes[0], mml);
119+
data.removeFromList('mrow', [mml]);
120+
}
121+
}
122+
}
123+
92124

93125
/**
94126
* @constructor
@@ -135,19 +167,21 @@ export const BaseConfiguration: Configuration = Configuration.create(
135167
[bitem.MmlItem.prototype.kind]: bitem.MmlItem,
136168
[bitem.FnItem.prototype.kind]: bitem.FnItem,
137169
[bitem.NotItem.prototype.kind]: bitem.NotItem,
170+
[bitem.NonscriptItem.prototype.kind]: bitem.NonscriptItem,
138171
[bitem.DotsItem.prototype.kind]: bitem.DotsItem,
139172
[bitem.ArrayItem.prototype.kind]: bitem.ArrayItem,
140173
[bitem.EqnArrayItem.prototype.kind]: bitem.EqnArrayItem,
141174
[bitem.EquationItem.prototype.kind]: bitem.EquationItem
142-
},
143-
options: {
144-
maxMacros: 1000,
145-
baseURL: (typeof(document) === 'undefined' ||
146-
document.getElementsByTagName('base').length === 0) ?
147-
'' : String(document.location).replace(/#.*$/, '')
148-
},
149-
tags: {
150-
base: BaseTags
151-
}
175+
},
176+
options: {
177+
maxMacros: 1000,
178+
baseURL: (typeof(document) === 'undefined' ||
179+
document.getElementsByTagName('base').length === 0) ?
180+
'' : String(document.location).replace(/#.*$/, '')
181+
},
182+
tags: {
183+
base: BaseTags
184+
},
185+
postprocessors: [[filterNonscript, -4]]
152186
}
153187
);

ts/input/tex/base/BaseItems.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,45 @@ export class NotItem extends BaseItem {
774774
}
775775
}
776776

777+
export class NonscriptItem extends BaseItem {
778+
779+
/**
780+
* @override
781+
*/
782+
public get kind() {
783+
return 'nonscript';
784+
}
785+
786+
/**
787+
* @override
788+
*/
789+
public checkItem(item: StackItem): CheckType {
790+
if (item.isKind('mml') && item.Size() === 1) {
791+
let mml = item.First;
792+
//
793+
// Space macros like \, wrap with an mstyle to set scriptlevel=0 (so size is independent of level)
794+
//
795+
if (mml.isKind('mstyle') && mml.notParent) {
796+
mml = NodeUtil.getChildren(NodeUtil.getChildren(mml)[0])[0];
797+
}
798+
if (mml.isKind('mspace')) {
799+
//
800+
// If the space is in an mstyle, wrap it in an mrow so we can test is scriptlevel.
801+
// The mrow will be removed in the post-filter.
802+
//
803+
if (mml !== item.First) {
804+
mml = this.create('node', 'mrow', [item.Pop()]);
805+
item.Push(mml);
806+
}
807+
//
808+
// Save the item for alter post-processing
809+
//
810+
this.factory.configuration.addNode('nonscript', item.First);
811+
}
812+
}
813+
return [[item], true];
814+
}
815+
}
777816

778817
/**
779818
* Item indicating a dots command has been encountered.

ts/input/tex/base/BaseMappings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ new sm.CommandMap('macros', {
572572
rule: 'rule',
573573
Rule: ['Rule'],
574574
Space: ['Rule', 'blank'],
575+
nonscript: 'Nonscript',
575576

576577
big: ['MakeBig', TEXCLASS.ORD, 0.85],
577578
Big: ['MakeBig', TEXCLASS.ORD, 1.15],

ts/input/tex/base/BaseMethods.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,16 @@ BaseMethods.Hskip = function(parser: TexParser, name: string) {
903903
};
904904

905905

906+
/**
907+
* Handle removal of spaces in script modes
908+
* @param {TexParser} parser The calling parser.
909+
* @param {string} name The macro name.
910+
*/
911+
BaseMethods.Nonscript = function(parser: TexParser, _name: string) {
912+
parser.Push(parser.itemFactory.create('nonscript'));
913+
};
914+
915+
906916
/**
907917
* Handle Rule and Space command
908918
* @param {TexParser} parser The calling parser.

0 commit comments

Comments
 (0)