parser_html.c 8.9 KB

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