Commit 932be110 authored by Giuseppe Tropea's avatar Giuseppe Tropea Committed by Giuseppe Tropea
Browse files

nodejs for parsing the json (created by the pandoc tool) that represents the docx of the API

parent 2497603d
Loading
Loading
Loading
Loading
+72 −0
Original line number Diff line number Diff line
const fs = require("fs");
const outputLogger = new console.Console(fs.createWriteStream("./output.log"));

let fullapi = fs.readFileSync('API.json', "utf8");
fullapi = JSON.parse(fullapi);

var i_am_inside = false;

var testcasemetadata = {};
for(let block of fullapi.blocks) {
    let type = block.t;
    let value = block.c;
    if (is_a_apigroup_header(type, value)) {
        if(value[2][0].t != "Str") {
            console.log("H2 group not Str!");
            process.exit();
        }
        testcasemetadata.group = value[2].slice(1).filter(el => el.t != "Space").map(el => el.c).join(" ");
        console.log("   H2 "+testcasemetadata.group);
    }
    if (is_a_apioperation_header(type, value)) {
        if(value[2][0].t != "Str") {
            console.log("H3 operation not Str!");
            exit();
        }
        testcasemetadata.operation = value[2].slice(1).filter(el => el.t != "Space").map(el => el.c).join(" ");
        console.log("         H3 "+testcasemetadata.group+" "+testcasemetadata.operation);
    }
    if (is_a_behaviour_header(type, value) && i_am_inside === false) {
        i_am_inside = true;
        outputLogger.log("entering "+value[2][0].c);
    }
    else if (i_am_inside === true && type === "Header") { // i get out once i reach whatever Header follows
        i_am_inside = false;
        outputLogger.log("exiting "+value[2][0].c);
    }
    else if (i_am_inside === true) {
        if (type === "BulletList") {
            outputLogger.log("..collecting BL of "+value.length);
        }
        else if (type === "Para") {
            outputLogger.log("..collecting PARA of "+value.length);
        }
        else {
            outputLogger.log("..not collecting "+type+": "+value);
        }
    }
}

// check if we are entering a level-4 header
function is_a_behaviour_header(type, value) {
    if (type === "Header" && value[0] === 4 && value[2][0].c.startsWith("5.") && value[2][2].c === "Behaviour") {
        return true;
    }
    return false;
}

// check if we are parsing a level-2 API group header
function is_a_apigroup_header(type, value) {
    if (type === "Header" && value[0] === 2 && value[2][0].c.startsWith("5.")) {
        return true;
    }
    return false;
}

// check if we are parsing a level-2 API group header
function is_a_apioperation_header(type, value) {
    if (type === "Header" && value[0] === 3 && value[2][0].c.startsWith("5.")) {
        return true;
    }
    return false;
}