changeset 2743:be34110271ed

Improve reformatter.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 08 Mar 2024 17:00:01 +0200
parents 822588370149
children 2b5001a8eddc
files src/reformat_wizards.php
diffstat 1 files changed, 36 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- 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);