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