1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | export function dedentBlockStringValue(rawString) {
|
10 |
|
11 | var lines = rawString.split(/\r\n|[\n\r]/g);
|
12 |
|
13 | var commonIndent = getBlockStringIndentation(rawString);
|
14 |
|
15 | if (commonIndent !== 0) {
|
16 | for (var i = 1; i < lines.length; i++) {
|
17 | lines[i] = lines[i].slice(commonIndent);
|
18 | }
|
19 | }
|
20 |
|
21 |
|
22 | var startLine = 0;
|
23 |
|
24 | while (startLine < lines.length && isBlank(lines[startLine])) {
|
25 | ++startLine;
|
26 | }
|
27 |
|
28 | var endLine = lines.length;
|
29 |
|
30 | while (endLine > startLine && isBlank(lines[endLine - 1])) {
|
31 | --endLine;
|
32 | }
|
33 |
|
34 |
|
35 | return lines.slice(startLine, endLine).join('\n');
|
36 | }
|
37 |
|
38 | function isBlank(str) {
|
39 | for (var i = 0; i < str.length; ++i) {
|
40 | if (str[i] !== ' ' && str[i] !== '\t') {
|
41 | return false;
|
42 | }
|
43 | }
|
44 |
|
45 | return true;
|
46 | }
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | export function getBlockStringIndentation(value) {
|
53 | var _commonIndent;
|
54 |
|
55 | var isFirstLine = true;
|
56 | var isEmptyLine = true;
|
57 | var indent = 0;
|
58 | var commonIndent = null;
|
59 |
|
60 | for (var i = 0; i < value.length; ++i) {
|
61 | switch (value.charCodeAt(i)) {
|
62 | case 13:
|
63 |
|
64 | if (value.charCodeAt(i + 1) === 10) {
|
65 | ++i;
|
66 | }
|
67 |
|
68 |
|
69 |
|
70 | case 10:
|
71 |
|
72 | isFirstLine = false;
|
73 | isEmptyLine = true;
|
74 | indent = 0;
|
75 | break;
|
76 |
|
77 | case 9:
|
78 |
|
79 | case 32:
|
80 |
|
81 | ++indent;
|
82 | break;
|
83 |
|
84 | default:
|
85 | if (isEmptyLine && !isFirstLine && (commonIndent === null || indent < commonIndent)) {
|
86 | commonIndent = indent;
|
87 | }
|
88 |
|
89 | isEmptyLine = false;
|
90 | }
|
91 | }
|
92 |
|
93 | return (_commonIndent = commonIndent) !== null && _commonIndent !== void 0 ? _commonIndent : 0;
|
94 | }
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | export function printBlockString(value) {
|
104 | var indentation = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
105 | var preferMultipleLines = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
106 | var isSingleLine = value.indexOf('\n') === -1;
|
107 | var hasLeadingSpace = value[0] === ' ' || value[0] === '\t';
|
108 | var hasTrailingQuote = value[value.length - 1] === '"';
|
109 | var hasTrailingSlash = value[value.length - 1] === '\\';
|
110 | var printAsMultipleLines = !isSingleLine || hasTrailingQuote || hasTrailingSlash || preferMultipleLines;
|
111 | var result = '';
|
112 |
|
113 | if (printAsMultipleLines && !(isSingleLine && hasLeadingSpace)) {
|
114 | result += '\n' + indentation;
|
115 | }
|
116 |
|
117 | result += indentation ? value.replace(/\n/g, '\n' + indentation) : value;
|
118 |
|
119 | if (printAsMultipleLines) {
|
120 | result += '\n';
|
121 | }
|
122 |
|
123 | return '"""' + result.replace(/"""/g, '\\"""') + '"""';
|
124 | }
|