1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- export function dedentBlockStringValue(rawString) {
-
- var lines = rawString.split(/\r\n|[\n\r]/g);
- var commonIndent = getBlockStringIndentation(lines);
- if (commonIndent !== 0) {
- for (var i = 1; i < lines.length; i++) {
- lines[i] = lines[i].slice(commonIndent);
- }
- }
- while (lines.length > 0 && isBlank(lines[0])) {
- lines.shift();
- }
- while (lines.length > 0 && isBlank(lines[lines.length - 1])) {
- lines.pop();
- }
- return lines.join('\n');
- }
- export function getBlockStringIndentation(lines) {
- var commonIndent = null;
- for (var i = 1; i < lines.length; i++) {
- var line = lines[i];
- var indent = leadingWhitespace(line);
- if (indent === line.length) {
- continue;
- }
- if (commonIndent === null || indent < commonIndent) {
- commonIndent = indent;
- if (commonIndent === 0) {
- break;
- }
- }
- }
- return commonIndent === null ? 0 : commonIndent;
- }
- function leadingWhitespace(str) {
- var i = 0;
- while (i < str.length && (str[i] === ' ' || str[i] === '\t')) {
- i++;
- }
- return i;
- }
- function isBlank(str) {
- return leadingWhitespace(str) === str.length;
- }
- export function printBlockString(value) {
- var indentation = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
- var preferMultipleLines = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
- var isSingleLine = value.indexOf('\n') === -1;
- var hasLeadingSpace = value[0] === ' ' || value[0] === '\t';
- var hasTrailingQuote = value[value.length - 1] === '"';
- var printAsMultipleLines = !isSingleLine || hasTrailingQuote || preferMultipleLines;
- var result = '';
- if (printAsMultipleLines && !(isSingleLine && hasLeadingSpace)) {
- result += '\n' + indentation;
- }
- result += indentation ? value.replace(/\n/g, '\n' + indentation) : value;
- if (printAsMultipleLines) {
- result += '\n';
- }
- return '"""' + result.replace(/"""/g, '\\"""') + '"""';
- }
|