parser_html.c 9.0 KB

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