Skip to content

Instantly share code, notes, and snippets.

@jeroenvollenbrock
Created July 6, 2020 12:55
Show Gist options
  • Select an option

  • Save jeroenvollenbrock/34eef237b9d928f9b2e4ee598606522b to your computer and use it in GitHub Desktop.

Select an option

Save jeroenvollenbrock/34eef237b9d928f9b2e4ee598606522b to your computer and use it in GitHub Desktop.

Revisions

  1. jeroenvollenbrock created this gist Jul 6, 2020.
    58 changes: 58 additions & 0 deletions simplemind2plantuml.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    //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');
    });