[pmwiki-users] How good (and usefull) is AI at writing basic (or not so basic) PmWiki code ?
    ABClf 
    languefrancaise at gmail.com
       
    Tue May 13 05:56:49 PDT 2025
    
    
  
I was looking for something easy (I guess) which might be useful : get a
markup for extracting a line in some Group.MyPage according to regex
pattern.
I have asked ChatGPT (free version) to do the job ;
first attempt didn't work, but the scenario was credible (markup will be
like : (:ExtractLine Profil.JeanDoe "^Nom:" :))
First result given was :
<?php if (!defined('PmWiki')) exit();
Markup('ExtractLine', 'directives',
  '/\\(:ExtractLine\\s+(\\S+)\\s+([\'"]).+?\\2\\s*:\\)/e',
  "ExtractLineFromPage('$1', PSS('$2'))");
function ExtractLineFromPage($pagename, $pattern) {
    $page = ReadPage($pagename, READPAGE_CURRENT);
    if (!$page) return "Page non trouvée";
    $text = $page['text'];
    $lines = explode("\n", $text);
    foreach ($lines as $line) {
        if (preg_match($pattern, $line)) {
            return Keep($line);  // Keep() évite l'interprétation wiki
        }
    }
Saved as a cookbook and called in config, I am given an error by PmWiki
internal debugging code (message : code obsolete).
Ok. I copy the error message to ChatGPT and suggest him a link to
PmWiki/CustomMarkup.
One second later, I am given an updated code which works :
<?php if (!defined('PmWiki')) exit();
Markup('ExtractLine', 'directives',
  '/\\(:ExtractLine\\s+(\\S+)\\s+([\'"])(.+?)\\2\\s*:\\)/',
  'ExtractLineMarkup');
function ExtractLineMarkup($m) {
    $targetPage = $m[1];
    $pattern = $m[3]; // C'est le motif entre guillemets
    return ExtractLineFromPage($targetPage, $pattern);
}
function ExtractLineFromPage($pagename, $pattern) {
    $page = ReadPage($pagename, READPAGE_CURRENT);
    if (!$page) return "Page non trouvée";
    $text = $page['text'];
    $lines = explode("\n", $text);
    foreach ($lines as $line) {
        if (@preg_match("/$pattern/", $line)) {
            return Keep($line);  // Empêche l'interprétation du markup
        }
    }
    return "Aucune ligne ne correspond au motif.";
}
This code gets only the first line meeting the pattern.
Just ask if you need all the lines and wait one more second :
<?php if (!defined('PmWiki')) exit();
Markup('ExtractLine', 'directives',
  '/\\(:ExtractLine\\s+(\\S+)\\s+([\'"])(.+?)\\2\\s*:\\)/',
  'ExtractLineMarkup');
function ExtractLineMarkup($m) {
    $targetPage = $m[1];
    $pattern = $m[3]; // Motif brut sans délimiteurs
    return ExtractAllMatchingLines($targetPage, $pattern);
}
function ExtractAllMatchingLines($pagename, $pattern) {
    $page = ReadPage($pagename, READPAGE_CURRENT);
    if (!$page) return "Page non trouvée";
    $text = $page['text'];
    $lines = explode("\n", $text);
    $matches = [];
    foreach ($lines as $line) {
        if (@preg_match("/$pattern/", $line)) {
            $matches[] = Keep($line);  // Empêche le parsing wiki
        }
    }
    if (count($matches) > 0) {
        return implode("<br />\n", $matches);
    } else {
        return "Aucune ligne ne correspond au motif.";
    }
}
Might be easy for smart people, but I'm still so admirative. For the result
I get so quickly, and for the easy way to get it, fluently, in 3, 4
"conversational" steps. That's not something I would have expected 1 year
ago. One more second and I believe you get the extracted line cleaned up
from PmWiki markup if you need it.
My question is two open questions :
1. is the Ai generated code well done ? Ok, it works, but is it well
written ? secure enough ? doesn't it forget something important ? efficient
enough for speed ? (I believe it is, because here it is something very
basic).
2. do you play with AI tools like ChapGPT, Deepseek, Mistral for improving
your PmWiki ? What do you do (can we do something else than markups and
recipes ?) Does it work fine for you ? Which AI do you find is the best for
this kind of work ?
Gilles.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.pmichaud.com/pipermail/pmwiki-users/attachments/20250513/c10a83ba/attachment.html>
    
    
More information about the pmwiki-users
mailing list