Add web version

This commit is contained in:
Rick 2020-10-17 15:25:06 +02:00
parent 5611a930e1
commit 296d637914
Signed by: Rick
GPG key ID: 9570A7DB7CB2F436
3 changed files with 135 additions and 0 deletions

26
changeDom.js Normal file
View file

@ -0,0 +1,26 @@
/**
* Functions to change the output textarea and get the input textarea
*
* @author rick@gnous.eu
* @licence GPL3
*/
/**
* Set a text in a textarea with id output
*
* @param {string} outputText - text will be set in textarea
*/
function setOutputText(outputText) {
let textArea = document.getElementById("output");
textArea.value = outputText;
}
/**
* Parse the conf in the textarea with id input and print it in
* textarea with id output
*/
function letsParse() {
let textAreaValue = document.getElementById("input").value;
let parsedText = parser.parseTxt(textAreaValue);
setOutputText(parsedText);
}

19
index.html Normal file
View file

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Parser Juniper</title>
</head>
<body>
<form action="">
<textarea id="input" name="inputConf" cols="30" rows="10"></textarea>
<textarea id="output" name="outputConf" cols="30" rows="10" disabled></textarea>
<input type="button" onClick="letsParse()" value="Parse !">
</form>
<script src="parser.js"></script>
<script src="changeDom.js"></script>
<h2>Informations</h2>
</body>
</html>

90
parser.js Normal file
View file

@ -0,0 +1,90 @@
/**
* Parse a Juniper conf into a series of set commands
*
* Tim Price writes the parser in PHP (https://github.com/pgnuta/juniper-config-to-set)
* and I just rewrite it in JS with HTML frontend.
*
* @author Tim Price | rick@gnous.eu
* @licence GPL3
*/
class ParserJuniper {
constructor() {
this.tree = [];
}
resetTree() {
this.tree = [];
}
/**
* Concatenate all item of the tree
*
* @return {String} the string with all items
*/
printTree() {
let ret = "";
this.tree.forEach(function(item, index, array) {
ret = item + ret;
});
return ret;
}
/**
* Parse a line of juniper conf
*
* @param {String} line - line of juniper conf
* @return {String} a set command for line or an empty String if its comment
*/
parse(line) {
let ret = "";
line = line.trim();
if (line.charAt(0) !== '#') {
if (line.includes('#')) {
line = line.split('#')[0].trim();
}
let idEndline = line.length - 1;
if (line.charAt(idEndline) === ';') {
line = line.slice(0, -1);
if (this.tree.length === 0) {
ret = "set " + line;
} else {
ret = "set " + this.printTree() + line;
}
}
if (line.charAt(idEndline) === '{') {
line = line.slice(0, -1);
this.tree.unshift(line);
}
if (line.charAt(idEndline) === '}') {
this.tree.shift();
}
}
return ret;
}
/**
* Parse all lines of the text
*
* @param {String} text - juniper conf
* @return {String} series of set commands
*/
parseTxt(text) {
this.resetTree();
let ret = "";
let lineConf;
text = text.split("\n");
for(let i = 0; i < text.length; i++) {
lineConf = this.parse(text[i]);
if (lineConf !== "") {
ret += lineConf + "\n";
}
}
return ret;
}
}
const parser = new ParserJuniper();