2021-01-21 22:06:52 +01:00
|
|
|
'use strict';
|
2013-02-12 20:45:46 +01:00
|
|
|
/**
|
|
|
|
* Helpers for export requests
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-01-21 22:06:52 +01:00
|
|
|
const Changeset = require('../../static/js/Changeset');
|
2013-02-12 20:45:46 +01:00
|
|
|
|
2021-01-21 22:06:52 +01:00
|
|
|
exports.getPadPlainText = (pad, revNum) => {
|
2020-11-23 19:24:19 +01:00
|
|
|
const _analyzeLine = exports._analyzeLine;
|
|
|
|
const atext = ((revNum !== undefined) ? pad.getInternalRevisionAText(revNum) : pad.atext);
|
|
|
|
const textLines = atext.text.slice(0, -1).split('\n');
|
|
|
|
const attribLines = Changeset.splitAttributionLines(atext.attribs, atext.text);
|
|
|
|
const apool = pad.pool;
|
2013-02-12 20:45:46 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const pieces = [];
|
|
|
|
for (let i = 0; i < textLines.length; i++) {
|
|
|
|
const line = _analyzeLine(textLines[i], attribLines[i], apool);
|
|
|
|
if (line.listLevel) {
|
|
|
|
const numSpaces = line.listLevel * 2 - 1;
|
|
|
|
const bullet = '*';
|
2013-02-12 20:45:46 +01:00
|
|
|
pieces.push(new Array(numSpaces + 1).join(' '), bullet, ' ', line.text, '\n');
|
2020-11-23 19:24:19 +01:00
|
|
|
} else {
|
2013-02-12 20:45:46 +01:00
|
|
|
pieces.push(line.text, '\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pieces.join('');
|
2013-03-24 01:18:44 +01:00
|
|
|
};
|
2013-02-12 20:45:46 +01:00
|
|
|
|
|
|
|
|
2021-01-21 22:06:52 +01:00
|
|
|
exports._analyzeLine = (text, aline, apool) => {
|
2020-11-23 19:24:19 +01:00
|
|
|
const line = {};
|
2013-02-12 20:45:46 +01:00
|
|
|
|
|
|
|
// identify list
|
2020-11-23 19:24:19 +01:00
|
|
|
let lineMarker = 0;
|
2013-02-12 20:45:46 +01:00
|
|
|
line.listLevel = 0;
|
2020-11-23 19:24:19 +01:00
|
|
|
if (aline) {
|
|
|
|
const opIter = Changeset.opIterator(aline);
|
|
|
|
if (opIter.hasNext()) {
|
|
|
|
let listType = Changeset.opAttributeValue(opIter.next(), 'list', apool);
|
|
|
|
if (listType) {
|
2013-02-12 20:45:46 +01:00
|
|
|
lineMarker = 1;
|
2015-01-19 00:24:20 +01:00
|
|
|
listType = /([a-z]+)([0-9]+)/.exec(listType);
|
2020-11-23 19:24:19 +01:00
|
|
|
if (listType) {
|
2013-02-12 20:45:46 +01:00
|
|
|
line.listTypeName = listType[1];
|
|
|
|
line.listLevel = Number(listType[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-23 19:24:19 +01:00
|
|
|
const opIter2 = Changeset.opIterator(aline);
|
|
|
|
if (opIter2.hasNext()) {
|
|
|
|
const start = Changeset.opAttributeValue(opIter2.next(), 'start', apool);
|
|
|
|
if (start) {
|
|
|
|
line.start = start;
|
2020-06-05 21:54:16 +02:00
|
|
|
}
|
|
|
|
}
|
2013-02-12 20:45:46 +01:00
|
|
|
}
|
2020-11-23 19:24:19 +01:00
|
|
|
if (lineMarker) {
|
2013-02-12 20:45:46 +01:00
|
|
|
line.text = text.substring(1);
|
|
|
|
line.aline = Changeset.subattribution(aline, 1);
|
2020-11-23 19:24:19 +01:00
|
|
|
} else {
|
2013-02-12 20:45:46 +01:00
|
|
|
line.text = text;
|
|
|
|
line.aline = aline;
|
|
|
|
}
|
|
|
|
return line;
|
2013-03-24 01:18:44 +01:00
|
|
|
};
|
2013-02-12 20:45:46 +01:00
|
|
|
|
|
|
|
|
2021-01-21 22:06:52 +01:00
|
|
|
exports._encodeWhitespace =
|
|
|
|
(s) => s.replace(/[^\x21-\x7E\s\t\n\r]/gu, (c) => `&#${c.codePointAt(0)};`);
|