Keep trkpt and wpt on single line
to better match BRouter output
This commit is contained in:
parent
265583e0c5
commit
7caa7aeba6
3 changed files with 43 additions and 23 deletions
|
|
@ -104,23 +104,45 @@ BR.Gpx = {
|
||||||
const newline = '\n';
|
const newline = '\n';
|
||||||
|
|
||||||
// Remove all the newlines and then remove all the spaces between tags
|
// Remove all the newlines and then remove all the spaces between tags
|
||||||
xml = xml.replace(/\s*(\r\n|\n|\r)\s*/gm, ' ').replace(/>\s+</g, '><');
|
xml = xml.replace(/>\s*(\r\n|\n|\r)\s*</gm, '><').replace(/>\s+</g, '><');
|
||||||
|
|
||||||
// break into lines
|
|
||||||
const reg = /(>)(<)(\/?)/g;
|
|
||||||
let pad = 0;
|
|
||||||
|
|
||||||
xml = xml.replace('<metadata/>', '');
|
xml = xml.replace('<metadata/>', '');
|
||||||
xml = xml.replace(reg, `$1${newline}$2$3`);
|
|
||||||
|
|
||||||
let lines = xml.split(newline);
|
// break into lines, keeping defined tags on a single line
|
||||||
|
const reg = /><(\/?)([\w!?][^ />]*)/g;
|
||||||
|
const singleLineTagList = ['trkpt', 'wpt'];
|
||||||
|
let lines = [];
|
||||||
|
let singleLineTag = null;
|
||||||
|
let startIndex = 0;
|
||||||
|
let match;
|
||||||
|
while ((match = reg.exec(xml)) !== null) {
|
||||||
|
const tag = match[2];
|
||||||
|
if (singleLineTag) {
|
||||||
|
if (singleLineTag === tag) {
|
||||||
|
singleLineTag = null;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (singleLineTagList.includes(tag)) {
|
||||||
|
singleLineTag = tag;
|
||||||
|
}
|
||||||
|
let endIndex = match.index + 1;
|
||||||
|
lines.push(xml.substring(startIndex, endIndex));
|
||||||
|
startIndex = endIndex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lines.push(xml.substring(startIndex));
|
||||||
|
|
||||||
|
// indent
|
||||||
|
const startTextEnd = /.+<\/\w[^>]*>$/;
|
||||||
|
const endTag = /^<\/\w/;
|
||||||
|
const startTag = /^<\w[^>]*[^\/]>.*$/;
|
||||||
|
let pad = 0;
|
||||||
lines = lines.map((node, index) => {
|
lines = lines.map((node, index) => {
|
||||||
let indent = 0;
|
let indent = 0;
|
||||||
if (node.match(/.+<\/\w[^>]*>$/)) {
|
if (node.match(startTextEnd)) {
|
||||||
indent = 0;
|
indent = 0;
|
||||||
} else if (node.match(/^<\/\w/) && pad > 0) {
|
} else if (node.match(endTag) && pad > 0) {
|
||||||
pad -= 1;
|
pad -= 1;
|
||||||
} else if (node.match(/^<\w[^>]*[^\/]>.*$/)) {
|
} else if (node.match(startTag)) {
|
||||||
indent = 1;
|
indent = 1;
|
||||||
} else {
|
} else {
|
||||||
indent = 0;
|
indent = 0;
|
||||||
|
|
@ -131,10 +153,10 @@ BR.Gpx = {
|
||||||
return PADDING.repeat(pad - indent) + node;
|
return PADDING.repeat(pad - indent) + node;
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const [i, line] of lines.entries()) {
|
|
||||||
// break gpx attributes into separate lines
|
// break gpx attributes into separate lines
|
||||||
if (line.includes('<gpx ')) {
|
for (const [i, line] of lines.entries()) {
|
||||||
lines[i] = line.replace(/ ([a-z:]+=")/gi, ` ${newline}${PADDING}$1`);
|
if (line.includes('<gpx ') && !line.includes(newline)) {
|
||||||
|
lines[i] = line.replace(/ (\w[^=" ]*=")/g, ` ${newline}${PADDING}$1`);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -259,8 +259,7 @@
|
||||||
let comment = `
|
let comment = `
|
||||||
<!-- $transport-mode$${this.transportMode}$ -->
|
<!-- $transport-mode$${this.transportMode}$ -->
|
||||||
<!-- cmd idx lon lat d2next geometry -->
|
<!-- cmd idx lon lat d2next geometry -->
|
||||||
<!-- $turn-instruction-start$
|
<!-- $turn-instruction-start$`;
|
||||||
`;
|
|
||||||
|
|
||||||
this._loopHints((hint, cmd, coord) => {
|
this._loopHints((hint, cmd, coord) => {
|
||||||
const pad = (obj = '', len) => {
|
const pad = (obj = '', len) => {
|
||||||
|
|
@ -276,8 +275,7 @@
|
||||||
turn += hint.geometry;
|
turn += hint.geometry;
|
||||||
|
|
||||||
comment += `
|
comment += `
|
||||||
$turn$${turn}$
|
$turn$${turn}$`;
|
||||||
`;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
comment += `
|
comment += `
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ function adoptGpx(gpx, replaceCreator = true) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function read(fileName, replaceCreator) {
|
function read(fileName, replaceCreator) {
|
||||||
return BR.Gpx.pretty(adoptGpx(fs.readFileSync(path + fileName, 'utf8'), replaceCreator));
|
return adoptGpx(fs.readFileSync(path + fileName, 'utf8'), replaceCreator);
|
||||||
}
|
}
|
||||||
|
|
||||||
test('simple track', () => {
|
test('simple track', () => {
|
||||||
|
|
@ -48,14 +48,14 @@ test('simple track', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('waypoints', () => {
|
test('waypoints', () => {
|
||||||
const brouterGpx = read('waypoints.gpx');
|
const brouterGpx = BR.Gpx.pretty(read('waypoints.gpx'));
|
||||||
const gpx = BR.Gpx.format(waypointsGeoJson, 5);
|
const gpx = BR.Gpx.format(waypointsGeoJson, 5);
|
||||||
expect(gpx).toEqual(brouterGpx);
|
expect(gpx).toEqual(brouterGpx);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('voice hints', () => {
|
describe('voice hints', () => {
|
||||||
test('2-locus', () => {
|
test('2-locus', () => {
|
||||||
let brouterGpx = read('2-locus.gpx');
|
let brouterGpx = BR.Gpx.pretty(read('2-locus.gpx'));
|
||||||
brouterGpx = brouterGpx.replace(/\n\s*<\/extensions>\n\s*<extensions>/, ''); // ignore (invalid) double tag
|
brouterGpx = brouterGpx.replace(/\n\s*<\/extensions>\n\s*<extensions>/, ''); // ignore (invalid) double tag
|
||||||
// ignore float rounding differences
|
// ignore float rounding differences
|
||||||
brouterGpx = brouterGpx.replace(
|
brouterGpx = brouterGpx.replace(
|
||||||
|
|
@ -68,7 +68,7 @@ describe('voice hints', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('3-osmand', () => {
|
test('3-osmand', () => {
|
||||||
const brouterGpx = read('3-osmand.gpx', false);
|
const brouterGpx = BR.Gpx.pretty(read('3-osmand.gpx', false));
|
||||||
const gpx = BR.Gpx.format(geoJson, 3);
|
const gpx = BR.Gpx.format(geoJson, 3);
|
||||||
expect(gpx).toEqual(brouterGpx);
|
expect(gpx).toEqual(brouterGpx);
|
||||||
});
|
});
|
||||||
|
|
@ -88,7 +88,7 @@ describe('voice hints', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('6-orux', () => {
|
test('6-orux', () => {
|
||||||
let brouterGpx = read('6-orux.gpx');
|
let brouterGpx = BR.Gpx.pretty(read('6-orux.gpx'));
|
||||||
const gpx = BR.Gpx.format(geoJson, 6);
|
const gpx = BR.Gpx.format(geoJson, 6);
|
||||||
expect(gpx).toEqual(brouterGpx);
|
expect(gpx).toEqual(brouterGpx);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue