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
|
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
|
+
+
+
+
-
-
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
|
// JavaScript
/*jslint esversion: 6*/
/*global require,exports*/
"use strict";
if (typeof(require) !== 'undefined') {
const helpers = require("resource://grammalecte/helpers.js");
var helpers = require("resource://grammalecte/helpers.js");
}
var text = {
function* getParagraph (sText) {
// generator: returns paragraphs of text
let iStart = 0;
let iEnd = 0;
sText = sText.replace("\r", "");
while ((iEnd = sText.indexOf("\n", iStart)) !== -1) {
yield sText.slice(iStart, iEnd);
iStart = iEnd + 1;
}
yield sText.slice(iStart);
}
getParagraph: function* (sText) {
// generator: returns paragraphs of text
let iStart = 0;
let iEnd = 0;
sText = sText.replace("\r\n", "\n").replace("\r", "\n");
while ((iEnd = sText.indexOf("\n", iStart)) !== -1) {
yield sText.slice(iStart, iEnd);
iStart = iEnd + 1;
}
yield sText.slice(iStart);
},
function* wrap (sText, nWidth=80) {
// generator: returns text line by line
while (sText) {
if (sText.length >= nWidth) {
let nEnd = sText.lastIndexOf(" ", nWidth) + 1;
if (nEnd > 0) {
yield sText.slice(0, nEnd);
sText = sText.slice(nEnd);
} else {
yield sText.slice(0, nWidth);
sText = sText.slice(nWidth);
}
} else {
break;
}
}
yield sText;
}
wrap: function* (sText, nWidth=80) {
// generator: returns text line by line
while (sText) {
if (sText.length >= nWidth) {
let nEnd = sText.lastIndexOf(" ", nWidth) + 1;
if (nEnd > 0) {
yield sText.slice(0, nEnd);
sText = sText.slice(nEnd);
} else {
yield sText.slice(0, nWidth);
sText = sText.slice(nWidth);
}
} else {
break;
}
}
yield sText;
},
function getReadableError (oErr) {
// Returns an error oErr as a readable error
try {
let sResult = "\n* " + oErr['nStart'] + ":" + oErr['nEnd']
+ " # " + oErr['sLineId'] + " # " + oErr['sRuleId'] + ":\n";
sResult += " " + oErr["sMessage"];
if (oErr["aSuggestions"].length > 0) {
sResult += "\n > Suggestions : " + oErr["aSuggestions"].join(" | ");
}
if (oErr["URL"] !== "") {
sResult += "\n > URL: " + oErr["URL"];
}
return sResult;
}
catch (e) {
helpers.logerror(e);
return "\n# Error. Data: " + oErr.toString();
}
}
getReadableError: function (oErr) {
// Returns an error oErr as a readable error
try {
let sResult = "\n* " + oErr['nStart'] + ":" + oErr['nEnd']
+ " # " + oErr['sLineId'] + " # " + oErr['sRuleId'] + ":\n";
sResult += " " + oErr["sMessage"];
if (oErr["aSuggestions"].length > 0) {
sResult += "\n > Suggestions : " + oErr["aSuggestions"].join(" | ");
}
if (oErr["URL"] !== "") {
sResult += "\n > URL: " + oErr["URL"];
}
return sResult;
}
catch (e) {
helpers.logerror(e);
return "\n# Error. Data: " + oErr.toString();
}
}
};
if (typeof(exports) !== 'undefined') {
exports.getParagraph = getParagraph;
exports.wrap = wrap;
exports.getReadableError = getReadableError;
exports.getParagraph = text.getParagraph;
exports.wrap = text.wrap;
exports.getReadableError = text.getReadableError;
}
|