thank you flying spagetti monster. I finally got this extension working

This commit is contained in:
david 2023-02-03 20:31:12 -06:00
parent e304b29bbd
commit 9d2a550cc8
3 changed files with 309 additions and 91 deletions

185
main.js
View file

@ -28,6 +28,16 @@ __export(src_exports, {
}); });
module.exports = __toCommonJS(src_exports); module.exports = __toCommonJS(src_exports);
var import_obsidian = require("obsidian"); var import_obsidian = require("obsidian");
var nodeid = 1;
var edgeid = 5555;
var workingx = 0;
var workingy = 0;
var nodes = [];
var edges = [];
var lines = [];
var fromnode = -1;
var locations = {};
var visitslocs = {};
var DEFAULT_SETTINGS = { var DEFAULT_SETTINGS = {
mySetting: "default" mySetting: "default"
}; };
@ -40,75 +50,38 @@ var MyPlugin = class extends import_obsidian.Plugin {
ribbonIconEl.addClass("my-plugin-ribbon-class"); ribbonIconEl.addClass("my-plugin-ribbon-class");
const statusBarItemEl = this.addStatusBarItem(); const statusBarItemEl = this.addStatusBarItem();
statusBarItemEl.setText("Status Bar Text"); statusBarItemEl.setText("Status Bar Text");
this.addCommand({
id: "run-x86-parser",
name: "Parse x86 Codeblocks",
callback: () => {
var tmp = document.getElementsByClassName("HyperMD-codeblock-begin-bg");
for (var i = 0; i < tmp.length; i++) {
if (tmp[i].innerHTML.contains("x86")) {
tmp[i].classList.add("x86-instruction");
var currelement = tmp[i];
while (!currelement.classList.value.contains("HyperMD-codeblock-end")) {
currelement.classList.add("x86-instruction");
if (currelement.nextElementSibling != null) {
currelement = currelement.nextElementSibling;
if (currelement.classList.value.contains("HyperMD-codeblock-end")) {
break;
}
} else {
console.log("end of document reached before end of codeblock");
break;
}
var orightml = currelement.innerHTML;
var innerpart = orightml.split(">")[1].split("<")[0];
var firstspace = innerpart.indexOf(" ");
var innerpart = '<span class="cm-hmd-codeblock x86-instruction">' + innerpart.split(" ")[0] + "</span>" + innerpart.slice(firstspace, innerpart.length);
var testing = orightml.split(">")[0] + ">" + innerpart + "<" + orightml.split(">")[1].split("<")[1];
currelement.innerHTML = testing;
console.log(currelement.innerHTML);
}
console.log("Done with that x86 codeblock");
}
}
}
});
this.addCommand({ this.addCommand({
id: "create-flow-diagram", id: "create-flow-diagram",
name: "Convert x86 assembly into a flow diagram on a canvas", name: "Convert x86 assembly into a flow diagram on a canvas",
editorCallback: (editor, view) => { editorCallback: (editor, view) => {
console.log(editor.getSelection()); console.log(editor.getSelection());
console.log(this.app.vault.getName()); console.log(this.app.vault.getName());
let nodes = []; lines = [];
nodes = [];
nodes = [];
edges = [];
nodeid = 1;
edgeid = 5555;
workingx = 0;
workingy = 0;
var tmp = editor.getSelection().split("\n"); var tmp = editor.getSelection().split("\n");
let lines = [];
var nodeid = 1;
var workingx = 0;
var workingy = 0;
tmp.forEach((element) => { tmp.forEach((element) => {
if (element != "") { if (element != "") {
lines.push(element); lines.push(element);
} }
}); });
var currnode = "```\n";
lines.forEach((line, linenum) => { lines.forEach((line, linenum) => {
console.log(line); if (line.split("")[0] != " " && line.split("")[0] != " ") {
if (line.split("")[0] == " " || line.split("")[0] == " ") { var newkey = line.trim().split("#")[0].trim();
if (line.trim().split("")[0] == "j") { if (!locations[newkey]) {
currnode = currnode + line + "```"; locations[newkey] = linenum;
nodes.push({ "id": nodeid, "x": workingx, "y": workingy, "width": 250, "height": 300, "type": "text", "text": currnode }); visitslocs[newkey] = 0;
nodeid = nodeid + 1;
workingy = workingy + 350;
currnode = "```\n";
} else {
currnode = currnode + line + "\n";
} }
} else {
} }
}); });
var thing = '{ "nodes":' + JSON.stringify(nodes) + "}"; generatenodes(0, lines, fromnode);
console.log(thing); this.app.vault.create("/pleasegod.canvas", '{ "nodes":' + JSON.stringify(nodes) + ',"edges":' + JSON.stringify(edges) + "}");
this.app.vault.create("./testing.canvas", thing); console.log('{ "nodes":' + JSON.stringify(nodes) + ',"edges":' + JSON.stringify(edges) + "}");
} }
}); });
this.addCommand({ this.addCommand({
@ -125,9 +98,6 @@ var MyPlugin = class extends import_obsidian.Plugin {
} }
}); });
this.addSettingTab(new SampleSettingTab(this.app, this)); this.addSettingTab(new SampleSettingTab(this.app, this));
this.registerDomEvent(document, "click", (evt) => {
console.log("click1", evt);
});
this.registerInterval(window.setInterval(() => console.log("setInterval"), 5 * 60 * 1e3)); this.registerInterval(window.setInterval(() => console.log("setInterval"), 5 * 60 * 1e3));
} }
onunload() { onunload() {
@ -168,3 +138,106 @@ var SampleSettingTab = class extends import_obsidian.PluginSettingTab {
})); }));
} }
}; };
function generatenodes(linenum, text, fromnode2) {
console.log(locations);
var retarray = MakeNodeFromLineToNextJump(linenum, text, fromnode2);
var newnode = retarray[0];
fromnode2 = newnode["id"];
console.log("fromnode is " + fromnode2);
var whereto = retarray[1];
var wegood = nodeAlredyAdded(newnode);
if (wegood) {
return;
} else {
nodes.push(newnode);
}
console.log('{ "nodes":' + JSON.stringify(nodes) + "}");
console.log("need to jump here: " + whereto);
if (whereto == "fin") {
return;
}
generatenodes(locations[whereto[0]], text, fromnode2);
if (whereto.length == 2) {
console.log("Oh boy, we at a split");
console.log("going to line: " + whereto[1]);
generatenodes(whereto[1], text, fromnode2);
}
return;
}
function nodeAlredyAdded(checknode) {
var retval = false;
nodes.forEach((node) => {
if (checknode["startline"] == node["startline"] && checknode["endline"] == node["endline"]) {
retval = true;
}
});
return retval;
}
function MakeNodeFromLineToNextJump(linenum, text, fromnode2) {
var currnode = "```\n";
var i = linenum;
var newnode;
var jmploc;
var newedge = {};
console.log("curr line: " + i);
while (i < text.length) {
var line = text[i];
console.log("currently processing this line: " + line);
if (line.split("")[0] == " " || line.split("")[0] == " ") {
if (line.trim().split("")[0] == "j") {
currnode = currnode + line + "\n```";
newnode = { "id": nodeid, "x": workingx, "y": workingy, "width": 550, "height": 25 * currnode.split("\n").length, "type": "text", "text": currnode, "startline": linenum, "endline": i };
nodeid = nodeid + 1;
workingy = workingy + 350;
console.log(line.trim().slice(0, 3));
if (fromnode2 != -1) {
newedge = { "id": edgeid, "fromNode": fromnode2, "fromSide": "bottom", "toNode": newnode["id"], "toSide": "top", "label": "" };
edges.push(newedge);
edgeid = edgeid + 1;
}
if (line.trim().slice(0, 3) == "jmp") {
jmploc = [line.trim().slice(line.trim().indexOf(" ") + 1, line.length).split("#")[0].trim()];
} else {
jmploc = [line.trim().slice(line.trim().indexOf(" ") + 1, line.length).split("#")[0].trim(), i + 1];
}
i = text.length + 20;
} else {
currnode = currnode + line + "\n";
}
} else {
if (visitslocs[line.trim()] == 0) {
currnode = currnode + line + "\n";
visitslocs[line.trim()] = nodeid;
} else {
currnode = currnode + "\n```";
newnode = { "id": nodeid, "x": workingx, "y": workingy, "width": 550, "height": 25 * currnode.split("\n").length, "type": "text", "text": currnode, "startline": linenum, "endline": i };
workingy = workingy + 350;
if (fromnode2 != -1) {
newedge = { "id": edgeid, "fromNode": fromnode2, "fromSide": "bottom", "toNode": newnode["id"], "toSide": "top", "label": "" };
edges.push(newedge);
edgeid = edgeid + 1;
newedge = { "id": edgeid, "fromNode": nodeid, "fromSide": "bottom", "toNode": visitslocs[line.trim()], "toSide": "top", "label": "" };
edges.push(newedge);
edgeid = edgeid + 1;
}
nodeid = nodeid + 1;
i = text.length + 20;
jmploc = "fin";
}
}
i = i + 1;
}
if (i != text.length + 21) {
currnode = currnode + "```";
newnode = { "id": nodeid, "x": workingx, "y": workingy, "width": 550, "height": 25 * currnode.split("\n").length, "type": "text", "text": currnode, "startline": linenum, "endline": i };
jmploc = "fin";
nodeid = nodeid + 1;
workingy = workingy + 350;
if (fromnode2 != -1) {
newedge = { "id": edgeid, "fromNode": fromnode2, "fromSide": "bottom", "toNode": newnode["id"], "toSide": "top", "label": "" };
edges.push(newedge);
edgeid = edgeid + 1;
}
}
return [newnode, jmploc];
}

