wikiparser-node 0.4.0 → 0.6.1

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.
Files changed (87) hide show
  1. package/config/default.json +129 -66
  2. package/config/zhwiki.json +4 -4
  3. package/index.js +97 -65
  4. package/lib/element.js +159 -302
  5. package/lib/node.js +384 -198
  6. package/lib/ranges.js +3 -4
  7. package/lib/text.js +65 -36
  8. package/lib/title.js +9 -8
  9. package/mixin/fixedToken.js +4 -4
  10. package/mixin/hidden.js +2 -0
  11. package/mixin/sol.js +16 -7
  12. package/package.json +14 -3
  13. package/parser/brackets.js +8 -2
  14. package/parser/commentAndExt.js +1 -1
  15. package/parser/converter.js +1 -1
  16. package/parser/externalLinks.js +2 -2
  17. package/parser/hrAndDoubleUnderscore.js +8 -7
  18. package/parser/links.js +8 -9
  19. package/parser/magicLinks.js +1 -1
  20. package/parser/selector.js +5 -5
  21. package/parser/table.js +18 -16
  22. package/src/arg.js +71 -42
  23. package/src/atom/index.js +7 -5
  24. package/src/attribute.js +102 -64
  25. package/src/charinsert.js +91 -0
  26. package/src/converter.js +34 -15
  27. package/src/converterFlags.js +87 -40
  28. package/src/converterRule.js +59 -53
  29. package/src/extLink.js +45 -37
  30. package/src/gallery.js +71 -16
  31. package/src/hasNowiki/index.js +42 -0
  32. package/src/hasNowiki/pre.js +40 -0
  33. package/src/heading.js +41 -18
  34. package/src/html.js +76 -48
  35. package/src/imageParameter.js +73 -51
  36. package/src/imagemap.js +205 -0
  37. package/src/imagemapLink.js +43 -0
  38. package/src/index.js +243 -138
  39. package/src/link/category.js +10 -14
  40. package/src/link/file.js +112 -56
  41. package/src/link/galleryImage.js +74 -10
  42. package/src/link/index.js +86 -61
  43. package/src/magicLink.js +48 -21
  44. package/src/nested/choose.js +24 -0
  45. package/src/nested/combobox.js +23 -0
  46. package/src/nested/index.js +88 -0
  47. package/src/nested/references.js +23 -0
  48. package/src/nowiki/comment.js +18 -4
  49. package/src/nowiki/dd.js +2 -2
  50. package/src/nowiki/doubleUnderscore.js +16 -11
  51. package/src/nowiki/index.js +12 -0
  52. package/src/nowiki/quote.js +28 -1
  53. package/src/onlyinclude.js +15 -8
  54. package/src/paramTag/index.js +83 -0
  55. package/src/paramTag/inputbox.js +42 -0
  56. package/src/parameter.js +73 -46
  57. package/src/syntax.js +9 -1
  58. package/src/table/index.js +58 -44
  59. package/src/table/td.js +63 -63
  60. package/src/table/tr.js +52 -35
  61. package/src/tagPair/ext.js +60 -43
  62. package/src/tagPair/include.js +11 -1
  63. package/src/tagPair/index.js +29 -20
  64. package/src/transclude.js +214 -166
  65. package/tool/index.js +720 -439
  66. package/util/base.js +17 -0
  67. package/util/debug.js +1 -1
  68. package/{test/util.js → util/diff.js} +15 -19
  69. package/util/lint.js +40 -0
  70. package/util/string.js +37 -20
  71. package/.eslintrc.json +0 -714
  72. package/errors/README +0 -1
  73. package/jsconfig.json +0 -7
  74. package/printed/README +0 -1
  75. package/printed/example.json +0 -120
  76. package/test/api.js +0 -83
  77. package/test/real.js +0 -133
  78. package/test/test.js +0 -28
  79. package/typings/api.d.ts +0 -13
  80. package/typings/array.d.ts +0 -28
  81. package/typings/event.d.ts +0 -24
  82. package/typings/index.d.ts +0 -94
  83. package/typings/node.d.ts +0 -29
  84. package/typings/parser.d.ts +0 -16
  85. package/typings/table.d.ts +0 -14
  86. package/typings/token.d.ts +0 -22
  87. package/typings/tool.d.ts +0 -11
