GRASS GIS 7 Programmer's Manual  7.0.2(2015)-r00000
parser_html.c
Go to the documentation of this file.
1 
14 #include <stdio.h>
15 #include <string.h>
16 
17 #include <grass/gis.h>
18 #include <grass/glocale.h>
19 
20 #include "parser_local_proto.h"
21 
22 static void print_escaped_for_html(FILE *, const char *);
23 static void print_escaped_for_html_options(FILE *, const char *);
24 static void print_escaped_for_html_keywords(FILE * , const char *);
25 
29 void G__usage_html(void)
30 {
31  struct Option *opt;
32  struct Flag *flag;
33  const char *type;
34  int new_prompt = 0;
35 
36  new_prompt = G__uses_new_gisprompt();
37 
38  if (!st->pgm_name) /* v.dave && r.michael */
39  st->pgm_name = G_program_name();
40  if (!st->pgm_name)
41  st->pgm_name = "??";
42 
43  fprintf(stdout,
44  "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n");
45  fprintf(stdout, "<html>\n<head>\n");
46  fprintf(stdout, "<title>GRASS GIS manual: %s</title>\n", st->pgm_name);
47  fprintf(stdout,
48  "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n");
49  fprintf(stdout,
50  "<link rel=\"stylesheet\" href=\"grassdocs.css\" type=\"text/css\">\n");
51  fprintf(stdout, "</head>\n");
52  fprintf(stdout, "<body bgcolor=\"white\">\n");
53  fprintf(stdout, "<div id=\"container\">\n\n");
54  fprintf(stdout,
55  "<a href=\"index.html\"><img src=\"grass_logo.png\" alt=\"GRASS logo\"></a>\n");
56  fprintf(stdout, "<hr class=\"header\">\n\n");
57  fprintf(stdout, "<h2>%s</h2>\n", _("NAME"));
58  fprintf(stdout, "<em><b>%s</b></em> ", st->pgm_name);
59 
60  if (st->module_info.label || st->module_info.description)
61  fprintf(stdout, " - ");
62 
63  if (st->module_info.label)
64  fprintf(stdout, "%s<BR>\n", st->module_info.label);
65 
66  if (st->module_info.description)
67  fprintf(stdout, "%s\n", st->module_info.description);
68 
69 
70  fprintf(stdout, "<h2>%s</h2>\n", _("KEYWORDS"));
71  if (st->module_info.keywords) {
72  G__print_keywords(stdout, print_escaped_for_html_keywords);
73  fprintf(stdout, "\n");
74  }
75  fprintf(stdout, "<h2>%s</h2>\n", _("SYNOPSIS"));
76  fprintf(stdout, "<div id=\"name\"><b>%s</b><br></div>\n", st->pgm_name);
77  fprintf(stdout, "<b>%s --help</b><br>\n", st->pgm_name);
78 
79  fprintf(stdout, "<div id=\"synopsis\"><b>%s</b>", st->pgm_name);
80 
81  /* print short version first */
82  if (st->n_flags) {
83  flag = &st->first_flag;
84  fprintf(stdout, " [-<b>");
85  while (flag != NULL) {
86  fprintf(stdout, "%c", flag->key);
87  flag = flag->next_flag;
88  }
89  fprintf(stdout, "</b>] ");
90  }
91  else
92  fprintf(stdout, " ");
93 
94  if (st->n_opts) {
95  opt = &st->first_option;
96 
97  while (opt != NULL) {
98  if (opt->key_desc != NULL)
99  type = opt->key_desc;
100  else
101  switch (opt->type) {
102  case TYPE_INTEGER:
103  type = "integer";
104  break;
105  case TYPE_DOUBLE:
106  type = "float";
107  break;
108  case TYPE_STRING:
109  type = "string";
110  break;
111  default:
112  type = "string";
113  break;
114  }
115  if (!opt->required)
116  fprintf(stdout, " [");
117  fprintf(stdout, "<b>%s</b>=<em>%s</em>", opt->key, type);
118  if (opt->multiple) {
119  fprintf(stdout, "[,<i>%s</i>,...]", type);
120  }
121  if (!opt->required)
122  fprintf(stdout, "] ");
123 
124  opt = opt->next_opt;
125  fprintf(stdout, " ");
126  }
127  }
128  if (new_prompt)
129  fprintf(stdout, " [--<b>overwrite</b>] ");
130 
131  fprintf(stdout, " [--<b>help</b>] ");
132  fprintf(stdout, " [--<b>verbose</b>] ");
133  fprintf(stdout, " [--<b>quiet</b>] ");
134  fprintf(stdout, " [--<b>ui</b>] ");
135 
136  fprintf(stdout, "\n</div>\n");
137 
138  /* now long version */
139  fprintf(stdout, "\n");
140  fprintf(stdout, "<div id=\"flags\">\n");
141  fprintf(stdout, "<h3>%s:</h3>\n", _("Flags"));
142  fprintf(stdout, "<dl>\n");
143  if (st->n_flags) {
144  flag = &st->first_flag;
145  while (st->n_flags && flag != NULL) {
146  fprintf(stdout, "<dt><b>-%c</b></dt>\n", flag->key);
147 
148  if (flag->label) {
149  fprintf(stdout, "<dd>");
150  fprintf(stdout, "%s", flag->label);
151  fprintf(stdout, "</dd>\n");
152  }
153 
154  if (flag->description) {
155  fprintf(stdout, "<dd>");
156  fprintf(stdout, "%s", flag->description);
157  fprintf(stdout, "</dd>\n");
158  }
159 
160  flag = flag->next_flag;
161  fprintf(stdout, "\n");
162  }
163  }
164  if (new_prompt) {
165  fprintf(stdout, "<dt><b>--overwrite</b></dt>\n");
166  fprintf(stdout, "<dd>%s</dd>\n",
167  _("Allow output files to overwrite existing files"));
168  }
169  /* these flags are always available */
170  fprintf(stdout, "<dt><b>--help</b></dt>\n");
171  fprintf(stdout, "<dd>%s</dd>\n", _("Print usage summary"));
172 
173  fprintf(stdout, "<dt><b>--verbose</b></dt>\n");
174  fprintf(stdout, "<dd>%s</dd>\n", _("Verbose module output"));
175 
176  fprintf(stdout, "<dt><b>--quiet</b></dt>\n");
177  fprintf(stdout, "<dd>%s</dd>\n", _("Quiet module output"));
178 
179  fprintf(stdout, "<dt><b>--ui</b></dt>\n");
180  fprintf(stdout, "<dd>%s</dd>\n", _("Force launching GUI dialog"));
181 
182  fprintf(stdout, "</dl>\n");
183  fprintf(stdout, "</div>\n");
184 
185  fprintf(stdout, "\n");
186  fprintf(stdout, "<div id=\"parameters\">\n");
187  if (st->n_opts) {
188  opt = &st->first_option;
189  fprintf(stdout, "<h3>%s:</h3>\n", _("Parameters"));
190  fprintf(stdout, "<dl>\n");
191 
192  while (opt != NULL) {
193  /* TODO: make this a enumeration type? */
194  if (opt->key_desc != NULL)
195  type = opt->key_desc;
196  else
197  switch (opt->type) {
198  case TYPE_INTEGER:
199  type = "integer";
200  break;
201  case TYPE_DOUBLE:
202  type = "float";
203  break;
204  case TYPE_STRING:
205  type = "string";
206  break;
207  default:
208  type = "string";
209  break;
210  }
211  fprintf(stdout, "<dt><b>%s</b>=<em>%s", opt->key, type);
212  if (opt->multiple) {
213  fprintf(stdout, "[,<i>%s</i>,...]", type);
214  }
215  fprintf(stdout, "</em>");
216  if (opt->required) {
217  fprintf(stdout, "&nbsp;<b>[required]</b>");
218  }
219  fprintf(stdout, "</dt>\n");
220 
221  if (opt->label) {
222  fprintf(stdout, "<dd>");
223  print_escaped_for_html(stdout, opt->label);
224  fprintf(stdout, "</dd>\n");
225  }
226  if (opt->description) {
227  fprintf(stdout, "<dd>");
228  print_escaped_for_html(stdout, opt->description);
229  fprintf(stdout, "</dd>\n");
230  }
231 
232  if (opt->options) {
233  fprintf(stdout, "<dd>%s: <em>", _("Options"));
234  print_escaped_for_html_options(stdout, opt->options);
235  fprintf(stdout, "</em></dd>\n");
236  }
237 
238  if (opt->def) {
239  fprintf(stdout, "<dd>%s: <em>", _("Default"));
240  print_escaped_for_html(stdout, opt->def);
241  fprintf(stdout, "</em></dd>\n");
242  }
243 
244  if (opt->descs) {
245  int i = 0;
246 
247  while (opt->opts[i]) {
248  if (opt->descs[i]) {
249  fprintf(stdout, "<dd><b>");
250  if (opt->gisprompt) {
251  char *thumbnails = NULL;
252 
253  if (strcmp(opt->gisprompt,
254  "old,colortable,colortable") == 0)
255  thumbnails = "colortables";
256  else if (strcmp(opt->gisprompt,
257  "old,barscale,barscale") == 0)
258  thumbnails = "barscales";
259 
260  if (thumbnails)
261  fprintf(stdout, "<img width=\"80\" height=\"12\" "
262  "src=\"%s/%s.png\" alt=\"%s\">",
263  thumbnails, opt->opts[i], opt->opts[i]);
264  }
265  print_escaped_for_html(stdout, opt->opts[i]);
266  fprintf(stdout, "</b>: ");
267  print_escaped_for_html(stdout, opt->descs[i]);
268  fprintf(stdout, "</dd>\n");
269  }
270  i++;
271  }
272  }
273 
274  opt = opt->next_opt;
275  fprintf(stdout, "\n");
276  }
277  fprintf(stdout, "</dl>\n");
278  }
279  fprintf(stdout, "</div>\n");
280 
281  fprintf(stdout, "</body>\n</html>\n");
282 }
283 
284 
288 #define do_escape(c,escaped) case c: fputs(escaped,f);break
289 void print_escaped_for_html(FILE * f, const char *str)
290 {
291  const char *s;
292 
293  for (s = str; *s; s++) {
294  switch (*s) {
295  do_escape('&', "&amp;");
296  do_escape('<', "&lt;");
297  do_escape('>', "&gt;");
298  do_escape('\n', "<br>");
299  do_escape('\t', "&nbsp;&nbsp;&nbsp;&nbsp;");
300  default:
301  fputc(*s, f);
302  }
303  }
304 }
305 
306 void print_escaped_for_html_options(FILE * f, const char *str)
307 {
308  const char *s;
309 
310  for (s = str; *s; s++) {
311  switch (*s) {
312  do_escape('&', "&amp;");
313  do_escape('<', "&lt;");
314  do_escape('>', "&gt;");
315  do_escape('\n', "<br>");
316  do_escape('\t', "&nbsp;&nbsp;&nbsp;&nbsp;");
317  do_escape(',', ", ");
318  default:
319  fputc(*s, f);
320  }
321  }
322 }
323 
324 void print_escaped_for_html_keywords(FILE * f, const char * str)
325 {
326  /* generate HTML links */
327 
328  /* HTML link only for second keyword */
329  if (st->n_keys > 1 &&
330  strcmp(st->module_info.keywords[1], str) == 0) {
331 
332  const char *s;
333 
334  /* TODO: fprintf(f, _("topic: ")); */
335  fprintf(f, "<a href=\"topic_");
336  for (s = str; *s; s++) {
337  switch (*s) {
338  do_escape(' ', "_");
339  default:
340  fputc(*s, f);
341  }
342  }
343  fprintf(f, ".html\">%s</a>", str);
344  }
345  else { /* first and other than second keyword */
346  if (st->n_keys > 0 &&
347  strcmp(st->module_info.keywords[0], str) == 0) {
348  /* command family */
349  const char *s;
350 
351  fprintf(f, "<a href=\"");
352  for (s = str; *s; s++) {
353  switch (*s) {
354  do_escape(' ', "_");
355  default:
356  fputc(*s, f);
357  }
358  }
359  fprintf(f, ".html\">%s</a>", str);
360  } else {
361  /* keyword index */
362  if (st->n_keys > 0 &&
363  strcmp(st->module_info.keywords[2], str) == 0) {
364 
365  /* TODO: fprintf(f, _("keywords: ")); */
366  fprintf(f, "<a href=\"keywords.html#%s\">%s</a>", str, str);
367  } else {
368  fprintf(f, "<a href=\"keywords.html#%s\">%s</a>", str, str);
369  }
370  }
371  }
372 }
373 #undef do_escape
int G__uses_new_gisprompt(void)
Definition: parser.c:748
void G__usage_html(void)
Print module usage description in HTML format.
Definition: parser_html.c:29
#define NULL
Definition: ccmath.h:32
struct state * st
Definition: parser.c:101
const char * G_program_name(void)
Return module name.
Definition: progrm_nme.c:27
void G__print_keywords(FILE *fd, void(*format)(FILE *, const char *))
Print list of keywords (internal use only)
Definition: parser.c:784
#define do_escape(c, escaped)
Format text for HTML output.
Definition: parser_html.c:288