GRASS GIS 7 Programmer's Manual  7.7.svn(2018)-r73380
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
parser_html.c
Go to the documentation of this file.
1 /*!
2  \file lib/gis/parser_html.c
3 
4  \brief GIS Library - Argument parsing functions (HTML output)
5 
6  (C) 2001-2009, 2011-2013 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Original author CERL
12 */
13 
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 
26 /*!
27  \brief Print module usage description in HTML format.
28 */
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  else if (strcmp(opt->gisprompt,
260  "old,northarrow,northarrow") == 0)
261  thumbnails = "northarrows";
262 
263  if (thumbnails)
264  fprintf(stdout, "<img height=\"12\" "
265  "style=\"max-width: 80;\" "
266  "src=\"%s/%s.png\" alt=\"%s\"> ",
267  thumbnails, opt->opts[i], opt->opts[i]);
268  }
269  print_escaped_for_html(stdout, opt->opts[i]);
270  fprintf(stdout, "</b>: ");
271  print_escaped_for_html(stdout, opt->descs[i]);
272  fprintf(stdout, "</dd>\n");
273  }
274  i++;
275  }
276  }
277 
278  opt = opt->next_opt;
279  fprintf(stdout, "\n");
280  }
281  fprintf(stdout, "</dl>\n");
282  }
283  fprintf(stdout, "</div>\n");
284 
285  fprintf(stdout, "</body>\n</html>\n");
286 }
287 
288 
289 /*!
290  * \brief Format text for HTML output
291  */
292 #define do_escape(c,escaped) case c: fputs(escaped,f);break
293 void print_escaped_for_html(FILE * f, const char *str)
294 {
295  const char *s;
296 
297  for (s = str; *s; s++) {
298  switch (*s) {
299  do_escape('&', "&amp;");
300  do_escape('<', "&lt;");
301  do_escape('>', "&gt;");
302  do_escape('\n', "<br>");
303  do_escape('\t', "&nbsp;&nbsp;&nbsp;&nbsp;");
304  default:
305  fputc(*s, f);
306  }
307  }
308 }
309 
310 void print_escaped_for_html_options(FILE * f, const char *str)
311 {
312  const char *s;
313 
314  for (s = str; *s; s++) {
315  switch (*s) {
316  do_escape('&', "&amp;");
317  do_escape('<', "&lt;");
318  do_escape('>', "&gt;");
319  do_escape('\n', "<br>");
320  do_escape('\t', "&nbsp;&nbsp;&nbsp;&nbsp;");
321  do_escape(',', ", ");
322  default:
323  fputc(*s, f);
324  }
325  }
326 }
327 
328 void print_escaped_for_html_keywords(FILE * f, const char * str)
329 {
330  /* generate HTML links */
331 
332  /* HTML link only for second keyword */
333  if (st->n_keys > 1 &&
334  strcmp(st->module_info.keywords[1], str) == 0) {
335 
336  const char *s;
337 
338  /* TODO: fprintf(f, _("topic: ")); */
339  fprintf(f, "<a href=\"topic_");
340  for (s = str; *s; s++) {
341  switch (*s) {
342  do_escape(' ', "_");
343  default:
344  fputc(*s, f);
345  }
346  }
347  fprintf(f, ".html\">%s</a>", str);
348  }
349  else { /* first and other than second keyword */
350  if (st->n_keys > 0 &&
351  strcmp(st->module_info.keywords[0], str) == 0) {
352  /* command family */
353  const char *s;
354 
355  fprintf(f, "<a href=\"");
356  for (s = str; *s; s++) {
357  switch (*s) {
358  do_escape(' ', "_");
359  default:
360  fputc(*s, f);
361  }
362  }
363  fprintf(f, ".html\">%s</a>", str);
364  } else {
365  /* keyword index */
366  if (st->n_keys > 0 &&
367  strcmp(st->module_info.keywords[2], str) == 0) {
368 
369  /* TODO: fprintf(f, _("keywords: ")); */
370  fprintf(f, "<a href=\"keywords.html#%s\">%s</a>", str, str);
371  } else {
372  fprintf(f, "<a href=\"keywords.html#%s\">%s</a>", str, str);
373  }
374  }
375  }
376 }
377 #undef do_escape
int G__uses_new_gisprompt(void)
Definition: parser.c:861
const char ** opts
Definition: gis.h:516
void G__usage_html(void)
Print module usage description in HTML format.
Definition: parser_html.c:29
#define TYPE_DOUBLE
Definition: gis.h:142
#define TYPE_STRING
Definition: gis.h:143
#define NULL
Definition: ccmath.h:32
const char * def
Definition: gis.h:523
const char * description
Definition: gis.h:519
#define TYPE_INTEGER
Definition: gis.h:141
int type
Definition: gis.h:512
struct state * st
Definition: parser.c:103
int multiple
Definition: gis.h:514
Structure that stores flag info.
Definition: gis.h:538
char key
Definition: gis.h:540
int required
Definition: gis.h:513
const char * G_program_name(void)
Return module name.
Definition: progrm_nme.c:28
struct Flag * next_flag
Definition: gis.h:547
const char * label
Definition: gis.h:544
const char * label
Definition: gis.h:518
const char ** descs
Definition: gis.h:521
Structure that stores option information.
Definition: gis.h:509
#define _(str)
Definition: glocale.h:13
const char * description
Definition: gis.h:545
const char * key
Definition: gis.h:511
const char * gisprompt
Definition: gis.h:526
void G__print_keywords(FILE *fd, void(*format)(FILE *, const char *))
Print list of keywords (internal use only)
Definition: parser.c:897
const char * options
Definition: gis.h:515
const char * key_desc
Definition: gis.h:517
#define do_escape(c, escaped)
Format text for HTML output.
Definition: parser_html.c:292
struct Option * next_opt
Definition: gis.h:525