Debian Codegraph @

ts/enrich_mathml/enrich_attr.ts

raw symbols SCIP
1 //
2 // Copyright 2015-21 Volker Sorge
3 //
4 // Licensed under the Apache on 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 /**
17 * @file Attribute handling for the semantic enrichment.
18 *
19 * Take a MathML element, compute the semantic tree and reinject the semantic
20 * information into the MathML.
21 * @author [email protected] (Volker Sorge)
22 */
23
24 import * as DomUtilscip-typescript npm . . common/`dom_util.ts`/ from '../common/dom_util.js'scip-typescript npm . . common/`dom_util.ts`/;
25 import { SemanticRolescip-typescript npm . . semantic_tree/`semantic_meaning.ts`/SemanticRole# } from '../semantic_tree/semantic_meaning.js'scip-typescript npm . . semantic_tree/`semantic_meaning.ts`/;
26 import { SemanticNodescip-typescript npm . . semantic_tree/`semantic_node.ts`/SemanticNode# } from '../semantic_tree/semantic_node.js'scip-typescript npm . . semantic_tree/`semantic_node.ts`/;
27
28 /**
29 * Prefix for semantic attributes.
30 */
31 const Prefixscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Prefix. = 'data-semantic-';
32
33 // TODO (TS): Do something about the prefix computation.
34 /**
35 * Mapping for attributes used in semantic enrichment.
36 */
37 export enum Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute# {
38 ADDEDscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#ADDED. = 'data-semantic-added',
39 ALTERNATIVEscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#ALTERNATIVE. = 'data-semantic-alternative',
40 CHILDRENscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#CHILDREN. = 'data-semantic-children',
41 COLLAPSEDscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#COLLAPSED. = 'data-semantic-collapsed',
42 CONTENTscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#CONTENT. = 'data-semantic-content',
43 EMBELLISHEDscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#EMBELLISHED. = 'data-semantic-embellished',
44 FENCEPOINTERscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#FENCEPOINTER. = 'data-semantic-fencepointer',
45 FONTscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#FONT. = 'data-semantic-font',
46 IDscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#ID. = 'data-semantic-id',
47 ANNOTATIONscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#ANNOTATION. = 'data-semantic-annotation',
48 ATTRIBUTESscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#ATTRIBUTES. = 'data-semantic-attributes',
49 OPERATORscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#OPERATOR. = 'data-semantic-operator',
50 OWNSscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#OWNS. = 'data-semantic-owns',
51 PARENTscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#PARENT. = 'data-semantic-parent',
52 POSTFIXscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#POSTFIX. = 'data-semantic-postfix',
53 PREFIXscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#PREFIX. = 'data-semantic-prefix',
54 ROLEscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#ROLE. = 'data-semantic-role',
55 SPEECHscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#SPEECH. = 'data-semantic-speech',
56 STRUCTUREscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#STRUCTURE. = 'data-semantic-structure',
57 SUMMARYscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#SUMMARY. = 'data-semantic-summary',
58 TYPEscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#TYPE. = 'data-semantic-type'
59 }
60
61 export const EnrichAttributesscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/EnrichAttributes.: string[] = [
62 Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.ADDEDscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#ADDED.,
63 Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.ALTERNATIVEscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#ALTERNATIVE.,
64 Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.CHILDRENscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#CHILDREN.,
65 Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.COLLAPSEDscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#COLLAPSED.,
66 Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.CONTENTscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#CONTENT.,
67 Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.EMBELLISHEDscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#EMBELLISHED.,
68 Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.FENCEPOINTERscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#FENCEPOINTER.,
69 Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.FONTscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#FONT.,
70 Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.IDscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#ID.,
71 Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.ANNOTATIONscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#ANNOTATION.,
72 Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.ATTRIBUTESscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#ATTRIBUTES.,
73 Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.OPERATORscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#OPERATOR.,
74 Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.OWNSscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#OWNS.,
75 Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.PARENTscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#PARENT.,
76 Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.POSTFIXscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#POSTFIX.,
77 Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.PREFIXscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#PREFIX.,
78 Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.ROLEscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#ROLE.,
79 Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.SPEECHscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#SPEECH.,
80 Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.STRUCTUREscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#STRUCTURE.,
81 Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.SUMMARYscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#SUMMARY.,
82 Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.TYPEscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#TYPE.
83 ];
84
85 /**
86 * Concatenates node ids into a comma separated lists.
87 *
88 * @param nodes The list of nodes.
89 * @returns The comma separated lists.
90 */
91 export function makeIdListscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/makeIdList().(nodesscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/makeIdList().(nodes): SemanticNodescip-typescript npm . . semantic_tree/`semantic_node.ts`/SemanticNode#[]): string {
92 return nodesscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/makeIdList().(nodes)
93 .mapscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#map().(function (node) {
94 return node.idscip-typescript npm . . semantic_tree/`semantic_node.ts`/SemanticNode#`<constructor>`().(id);
95 })
96 .joinscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#join().(',');
97 }
98
99 /**
100 * Sets semantic attributes in a MathML node.
101 *
102 * @param mml The MathML node.
103 * @param semantic The semantic tree node.
104 */
105 export function setAttributesscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setAttributes().(mmlscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setAttributes().(mml): Elementscip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/Element#, semanticscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setAttributes().(semantic): SemanticNodescip-typescript npm . . semantic_tree/`semantic_node.ts`/SemanticNode#) {
106 mmlscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setAttributes().(mml).setAttributescip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/Element#setAttribute().(Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.TYPEscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#TYPE., semanticscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setAttributes().(semantic).typescip-typescript npm . . semantic_tree/`semantic_node.ts`/SemanticNode#type.);
107 const attributes = semanticscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setAttributes().(semantic).allAttributesscip-typescript npm . . semantic_tree/`semantic_node.ts`/SemanticNode#allAttributes().();
108 for (let i = 0, attr; (attr = attributes[i]); i++) {
109 mmlscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setAttributes().(mml).setAttributescip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/Element#setAttribute().(Prefixscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Prefix. + attr[0].toLowerCasescip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/String#toLowerCase().(), attr[1]);
110 }
111 if (semanticscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setAttributes().(semantic).childNodesscip-typescript npm . . semantic_tree/`semantic_node.ts`/SemanticNode#childNodes..lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length.) {
112 mmlscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setAttributes().(mml).setAttributescip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/Element#setAttribute().(Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.CHILDRENscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#CHILDREN., makeIdListscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/makeIdList().(semanticscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setAttributes().(semantic).childNodesscip-typescript npm . . semantic_tree/`semantic_node.ts`/SemanticNode#childNodes.));
113 }
114 if (semanticscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setAttributes().(semantic).contentNodesscip-typescript npm . . semantic_tree/`semantic_node.ts`/SemanticNode#contentNodes..lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length.) {
115 mmlscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setAttributes().(mml).setAttributescip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/Element#setAttribute().(Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.CONTENTscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#CONTENT., makeIdListscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/makeIdList().(semanticscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setAttributes().(semantic).contentNodesscip-typescript npm . . semantic_tree/`semantic_node.ts`/SemanticNode#contentNodes.));
116 }
117 if (semanticscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setAttributes().(semantic).parentscip-typescript npm . . semantic_tree/`semantic_node.ts`/SemanticNode#parent.) {
118 mmlscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setAttributes().(mml).setAttributescip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/Element#setAttribute().(Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.PARENTscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#PARENT., semanticscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setAttributes().(semantic).parentscip-typescript npm . . semantic_tree/`semantic_node.ts`/SemanticNode#parent..idscip-typescript npm . . semantic_tree/`semantic_node.ts`/SemanticNode#`<constructor>`().(id).toStringscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Number#toString().());
119 }
120 // Dealing with external attributes here.
121 const external = semanticscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setAttributes().(semantic).attributesXmlscip-typescript npm . . semantic_tree/`semantic_node.ts`/SemanticNode#attributesXml().();
122 if (external) {
123 mmlscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setAttributes().(mml).setAttributescip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/Element#setAttribute().(Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.ATTRIBUTESscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#ATTRIBUTES., external);
124 }
125 setPostfixscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setPostfix().(mmlscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setAttributes().(mml), semanticscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setAttributes().(semantic));
126 }
127
128 /**
129 * Sets postfix attributes to surface properties via suffixes. Examples: link,
130 * image, etc.
131 *
132 * @param mml The MathML node.
133 * @param semantic The semantic tree node.
134 */
135 function setPostfixscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setPostfix().(mmlscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setPostfix().(mml): Elementscip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/Element#, semanticscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setPostfix().(semantic): SemanticNodescip-typescript npm . . semantic_tree/`semantic_node.ts`/SemanticNode#) {
136 const postfix = [];
137 if (semanticscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setPostfix().(semantic).rolescip-typescript npm . . semantic_tree/`semantic_node.ts`/SemanticNode#role. === SemanticRolescip-typescript npm . . semantic_tree/`semantic_meaning.ts`/SemanticRole#.MGLYPHscip-typescript npm . . semantic_tree/`semantic_meaning.ts`/Roles#MGLYPH.) {
138 postfix.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().('image');
139 }
140 if (semanticscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setPostfix().(semantic).attributesscip-typescript npm . . semantic_tree/`semantic_node.ts`/SemanticNode#attributes.['href']) {
141 postfix.pushscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#push().('link');
142 }
143 if (postfix.lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length.) {
144 mmlscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/setPostfix().(mml).setAttributescip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/Element#setAttribute().(Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.POSTFIXscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#POSTFIX., postfix.joinscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#join().(' '));
145 }
146 }
147
148 /**
149 * Removes the semantic prefix from the attributes of an enriched MathML element
150 * given as a serialised string. This is useful for more concise display.
151 *
152 * NOTE THAT THIS METHOD IS FRAGILE!
153 *
154 * The result is not necessarily a meaningful XML expression. Attributes might
155 * overwrite or be shadowed by other attributes already in the node. For
156 * example, with both PREFIX-attr and attr present, the latter is overwritten by
157 * the operation.
158 *
159 * @param mml The MathML node.
160 * @returns The MathML node with rewritten attributes.
161 */
162 export function removeAttributePrefixscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/removeAttributePrefix().(mmlscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/removeAttributePrefix().(mml): string): string {
163 return mmlscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/removeAttributePrefix().(mml).toStringscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/String#toString().().replacescip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/String#replace().(new RegExpscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/RegExp#(Prefixscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Prefix., 'g'), '');
164 }
165
166 /**
167 * Creates an data semantic attribute by adding the correct prefix.
168 *
169 * @param attr The attribute.
170 * @returns The completed attribute.
171 */
172 // TODO (TS): Again this should have been return time Attribute
173 export function addPrefixscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/addPrefix().(attrscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/addPrefix().(attr): string): Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute# {
174 return (Prefixscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Prefix. + attrscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/addPrefix().(attr)) as Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#;
175 }
176
177 /**
178 * @returns A newly created mrow with an added attribute.
179 */
180 export function addMrowscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/addMrow().(): Elementscip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/Element# {
181 const mrow = DomUtilscip-typescript npm . . common/`dom_util.ts`/.createElementscip-typescript npm . . common/`dom_util.ts`/createElement().('mrow');
182 mrow.setAttributescip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/Element#setAttribute().(Attributescip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#.ADDEDscip-typescript npm . . enrich_mathml/`enrich_attr.ts`/Attribute#ADDED., 'true');
183 return mrow;
184 }