Générateur Rexx de fichiers RSS 2.0
Présentation
Voici un script sans fioritures, programmé en Rexx. Son but est de lire des données récupérées plus ou moins laborieusement dans un fichier texte, et de les sortir convenablement emballées dans un fichier RSS réglementaire. De fastidieuses séances de copier-coller sont ainsi épargnées au webmestre.
Quelques finitions doivent être encore apportées au fichier sorti (titre général, build et pubDate…). On peut les réaliser en un tournemain avec un bon éditeur (fonction rechercher/remplacer…). Mais d’autres méthodes sont encore plus efficaces. Les transformation XSLT, par exemple, ne sont pas pour les chiens : voir notre modeste contribution.
Exemple
La source d’origine :
Illzach 03/05/09 - CHAMP. D'ALSACE INTERCLUBS R1 et R2 ** 3000m SEF 1 20'18s00 SCHELCHER Isabelle Fc Mulhouse ** 5000m SEM 1 33'58s28 ELOY… http://bases.athle.com/asp.net/liste.aspx?frmbase=resultats&frmmode=1&frmespace=0&frmcompetition=031356 Haguenau 03/05/09 - Interclubs 1er Tour : N1 A B C ** 3000m SEF 1 13'28s93 DELON Laurene Pays de Colmar Athle JUF 2 15'38s13 … http://bases.athle.com/asp.net/liste.aspx?frmbase=resultats&frmmode=1&frmespace=0&frmcompetition=036372 Bordeaux 03/05/09 - Interclubs 1er Tour : N1 A C ** 3000m SEF 1 15'00s87 NARGUET Sandrine Sa Merignac, 2 15'14s95 MARCOU Amandine Sa Merignac… http://bases.athle.com/asp.net/liste.aspx?frmbase=resultats&frmmode=1&frmespace=0&frmcompetition=038771
Remarques :
- Chaque item est séparée des autres par au moins une ligne vide.
- La première ligne d’un item contient le titre (balise title).
Le résultat :
<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0"
xmlns="http://blogs.law.harvard.edu/tech/rss"
xmlns:dg="http://ns.dg77.net/XML/"
>
<dg:d:description>
<dg:cre></dg:cre>
<dg:upd></dg:upd>
</dg:description>
<channel>
<title></title>
<description></description>
<link></link>
<lastBuildDate></lastBuildDate>
<item>
<guid>01779</guid>
<title>Illzach 03/05/09 - CHAMP. D'ALSACE INTERCLUBS R1 et R2</title>
<description>
** 3000m SEF
1 20'18s00 SCHELCHER Isabelle Fc Mulhouse
** 5000m SEM
1 33'58s28 ELOY…
</description>
<link>http://bases.athle.com/asp.net/liste.aspx?frmbase=resultats&frmmode=1&frmespace=0&frmcompetition=031356</link>
<pubDate></pubDate>
</item>
<item>
<guid>1780</guid>
<title>Haguenau 03/05/09 - Interclubs 1er Tour : N1 A B C</title>
<description>
** 3000m SEF
1 13'28s93 DELON Laurene Pays de Colmar Athle JUF,
2 15'38s13 …
</description>
<link>http://bases.athle.com/asp.net/liste.aspx?frmbase=resultats&frmmode=1&frmespace=0&frmcompetition=036372</link>
<pubDate></pubDate>
</item>
<item>
<guid>1781</guid>
<title>Bordeaux 03/05/09 - Interclubs 1er Tour : N1 A C</title>
<description>
** 3000m SEF
1 15'00s87 NARGUET Sandrine Sa Merignac,
2 15'14s95 MARCOU Amandine Sa Merignac…
</description>
<link>http://bases.athle.com/asp.net/liste.aspx?frmbase=resultats&frmmode=1&frmespace=0&frmcompetition=038771</link>
<pubDate></pubDate>
</item>
</channel>
</rss>
Le programme
/* Met dans un fichier RSS 2.0 le contenu d'un fichier texte */
/* Pour chaque item :
- Au moins une ligne vide entre chaque item
- 1ere ligne : titre
- ligne(s) intermediaire(s) : description
- derniere ligne : lien
*/
say " Fichier lu ? " ; pull entree
say " Fichier sorti ? " ; pull sortie
say " Premier numero a utiliser ? " ; pull numero
if sortie="" then exit
OKF = 0 /* 1=au moins une ligne non vide */
ITEM = 0 /* 1=sortie d'un item commencee */
TITR = 0 /* 1=le titre de l'item est sorti */
DESCR = 0 /* 1=sortie description commencee */
DO while lines(entree)>0
ligne = linein(entree)
parse var ligne 1 zontest 2 /* 1er caractere de la ligne */
if zontest=" " then do /* ligne blanche */
if ITEM==1 then call FINITM
end
else do
if OKF==0 then call DEB /* 1ere sortie : mettre les entetes */
ITEM = 1
if TITR = 0 then do /* 1ere ligne item doit contenir le titre */
call lineout sortie, '<item>'
if numero \= 0 then do
call lineout sortie, ' <guid>'numero'</guid>'
numero = numero + 1
end
call lineout sortie, ' <title>'ligne'</title>'
TITR = 1
end
else do
parse var ligne 1 zontest 8
if zontest == 'http://' then do
if DESCR == 1 then call FDESC
/* remplace les "&" par l'entite correspondante */
call NETTOIE '&', '&'
call lineout sortie, ' <link>'ligne'</link>'
end
else do
if DESCR == 0 then call lineout sortie, ' <description>'
DESCR = 1
call NETTOIE '--', '- - ' /* "--" indesirable en XML */
call NETTOIE '<', '<'
call lineout sortie, ' 'ligne
end
end
end
END
if ITEM == 1 then call FINITM
if OKF == 1 then do
call lineout sortie, ' </channel>'
call lineout sortie, '</rss>'
end
exit
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
DEB:
OKF = 1
/* - Sort Tags de debut */
call lineout sortie, '<?xml version="1.0" encoding="iso-8859-1"?>'
call lineout sortie, '<rss version="2.0" '
call lineout sortie, 'xmlns="http://blogs.law.harvard.edu/tech/rss"'
call lineout sortie, 'xmlns:dg="http://exe.dg77.net/XML/" '
call lineout sortie, '>'
call lineout sortie, ' <dg:description>'
call lineout sortie, ' <dg:cre></dg:cre>'
call lineout sortie, ' <dg:upd></dg:upd>'
call lineout sortie, ' </dg:description>'
call lineout sortie, ' <channel>'
call lineout sortie, ' <title></title>'
call lineout sortie, ' <description></description>'
call lineout sortie, ' <link></link>'
call lineout sortie, ' <lastBuildDate></lastBuildDate>'
return
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Termine description */
FDESC:
call lineout sortie, ' </description>'
DESCR = 0
return
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Termine un item */
FINITM:
if DESCR==1 then call FDESC
call lineout sortie, ' <pubDate></pubDate>'
call lineout sortie, '</item>'
ITEM = 0
TITR = 0
return
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
NETTOIE:
parse arg chaine1, chaine2
apres = ligne
ligne = ''
avant = ''
do while apres \= ''
numcar = Pos(chaine1,apres)
if numcar==0 then do
ligne = ligne || apres
leave
end
numcar=numcar-1
if numcar > 0 then do
avant = avant || substr(apres,1,numcar) || chaine2
ligne = avant
end
numcar = numcar+2
apres = substr(apres,numcar)
end
return