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.

Automatic images gallery

With flickity and php you can realize an automatic images gallery, with automatic titles and (if they are present) metatags:

<div id="gallery" style="width: 50%; margin-left: auto; margin-right: auto; margin-bottom: 4%;">
<div class="carousel" title="cliccare su una immagine per ingrandirla" data-flickity='{ "cellAlign": "center", "contain": true, "fullscreen": true, "wrapAround": true, "lazyLoad": 1 }'>

<?php

$images = glob("your-images-path/*.jpg");
$flag=1;

foreach($images as $image)
{
  // Ricava il titolo dell'immagine (metatag o nome del file)
  $imagedescription = '';
  if (function_exists('exif_read_data')) {
    $exifData = exif_read_data($image);

    // Controlla se esiste un campo di commento o descrizione
    if (isset($exifData['ImageDescription'])) {
      $imagedescription = $exifData['ImageDescription']; // Titolo o descrizione nell'EXIF
    }
  }

  $imageTitle = '';

  // Verifica se la funzione IPTC è disponibile
  if (function_exists('iptcparse')) {
    // Ottieni i metadati IPTC dall'immagine
    $iptcData = iptcparse(@iptcembed('', $image)); // iptcembed recupera i metadati IPTC da un'immagine

    // Controlla se il campo "headline" (titolo) è presente nei metadati IPTC
    if (isset($iptcData[2])) {
      $imageTitle = $iptcData[2][0]; // Il campo 2 è "headline" nell'IPTC
      // Troncamento del titolo se troppo lungo
      $maxLength = 50; // Lunghezza massima del titolo
      if (strlen($imageTitle) > $maxLength) {
        $imageTitle = substr($imageTitle, 0, $maxLength) . '...';
      }
    }
  }

  // Se non c'è un titolo IPTC, usa il nome del file senza trattini
  if (empty($imageTitle)) {
    $imageTitle = pathinfo($image, PATHINFO_FILENAME); // Ottieni il nome del file senza estensione

    // Rimuovi trattini dal nome del file, se presenti
    $imageTitle = str_replace('-', ' ', $imageTitle);
  }


  echo '<div class="carousel-cell' .($flag?' active':''). '">';
  echo '<img title="'. $imagedescription .'" data-flickity-lazyload="'. $image .'" onclick="onClick(this)" />';
  echo "<span class='carusel-image-title'>$imageTitle</span>";
  echo '</div>';
  $flag=0;
}
?>

</div>
</div>

convert pdf to svg

A good website is Convertio: it can convert, for free, your pdf to an svg absolutely identical, from a graphical point of view, with your original pdf, and the text remain text.

But on the other hand the resulting svg file is much bigger than the original pdf, because it is not optimized at all.

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>

php include: no absolute path

To get include command working you should avoid an absolute path: i.g. not

http://somepath/somefile

But, i.g.:

../somepath/somefile

2) In localhost it would be possible this workaround:

$yourvariablepath = realpath($_SERVER["DOCUMENT_ROOT"] . "/a/path");

But this doesn’t work in remote.

3) Another workaround to include a content from a file in another absolute path could be the object tag:

<object data=”your-file-even-on-another-server” />

But an object in another page is an island, a stranger “guest”, without any sharing with the “host” page.

4) Another way (for short text) is to use javascript.

Regex : an alphabetic letter

To mean one letter, such as ‘a’ or ‘G’, you can use [A-z].

I.g. if you want find any occurrence of strings such as 7956A, 5476H, 3865V, you can use

\d\d\d\d[A-z] 

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")}