File indexing completed on 2024-12-29 04:51:05

0001 /*
0002    SPDX-FileCopyrightText: 2019-2022 Volker Krause <vkrause@kde.org>
0003    SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 function parseTicket(code, node) {
0007     // VorteilsCard
0008     if (code.ticketLayout && code.ticketLayout.type == "RCT2" && code.ticketLayout.text(0, 0, 50, 1).match(/VORTEILSCARD/i)) {
0009         let card = JsonLd.newObject("ProgramMembership");
0010         card.programName = code.ticketLayout.text(0, 0, 50, 1);
0011         card.membershipNumber = code.ticketLayout.text(1, 1, 16, 1);
0012         card.member = JsonLd.newObject("Person");
0013         card.member.givenName = code.ticketLayout.text(1, 52, 19 ,1);
0014         card.member.familyName = code.ticketLayout.text(2, 52, 19 ,1);
0015         card.token = 'aztecbin:' + ByteArray.toBase64(code.rawData);
0016         card.validFrom = JsonLd.readQDateTime(code, 'validFrom');
0017         card.validUntil = JsonLd.readQDateTime(code, 'validUntil');
0018         return card.programName != undefined ? card : undefined;
0019     }
0020 
0021     // decode 118199 vendor block
0022     let res = node.result[0];
0023     const block = code.block("118199");
0024     const json = JSON.parse(block.contentText);
0025     if (!res.reservationFor.trainNumber)
0026         res.reservationFor.trainNumber = json["Z"];
0027 
0028     return res;
0029 }
0030 
0031 function parsePage(pdf, node, triggerNode) {
0032     const details = pdf.pages[triggerNode.location].textInRect(0, 0.35, 0.65, 1);
0033     let reservations = [];
0034     let idx = 0;
0035     while (true) {
0036         const leg = details.substr(idx).match(/(\S.*\S)  +(\d\d\.\d\d\.\d{4}) +(\d\d:\d\d) +(.*)\n *(\S.*\S)  +(\d\d\.\d\d\.\d{4}) +(\d\d:\d\d)/);
0037         if (!leg)
0038             break;
0039         idx += leg.index + leg[0].length;
0040 
0041         let res = JsonLd.newTrainReservation();
0042         res.reservationFor.departureStation.name = leg[1];
0043         res.reservationFor.departureTime = JsonLd.toDateTime(leg[2] + ' ' + leg[3], 'dd.MM.yyyy hh:mm', 'de');
0044         res.reservationFor.trainNumber = leg[4];
0045         res.reservationFor.arrivalStation.name = leg[5];
0046         res.reservationFor.arrivalTime = JsonLd.toDateTime(leg[6] + ' ' + leg[7], 'dd.MM.yyyy hh:mm', 'de');
0047         res = JsonLd.apply(triggerNode.result[0], res);
0048         reservations.push(res);
0049     }
0050 
0051     if (reservations.length > 0)
0052         return reservations;
0053 
0054     // look for non-elided station names
0055     const text = pdf.pages[triggerNode.location].text;
0056     const legs = text.match(/VON +-> NACH.*\n.*? ([A-Z].*) +-> (.*?)  .*\n.*? ([\w\*].*) +-> (.*?)  /);
0057     if (!legs) { return triggerNode.result; }
0058 
0059     var res = triggerNode.parent.result[0];
0060     res.reservationFor.departureStation.name = legs[1];
0061     res.reservationFor.arrivalStation.name = legs[2];
0062     reservations.push(res);
0063     if (triggerNode.parent.result.length == 2) {
0064         var res = triggerNode.parent.result[1];
0065         res.reservationFor.departureStation.name = legs[3];
0066         res.reservationFor.arrivalStation.name = legs[4];
0067         reservations.push(res);
0068     }
0069 
0070     return reservations;
0071 }