parser_html.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*!
  2. \file lib/gis/parser_html.c
  3. \brief GIS Library - Argument parsing functions (HTML output)
  4. (C) 2001-2009, 2011-2020 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Original author CERL
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <grass/gis.h>
  12. #include <grass/glocale.h>
  13. #include "parser_local_proto.h"
  14. static void print_escaped_for_html(FILE *, const char *);
  15. static void print_escaped_for_html_options(FILE *, const char *);
  16. static void print_escaped_for_html_keywords(FILE * , const char *);
  17. /*!
  18. \brief Print module usage description in HTML format.
  19. */
  20. void G__usage_html(void)
  21. {
  22. struct Option *opt;
  23. struct Flag *flag;
  24. const char *type;
  25. int new_prompt = 0;
  26. new_prompt = G__uses_new_gisprompt();
  27. if (!st->pgm_name) /* v.dave && r.michael */
  28. st->pgm_name = G_program_name();
  29. if (!st->pgm_name)
  30. st->pgm_name = "??";
  31. fprintf(stdout,
  32. "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n");
  33. fprintf(stdout, "<html>\n<head>\n");
  34. fprintf(stdout,
  35. " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n");
  36. fprintf(stdout, " <title>%s - GRASS GIS manual</title>\n", st->pgm_name);
  37. fprintf(stdout, " <meta name=\"description\" content=\"%s", st->pgm_name);
  38. if (st->module_info.description)
  39. fprintf(stdout, ": %s\">", st->module_info.description);
  40. else
  41. fprintf(stderr,"<%s.html> is missing the description", st->pgm_name);
  42. fprintf(stdout, "\n");
  43. if (st->module_info.keywords) {
  44. fprintf(stdout, " <meta name=\"keywords\" content=\"");
  45. G__print_keywords(stdout, NULL);
  46. fprintf(stdout, "\">");
  47. fprintf(stdout, "\n");
  48. }
  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. if (st->module_info.label || st->module_info.description)
  60. fprintf(stdout, " - ");
  61. if (st->module_info.label)
  62. fprintf(stdout, "%s<BR>\n", st->module_info.label);
  63. if (st->module_info.description)
  64. fprintf(stdout, "%s\n", st->module_info.description);
  65. fprintf(stdout, "<h2>%s</h2>\n", _("KEYWORDS"));
  66. if (st->module_info.keywords) {
  67. G__print_keywords(stdout, print_escaped_for_html_keywords);
  68. fprintf(stdout, "\n");
  69. }
  70. fprintf(stdout, "<h2>%s</h2>\n", _("SYNOPSIS"));
  71. fprintf(stdout, "<div id=\"name\"><b>%s</b><br></div>\n", st->pgm_name);
  72. fprintf(stdout, "<b>%s --help</b><br>\n", st->pgm_name);
  73. fprintf(stdout, "<div id=\"synopsis\"><b>%s</b>", st->pgm_name);
  74. /* print short version first */
  75. if (st->n_flags) {
  76. flag = &st->first_flag;
  77. fprintf(stdout, " [-<b>");
  78. while (flag != NULL) {
  79. fprintf(stdout, "%c", flag->key);
  80. flag = flag->next_flag;
  81. }
  82. fprintf(stdout, "</b>] ");
  83. }
  84. else
  85. fprintf(stdout, " ");
  86. if (st->n_opts) {
  87. opt = &st->first_option;
  88. while (opt != NULL) {
  89. if (opt->key_desc != NULL)
  90. type = opt->key_desc;
  91. else
  92. switch (opt->type) {
  93. case TYPE_INTEGER:
  94. type = "integer";
  95. break;
  96. case TYPE_DOUBLE:
  97. type = "float";
  98. break;
  99. case TYPE_STRING:
  100. type = "string";
  101. break;
  102. default:
  103. type = "string";
  104. break;
  105. }
  106. if (!opt->required)
  107. fprintf(stdout, " [");
  108. fprintf(stdout, "<b>%s</b>=<em>%s</em>", opt->key, type);
  109. if (opt->multiple) {
  110. fprintf(stdout, "[,<i>%s</i>,...]", type);
  111. }
  112. if (!opt->required)
  113. fprintf(stdout, "] ");
  114. opt = opt->next_opt;
  115. fprintf(stdout, " ");
  116. }
  117. }
  118. if (new_prompt)
  119. fprintf(stdout, " [--<b>overwrite</b>] ");
  120. fprintf(stdout, " [--<b>help</b>] ");
  121. fprintf(stdout, " [--<b>verbose</b>] ");
  122. fprintf(stdout, " [--<b>quiet</b>] ");
  123. fprintf(stdout, " [--<b>ui</b>] ");
  124. fprintf(stdout, "\n</div>\n");
  125. /* now long version */
  126. fprintf(stdout, "\n");
  127. fprintf(stdout, "<div id=\"flags\">\n");
  128. fprintf(stdout, "<h3>%s:</h3>\n", _("Flags"));
  129. fprintf(stdout, "<dl>\n");
  130. if (st->n_flags) {
  131. flag = &st->first_flag;
  132. while (st->n_flags && flag != NULL) {
  133. fprintf(stdout, "<dt><b>-%c</b></dt>\n", flag->key);
  134. if (flag->label) {
  135. fprintf(stdout, "<dd>");
  136. fprintf(stdout, "%s", flag->label);
  137. fprintf(stdout, "</dd>\n");
  138. }
  139. if (flag->description) {
  140. fprintf(stdout, "<dd>");
  141. fprintf(stdout, "%s", flag->description);
  142. fprintf(stdout, "</dd>\n");
  143. }
  144. flag = flag->next_flag;
  145. fprintf(stdout, "\n");
  146. }
  147. }
  148. if (new_prompt) {
  149. fprintf(stdout, "<dt><b>--overwrite</b></dt>\n");
  150. fprintf(stdout, "<dd>%s</dd>\n",
  151. _("Allow output files to overwrite existing files"));
  152. }
  153. /* these flags are always available */
  154. fprintf(stdout, "<dt><b>--help</b></dt>\n");
  155. fprintf(stdout, "<dd>%s</dd>\n", _("Print usage summary"));
  156. fprintf(stdout, "<dt><b>--verbose</b></dt>\n");
  157. fprintf(stdout, "<dd>%s</dd>\n", _("Verbose module output"));
  158. fprintf(stdout, "<dt><b>--quiet</b></dt>\n");
  159. fprintf(stdout, "<dd>%s</dd>\n", _("Quiet module output"));
  160. fprintf(stdout, "<dt><b>--ui</b></dt>\n");
  161. fprintf(stdout, "<dd>%s</dd>\n", _("Force launching GUI dialog"));
  162. fprintf(stdout, "</dl>\n");
  163. fprintf(stdout, "</div>\n");
  164. fprintf(stdout, "\n");
  165. fprintf(stdout, "<div id=\"parameters\">\n");
  166. if (st->n_opts) {
  167. opt = &st->first_option;
  168. fprintf(stdout, "<h3>%s:</h3>\n", _("Parameters"));
  169. fprintf(stdout, "<dl>\n");
  170. while (opt != NULL) {
  171. /* TODO: make this a enumeration type? */
  172. if (opt->key_desc != NULL)
  173. type = opt->key_desc;
  174. else
  175. switch (opt->type) {
  176. case TYPE_INTEGER:
  177. type = "integer";
  178. break;
  179. case TYPE_DOUBLE:
  180. type = "float";
  181. break;
  182. case TYPE_STRING:
  183. type = "string";
  184. break;
  185. default:
  186. type = "string";
  187. break;
  188. }
  189. fprintf(stdout, "<dt><b>%s</b>=<em>%s", opt->key, type);
  190. if (opt->multiple) {
  191. fprintf(stdout, "[,<i>%s</i>,...]", type);
  192. }
  193. fprintf(stdout, "</em>");
  194. if (opt->required) {
  195. fprintf(stdout, "&nbsp;<b>[required]</b>");
  196. }
  197. fprintf(stdout, "</dt>\n");
  198. if (opt->label) {
  199. fprintf(stdout, "<dd>");
  200. print_escaped_for_html(stdout, opt->label);
  201. fprintf(stdout, "</dd>\n");
  202. }
  203. if (opt->description) {
  204. fprintf(stdout, "<dd>");
  205. print_escaped_for_html(stdout, opt->description);
  206. fprintf(stdout, "</dd>\n");
  207. }
  208. if (opt->options) {
  209. fprintf(stdout, "<dd>%s: <em>", _("Options"));
  210. print_escaped_for_html_options(stdout, opt->options);
  211. fprintf(stdout, "</em></dd>\n");
  212. }
  213. if (opt->def) {
  214. fprintf(stdout, "<dd>%s: <em>", _("Default"));
  215. print_escaped_for_html(stdout, opt->def);
  216. fprintf(stdout, "</em></dd>\n");
  217. }
  218. if (opt->descs) {
  219. int i = 0;
  220. while (opt->opts[i]) {
  221. if (opt->descs[i]) {
  222. fprintf(stdout, "<dd><b>");
  223. if (opt->gisprompt) {
  224. char *thumbnails = NULL;
  225. if (strcmp(opt->gisprompt,
  226. "old,colortable,colortable") == 0)
  227. thumbnails = "colortables";
  228. else if (strcmp(opt->gisprompt,
  229. "old,barscale,barscale") == 0)
  230. thumbnails = "barscales";
  231. else if (strcmp(opt->gisprompt,
  232. "old,northarrow,northarrow") == 0)
  233. thumbnails = "northarrows";
  234. if (thumbnails)
  235. fprintf(stdout, "<img height=\"12\" "
  236. "style=\"max-width: 80;\" "
  237. "src=\"%s/%s.png\" alt=\"%s\"> ",
  238. thumbnails, opt->opts[i], opt->opts[i]);
  239. }
  240. print_escaped_for_html(stdout, opt->opts[i]);
  241. fprintf(stdout, "</b>: ");
  242. print_escaped_for_html(stdout, opt->descs[i]);
  243. fprintf(stdout, "</dd>\n");
  244. }
  245. i++;
  246. }
  247. }
  248. opt = opt->next_opt;
  249. fprintf(stdout, "\n");
  250. }
  251. fprintf(stdout, "</dl>\n");
  252. }
  253. fprintf(stdout, "</div>\n");
  254. fprintf(stdout, "</body>\n</html>\n");
  255. }
  256. /*!
  257. * \brief Format text for HTML output
  258. */
  259. #define do_escape(c,escaped) case c: fputs(escaped,f);break
  260. void print_escaped_for_html(FILE * f, const char *str)
  261. {
  262. const char *s;
  263. for (s = str; *s; s++) {
  264. switch (*s) {
  265. do_escape('&', "&amp;");
  266. do_escape('<', "&lt;");
  267. do_escape('>', "&gt;");
  268. do_escape('\n', "<br>");
  269. do_escape('\t', "&nbsp;&nbsp;&nbsp;&nbsp;");
  270. default:
  271. fputc(*s, f);
  272. }
  273. }
  274. }
  275. void print_escaped_for_html_options(FILE * f, const char *str)
  276. {
  277. const char *s;
  278. for (s = str; *s; s++) {
  279. switch (*s) {
  280. do_escape('&', "&amp;");
  281. do_escape('<', "&lt;");
  282. do_escape('>', "&gt;");
  283. do_escape('\n', "<br>");
  284. do_escape('\t', "&nbsp;&nbsp;&nbsp;&nbsp;");
  285. do_escape(',', ", ");
  286. default:
  287. fputc(*s, f);
  288. }
  289. }
  290. }
  291. void print_escaped_for_html_keywords(FILE * f, const char * str)
  292. {
  293. /* generate HTML links */
  294. /* HTML link only for second keyword */
  295. if (st->n_keys > 1 &&
  296. strcmp(st->module_info.keywords[1], str) == 0) {
  297. const char *s;
  298. /* TODO: fprintf(f, _("topic: ")); */
  299. fprintf(f, "<a href=\"topic_");
  300. for (s = str; *s; s++) {
  301. switch (*s) {
  302. do_escape(' ', "_");
  303. default:
  304. fputc(*s, f);
  305. }
  306. }
  307. fprintf(f, ".html\">%s</a>", str);
  308. }
  309. else { /* first and other than second keyword */
  310. if (st->n_keys > 0 &&
  311. strcmp(st->module_info.keywords[0], str) == 0) {
  312. /* command family */
  313. const char *s;
  314. fprintf(f, "<a href=\"");
  315. for (s = str; *s; s++) {
  316. switch (*s) {
  317. do_escape(' ', "_");
  318. default:
  319. fputc(*s, f);
  320. }
  321. }
  322. fprintf(f, ".html\">%s</a>", str);
  323. } else {
  324. /* keyword index */
  325. if (st->n_keys > 0 &&
  326. strcmp(st->module_info.keywords[2], str) == 0) {
  327. /* TODO: fprintf(f, _("keywords: ")); */
  328. fprintf(f, "<a href=\"keywords.html#%s\">%s</a>", str, str);
  329. } else {
  330. fprintf(f, "<a href=\"keywords.html#%s\">%s</a>", str, str);
  331. }
  332. }
  333. }
  334. }
  335. #undef do_escape