Skip to content

Instantly share code, notes, and snippets.

@StoneCypher
Created December 3, 2014 00:09
Show Gist options
  • Select an option

  • Save StoneCypher/5dee178e9fbfda26ea08 to your computer and use it in GitHub Desktop.

Select an option

Save StoneCypher/5dee178e9fbfda26ea08 to your computer and use it in GitHub Desktop.
<?php
// must already have cross-references resolved into a flat list
// see $wiki_line in exdata
function EncodeLine($line) {
$dump = '';
foreach ($line as $index => $row) {
$dump .= ($index === 0)? "Item\n" : "Item $index\n";
$dump .= " Type: " . $row['type'];
foreach ($row as $key => $val) { $dump .= " $key = $val\n"; }
}
return $dump;
}
// turns a tree encoding into a flat list
// should turn $wiki_line into $wiki_deep
function Linearize($tree, $base, $old_rewrite) {
$output = array();
$stash = array();
$rewrite = array();
$base_ofs = count($base);
$stash_ofs = 0;
$or_ofs = count($old_rewrite);
foreach ($tree as $index => $row) {
$output[] = $row;
$i = 0;
foreach ($row as $k => $v) {
if (is_array($v)) {
$stash[] = $v;
$rewrite[] = array('item' => $base_ofs + $i, 'k' => $k, 'si' => ($or_ofs + ($stash_ofs++));
}
++$i;
}
}
if (count($rewrite)) {
Linearize($stash, array_merge($base, $output), array_merge($old_rewrite, $rewrite));
} else {
$res = array_merge($base, $output);
foreach ($old_rewrite as $rule) {
$res[$rule['item']][$rule['k']] = 'Item (' . $rule['si'] . ')';
}
}
}
?>
@StoneCypher
Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment