parser_html.c 9.1 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, " [--<b>ui</b>] ");
  112. fprintf(stdout, "\n</div>\n");
  113. /* now long version */
  114. fprintf(stdout, "\n");
  115. fprintf(stdout, "<div id=\"flags\">\n");
  116. if (st->n_flags || new_prompt) {
  117. flag = &st->first_flag;
  118. fprintf(stdout, "<h3>%s:</h3>\n", _("Flags"));
  119. fprintf(stdout, "<dl>\n");
  120. while (st->n_flags && flag != NULL) {
  121. fprintf(stdout, "<dt><b>-%c</b></dt>\n", flag->key);
  122. if (flag->label) {
  123. fprintf(stdout, "<dd>");
  124. fprintf(stdout, "%s", flag->label);
  125. fprintf(stdout, "</dd>\n");
  126. }
  127. if (flag->description) {
  128. fprintf(stdout, "<dd>");
  129. fprintf(stdout, "%s", flag->description);
  130. fprintf(stdout, "</dd>\n");
  131. }
  132. flag = flag->next_flag;
  133. fprintf(stdout, "\n");
  134. }
  135. if (new_prompt) {
  136. fprintf(stdout, "<dt><b>--overwrite</b></dt>\n");
  137. fprintf(stdout, "<dd>%s</dd>\n",
  138. _("Allow output files to overwrite existing files"));
  139. }
  140. fprintf(stdout, "<dt><b>--help</b></dt>\n");
  141. fprintf(stdout, "<dd>%s</dd>\n", _("Print usage summary"));
  142. fprintf(stdout, "<dt><b>--verbose</b></dt>\n");
  143. fprintf(stdout, "<dd>%s</dd>\n", _("Verbose module output"));
  144. fprintf(stdout, "<dt><b>--quiet</b></dt>\n");
  145. fprintf(stdout, "<dd>%s</dd>\n", _("Quiet module output"));
  146. fprintf(stdout, "<dt><b>--ui</b></dt>\n");
  147. fprintf(stdout, "<dd>%s</dd>\n", _("Force launching GUI dialog"));
  148. fprintf(stdout, "</dl>\n");
  149. }
  150. fprintf(stdout, "</div>\n");
  151. fprintf(stdout, "\n");
  152. fprintf(stdout, "<div id=\"parameters\">\n");
  153. if (st->n_opts) {
  154. opt = &st->first_option;
  155. fprintf(stdout, "<h3>%s:</h3>\n", _("Parameters"));
  156. fprintf(stdout, "<dl>\n");
  157. while (opt != NULL) {
  158. /* TODO: make this a enumeration type? */
  159. if (opt->key_desc != NULL)
  160. type = opt->key_desc;
  161. else
  162. switch (opt->type) {
  163. case TYPE_INTEGER:
  164. type = "integer";
  165. break;
  166. case TYPE_DOUBLE:
  167. type = "float";
  168. break;
  169. case TYPE_STRING:
  170. type = "string";
  171. break;
  172. default:
  173. type = "string";
  174. break;
  175. }
  176. fprintf(stdout, "<dt><b>%s</b>=<em>%s", opt->key, type);
  177. if (opt->multiple) {
  178. fprintf(stdout, "[,<i>%s</i>,...]", type);
  179. }
  180. fprintf(stdout, "</em>");
  181. if (opt->required) {
  182. fprintf(stdout, "&nbsp;<b>[required]</b>");
  183. }
  184. fprintf(stdout, "</dt>\n");
  185. if (opt->label) {
  186. fprintf(stdout, "<dd>");
  187. print_escaped_for_html(stdout, opt->label);
  188. fprintf(stdout, "</dd>\n");
  189. }
  190. if (opt->description) {
  191. fprintf(stdout, "<dd>");
  192. print_escaped_for_html(stdout, opt->description);
  193. fprintf(stdout, "</dd>\n");
  194. }
  195. if (opt->options) {
  196. fprintf(stdout, "<dd>%s: <em>", _("Options"));
  197. print_escaped_for_html_options(stdout, opt->options);
  198. fprintf(stdout, "</em></dd>\n");
  199. }
  200. if (opt->def) {
  201. fprintf(stdout, "<dd>%s: <em>", _("Default"));
  202. print_escaped_for_html(stdout, opt->def);
  203. fprintf(stdout, "</em></dd>\n");
  204. }
  205. if (opt->descs) {
  206. int i = 0;
  207. while (opt->opts[i]) {
  208. if (opt->descs[i]) {
  209. fprintf(stdout, "<dd><b>");
  210. if (opt->gisprompt) {
  211. char *thumbnails = NULL;
  212. if (strcmp(opt->gisprompt,
  213. "old,colortable,colortable") == 0)
  214. thumbnails = "colortables";
  215. else if (strcmp(opt->gisprompt,
  216. "old,barscale,barscale") == 0)
  217. thumbnails = "barscales";
  218. if (thumbnails)
  219. fprintf(stdout, "<img width=\"80\" height=\"12\" "
  220. "src=\"%s/%s.png\" alt=\"%s\">",
  221. thumbnails, opt->opts[i], opt->opts[i]);
  222. }
  223. print_escaped_for_html(stdout, opt->opts[i]);
  224. fprintf(stdout, "</b>: ");
  225. print_escaped_for_html(stdout, opt->descs[i]);
  226. fprintf(stdout, "</dd>\n");
  227. }
  228. i++;
  229. }
  230. }
  231. opt = opt->next_opt;
  232. fprintf(stdout, "\n");
  233. }
  234. fprintf(stdout, "</dl>\n");
  235. }
  236. fprintf(stdout, "</div>\n");
  237. fprintf(stdout, "</body>\n</html>\n");
  238. }
  239. /*!
  240. * \brief Format text for HTML output
  241. */
  242. #define do_escape(c,escaped) case c: fputs(escaped,f);break
  243. void print_escaped_for_html(FILE * f, const char *str)
  244. {
  245. const char *s;
  246. for (s = str; *s; s++) {
  247. switch (*s) {
  248. do_escape('&', "&amp;");
  249. do_escape('<', "&lt;");
  250. do_escape('>', "&gt;");
  251. do_escape('\n', "<br>");
  252. do_escape('\t', "&nbsp;&nbsp;&nbsp;&nbsp;");
  253. default:
  254. fputc(*s, f);
  255. }
  256. }
  257. }
  258. void print_escaped_for_html_options(FILE * f, const char *str)
  259. {
  260. const char *s;
  261. for (s = str; *s; s++) {
  262. switch (*s) {
  263. do_escape('&', "&amp;");
  264. do_escape('<', "&lt;");
  265. do_escape('>', "&gt;");
  266. do_escape('\n', "<br>");
  267. do_escape('\t', "&nbsp;&nbsp;&nbsp;&nbsp;");
  268. do_escape(',', ", ");
  269. default:
  270. fputc(*s, f);
  271. }
  272. }
  273. }
  274. void print_escaped_for_html_keywords(FILE * f, const char * str)
  275. {
  276. /* HTML link only for second keyword */
  277. if (st->n_keys > 1 &&
  278. strcmp(st->module_info.keywords[1], str) == 0) {
  279. const char *s;
  280. fprintf(f, "<a href=\"topic_");
  281. for (s = str; *s; s++) {
  282. switch (*s) {
  283. do_escape(' ', "_");
  284. default:
  285. fputc(*s, f);
  286. }
  287. }
  288. fprintf(f, ".html\">%s</a>", str);
  289. }
  290. else {
  291. fprintf(f, "%s", str);
  292. }
  293. }
  294. #undef do_escape