Wicri:Dilib source, module Nlm, commande NlmPmc2Wicri
De Wicri Outils
Code source
/* -*- coding: utf-8 -*- */
/*
NlmPmc2Wicri.c
*/
#include <stdio.h> /* for printf */
#include <stdlib.h> /* for exit */
#include <getopt.h>
#include "SxmlNode.h"
void proceedText(SxmlNode *textNode)
{
SxmlNode *elemText;
SxmlReset (textNode);
while ((elemText=SxmlNextNode(textNode)))
{
if (SxmlIsText(elemText))
{
SxmlPrint(elemText);
continue;
}
if(SxmlNodeHasName(elemText,"sup"))
{
printf ("<sup>");
proceedText(elemText);
printf ("</sup>");
continue;
}
if(SxmlNodeHasName(elemText,"xref"))
{
char *rid;
rid=SxmlGetAttribute(elemText,"rid");
printf ("<span id=\"pmc xref %s\">[[#pmc %s|", rid, rid);
proceedText(elemText);
printf ("]]</span>");
continue;
}
}
}
void proceedTitle(SxmlNode *titleNode, int level)
{
switch(level)
{
case 1: printf("=="); break;
case 2: printf("==="); break;
case 3: printf("===="); break;
}
proceedText(titleNode);
switch(level)
{
case 1: printf("==\n"); break;
case 2: printf("===\n"); break;
case 3: printf("====\n"); break;
}
}
void proceedP(SxmlNode *parNode)
{
proceedText(parNode);
printf("\n\n");
}
void proceedSec(SxmlNode *secNode, int level)
{
SxmlNode *titleNode;
SxmlNode *partSec;
int tagSec1;
tagSec1=0;
SxmlReset (secNode);
while ((partSec=SxmlNextNode(secNode)))
{
if(SxmlNodeHasName(partSec,"title"))
{
proceedTitle(partSec, level);
continue;
}
if (level==1)
{
if (tagSec1==0)
{
printf ("{{Begin article body}}\n");
tagSec1=1;
}
}
if(SxmlNodeHasName(partSec,"sec"))
{
proceedSec(partSec, level+1);
continue;
}
if(SxmlNodeHasName(partSec,"p"))
{
proceedP(partSec);
continue;
}
}
if (tagSec1==1) printf ("{{End article body}}\n");
}
void proceedBody(SxmlNode *bodyNode)
{
SxmlNode *bodyNodeItem;
SxmlReset (bodyNode);
while ((bodyNodeItem=SxmlNextNode(bodyNode)))
{
if(SxmlNodeHasName(bodyNodeItem,"sec")) proceedSec(bodyNodeItem, 1);
}
}
void proceedRef(SxmlNode *refItem)
{
char *id;
SxmlNode *mixedCit;
SxmlNode *refItemItem;
id=SxmlGetAttribute(refItem, "id");
if (!id)return;
mixedCit=SxmlGetFirstChildByTagName(refItem, "mixed-citation");
if (!mixedCit)return;
printf("#");
printf("<span id=\"pmc %s\">[[#pmc xref %s|↑]]</span>", id, id);
SxmlReset(mixedCit);
while ((refItemItem=SxmlNextNode(mixedCit)))
{
if (SxmlNodeHasName(refItemItem, "name"))
{
SxmlNode *surnameNode;
SxmlNode *givenNameNode;
char *surname;
char *givenName;
surnameNode=SxmlGetFirstChildByTagName(refItemItem,"surname");
if (!surnameNode)continue;
surname =SxmlLeafText(surnameNode);
givenNameNode=SxmlGetFirstChildByTagName(refItemItem,"given-names");
if (!givenNameNode)continue;
givenName=SxmlLeafText(givenNameNode);
printf ("[[Has cited author::%s %s|%s %s]]", givenName, surname, surname, givenName);
if (SxmlGetNextSiblingByTagName(refItemItem, "name")) printf(", ");
else printf(". ");
continue;
}
if (SxmlNodeHasName(refItemItem, "article-title"))
{
printf("%s. ", SxmlLeafText(refItemItem));
continue;
}
if (SxmlNodeHasName(refItemItem, "source"))
{
printf("''%s'', ", SxmlLeafText(refItemItem));
continue;
}
}
printf("\n");
}
void proceedRefList(SxmlNode *refList)
{
SxmlNode *refItem;
SxmlReset(refList);
while((refItem=SxmlNextNode(refList)))
{
if (SxmlNodeHasName(refItem, "ref"))
proceedRef(refItem);
}
}
void proceedBack(SxmlNode *backNode)
{
SxmlNode *backNodeItem;
SxmlReset (backNode);
while ((backNodeItem=SxmlNextNode(backNode)))
{
if(SxmlNodeHasName(backNodeItem,"sec"))
{
proceedSec(backNodeItem, 1);
continue;
}
if(SxmlNodeHasName(backNodeItem,"ref-list"))
{
printf("==References==\n");
printf ("{{Begin article body}}\n");
proceedRefList(backNodeItem);
printf ("{{End article body}}\n");
}
}
}
void proceedArticle(SxmlNode *articleNode)
{
SxmlNode *bodyNode;
SxmlNode *backNode;
bodyNode=SxmlGetFirstChildByTagName(articleNode, "body");
if (bodyNode) proceedBody(bodyNode);
backNode=SxmlGetFirstChildByTagName(articleNode, "back");
if (backNode) proceedBack(backNode);
}
int main(int argc, char **argv)
{
SxmlNode *docInput;
while ((docInput=SxmlInputNextDocumentElement()))
{
if(SxmlNodeHasName(docInput,"article"))
{
proceedArticle(docInput);
}
}
}
Makefile
A insérer dans $DILIB_IMPORT/Nlm/Nlm.mk
$(DILIB)/bin/NlmPmc2Wicri \
$(DILIB)/bin/NlmPmc2Wicri: $(DILIB_IMPORT)/Nlm/NlmPmc2Wicri.c \
$(DILIB_TARGET)/Date/SgmlPath.lib
:$(CC) $(DILIB_IMPORT)/Nlm/NlmPmc2Wicri.c -Wall -I$(DILIB)/include \
$(DILIB)/lib/libDilib.a -o $@
Mise en œuvre
La suite de commande ci-dessous range dans un répertoire importNlm le fichier NlmPmc2Wicri.c et les mises à jour de makefile dans importNlm/NlmUpdate.mk.
WicriGetPage -l wicri-outils.fr -p "Wicri:Dilib source, module Nlm, commande NlmPmc2Wicri" \
| MediaWikiExtractSources -w | HfdStoreFile
- Simple test
Pour tester la commande en local, faire :
gcc importNlm/NlmPmc2Wicri.c $DILIB_CC -o importNlm/NlmPmc2Wicri
cat myPmc_resultFile.xml \
|NlmPmc2Sxml \
| Utf8FromHexEntity \
| SxmlUnIndent \
| importNlm/NlmPmc2Wicri