Created
February 28, 2023 04:08
-
-
Save shellcatt/682d44d9649eebe599a3d95c835dc684 to your computer and use it in GitHub Desktop.
FreeMind nodes expander by keyword match
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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