parser_rest.c 6.7 KB

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