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