parser_html.c 8.8 KB

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