package/util/base.js ADDED
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * 是否是普通对象
5
+ * @param {*} obj 对象
6
+ */
7
+ const isPlainObject = obj => Boolean(obj) && Object.getPrototypeOf(obj).constructor === Object;
8
+
9
+ /**
10
+ * 延时
11
+ * @param {number} t 秒数
12
+ */
13
+ const sleep = t => new Promise(resolve => {
14
+ setTimeout(resolve, t * 1000);
15
+ });
16
+
17
+ module.exports = {isPlainObject, sleep};
package/util/debug.js CHANGED
@@ -3,7 +3,7 @@
3
3
  /**
4
4
  * 定制TypeError消息
5
5
  * @param {Function} constructor 类
6
- * @param {string} method 方法名称
6
+ * @param {string} method
7
7
  * @param {...string} args 可接受的参数类型
8
8
  * @throws `TypeError`
9
9
  */
@@ -1,4 +1,5 @@
1
1
  'use strict';
2
+
2
3
  const {spawn} = require('child_process'),
3
4
  fs = require('fs/promises');
4
5
 
@@ -17,7 +18,7 @@ const cmd = (command, args) => new Promise(resolve => {
17
18
 
18
19
  /**
19
20
  * 清除进程并返回
20
- * @param {any} val 返回值
21
+ * @param {*} val 返回值
21
22
  */
22
23
  const r = val => {
23
24
  clearTimeout(timer);
@@ -49,32 +50,27 @@ const cmd = (command, args) => new Promise(resolve => {
49
50
 
50
51
  /**
51
52
  * 比较两个文件
52
- * @param {string} oldfile 旧文件
53
- * @param {string} newfile 新文件
53
+ * @param {string} oldStr 旧文本
54
+ * @param {string} newStr 新文本
55
+ * @param {string} uid 唯一标识
54
56
  */
55
- const diff = async (oldfile, newfile) => {
56
- if (oldfile === newfile) {
57
+ const diff = async (oldStr, newStr, uid = '') => {
58
+ if (oldStr === newStr) {
57
59
  return;
58
60
  }
59
- await Promise.all([fs.writeFile('npmTestOldContent', oldfile), fs.writeFile('npmTestNewContent', newfile)]);
61
+ const oldFile = `diffOld${uid}`,
62
+ newFile = `diffNew${uid}`;
63
+ await Promise.all([fs.writeFile(oldFile, oldStr), fs.writeFile(newFile, newStr)]);
60
64
  const stdout = await cmd('git', [
61
65
  'diff',
62
- '--color-words=[\xc0-\xff][\x80-\xbf]+|<?/?\\w+/?>?|[^[:space:]]',
66
+ '--color-words=[\xC0-\xFF][\x80-\xBF]+|<?/?\\w+/?>?|[^[:space:]]',
63
67
  '-U0',
64
68
  '--no-index',
65
- 'npmTestOldContent',
66
- 'npmTestNewContent',
69
+ oldFile,
70
+ newFile,
67
71
  ]);
68
- await Promise.all([fs.unlink('npmTestOldContent'), fs.unlink('npmTestNewContent')]);
72
+ await Promise.all([fs.unlink(oldFile), fs.unlink(newFile)]);
69
73
  console.log(stdout?.split('\n')?.slice(4)?.join('\n'));
70
74
  };
71
75
 
72
- /**
73
- * 延时
74
- * @param {number} t 秒数
75
- */
76
- const sleep = t => new Promise(resolve => {
77
- setTimeout(resolve, t * 1000);
78
- });
79
-
80
- module.exports = {diff, sleep};
76
+ module.exports = diff;
package/util/lint.js ADDED
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ const Token = require('../src');
4
+
5
+ /**
6
+ * 生成对于子节点的LintError对象
7
+ * @param {Token} child 子节点
8
+ * @param {{top: number, left: number}} boundingRect 父节点的绝对定位
9
+ * @param {string} message 错误信息
10
+ * @param {'error'|'warning'} severity 严重程度
11
+ * @returns {LintError}
12
+ */
13
+ const generateForChild = (child, boundingRect, message, severity = 'error') => {
14
+ const {style: {top: offsetTop, left: offsetLeft, height, width}} = child,
15
+ {top, left} = boundingRect,
16
+ startLine = top + offsetTop,
17
+ endLine = startLine + height - 1,
18
+ startCol = offsetTop ? offsetLeft : left + offsetLeft,
19
+ endCol = height > 1 ? width : startCol + width;
20
+ return {message, severity, startLine, endLine, startCol, endCol};
21
+ };
22
+
23
+ /**
24
+ * 生成对于自己的LintError对象
25
+ * @param {Token} token 节点
26
+ * @param {{top: number, left: number}} boundingRect 绝对定位
27
+ * @param {string} message 错误信息
28
+ * @param {'error'|'warning'} severity 严重程度
29
+ * @returns {LintError}
30
+ */
31
+ const generateForSelf = (token, boundingRect, message, severity = 'error') => ({
32
+ message,
33
+ severity,
34
+ startLine: boundingRect.top,
35
+ endLine: boundingRect.top + token.offsetHeight - 1,
36
+ startCol: boundingRect.left,
37
+ endCol: token.offsetHeight > 1 ? token.offsetWidth : boundingRect.left + token.offsetWidth,
38
+ });
39
+
40
+ module.exports = {generateForChild, generateForSelf};
package/util/string.js CHANGED
@@ -1,11 +1,7 @@
1
1
  'use strict';
2
2
 
3
- /**
4
- * optionally convert to lower cases
5
- * @param {string} val 属性值
6
- * @param {string|undefined} i 是否对大小写不敏感
7
- */
8
- const toCase = (val, i) => i ? val.toLowerCase() : val;
3
+ const extUrlChar = '(?:\\[[\\da-f:.]+\\]|[^[\\]<>"\\0-\\x1F\\x7F\\p{Zs}\\uFFFD])'
4
+ + '(?:[^[\\]<>"\\0-\\x1F\\x7F\\p{Zs}\\uFFFD]|\\0\\d+c\\x7F)*';
9
5
 
10
6
  /**
11
7
  * remove half-parsed comment-like tokens
@@ -14,20 +10,27 @@ const toCase = (val, i) => i ? val.toLowerCase() : val;
14
10
  const removeComment = str => str.replaceAll(/\0\d+c\x7F/gu, '');
15
11
 
16
12
  /**
17
- * escape special chars for RegExp constructor
18
- * @param {string} str RegExp source
13
+ * 以HTML格式打印
14
+ * @param {(AstText|AstElement)[]} childNodes 子节点
15
+ * @param {printOpt} opt 选项
19
16
  */
20
- const escapeRegExp = str => str.replaceAll(/[\\{}()|.?*+^$[\]]/gu, '\\$&');
17
+ const print = (childNodes, opt = {}) => {
18
+ const AstText = require('../lib/text'),
19
+ AstElement = require('../lib/element');
20
+ const {pre = '', post = '', sep = ''} = opt,
21
+ entities = {'&': 'amp', '<': 'lt', '>': 'gt'};
22
+ return `${pre}${childNodes.map(
23
+ child => child instanceof AstElement
24
+ ? child.print()
25
+ : String(child).replaceAll(/[&<>]/gu, p => `&${entities[p]};`),
26
+ ).join(sep)}${post}`;
27
+ };
21
28
 
22
29
  /**
23
- * extract effective wikitext
24
- * @param {(string|AstNode)[]} childNodes a Token's contents
25
- * @param {string} separator delimiter between nodes
30
+ * escape special chars for RegExp constructor
31
+ * @param {string} str RegExp source
26
32
  */
27
- const text = (childNodes, separator = '') => {
28
- const AstNode = require('../lib/node');
29
- return childNodes.map(child => typeof child === 'string' ? child : child.text()).join(separator);
30
- };
33
+ const escapeRegExp = str => str.replaceAll(/[\\{}()|.?*+^$[\]]/gu, '\\$&');
31
34
 
32
35
  /**
33
36
  * a more sophisticated string-explode function
@@ -59,6 +62,23 @@ const explode = (start, end, separator, str) => {
59
62
  return exploded;
60
63
  };
61
64
 
65
+ /**
66
+ * optionally convert to lower cases
67
+ * @param {string} val 属性值
68
+ * @param {string|undefined} i 是否对大小写不敏感
69
+ */
70
+ const toCase = (val, i) => i ? val.toLowerCase() : val;
71
+
72
+ /**
73
+ * extract effective wikitext
74
+ * @param {(string|AstNode)[]} childNodes a Token's contents
75
+ * @param {string} separator delimiter between nodes
76
+ */
77
+ const text = (childNodes, separator = '') => {
78
+ const AstNode = require('../lib/node');
79
+ return childNodes.map(child => typeof child === 'string' ? child : child.text()).join(separator);
80
+ };
81
+
62
82
  /**
63
83
  * escape newlines
64
84
  * @param {string} str 原字符串
@@ -82,7 +102,4 @@ const normalizeSpace = token => {
82
102
  }
83
103
  };
84
104
 
85
- const extUrlChar = '(?:\\[[\\da-f:.]+\\]|[^[\\]<>"\\0-\\x1f\\x7f\\p{Zs}\\ufffd])'
86
- + '(?:[^[\\]<>"\\0-\\x1f\\x7f\\p{Zs}\\ufffd]|\\0\\d+c\\x7f)*';
87
-
88
- module.exports = {toCase, removeComment, escapeRegExp, text, explode, noWrap, normalizeSpace, extUrlChar};
105
+ module.exports = {extUrlChar, removeComment, print, escapeRegExp, explode, toCase, text, noWrap, normalizeSpace};