Plist viewer operation added.

This commit is contained in:
n1073645 2019-11-22 15:39:43 +00:00
parent 63bb19d48d
commit 8e1e1d56ca

View File

@ -46,112 +46,66 @@ class PLISTViewer extends Operation {
*/ */
run(input, args) { run(input, args) {
const reserved = [["<string>","</string>",8], // Regexes are designed to transform the xml format into a reasonably more readable string format.
["<real>","</real>",6], input = input.slice(input.indexOf("<plist"))
["<integer>","</integer>",9], .replace(/<plist.+>/g, "plist => ")
["<date>","</date>", 6], .replace(/<dict>/g, "{")
["<data>","</data>",6], .replace(/<\/dict>/g, "}")
["<array>","</array>",7], .replace(/<array>/g, "[")
["<dict>","</dict>",6], .replace(/<\/array>/g, "]")
["<key>","</key>",5], .replace(/<key>.+<\/key>/g, m => `${m.slice(5, m.indexOf(/<\/key>/g)-5)}\t=> `)
["<false/>",false,8], .replace(/<real>.+<\/real>/g, m => `${m.slice(6, m.indexOf(/<\/real>/g)-6)}\n`)
["<true/>",true,7]]; .replace(/<string>.+<\/string>/g, m => `${m.slice(8, m.indexOf(/<\/string>/g)-8)}\n`)
.replace(/<integer>.+<\/integer>/g, m => `${m.slice(9, m.indexOf(/<\/integer>/g)-9)}\n`)
function the_viewer(input, dictionary_flag){ .replace(/<false\/>/g, m => "false")
var new_dict = new Array(); .replace(/<true\/>/g, m => "true")
var result = new Array(); .replace(/<\/plist>/g, "/plist")
var new_key = null; .replace(/<date>.+<\/date>/g, m => `${m.slice(6, m.indexOf(/<\/integer>/g)-6)}`)
while(dictionary_flag ? input.slice(0,7) != "</dict>" : input.slice(0,8) != "</array>"){ .replace(/<data>(\s|.)+?<\/data>/g, m => `${m.slice(6, m.indexOf(/<\/data>/g)-6)}`)
reserved.forEach( function (elem, index){ .replace(/[ \t\r\f\v]/g, "");
var element = elem[0];
var endelement = elem[1];
var length = elem[2];
let temp = input.slice(0,length);
if(temp == element){
input = input.slice(length);
if(temp == "<dict>"){
var returned = the_viewer(input, true);
input = returned[1];
if(new_key)
new_dict[new_key] = returned[0];
else
new_dict["plist"] = returned[0];
new_key = null;
}else if(temp == "<array>"){
var returned = the_viewer(input, false);
if(dictionary_flag)
new_dict[new_key] = returned[0];
else
result.push(returned[0]);
input = returned[1];
new_key = null;
}else if(temp == "<key>"){
var end = input.indexOf(endelement);
new_key = input.slice(0, end);
input = input.slice(end+length+1);
}else if(temp == "<true/>" || temp == "<false/>"){
new_dict[new_key] = endelement;
new_key = null;
}else{
var end = input.indexOf(endelement);
var toadd = input.slice(0, end);
if(temp == "<integer>")
toadd = parseInt(toadd);
else if(temp == "<real>")
toadd = parseFloat(toadd);
if(dictionary_flag){
new_dict[new_key] = toadd;
new_key = null;
}else{
result.push(toadd);
}
input = input.slice(end+length+1);
}
}
});
}
if(dictionary_flag){
input = input.slice(7);
return [new_dict, input];
}else{
input = input.slice(8);
return [result, input];
}
}
let result = ""; let result = "";
function print_it(input, depth) {
Object.keys(input).forEach((key, index) => { /**
if(typeof(input[key]) == "object") { * Formats the input after the regex has replaced all of the relevant parts.
result += (("\t".repeat(depth)) + key + ": {\n"); *
print_it(input[key], depth+1); * @param {array} input
result += (("\t".repeat(depth)) + "}\n"); * @param {number} depthCount
*/
function printIt(input, depthCount) {
if (!(input.length))
return;
// If the current position points at a larger dynamic structure.
if (input[0].indexOf("=>") !== -1) {
// If the LHS also points at a larger structure (nested plists in a dictionary).
if (input[1].indexOf("=>") !== -1) {
result += ("\t".repeat(depthCount)) + input[0].slice(0, -2) + " => " + input[1].slice(0, -2) + " =>\n";
} else { } else {
result += (("\t".repeat(depth)) + key + " : " + input[key] + "\n"); result += ("\t".repeat(depthCount)) + input[0].slice(0, -2) + " => " + input[1] + "\n";
} }
});
// Controls the tab depth for how many opening braces there have been.
if (input[1] === "{" || input[1] === "[") {
depthCount += 1;
}
input = input.slice(1);
} else {
// Controls the tab depth for how many closing braces there have been.
if (input[0] === "}" || input[0] === "]")
depthCount--;
// Has to be here since the formatting breaks otherwise.
result += ("\t".repeat(depthCount)) + input[0] + "\n";
if (input[0] === "{" || input[0] === "[")
depthCount++;
}
printIt(input.slice(1), depthCount);
} }
while (input.indexOf("<plist") !== -1){ input = input.split("\n").filter(e => e !== "");
input = input.replace(/<plist.+>/, "<dict>"); printIt(input, 0);
}
while (input.indexOf("</plist>") !== -1){
input = input.replace(/<\/plist>/, "</dict>");
}
console.log(input);
while(input.indexOf("\n") !== -1)
input = input.replace("\n", "");
while(input.indexOf("\t") !== -1)
input = input.replace("\t", "");
while(input.indexOf(" ") !== -1)
input = input.replace(" ", "");
console.log(input);
input = input.slice(input.indexOf("<dict>")+6);
//return input
var other = the_viewer(input, 1);
print_it(other[0],1);
result = "{\n" + result;
result += "}";
return result; return result;
} }
} }