Skip to content

Instantly share code, notes, and snippets.

@jeroenvollenbrock
Created July 6, 2020 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeroenvollenbrock/34eef237b9d928f9b2e4ee598606522b to your computer and use it in GitHub Desktop.
Save jeroenvollenbrock/34eef237b9d928f9b2e4ee598606522b to your computer and use it in GitHub Desktop.
//Usage: node simplemind2plantuml.js <path/to/smmx> > output.plantuml
//Requires fast-xml-parser and he npm modules
const xml = require('fast-xml-parser');
const he = require('he');
const fs = require('fs').promises;
function componentToHex(c) {
const hex = parseInt(c).toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
function traverse(root, prefix = '*') {
let flags = '';
if(root.style && root.style.fillcolor) {
const {r,g,b} = root.style.fillcolor;
flags += '['+rgbToHex(r,g,b)+']';
}
let text = root.text.replace(/\\N/g, '\n');
if(text.includes('\n')) {
text += ';';
flags += ':';
}
console.log(prefix + flags, text);
root.children.forEach(child => traverse(child, prefix+'*'));
}
fs.readFile(process.argv[2], 'utf-8').then(data => {
const mmdata = xml.parse(data, {
attributeNamePrefix: '',
ignoreAttributes: false,
attrValueProcessor: (val, attrName) => he.decode(val, {isAttributeValue: true}),//default is a=>a
});
const mindmap = mmdata['simplemind-mindmaps'].mindmap.topics.topic;
const lookup = new Map();
const root = mindmap.find(topic => topic.parent === '-1');
mindmap.forEach(topic => {
topic.children = [];
lookup.set(topic.id, topic);
});
mindmap.forEach(topic => {
if(topic.parent === '-1') return;
lookup.get(topic.parent).children.push(topic);
});
console.log('@startmindmap');
traverse(root);
console.log('@endmindmap');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment