# HG changeset patch # User Matti Hamalainen # Date 1709910001 -7200 # Node ID be34110271ed3c82c79e5abfc576dfd433e6ae59 # Parent 82258837014911f44434121b1b14d8c4a8c60fa6 Improve reformatter. diff -r 822588370149 -r be34110271ed src/reformat_wizards.php --- a/src/reformat_wizards.php Fri Mar 08 15:29:08 2024 +0200 +++ b/src/reformat_wizards.php Fri Mar 08 17:00:01 2024 +0200 @@ -5,21 +5,24 @@ function mpWriteWizInfoFile($filename, &$table) { + // "Categorize" the entries based on the data they have $categories = []; - foreach ($table as $name => $data) { $score = 0; if (isset($data["countries"])) $score++; - if (isset($data["desc"])) $score++; + if (isset($data["homeURL"])) $score++; if (isset($data["homeURL"])) $score++; + if (isset($data["desc"])) $score = 5; $categories[$score][$name] = $data; } + // Sort categories descendingly krsort($categories, SORT_NUMERIC); + // File header if (($file = @fopen($filename, "w")) === false) return FALSE; @@ -29,32 +32,51 @@ foreach ($categories as $id => $cat) { - if ($id == 0) - ksort($cat, SORT_STRING); + // Sort each category alphabetically + ksort($cat, SORT_STRING); + // Output each entry in this category foreach ($cat as $name => $data) { + $str = $name.";". + (isset($data["countries"]) ? implode("|", $data["countries"]) : "").";". + (isset($data["homeURL"]) ? $data["homeURL"] : "").";". + (isset($data["imageURL"]) ? $data["imageURL"] : "").";"; + + // Entries with some information are formatted differently if ($id > 0) { if (isset($data["desc"]) && strlen($data["desc"]) > 0) - $str = "\n\t".wordwrap($data["desc"], 68, "\n\t", false); - else - $str = ""; + { + // If we have a description, we format it via black magic: + // add linefeeds to certain elements ([p|br]) and then + // run through wordwrap(). Split to lines and indent each + // line with tab. Combine again. \:D\ + $str .= "\n". + rtrim( + implode("\n", array_map( + function ($item) + { + return "\t".$item; + }, + explode("\n", wordwrap( + preg_replace("#\s*\[(p|br)\]\s*#", "\n[\\1]\n", $data["desc"]), + 68, "\n") + )))); + } $str .= "\n\t\$\n\n"; } else { - $str = "\$\n"; + $str .= "\$\n"; } - fwrite($file, - $name.";". - (isset($data["countries"]) ? implode("|", $data["countries"]) : "").";". - (isset($data["homeURL"]) ? $data["homeURL"] : "").";". - (isset($data["imageURL"]) ? $data["imageURL"] : "").";".$str); + fwrite($file, $str); } - fwrite($file, "\n#########################################################\n\n"); + + fwrite($file, + "\n#########################################################\n\n"); } fclose($file);