2cc5bf50e339e577fbbc137a0170d50776f0aec1
[alexxy/gromacs.git] / src / contrib / hrefify.c
1 /*
2  * $Id$
3  * 
4  *       This source code is part of
5  * 
6  *        G   R   O   M   A   C   S
7  * 
8  * GROningen MAchine for Chemical Simulations
9  * 
10  *               VERSION 2.0
11  * 
12  * Copyright (c) 1991-1999
13  * BIOSON Research Institute, Dept. of Biophysical Chemistry
14  * University of Groningen, The Netherlands
15  * 
16  * Please refer to:
17  * GROMACS: A message-passing parallel molecular dynamics implementation
18  * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19  * Comp. Phys. Comm. 91, 43-56 (1995)
20  * 
21  * Also check out our WWW page:
22  * http://md.chem.rug.nl/~gmx
23  * or e-mail to:
24  * gromacs@chem.rug.nl
25  * 
26  * And Hey:
27  * Great Red Oystrich Makes All Chemists Sane
28  */
29 static char *SRCID_hrefify_c = "$Id$";
30
31 #include <math.h>
32 #include <string.h>
33 #include "smalloc.h"
34 #include "typedefs.h"
35 #include "macros.h"
36 #include "string2.h"
37 #include "confio.h"
38 #include "vec.h"
39 #include "statutil.h"
40 #include "copyrite.h"
41 #include "pdbio.h"
42
43 bool isword(char c)
44 {
45   return (isalnum(c) || (c=='-') || (c=='_'));
46 }
47
48 char *strncasestr(char *line,char *str)
49 {
50   char *dum;
51
52   dum=line;
53   if (dum) {
54     while (strlen(dum) && strncasecmp(dum,str,strlen(str)))
55       dum++;
56     if (strlen(dum)==0)
57       dum=NULL;
58   }
59
60   return dum;
61 }
62
63 char *strstr_href(char *line,bool *bInHREF,int *i_dat,int n_dat,char **dat)
64 {
65   char *start,*found,*href;
66   bool bIn;
67   int i;
68
69   found=NULL;
70   *i_dat=-1;
71   bIn=*bInHREF;
72   start=line;
73   do {
74     if (bIn) {
75       while (strlen(start) && (strncasecmp(start,"</a",3) != 0))
76         start++;
77       if (strlen(start)>0) {
78         start+=3;
79         bIn=FALSE;
80       }
81     }
82     else {
83       href=strncasestr(start,"<a href");
84       if (href)
85         bIn=TRUE;
86       i=0;
87       while((i<n_dat) && !found) {
88         found=strncasestr(start,dat[i]);
89         if (found) 
90           if (href && (found>href))
91             found=NULL;
92           else 
93             if (((found!=start) && isword(found[-1])) || 
94                 isword(found[strlen(dat[i])])) 
95               found=NULL;
96             else
97               *i_dat=i;
98         i++;
99       }
100     }
101   } while (strlen(start) && !found && href);
102   *bInHREF=bIn;
103
104   return found;
105 }
106
107 int main(int argc, char *argv[])
108 {
109   static char *desc[] = {
110     "hrefify adds href's for all the words in the input file which are not",
111     "already hyperlinked and which appear in the file specified with the",
112     "option [TT]-l[tt].[PAR]",
113     "If the href's should call a script, text can be added",
114     "with the [TT]-t[tt] option."
115   };
116
117   int n;
118   
119   char **text,**str,line[1024],*ptr,*ref,
120     start[STRLEN],word[STRLEN],end[STRLEN];
121   int n_text,n_str,i_str;
122   bool bInHREF,bIn;
123   
124   FILE    *fp;
125   char    title[STRLEN];
126   int     i,l,n_repl;
127   t_filenm fnm[] = {
128     { efDAT, "-l", "links", ffLIBRD },
129   };
130 #define NFILE asize(fnm)
131   static char *in=NULL,*out=NULL,*excl=NULL,*link_text=NULL;
132   static bool peratom=FALSE;
133   t_pargs pa[] = {
134     { "-f", FALSE, etSTR, &in, "HTML input" },
135     { "-o", FALSE, etSTR, &out, "HTML output" },
136     { "-e", FALSE, etSTR, &excl, "Exclude a string from HREF's, "
137       "when this option is not set, the filename without path and extension "
138       "will be excluded from HREF's"},
139     { "-t", FALSE, etSTR, &link_text, "Insert a string in front of the "
140       "href file name, useful for scripts" }
141   };
142   
143   CopyRight(stderr,argv[0]);
144   parse_common_args(&argc,argv,0,FALSE,NFILE,fnm,asize(pa),pa,
145                     asize(desc),desc,0,NULL);
146
147   if (!in || !out)
148     fatal_error(0,"Input or output filename is not set");
149
150   n_text = get_file(in, &text);
151   fprintf(stderr,"Read %d lines from %s\n",n_text,in);
152
153   n_str=get_file(ftp2fn(efDAT,NFILE,fnm),&str);  
154   fprintf(stderr,"Read %d strings %s\n",n_str,ftp2fn(efDAT,NFILE,fnm));
155   if (!excl) {
156     for (i=strlen(in)-1; i>0 && in[i-1]!='/'; i--);
157     excl=strdup(in+i);
158     for(i=strlen(excl)-1; i>0 && (excl[i]!='.'); i--);
159     if (excl[i]=='.')
160       excl[i]='\0';
161   }
162   fprintf(stderr,"Excluding '%s' from references\n",excl);
163   for(l=0; l<n_str && strcasecmp(str[l],excl); l++);
164   if (l<n_str) {
165     for(i=l+1; i<n_str; i++)
166       str[i-1]=str[i];
167     n_str--;
168   }
169
170   if (!link_text)
171     link_text=strdup("\0");
172   else
173     fprintf(stderr,"Adding '%s' to href's\n",link_text);
174
175   fp=ffopen(out,"w");
176
177   n_repl=0;
178   i_str=-1;
179   bInHREF=FALSE;
180   for(l=0; l<n_text; l++) {
181     strcpy(line,text[l]);
182     do {
183       bIn=bInHREF;
184       ptr=strstr_href(line,&bIn,&i_str,n_str,str);
185       if (ptr) {
186         ref=ptr;
187         if ((ref!=line) && (ref[-1]=='.')) {
188           ref--;
189           while((ref>line) && isword(ref[-1]))
190             ref--;
191         }
192         strcpy(start,line);
193         start[ref-line]='\0';
194         strcpy(word,ref);
195         word[ptr-ref+strlen(str[i_str])]='\0';
196         strcpy(end,ptr+strlen(str[i_str]));
197         sprintf(line,"%s<a href=\"%s%s.html\">%s</a>%s\0",
198                 start,link_text,str[i_str],word,end);
199         fprintf(stderr,"line %d: %s\n",l+1,str[i_str]);
200         n_repl++;
201       }
202     } while (ptr);
203     bInHREF=bIn;
204     fprintf(fp,"%s\n",line);
205   }
206
207   fclose(fp);
208   
209   fprintf(stderr,"Added %d HTML references\n",n_repl);
210
211   return 0;
212 }
213