Created
December 3, 2014 00:09
-
-
Save StoneCypher/5dee178e9fbfda26ea08 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| // 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'] . ')'; | |
| } | |
| } | |
| } | |
| ?> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see https://gist.github.com/StoneCypher/e33c3293dc5395c124b8 for source data