parser_html.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include <stdio.h>
  2. #include <grass/gis.h>
  3. #include <grass/glocale.h>
  4. #include "parser_local_proto.h"
  5. static void print_escaped_for_html(FILE * f, const char *str);
  6. static void print_escaped_for_html_options(FILE * f, const char *str);
  7. /*!
  8. \brief Print module usage description in HTML format.
  9. */
  10. void G__usage_html(void)
  11. {
  12. struct Option *opt;
  13. struct Flag *flag;
  14. const char *type;
  15. int new_prompt = 0;
  16. new_prompt = G__uses_new_gisprompt();
  17. if (!st->pgm_name) /* v.dave && r.michael */
  18. st->pgm_name = G_program_name();
  19. if (!st->pgm_name)
  20. st->pgm_name = "??";
  21. fprintf(stdout,
  22. "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n");
  23. fprintf(stdout, "<html>\n<head>\n");
  24. fprintf(stdout, "<title>GRASS GIS manual: %s</title>\n", st->pgm_name);
  25. fprintf(stdout,
  26. "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n");
  27. fprintf(stdout,
  28. "<link rel=\"stylesheet\" href=\"grassdocs.css\" type=\"text/css\">\n");
  29. fprintf(stdout, "</head>\n");
  30. fprintf(stdout, "<body bgcolor=\"white\">\n\n");
  31. fprintf(stdout,
  32. "<img src=\"grass_logo.png\" alt=\"GRASS logo\"><hr align=center size=6 noshade>\n\n");
  33. fprintf(stdout, "<h2>%s</h2>\n", _("NAME"));
  34. fprintf(stdout, "<em><b>%s</b></em> ", st->pgm_name);
  35. if (st->module_info.label || st->module_info.description)
  36. fprintf(stdout, " - ");
  37. if (st->module_info.label)
  38. fprintf(stdout, "%s<BR>\n", st->module_info.label);
  39. if (st->module_info.description)
  40. fprintf(stdout, "%s\n", st->module_info.description);
  41. fprintf(stdout, "<h2>%s</h2>\n", _("KEYWORDS"));
  42. if (st->module_info.keywords) {
  43. G__print_keywords(stdout, NULL);
  44. fprintf(stdout, "\n");
  45. }
  46. fprintf(stdout, "<h2>%s</h2>\n", _("SYNOPSIS"));
  47. fprintf(stdout, "<b>%s</b><br>\n", st->pgm_name);
  48. fprintf(stdout, "<b>%s help</b><br>\n", st->pgm_name);
  49. fprintf(stdout, "<b>%s</b>", st->pgm_name);
  50. /* print short version first */
  51. if (st->n_flags) {
  52. flag = &st->first_flag;
  53. fprintf(stdout, " [-<b>");
  54. while (flag != NULL) {
  55. fprintf(stdout, "%c", flag->key);
  56. flag = flag->next_flag;
  57. }
  58. fprintf(stdout, "</b>] ");
  59. }
  60. else
  61. fprintf(stdout, " ");
  62. if (st->n_opts) {
  63. opt = &st->first_option;
  64. while (opt != NULL) {
  65. if (opt->key_desc != NULL)
  66. type = opt->key_desc;
  67. else
  68. switch (opt->type) {
  69. case TYPE_INTEGER:
  70. type = "integer";
  71. break;
  72. case TYPE_DOUBLE:
  73. type = "float";
  74. break;
  75. case TYPE_STRING:
  76. type = "string";
  77. break;
  78. default:
  79. type = "string";
  80. break;
  81. }
  82. if (!opt->required)
  83. fprintf(stdout, " [");
  84. fprintf(stdout, "<b>%s</b>=<em>%s</em>", opt->key, type);
  85. if (opt->multiple) {
  86. fprintf(stdout, "[,<i>%s</i>,...]", type);
  87. }
  88. if (!opt->required)
  89. fprintf(stdout, "] ");
  90. opt = opt->next_opt;
  91. fprintf(stdout, " ");
  92. }
  93. }
  94. if (new_prompt)
  95. fprintf(stdout, " [--<b>overwrite</b>] ");
  96. fprintf(stdout, " [--<b>verbose</b>] ");
  97. fprintf(stdout, " [--<b>quiet</b>] ");
  98. fprintf(stdout, "\n");
  99. /* now long version */
  100. fprintf(stdout, "\n");
  101. if (st->n_flags || new_prompt) {
  102. flag = &st->first_flag;
  103. fprintf(stdout, "<h3>%s:</h3>\n", _("Flags"));
  104. fprintf(stdout, "<dl>\n");
  105. while (st->n_flags && flag != NULL) {
  106. fprintf(stdout, "<dt><b>-%c</b></dt>\n", flag->key);
  107. if (flag->label) {
  108. fprintf(stdout, "<dd>");
  109. fprintf(stdout, "%s", flag->label);
  110. fprintf(stdout, "</dd>\n");
  111. }
  112. if (flag->description) {
  113. fprintf(stdout, "<dd>");
  114. fprintf(stdout, "%s", flag->description);
  115. fprintf(stdout, "</dd>\n");
  116. }
  117. flag = flag->next_flag;
  118. fprintf(stdout, "\n");
  119. }
  120. if (new_prompt) {
  121. fprintf(stdout, "<dt><b>--overwrite</b></dt>\n");
  122. fprintf(stdout, "<dd>%s</dd>\n",
  123. _("Allow output files to overwrite existing files"));
  124. }
  125. fprintf(stdout, "<dt><b>--verbose</b></dt>\n");
  126. fprintf(stdout, "<dd>%s</dd>\n", _("Verbose module output"));
  127. fprintf(stdout, "<dt><b>--quiet</b></dt>\n");
  128. fprintf(stdout, "<dd>%s</dd>\n", _("Quiet module output"));
  129. fprintf(stdout, "</dl>\n");
  130. }
  131. fprintf(stdout, "\n");
  132. if (st->n_opts) {
  133. opt = &st->first_option;
  134. fprintf(stdout, "<h3>%s:</h3>\n", _("Parameters"));
  135. fprintf(stdout, "<dl>\n");
  136. while (opt != NULL) {
  137. /* TODO: make this a enumeration type? */
  138. if (opt->key_desc != NULL)
  139. type = opt->key_desc;
  140. else
  141. switch (opt->type) {
  142. case TYPE_INTEGER:
  143. type = "integer";
  144. break;
  145. case TYPE_DOUBLE:
  146. type = "float";
  147. break;
  148. case TYPE_STRING:
  149. type = "string";
  150. break;
  151. default:
  152. type = "string";
  153. break;
  154. }
  155. fprintf(stdout, "<dt><b>%s</b>=<em>%s", opt->key, type);
  156. if (opt->multiple) {
  157. fprintf(stdout, "[,<i>%s</i>,...]", type);
  158. }
  159. fprintf(stdout, "</em>");
  160. if (opt->required) {
  161. fprintf(stdout, "&nbsp;<b>[required]</b>");
  162. }
  163. fprintf(stdout, "</dt>\n");
  164. if (opt->label) {
  165. fprintf(stdout, "<dd>");
  166. print_escaped_for_html(stdout, opt->label);
  167. fprintf(stdout, "</dd>\n");
  168. }
  169. if (opt->description) {
  170. fprintf(stdout, "<dd>");
  171. print_escaped_for_html(stdout, opt->description);
  172. fprintf(stdout, "</dd>\n");
  173. }
  174. if (opt->options) {
  175. fprintf(stdout, "<dd>%s: <em>", _("Options"));
  176. print_escaped_for_html_options(stdout, opt->options);
  177. fprintf(stdout, "</em></dd>\n");
  178. }
  179. if (opt->def) {
  180. fprintf(stdout, "<dd>%s: <em>", _("Default"));
  181. print_escaped_for_html(stdout, opt->def);
  182. fprintf(stdout, "</em></dd>\n");
  183. }
  184. if (opt->descs) {
  185. int i = 0;
  186. while (opt->opts[i]) {
  187. if (opt->descs[i]) {
  188. fprintf(stdout, "<dd><b>");
  189. print_escaped_for_html(stdout, opt->opts[i]);
  190. fprintf(stdout, "</b>: ");
  191. print_escaped_for_html(stdout, opt->descs[i]);
  192. fprintf(stdout, "</dd>\n");
  193. }
  194. i++;
  195. }
  196. }
  197. opt = opt->next_opt;
  198. fprintf(stdout, "\n");
  199. }
  200. fprintf(stdout, "</dl>\n");
  201. }
  202. fprintf(stdout, "</body>\n</html>\n");
  203. }
  204. /*!
  205. * \brief Format text for HTML output
  206. */
  207. #define do_escape(c,escaped) case c: fputs(escaped,f);break
  208. void print_escaped_for_html(FILE * f, const char *str)
  209. {
  210. const char *s;
  211. for (s = str; *s; s++) {
  212. switch (*s) {
  213. do_escape('&', "&amp;");
  214. do_escape('<', "&lt;");
  215. do_escape('>', "&gt;");
  216. do_escape('\n', "<br>");
  217. do_escape('\t', "&nbsp;&nbsp;&nbsp;&nbsp;");
  218. default:
  219. fputc(*s, f);
  220. }
  221. }
  222. }
  223. void print_escaped_for_html_options(FILE * f, const char *str)
  224. {
  225. const char *s;
  226. for (s = str; *s; s++) {
  227. switch (*s) {
  228. do_escape('&', "&amp;");
  229. do_escape('<', "&lt;");
  230. do_escape('>', "&gt;");
  231. do_escape('\n', "<br>");
  232. do_escape('\t', "&nbsp;&nbsp;&nbsp;&nbsp;");
  233. do_escape(',', ", ");
  234. default:
  235. fputc(*s, f);
  236. }
  237. }
  238. }
  239. #undef do_escape