parser_html.c 10 KB

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