/
inline-parsers.js
230 lines (199 loc) · 6.37 KB
/
inline-parsers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// LICENSE : MIT
'use strict';
import assert from 'assert';
import { Syntax } from './mapping';
import {
parseBlockArg, findInlineTag, createNodeFromChunk, createCommentNodeFromLine, createStrNode,
createInlineNode, contextFromLine, offsetContext, contextNeedsUnescapeBraces, unescapeValue
} from './parser-utils';
/**
* parse a line.
* @param {Line} line - line to parse
* @return {[TxtNode]} TxtNodes
*/
export function parseLine(line) {
if (line.isComment) {
return [createCommentNodeFromLine(line)];
}
return parseText(line.text, contextFromLine(line));
}
const InlineParsers = {
// text tags
bou: inlineTextTagParser(Syntax.Bouten),
ami: inlineTextTagParser(Syntax.Amikake),
u: inlineTextTagParser(Syntax.Underline),
b: inlineTextTagParser(Syntax.Bold),
i: inlineTextTagParser(Syntax.Italic),
strong: inlineTextTagParser(Syntax.Strong),
em: inlineTextTagParser(Syntax.Emphasis),
tt: inlineTextTagParser(Syntax.Teletype),
tti: inlineTextTagParser(Syntax.TeletypeItalic),
ttb: inlineTextTagParser(Syntax.TeletypeBold),
tcy: inlineTextTagParser(Syntax.TateChuYoko),
// partially text tags
kw: parseKeywordTag,
ruby: parseRubyTag,
href: parseHrefTag,
// non-text tags
chap: inlineNonTextTagParser(Syntax.Reference),
title: inlineNonTextTagParser(Syntax.Reference),
chapref: inlineNonTextTagParser(Syntax.Reference),
list: inlineNonTextTagParser(Syntax.Reference),
img: inlineNonTextTagParser(Syntax.Reference),
table: inlineNonTextTagParser(Syntax.Reference),
hd: inlineNonTextTagParser(Syntax.Reference),
column: inlineNonTextTagParser(Syntax.Reference),
fn: inlineNonTextTagParser(Syntax.Reference),
code: withValue(inlineNonTextTagParser(Syntax.Code)),
comment: withValue(inlineNonTextTagParser(Syntax.Comment)),
uchar: inlineNonTextTagParser(Syntax.UnicodeChar),
br: inlineNonTextTagParser(Syntax.Break),
icon: inlineNonTextTagParser(Syntax.Icon),
m: inlineNonTextTagParser(Syntax.Math),
raw: inlineNonTextTagParser(Syntax.Raw),
};
/**
* get new inline tag parser to get value attribute.
* @param {function} inlineParser - Parser function of a inline tag
* @return {function} parser function
*/
function withValue(inlineParser) {
return (tag, context) => {
const node = inlineParser(tag, context);
node.value = unescapeValue(tag.content.raw, context);
return node;
};
}
/**
* get non-text tag parser function.
* @param {string} type - type of tag
* @return {function} parser function
*/
function inlineNonTextTagParser(type) {
return (tag, context) =>
parseInlineNonTextTag(type, tag, context);
}
/**
* get text tag parser function.
* @param {string} type - type of tag
* @return {function} parser function
*/
function inlineTextTagParser(type) {
return (tag, context) =>
parseInlineTextTag(type, tag, context);
}
/**
* parse non-text tag, which has no child.
* @param {string} type - type of tag
* @param {Tag} tag - tag to parse
* @param {Context} context - context of the node
* @return {TxtNode}
*/
function parseInlineNonTextTag(type, tag, context) {
const node = createInlineNode(type, tag.fullText, context);
return node;
}
/**
* parse text tag, which has child Str node.
* @param {string} type - type of tag
* @param {Tag} tag - tag to parse
* @param {Context} context - context of the node
* @return {TxtNode}
*/
function parseInlineTextTag(type, tag, context) {
const node = createInlineNode(type, tag.fullText, context);
const strContext = offsetContext(context, tag.content.index);
const strNode = createStrNode(tag.content.raw, strContext);
node.children = [strNode];
return node;
}
/**
* parse @<kw>{} tag.
* @param {Tag} tag - tag to parse
* @param {Context} context - context of the node
* @return {TxtNode}
*/
function parseKeywordTag(tag, context) {
const node = createInlineNode(Syntax.Keyword, tag.fullText, context);
const pieces = tag.content.raw.split(/\s*,\s*/, 2);
const word = pieces[0];
if (pieces.length === 2) {
node.alt = pieces[1];
}
const strNode = createStrNode(word, offsetContext(context, tag.content.index));
node.children = [strNode];
return node;
}
/**
* parse @<href>{} tag.
* @param {Tag} tag - tag to parse
* @param {Context} context - context of the node
* @return {TxtNode}
*/
function parseHrefTag(tag, context) {
const node = createInlineNode(Syntax.Href, tag.fullText, context);
const pieces = tag.content.raw.split(/\s*,\s*/, 2);
const url = pieces[0];
let label;
let labelOffset;
if (pieces.length === 2) {
label = pieces[1];
labelOffset = tag.content.index + tag.content.raw.indexOf(label, url.length);
assert(labelOffset >= tag.content.index);
} else {
label = url;
labelOffset = tag.content.index;
}
const strContext = offsetContext(context, labelOffset);
const strNode = createStrNode(label, strContext);
node.url = url;
node.children = [strNode];
return node;
}
/**
* parse @<ruby>{} tag.
* @param {Tag} tag - tag to parse
* @param {Context} context - context of the node
* @return {TxtNode}
*/
function parseRubyTag(tag, context) {
const node = createInlineNode(Syntax.Ruby, tag.fullText, context);
const pieces = tag.content.raw.split(/\s*,\s*/, 2);
assert(pieces.length === 2);
const rubyBase = pieces[0];
const rubyText = pieces[1];
const strNode = createStrNode(rubyBase, offsetContext(context, tag.content.index));
node.rubyText = rubyText;
node.children = [strNode];
return node;
}
/**
* parse inline tags and StrNodes from line.
* @param {string} text - Text of the line
* @param {Context} context - context of the node
* @return {[TxtNode]} TxtNodes in the line
*/
export function parseText(text, context) {
assert(!text.match(/[\r\n]/));
const nodes = [];
let tag;
while (tag = findInlineTag(text)) {
if (tag.precedingText !== '') {
const node = createStrNode(tag.precedingText, context);
nodes.push(node);
context = offsetContext(context, node.raw.length);
}
const parser = InlineParsers[tag.name];
if (parser) {
const node = parser(tag, contextNeedsUnescapeBraces(context));
nodes.push(node);
}
context = offsetContext(context, tag.fullText.length);
text = tag.followingText;
}
if (text.length) {
const node = createStrNode(text, context);
nodes.push(node);
}
return nodes;
}