automatize your website

Recently I managed to make some new improvements about website automation

After the automation of gallery, in a previous post, I did this other automation (with php):

list php pages with a given name

With the following code, you can list (and link) all webages php files containing in its filename the variable you can set:

<?php
echo "<h3>testi on-line di $autore</h3>";
echo "<ul class=\"flexmenu\">\n";

// Escape special characters in $autore to use safely in glob pattern
$phpfiles = glob("$path/*" . preg_quote($autore_di, '/') . "*.php"); // Get all PHP files with $autore in the name

foreach ($phpfiles as $phpfile) {
    $contents = file_get_contents($phpfile);

    preg_match('/\$title\s*=\s*"(.*?)";/', $contents, $titleMatch);
    preg_match('/\$subtitle\s*=\s*"(.*?)";/', $contents, $subtitleMatch);
    preg_match('/\$description\s*=\s*"(.*?)";/', $contents, $descriptionMatch);
    preg_match('/\$order\s*=\s*"(.*?)";/', $contents, $orderMatch);

    $title = $titleMatch[1] ?? 'No title';
    $subtitle = $subtitleMatch[1] ?? 'No subtitle';
    $order = $orderMatch[1] ?? '';
    $description = $descriptionMatch[1] ?? 'No description';

    // Correctly extract the filename
    $filename = $phpfile; // Use basename on $phpfile directly

    echo " <li role='menuitem' style=\"order:$order\"><a href=\"{$filename}\">{$title}</a>, <i>{$subtitle}</i>: {$description}</li>\n";
}

echo "</ul>";


echo "<h3>testi on-line su $autore</h3>";
echo "<ul class=\"flexmenu\">\n";

// Escape special characters in $autore to use safely in glob pattern
$phpfiles = glob("$path/*" . preg_quote($autore_su, '/') . "*.php"); // Get all PHP files with $autore in the name

foreach ($phpfiles as $phpfile) {
    $contents = file_get_contents($phpfile);

    preg_match('/\$title\s*=\s*"(.*?)";/', $contents, $titleMatch);
    preg_match('/\$subtitle\s*=\s*"(.*?)";/', $contents, $subtitleMatch);
    preg_match('/\$description\s*=\s*"(.*?)";/', $contents, $descriptionMatch);
    preg_match('/\$order\s*=\s*"(.*?)";/', $contents, $orderMatch);

    $title = $titleMatch[1] ?? 'No title';
    $subtitle = $subtitleMatch[1] ?? 'No subtitle';
    $order = $orderMatch[1] ?? '';
    $description = $descriptionMatch[1] ?? 'No description';

    // Correctly extract the filename
    $filename = $phpfile; // Use basename on $phpfile directly

    echo " <li role='menuitem' style=\"order:$order\"><a href=\"{$filename}\">{$title}</a>, <i>{$subtitle}</i>: {$description}</li>\n";
}

echo "</ul>";
?>

You can call that code in a webpage, with something like this:

<?php
/*files beginning with i must be uppercase */
$path="../testi";// your path
$autore="Aristotele";
$autore_di="Aristotele[di]";//texts of $autore
$autore_su="Aristotele[su]";//texts about $autore
include "$root/lista-files-testi-[autore]-online-con-metatags.inc";
?>

automatically list files in a website menu

You can create an automatic menu list of your files in a given folder, and in a folder with the same name of one file (without extension, of course). With a code like the following:

<?php
$phpfiles = glob("{$root}/$menupath/[^index]*.php");

foreach ($phpfiles as $phpfile) {
  $contents = file_get_contents($phpfile);

  // Match $order
  preg_match('/\$order\s*=\s*"(.*?)";/', $contents, $orderMatches);

  // Match $title
  preg_match('/\$title\s*=\s*"(.*?)";/', $contents, $titleMatches);

  // Assign the values
  $order = $orderMatches[1] ?? null;
  $title1 = $titleMatches[1] ?? null;

  $filename1 = basename($phpfile); // Get only the file name
  $filename = pathinfo($phpfile, PATHINFO_FILENAME);
  $filename = str_replace('-', ' ', $filename); // Replace hyphens with spaces in the filename
  $filename = ucfirst($filename); // Optional: Capitalize the first letter for better readability
  $link = "$root/$menupath/{$filename1}"; // Create the relative link

  // Start the list item for the menu
  echo "<li role='menuitem' style=\"order:$order\"><a href=\"{$link}\">{$title1}</a>";

  // Check if a sub-menu is required (look for a folder matching the filename)
  $folder = "{$root}/$menupath/{$filename}";
  if (is_dir($folder)) {
    // If the subfolder exists, generate the sublist automatically
    $subfiles = glob("{$folder}/[^index]*.php");
    if (count($subfiles) > 0) {
      echo "<ul class='flexmenu'>"; // Start sublist
      foreach ($subfiles as $subfile) {
        $subfileName = basename($subfile);
        $subfileTitle = pathinfo($subfile, PATHINFO_FILENAME);

        // Match $order for subfiles
        $subfileContents = file_get_contents($subfile);
        preg_match('/\$order\s*=\s*"(.*?)";/', $subfileContents, $subfileOrderMatches);
        $subfileOrder = $subfileOrderMatches[1] ?? null;

        // Remove hyphens and add colon after the main part of the title
        $subfileTitle = str_replace('-', ' ', $subfileTitle);

        // Separate the main part (before the colon) and the rest of the title
        $parts = explode(' ', $subfileTitle, 2); // Split into two parts: main and description
        $mainPart = $parts[0]; // This is the first word (e.g., "Aristotele")
        $description = isset($parts[1]) ? $parts[1] : ''; // Everything after the first space (e.g., "l etica")

        // Combine the main part with colon and the description
        $formattedTitle = ucfirst($mainPart) . ": " . $description;

        $subfileLink = "$root/$menupath/{$filename}/{$subfileName}";
        echo "<li style=\"order:$subfileOrder\"><a href=\"{$subfileLink}\">{$formattedTitle}</a></li>";
      }
      echo "</ul>"; // End sublist
    }
  }
  // End the list item for the menu
  echo "</li>\n";
}
?>

That you can call in a place of the whole menu with a code like this:

 $menupath="your/relative-path";
 include "menu-automatic.inc"; 

Where menu-automatic.inc is the name of the file containing the above code.

Regex replace short paragraph

To replace a paragraph <p> of less than x characters (30 in the example below) with a title (<h3> in the same example), you can use this regex code

search: <p>(.{0,29})</p>
replace: <h3>\1</h3>

Kate snippets code problem

Snippets in Kate are very useful, but a new snippet can cause a crash if you don’t pay attention to some symbols, such as “=”, or ” ” “. You have to “escape” this kind of symbols in the code of a snippet. To escape something, remind, you have to use “\“. I.g. to escape ", write \".

I.g. this code is not correct

${rangeCommand("<span lang='grc'>%%1</span>", "greco")}

While this one is correct
${rangeCommand("<span lang\='grc'>%%1</span>", "greco")}