GRASS GIS 8 Programmer's Manual  8.5.0dev(2025)-299a28a7d0
parser_md_common.c
Go to the documentation of this file.
1 /*!
2  \file lib/gis/parser_md_common.c
3 
4  \brief GIS Library - Argument parsing functions (Markdown output)
5 
6  (C) 2023-2025 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Martin Landa
12  */
13 #include <stdio.h>
14 #include <string.h>
15 
16 #include <grass/gis.h>
17 #include <grass/glocale.h>
18 
19 #include "parser_local_proto.h"
20 
21 /*!
22  * \brief Format text for Markdown output
23  */
24 #define do_escape(c, escaped) \
25  case c: \
26  fputs(escaped, f); \
27  break
28 
29 void G__md_print_escaped(FILE *f, const char *str)
30 {
31  const char *s;
32 
33  for (s = str; *s; s++) {
34  switch (*s) {
35  do_escape('\n', "\\\n");
36  do_escape('\t', "&nbsp;&nbsp;&nbsp;&nbsp;");
37  do_escape('<', "&lt;");
38  do_escape('>', "&gt;");
39  do_escape('*', "\\*");
40  default:
41  fputc(*s, f);
42  }
43  }
44 }
45 
46 void G__md_print_escaped_for_options(FILE *f, const char *str)
47 {
48  const char *s;
49 
50  for (s = str; *s; s++) {
51  switch (*s) {
52  do_escape('\n', "\n\n");
53  do_escape(',', ", ");
54  default:
55  fputc(*s, f);
56  }
57  }
58 }
59 
60 #undef do_escape
61 
62 // This can eventually go to a file with functions related to Option,
63 // but for now it is here until parser.c is refactored.
64 /**
65  * \brief Get number of tuple items if option is a tuple
66  *
67  * Note that parser code generally does not consider tuples with only one
68  * item, so this function never returns 1.
69  *
70  * The number of items is determined by counting commas in the option key
71  * description.
72  *
73  * \param opt Option definition
74  * \return Number of items or zero if not a tuple
75  */
76 int G__option_num_tuple_items(const struct Option *opt)
77 {
78  // If empty, it cannot be considered a tuple.
79  if (!opt->key_desc)
80  return 0;
81 
82  int n_items = 1;
83  const char *ptr;
84 
85  for (ptr = opt->key_desc; *ptr != '\0'; ptr++)
86  if (*ptr == ',')
87  n_items++;
88 
89  // Only one item is not considered a tuple.
90  if (n_items == 1)
91  return 0;
92  // Only two and more items are a tuple.
93  return n_items;
94 }
#define do_escape(c, escaped)
Format text for Markdown output.
void G__md_print_escaped(FILE *f, const char *str)
int G__option_num_tuple_items(const struct Option *opt)
Get number of tuple items if option is a tuple.
void G__md_print_escaped_for_options(FILE *f, const char *str)
Structure that stores option information.
Definition: gis.h:557
const char * key_desc
Definition: gis.h:564