parser_html.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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, "<b>%s</b><br>\n", st->pgm_name);
  56. fprintf(stdout, "<b>%s help</b><br>\n", st->pgm_name);
  57. fprintf(stdout, "<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");
  107. /* now long version */
  108. fprintf(stdout, "\n");
  109. if (st->n_flags || new_prompt) {
  110. flag = &st->first_flag;
  111. fprintf(stdout, "<h3>%s:</h3>\n", _("Flags"));
  112. fprintf(stdout, "<dl>\n");
  113. while (st->n_flags && flag != NULL) {
  114. fprintf(stdout, "<dt><b>-%c</b></dt>\n", flag->key);
  115. if (flag->label) {
  116. fprintf(stdout, "<dd>");
  117. fprintf(stdout, "%s", flag->label);
  118. fprintf(stdout, "</dd>\n");
  119. }
  120. if (flag->description) {
  121. fprintf(stdout, "<dd>");
  122. fprintf(stdout, "%s", flag->description);
  123. fprintf(stdout, "</dd>\n");
  124. }
  125. flag = flag->next_flag;
  126. fprintf(stdout, "\n");
  127. }
  128. if (new_prompt) {
  129. fprintf(stdout, "<dt><b>--overwrite</b></dt>\n");
  130. fprintf(stdout, "<dd>%s</dd>\n",
  131. _("Allow output files to overwrite existing files"));
  132. }
  133. fprintf(stdout, "<dt><b>--verbose</b></dt>\n");
  134. fprintf(stdout, "<dd>%s</dd>\n", _("Verbose module output"));
  135. fprintf(stdout, "<dt><b>--quiet</b></dt>\n");
  136. fprintf(stdout, "<dd>%s</dd>\n", _("Quiet module output"));
  137. fprintf(stdout, "</dl>\n");
  138. }
  139. fprintf(stdout, "\n");
  140. if (st->n_opts) {
  141. opt = &st->first_option;
  142. fprintf(stdout, "<h3>%s:</h3>\n", _("Parameters"));
  143. fprintf(stdout, "<dl>\n");
  144. while (opt != NULL) {
  145. /* TODO: make this a enumeration type? */
  146. if (opt->key_desc != NULL)
  147. type = opt->key_desc;
  148. else
  149. switch (opt->type) {
  150. case TYPE_INTEGER:
  151. type = "integer";
  152. break;
  153. case TYPE_DOUBLE:
  154. type = "float";
  155. break;
  156. case TYPE_STRING:
  157. type = "string";
  158. break;
  159. default:
  160. type = "string";
  161. break;
  162. }
  163. fprintf(stdout, "<dt><b>%s</b>=<em>%s", opt->key, type);
  164. if (opt->multiple) {
  165. fprintf(stdout, "[,<i>%s</i>,...]", type);
  166. }
  167. fprintf(stdout, "</em>");
  168. if (opt->required) {
  169. fprintf(stdout, "&nbsp;<b>[required]</b>");
  170. }
  171. fprintf(stdout, "</dt>\n");
  172. if (opt->label) {
  173. fprintf(stdout, "<dd>");
  174. print_escaped_for_html(stdout, opt->label);
  175. fprintf(stdout, "</dd>\n");
  176. }
  177. if (opt->description) {
  178. fprintf(stdout, "<dd>");
  179. print_escaped_for_html(stdout, opt->description);
  180. fprintf(stdout, "</dd>\n");
  181. }
  182. if (opt->options) {
  183. fprintf(stdout, "<dd>%s: <em>", _("Options"));
  184. print_escaped_for_html_options(stdout, opt->options);
  185. fprintf(stdout, "</em></dd>\n");
  186. }
  187. if (opt->def) {
  188. fprintf(stdout, "<dd>%s: <em>", _("Default"));
  189. print_escaped_for_html(stdout, opt->def);
  190. fprintf(stdout, "</em></dd>\n");
  191. }
  192. if (opt->descs) {
  193. int i = 0;
  194. while (opt->opts[i]) {
  195. if (opt->descs[i]) {
  196. fprintf(stdout, "<dd><b>");
  197. print_escaped_for_html(stdout, opt->opts[i]);
  198. fprintf(stdout, "</b>: ");
  199. print_escaped_for_html(stdout, opt->descs[i]);
  200. fprintf(stdout, "</dd>\n");
  201. }
  202. i++;
  203. }
  204. }
  205. opt = opt->next_opt;
  206. fprintf(stdout, "\n");
  207. }
  208. fprintf(stdout, "</dl>\n");
  209. }
  210. fprintf(stdout, "</body>\n</html>\n");
  211. }
  212. /*!
  213. * \brief Format text for HTML output
  214. */
  215. #define do_escape(c,escaped) case c: fputs(escaped,f);break
  216. void print_escaped_for_html(FILE * f, const char *str)
  217. {
  218. const char *s;
  219. for (s = str; *s; s++) {
  220. switch (*s) {
  221. do_escape('&', "&amp;");
  222. do_escape('<', "&lt;");
  223. do_escape('>', "&gt;");
  224. do_escape('\n', "<br>");
  225. do_escape('\t', "&nbsp;&nbsp;&nbsp;&nbsp;");
  226. default:
  227. fputc(*s, f);
  228. }
  229. }
  230. }
  231. void print_escaped_for_html_options(FILE * f, const char *str)
  232. {
  233. const char *s;
  234. for (s = str; *s; s++) {
  235. switch (*s) {
  236. do_escape('&', "&amp;");
  237. do_escape('<', "&lt;");
  238. do_escape('>', "&gt;");
  239. do_escape('\n', "<br>");
  240. do_escape('\t', "&nbsp;&nbsp;&nbsp;&nbsp;");
  241. do_escape(',', ", ");
  242. default:
  243. fputc(*s, f);
  244. }
  245. }
  246. }
  247. #undef do_escape