31
prototype.js vendored
View file

@ -25,6 +25,8 @@ var edges = [] //{"id":"d1e0d15da69178a9","fromNode":"4018052da21dde12","fromSid
var tmp = input.split("\n") var tmp = input.split("\n")
var lines = [] var lines = []
var fromnode = -1 var fromnode = -1
var locations = {}
var visitslocs = {}
//Recursive function to visit each node and generate the other nodes //Recursive function to visit each node and generate the other nodes
//Returns when it either hits the end of the assembly or returns to a node that has already been added //Returns when it either hits the end of the assembly or returns to a node that has already been added
function generatenodes(linenum, text, fromnode){ function generatenodes(linenum, text, fromnode){
@ -80,7 +82,7 @@ function MakeNodeFromLineToNextJump(linenum, text, fromnode) {
while(i < text.length){ while(i < text.length){
var line = text[i] var line = text[i]
console.log("currently processing this line: " + line) console.log("currently processing this line: " + line)
//if(line.split("")[0] == '\t' || line.split("")[0] == " "){ if(line.split("")[0] == '\t' || line.split("")[0] == " "){
if(line.trim().split("")[0] == 'j'){ if(line.trim().split("")[0] == 'j'){
currnode = currnode + line + "\n```" currnode = currnode + line + "\n```"
newnode = {"id":nodeid, "x": workingx, "y": workingy, "width": 550,"height": 25*currnode.split("\n").length, "type": "text", "text": currnode, "startline": linenum,"endline": i} newnode = {"id":nodeid, "x": workingx, "y": workingy, "width": 550,"height": 25*currnode.split("\n").length, "type": "text", "text": currnode, "startline": linenum,"endline": i}
@ -104,10 +106,30 @@ function MakeNodeFromLineToNextJump(linenum, text, fromnode) {
else{ else{
currnode = currnode + line + "\n" currnode = currnode + line + "\n"
} }
/*} }
else{ else{
//TODO: Handle location lines //TODO: Handle location lines
}*/ if(visitslocs[line.trim()] == 0){
currnode = currnode + line + "\n"
visitslocs[line.trim()] = nodeid
}
else{
currnode = currnode + "\n```"
newnode = {"id":nodeid, "x": workingx, "y": workingy, "width": 550,"height": 25*currnode.split("\n").length, "type": "text", "text": currnode, "startline": linenum,"endline": i}
workingy = workingy + 350
if(fromnode != -1){
var newedge = {"id":edgeid,"fromNode":fromnode,"fromSide":"bottom","toNode":newnode["id"],"toSide":"top","label":""}
edges.push(newedge)
edgeid = edgeid + 1
var newedge = {"id":edgeid,"fromNode":nodeid,"fromSide":"bottom","toNode":visitslocs[line.trim()],"toSide":"top","label":""}
edges.push(newedge)
edgeid = edgeid + 1
}
nodeid = nodeid + 1
i = text.length+20
jmploc = "fin"
}
}
i = i + 1 i = i + 1
} }
//We got to the end of the assembly //We got to the end of the assembly
@ -136,8 +158,6 @@ function starter() {
} }
}); });
locations = {}
lines.forEach((line,linenum) => { lines.forEach((line,linenum) => {
//Only look at lines that could be locations //Only look at lines that could be locations
if(line.split("")[0] != '\t' && line.split("")[0] != " "){ if(line.split("")[0] != '\t' && line.split("")[0] != " "){
@ -145,6 +165,7 @@ function starter() {
var newkey = line.trim().split("#")[0].trim() var newkey = line.trim().split("#")[0].trim()
if (!locations[newkey]) { if (!locations[newkey]) {
locations[newkey] = linenum; locations[newkey] = linenum;
visitslocs[newkey] = 0
} }
} }
}); });

View file

@ -1,6 +1,16 @@
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, Vault } from 'obsidian'; import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, Vault } from 'obsidian';
// Remember to rename these classes and interfaces! // Remember to rename these classes and interfaces!
var nodeid = 1;
var edgeid = 5555
var workingx = 0;
var workingy = 0;
var nodes: any[] = []
var edges: { id: number; fromNode: any; fromSide: string; toNode: number; toSide: string; label: string; }[] = [] //{"id":"d1e0d15da69178a9","fromNode":"4018052da21dde12","fromSide":"bottom","toNode":"0afaa4e14a75cfe1","toSide":"top","label":"false"}
var lines: string[] = []
var fromnode = -1
var locations: any = {}
var visitslocs: any = {}
interface MyPluginSettings { interface MyPluginSettings {
mySetting: string; mySetting: string;
@ -29,7 +39,7 @@ export default class MyPlugin extends Plugin {
statusBarItemEl.setText('Status Bar Text'); statusBarItemEl.setText('Status Bar Text');
// This adds a simple command that can be triggered anywhere // This adds a simple command that can be triggered anywhere
this.addCommand({ /*this.addCommand({
id: 'run-x86-parser', id: 'run-x86-parser',
name: 'Parse x86 Codeblocks', name: 'Parse x86 Codeblocks',
callback: () => { callback: () => {
@ -67,7 +77,7 @@ export default class MyPlugin extends Plugin {
} }
} }
} }
}); });*/
// This adds an editor command that can perform some operation on the current editor instance // This adds an editor command that can perform some operation on the current editor instance
this.addCommand({ this.addCommand({
id: 'create-flow-diagram', id: 'create-flow-diagram',
@ -75,41 +85,35 @@ export default class MyPlugin extends Plugin {
editorCallback: (editor: Editor, view: MarkdownView) => { editorCallback: (editor: Editor, view: MarkdownView) => {
console.log(editor.getSelection()); console.log(editor.getSelection());
console.log(this.app.vault.getName()); console.log(this.app.vault.getName());
let nodes: { id: number; x: number; y: number; width: number; height: number; type: string; text: string; }[] = [] lines = [];
nodes = [];
nodes = [];
edges = [];
nodeid = 1;
edgeid = 5555;
workingx = 0;
workingy = 0;
var tmp = editor.getSelection().split("\n") var tmp = editor.getSelection().split("\n")
let lines: string[] = []
var nodeid = 1;
var workingx = 0;
var workingy = 0;
tmp.forEach(element => { tmp.forEach(element => {
if(element != ""){ if(element != ""){
lines.push(element) lines.push(element)
} }
}); });
var currnode = "```\n"
lines.forEach((line,linenum) => { lines.forEach((line,linenum) => {
console.log(line) //Only look at lines that could be locations
if(line.split("")[0] == '\t' || line.split("")[0] == " "){ if(line.split("")[0] != '\t' && line.split("")[0] != " "){
if(line.trim().split("")[0] == 'j'){ //Cut out white space and comments (#)
currnode = currnode + line + "\n```" var newkey = line.trim().split("#")[0].trim()
nodes.push({"id":nodeid, "x": workingx, "y": workingy, "width": 250,"height": 300, "type": "text", "text": currnode}) if (!locations[newkey]) {
nodeid = nodeid + 1 locations[newkey] = linenum;
workingy = workingy + 350 visitslocs[newkey] = 0
currnode = "```\n"
} }
else{
currnode = currnode + line + "\n"
}
}
else{
//TODO: Handle location lines
} }
}); });
var thing = "{ \"nodes\":"+JSON.stringify(nodes) + "}" generatenodes(0,lines,fromnode)
console.log(thing) this.app.vault.create("/pleasegod.canvas","{ \"nodes\":"+JSON.stringify(nodes) + ",\"edges\":" + JSON.stringify(edges) + "}")
this.app.vault.create("./testing.canvas",thing) console.log("{ \"nodes\":"+JSON.stringify(nodes) + ",\"edges\":" + JSON.stringify(edges) + "}")
} }
}); });
// This adds a complex command that can check whether the current state of the app allows execution of the command // This adds a complex command that can check whether the current state of the app allows execution of the command
@ -137,9 +141,9 @@ export default class MyPlugin extends Plugin {
// If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin) // If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin)
// Using this function will automatically remove the event listener when this plugin is disabled. // Using this function will automatically remove the event listener when this plugin is disabled.
this.registerDomEvent(document, 'click', (evt: MouseEvent) => { //this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
console.log('click1', evt); // console.log('click1', evt);
}); //});
// When registering intervals, this function will automatically clear the interval when the plugin is disabled. // When registering intervals, this function will automatically clear the interval when the plugin is disabled.
this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000)); this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
@ -202,3 +206,123 @@ class SampleSettingTab extends PluginSettingTab {
})); }));
} }
} }
function generatenodes(linenum: any, text: any, fromnode: any){
console.log(locations)
var retarray = MakeNodeFromLineToNextJump(linenum, text, fromnode)
var newnode: any = retarray[0]
fromnode = newnode['id']
console.log("fromnode is " + fromnode)
var whereto: any = retarray[1]
//Check if node is already in list
var wegood = nodeAlredyAdded(newnode)
if(wegood){
return
}
else{
nodes.push(newnode)
}
console.log("{ \"nodes\":"+JSON.stringify(nodes) + "}")
console.log("need to jump here: " + whereto)
//We are at the end of the file
if(whereto == "fin"){
return
}
generatenodes(locations[whereto[0]], text, fromnode)
if(whereto.length == 2){
console.log("Oh boy, we at a split")
console.log("going to line: " + whereto[1])
generatenodes(whereto[1], text, fromnode)
}
return
}
//This function returns true if the node has already been added to the nodes array
function nodeAlredyAdded(checknode: any){
var retval = false
nodes.forEach(node => {
if(checknode["startline"] == node["startline"] && checknode["endline"] == node["endline"]){
retval = true
}
});
return retval
}
//This function takes a line number and the assembly and creates a node from that line to the next jump instruction
//It then returns the new node at retarray[0] and the location to jump to at retarray[1]
//TODO: FIgure out nodeid, x, and y values
function MakeNodeFromLineToNextJump(linenum: any, text: any, fromnode: any) {
var currnode = "```\n"
var i = linenum
var newnode
var jmploc
var newedge: any = {}
console.log("curr line: " + i)
while(i < text.length){
var line = text[i]
console.log("currently processing this line: " + line)
if(line.split("")[0] == '\t' || line.split("")[0] == " "){
if(line.trim().split("")[0] == 'j'){
currnode = currnode + line + "\n```"
newnode = {"id":nodeid, "x": workingx, "y": workingy, "width": 550,"height": 25*currnode.split("\n").length, "type": "text", "text": currnode, "startline": linenum,"endline": i}
nodeid = nodeid + 1
workingy = workingy + 350
//Only parse the first node
console.log(line.trim().slice(0,3))
if(fromnode != -1){
newedge = {"id":edgeid,"fromNode":fromnode,"fromSide":"bottom","toNode":newnode["id"],"toSide":"top","label":""}
edges.push(newedge)
edgeid = edgeid + 1
}
if(line.trim().slice(0,3) == 'jmp'){
jmploc = [line.trim().slice(line.trim().indexOf(" ")+1,line.length).split("#")[0].trim()]
}
else{
jmploc = [line.trim().slice(line.trim().indexOf(" ")+1,line.length).split("#")[0].trim(),i+1]
}
i = text.length+20
}
else{
currnode = currnode + line + "\n"
}
}
else{
//TODO: Handle location lines
if(visitslocs[line.trim()] == 0){
currnode = currnode + line + "\n"
visitslocs[line.trim()] = nodeid
}
else{
currnode = currnode + "\n```"
newnode = {"id":nodeid, "x": workingx, "y": workingy, "width": 550,"height": 25*currnode.split("\n").length, "type": "text", "text": currnode, "startline": linenum,"endline": i}
workingy = workingy + 350
if(fromnode != -1){
newedge = {"id":edgeid,"fromNode":fromnode,"fromSide":"bottom","toNode":newnode["id"],"toSide":"top","label":""}
edges.push(newedge)
edgeid = edgeid + 1
newedge = {"id":edgeid,"fromNode":nodeid,"fromSide":"bottom","toNode":visitslocs[line.trim()],"toSide":"top","label":""}
edges.push(newedge)
edgeid = edgeid + 1
}
nodeid = nodeid + 1
i = text.length+20
jmploc = "fin"
}
}
i = i + 1
}
//We got to the end of the assembly
if(i != text.length+21)
{
currnode = currnode + "```"
newnode = {"id":nodeid, "x": workingx, "y": workingy, "width": 550,"height": 25*currnode.split("\n").length, "type": "text", "text": currnode, "startline": linenum,"endline": i}
jmploc = "fin"
nodeid = nodeid + 1
workingy = workingy + 350
if(fromnode != -1){
newedge = {"id":edgeid,"fromNode":fromnode,"fromSide":"bottom","toNode":newnode["id"],"toSide":"top","label":""}
edges.push(newedge)
edgeid = edgeid + 1
}
}
return [newnode, jmploc]
}