occam-dom 5.2.28 → 5.2.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,9 +13,9 @@
|
|
|
13
13
|
|
|
14
14
|
## Introduction
|
|
15
15
|
|
|
16
|
-
Specifically, "outer" token arrays and
|
|
16
|
+
Specifically, "outer" token arrays and parse trees resulting from the user of Occam's [lexers](https://github.com/djalbat/occam-lexers) and [parsers](https://github.com/djalbat/occam-parsers), respectively, can be refined into "inner" token arrays and parse trees for convenience.
|
|
17
17
|
|
|
18
|
-
An example is helpful. Consider the following outer
|
|
18
|
+
An example is helpful. Consider the following outer parse tree that results from parsing some simple Markdown.
|
|
19
19
|
|
|
20
20
|
```
|
|
21
21
|
|
|
|
@@ -38,7 +38,7 @@ An example is helpful. Consider the following outer node tree that results from
|
|
|
38
38
|
"Heading"[word] [0] "Paragraph"[word] [2] "."[special] [2]
|
|
39
39
|
```
|
|
40
40
|
|
|
41
|
-
This contains far more information than is needed to render the markdown as HTML. The following inner
|
|
41
|
+
This contains far more information than is needed to render the markdown as HTML. The following inner parse tree will in fact suffice:
|
|
42
42
|
|
|
43
43
|
```
|
|
44
44
|
|
|
|
@@ -55,7 +55,7 @@ This contains far more information than is needed to render the markdown as HTML
|
|
|
55
55
|
plainText plainText
|
|
56
56
|
```
|
|
57
57
|
|
|
58
|
-
Locating the outer nodes that contribute to this inner
|
|
58
|
+
Locating the outer nodes that contribute to this inner parse tree is straightforward enough using Occam's [queries](https://github.com/djalbat/occam-query). However, the arrays of nodes that result are not in the above form. This package provides the means to form such parse trees.
|
|
59
59
|
|
|
60
60
|
## Installation
|
|
61
61
|
|
|
@@ -93,7 +93,7 @@ One last thing to bear in mind is that this package is included by way of a rela
|
|
|
93
93
|
|
|
94
94
|
## Usage
|
|
95
95
|
|
|
96
|
-
Suppose that a Markdown document is being
|
|
96
|
+
Suppose that a Markdown document is being processed by way of [Highmark](https://github.com/djalbat/highmark-markdown)'s lexer and parser:
|
|
97
97
|
|
|
98
98
|
```
|
|
99
99
|
const markdownLexer = MarkdownLexer.fromNothing(),
|
|
@@ -108,7 +108,7 @@ const content = markdown, ///
|
|
|
108
108
|
...
|
|
109
109
|
```
|
|
110
110
|
|
|
111
|
-
A number of arrays of nodes can be created from the
|
|
111
|
+
A number of arrays of nodes can be created from the "outer" document node using queries:
|
|
112
112
|
|
|
113
113
|
```
|
|
114
114
|
export function nodesFromNodeAndQueries(node, queries, nodes = []) {
|
|
@@ -122,7 +122,46 @@ export function nodesFromNodeAndQueries(node, queries, nodes = []) {
|
|
|
122
122
|
}
|
|
123
123
|
```
|
|
124
124
|
|
|
125
|
-
These can then be formed into an ""
|
|
125
|
+
These can then be formed into an "inner" node tree using the `topmostNodeFromOuterNodes()` function:
|
|
126
|
+
|
|
127
|
+
```
|
|
128
|
+
import { nodeUtilities } from "occam-dom";
|
|
129
|
+
|
|
130
|
+
const { topmostNodeFromOuterNodes } = nodeUtilities;
|
|
131
|
+
|
|
132
|
+
...
|
|
133
|
+
|
|
134
|
+
const outerNodes = nodes, ///
|
|
135
|
+
topmostNode = topmostNodeFromOuterNodes(outerNode);
|
|
136
|
+
|
|
137
|
+
...
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
You can also pass an optional callback argument to this function that maps outer nodes to inner node Classes:
|
|
141
|
+
|
|
142
|
+
```
|
|
143
|
+
import htmlNodeMap from "../map/node/html";
|
|
144
|
+
import TopmostHTMLNOde from "../node/html/topmost";
|
|
145
|
+
|
|
146
|
+
function ClassFromOuterNode(outerNode) {
|
|
147
|
+
let Class;
|
|
148
|
+
|
|
149
|
+
if (outerNode === null) {
|
|
150
|
+
Class = TopmostHTMLNode; ///
|
|
151
|
+
} else {
|
|
152
|
+
const nonTerminalNode = outerNode, ///
|
|
153
|
+
ruleName = nonTerminalNode.getRuleName();
|
|
154
|
+
|
|
155
|
+
Class = htmlNodeMap[ruleName] || HTMLNode;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return Class;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const topmostNode = topmostNodeFromOuterNodes(ClassFromOuterNode, outerNode);
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Note that the topmost inner node has no corresponding outer node and that a default of `HTMLNode` has been provided. There is no need to provide a unique class for every type of outer node that the algorithm may come across.
|
|
126
165
|
|
|
127
166
|
## Building
|
|
128
167
|
|