parser_html.c 7.2 KB

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