parser_html.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*!
  2. \file lib/gis/parser_html.c
  3. \brief GIS Library - Argument parsing functions (HTML output)
  4. (C) 2001-2009, 2011-2013 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 * f, const char *str);
  15. static void print_escaped_for_html_options(FILE * f, const char *str);
  16. /*!
  17. \brief Print module usage description in HTML format.
  18. */
  19. void G__usage_html(void)
  20. {
  21. struct Option *opt;
  22. struct Flag *flag;
  23. const char *type;
  24. int new_prompt = 0;
  25. new_prompt = G__uses_new_gisprompt();
  26. if (!st->pgm_name) /* v.dave && r.michael */
  27. st->pgm_name = G_program_name();
  28. if (!st->pgm_name)
  29. st->pgm_name = "??";
  30. fprintf(stdout,
  31. "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n");
  32. fprintf(stdout, "<html>\n<head>\n");
  33. fprintf(stdout, "<title>GRASS GIS manual: %s</title>\n", st->pgm_name);
  34. fprintf(stdout,
  35. "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n");
  36. fprintf(stdout,
  37. "<link rel=\"stylesheet\" href=\"grassdocs.css\" type=\"text/css\">\n");
  38. fprintf(stdout, "</head>\n");
  39. fprintf(stdout, "<body bgcolor=\"white\">\n\n");
  40. fprintf(stdout,
  41. "<img src=\"grass_logo.png\" alt=\"GRASS logo\"><hr align=center size=6 noshade>\n\n");
  42. fprintf(stdout, "<h2>%s</h2>\n", _("NAME"));
  43. fprintf(stdout, "<em><b>%s</b></em> ", st->pgm_name);
  44. if (st->module_info.label || st->module_info.description)
  45. fprintf(stdout, " - ");
  46. if (st->module_info.label)
  47. fprintf(stdout, "%s<BR>\n", st->module_info.label);
  48. if (st->module_info.description)
  49. fprintf(stdout, "%s\n", st->module_info.description);
  50. fprintf(stdout, "<h2>%s</h2>\n", _("KEYWORDS"));
  51. if (st->module_info.keywords) {
  52. G__print_keywords(stdout, NULL);
  53. fprintf(stdout, "\n");
  54. }
  55. fprintf(stdout, "<h2>%s</h2>\n", _("SYNOPSIS"));
  56. fprintf(stdout, "<div id=\"name\"><b>%s</b><br></div>\n", st->pgm_name);
  57. fprintf(stdout, "<b>%s help</b><br>\n", st->pgm_name);
  58. fprintf(stdout, "<div id=\"synopsis\"><b>%s</b>", st->pgm_name);
  59. /* print short version first */
  60. if (st->n_flags) {
  61. flag = &st->first_flag;
  62. fprintf(stdout, " [-<b>");
  63. while (flag != NULL) {
  64. fprintf(stdout, "%c", flag->key);
  65. flag = flag->next_flag;
  66. }
  67. fprintf(stdout, "</b>] ");
  68. }
  69. else
  70. fprintf(stdout, " ");
  71. if (st->n_opts) {
  72. opt = &st->first_option;
  73. while (opt != NULL) {
  74. if (opt->key_desc != NULL)
  75. type = opt->key_desc;
  76. else
  77. switch (opt->type) {
  78. case TYPE_INTEGER:
  79. type = "integer";
  80. break;
  81. case TYPE_DOUBLE:
  82. type = "float";
  83. break;
  84. case TYPE_STRING:
  85. type = "string";
  86. break;
  87. default:
  88. type = "string";
  89. break;
  90. }
  91. if (!opt->required)
  92. fprintf(stdout, " [");
  93. fprintf(stdout, "<b>%s</b>=<em>%s</em>", opt->key, type);
  94. if (opt->multiple) {
  95. fprintf(stdout, "[,<i>%s</i>,...]", type);
  96. }
  97. if (!opt->required)
  98. fprintf(stdout, "] ");
  99. opt = opt->next_opt;
  100. fprintf(stdout, " ");
  101. }
  102. }
  103. if (new_prompt)
  104. fprintf(stdout, " [--<b>overwrite</b>] ");
  105. fprintf(stdout, " [--<b>verbose</b>] ");
  106. fprintf(stdout, " [--<b>quiet</b>] ");
  107. fprintf(stdout, "\n</div>\n");
  108. /* now long version */
  109. fprintf(stdout, "\n");
  110. fprintf(stdout, "<div id=\"flags\">\n");
  111. if (st->n_flags || new_prompt) {
  112. flag = &st->first_flag;
  113. fprintf(stdout, "<h3>%s:</h3>\n", _("Flags"));
  114. fprintf(stdout, "<dl>\n");
  115. while (st->n_flags && flag != NULL) {
  116. fprintf(stdout, "<dt><b>-%c</b></dt>\n", flag->key);
  117. if (flag->label) {
  118. fprintf(stdout, "<dd>");
  119. fprintf(stdout, "%s", flag->label);
  120. fprintf(stdout, "</dd>\n");
  121. }
  122. if (flag->description) {
  123. fprintf(stdout, "<dd>");
  124. fprintf(stdout, "%s", flag->description);
  125. fprintf(stdout, "</dd>\n");
  126. }
  127. flag = flag->next_flag;
  128. fprintf(stdout, "\n");
  129. }
  130. if (new_prompt) {
  131. fprintf(stdout, "<dt><b>--overwrite</b></dt>\n");
  132. fprintf(stdout, "<dd>%s</dd>\n",
  133. _("Allow output files to overwrite existing files"));
  134. }
  135. fprintf(stdout, "<dt><b>--verbose</b></dt>\n");
  136. fprintf(stdout, "<dd>%s</dd>\n", _("Verbose module output"));
  137. fprintf(stdout, "<dt><b>--quiet</b></dt>\n");
  138. fprintf(stdout, "<dd>%s</dd>\n", _("Quiet module output"));
  139. fprintf(stdout, "</dl>\n");
  140. }
  141. fprintf(stdout, "</div>\n");
  142. fprintf(stdout, "\n");
  143. fprintf(stdout, "<div id=\"parameters\">\n");
  144. if (st->n_opts) {
  145. opt = &st->first_option;
  146. fprintf(stdout, "<h3>%s:</h3>\n", _("Parameters"));
  147. fprintf(stdout, "<dl>\n");
  148. while (opt != NULL) {
  149. /* TODO: make this a enumeration type? */
  150. if (opt->key_desc != NULL)
  151. type = opt->key_desc;
  152. else
  153. switch (opt->type) {
  154. case TYPE_INTEGER:
  155. type = "integer";
  156. break;
  157. case TYPE_DOUBLE:
  158. type = "float";
  159. break;
  160. case TYPE_STRING:
  161. type = "string";
  162. break;
  163. default:
  164. type = "string";
  165. break;
  166. }
  167. fprintf(stdout, "<dt><b>%s</b>=<em>%s", opt->key, type);
  168. if (opt->multiple) {
  169. fprintf(stdout, "[,<i>%s</i>,...]", type);
  170. }
  171. fprintf(stdout, "</em>");
  172. if (opt->required) {
  173. fprintf(stdout, "&nbsp;<b>[required]</b>");
  174. }
  175. fprintf(stdout, "</dt>\n");
  176. if (opt->label) {
  177. fprintf(stdout, "<dd>");
  178. print_escaped_for_html(stdout, opt->label);
  179. fprintf(stdout, "</dd>\n");
  180. }
  181. if (opt->description) {
  182. fprintf(stdout, "<dd>");
  183. print_escaped_for_html(stdout, opt->description);
  184. fprintf(stdout, "</dd>\n");
  185. }
  186. if (opt->options) {
  187. fprintf(stdout, "<dd>%s: <em>", _("Options"));
  188. print_escaped_for_html_options(stdout, opt->options);
  189. fprintf(stdout, "</em></dd>\n");
  190. }
  191. if (opt->def) {
  192. fprintf(stdout, "<dd>%s: <em>", _("Default"));
  193. print_escaped_for_html(stdout, opt->def);
  194. fprintf(stdout, "</em></dd>\n");
  195. }
  196. if (opt->descs) {
  197. int i = 0;
  198. while (opt->opts[i]) {
  199. if (opt->descs[i]) {
  200. fprintf(stdout, "<dd><b>");
  201. if (opt->gisprompt) {
  202. char *thumbnails = NULL;
  203. if (strcmp(opt->gisprompt,
  204. "old,colortable,colortable") == 0)
  205. thumbnails = "colortables";
  206. else if (strcmp(opt->gisprompt,
  207. "old,barscale,barscale") == 0)
  208. thumbnails = "barscales";
  209. if (thumbnails)
  210. fprintf(stdout, "<img width=\"80\" height=\"12\" "
  211. "src=\"%s/%s.png\" alt=\"%s\">",
  212. thumbnails, opt->opts[i], opt->opts[i]);
  213. }
  214. print_escaped_for_html(stdout, opt->opts[i]);
  215. fprintf(stdout, "</b>: ");
  216. print_escaped_for_html(stdout, opt->descs[i]);
  217. fprintf(stdout, "</dd>\n");
  218. }
  219. i++;
  220. }
  221. }
  222. opt = opt->next_opt;
  223. fprintf(stdout, "\n");
  224. }
  225. fprintf(stdout, "</dl>\n");
  226. }
  227. fprintf(stdout, "</div>\n");
  228. fprintf(stdout, "</body>\n</html>\n");
  229. }
  230. /*!
  231. * \brief Format text for HTML output
  232. */
  233. #define do_escape(c,escaped) case c: fputs(escaped,f);break
  234. void print_escaped_for_html(FILE * f, const char *str)
  235. {
  236. const char *s;
  237. for (s = str; *s; s++) {
  238. switch (*s) {
  239. do_escape('&', "&amp;");
  240. do_escape('<', "&lt;");
  241. do_escape('>', "&gt;");
  242. do_escape('\n', "<br>");
  243. do_escape('\t', "&nbsp;&nbsp;&nbsp;&nbsp;");
  244. default:
  245. fputc(*s, f);
  246. }
  247. }
  248. }
  249. void print_escaped_for_html_options(FILE * f, const char *str)
  250. {
  251. const char *s;
  252. for (s = str; *s; s++) {
  253. switch (*s) {
  254. do_escape('&', "&amp;");
  255. do_escape('<', "&lt;");
  256. do_escape('>', "&gt;");
  257. do_escape('\n', "<br>");
  258. do_escape('\t', "&nbsp;&nbsp;&nbsp;&nbsp;");
  259. do_escape(',', ", ");
  260. default:
  261. fputc(*s, f);
  262. }
  263. }
  264. }
  265. #undef do_escape