Skip to content

Instantly share code, notes, and snippets.

@shellcatt
Created February 28, 2023 04:08
Show Gist options
  • Save shellcatt/682d44d9649eebe599a3d95c835dc684 to your computer and use it in GitHub Desktop.
Save shellcatt/682d44d9649eebe599a3d95c835dc684 to your computer and use it in GitHub Desktop.
FreeMind nodes expander by keyword match
<?php
// Define an array of keywords to search for ... somehow
$keywords = array('term1', 'term2', 'term3');
// Define input & output
$input_file = 'input.mm';
// $output_file = 'output.mm';
// Load the FreeMind XML file
$doc = new DOMDocument();
$doc->load($input_file);
// Find all top-level nodes
$nodes = $doc->getElementsByTagName('node');
// Call expandNode on each top-level node
foreach ($nodes as $node) {
traverseNode($node, $keywords);
}
// Output modified XML file
echo $doc->saveXML();
// Or save it
// $doc->saveXML($output_file);
##
## Functions
##
// Expand all parent nodes
function traverseNode(&$node, &$keywords) {
if (matchNode($node, $keywords)) {
// Recursively expand this and all parent nodes
expandNode($node);
}
// Recursively traverse child nodes
foreach ($node->childNodes as $child) {
if ($child->nodeName === 'node') {
traverseNode($child, $keywords);
}
// else {
// $node->setAttribute('FOLDED', 'true');
// }
}
}
function expandNode(&$node) {
$node->setAttribute('FOLDED', 'false');
if ($node->parentNode && $node->parentNode->nodeName === 'node') {
expandNode($node->parentNode);
}
}
// Check if any of the keywords are found in the node's text
function matchNode(&$node, &$keywords) {
foreach ($keywords as $keyword) {
if (stripos($node->getAttribute('TEXT'), $keyword) !== false) {
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment