GRASS 8 Programmer's Manual 8.6.0dev(2026)-c83afef6d3
Loading...
Searching...
No Matches
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 SPDX-FileCopyrightText: 2023-2025 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Martin Landa
10 */
11#include <stdio.h>
12#include <string.h>
13
14#include <grass/gis.h>
15#include <grass/glocale.h>
16
17#include "parser_local_proto.h"
18
19/*!
20 * \brief Format text for Markdown output
21 */
22#define do_escape(c, escaped) \
23 case c: \
24 fputs(escaped, f); \
25 break
26
27void G__md_print_escaped(FILE *f, const char *str, const char *indent)
28{
29 const char *s;
30 for (s = str; *s; s++) {
31 switch (*s) {
32 case '\n':
33 fputs(MD_NEWLINE "\n", f);
34 fputs(indent, f);
35 break;
36 case '\t':
37 fputs("&nbsp;&nbsp;&nbsp;&nbsp;", f);
38 break;
39 case '<':
40 fputs("&lt;", f);
41 break;
42 case '>':
43 fputs("&gt;", f);
44 break;
45 case '*':
46 fputs("\\*", f);
47 break;
48 default:
49 fputc(*s, f);
50 }
51 }
52}
53
54void G__md_print_escaped_for_options(FILE *f, const char *str)
55{
56 const char *s;
57
58 for (s = str; *s; s++) {
59 switch (*s) {
60 do_escape('\n', "\n\n");
61 do_escape(',', ", ");
62 default:
63 fputc(*s, f);
64 }
65 }
66}
67
68#undef do_escape
69
70// This can eventually go to a file with functions related to Option,
71// but for now it is here until parser.c is refactored.
72/**
73 * \brief Get number of tuple items if option is a tuple
74 *
75 * Note that parser code generally does not consider tuples with only one
76 * item, so this function never returns 1.
77 *
78 * The number of items is determined by counting commas in the option key
79 * description.
80 *
81 * \param opt Option definition
82 * \return Number of items or zero if not a tuple
83 */
85{
86 // If empty, it cannot be considered a tuple.
87 if (!opt->key_desc)
88 return 0;
89
90 int n_items = 1;
91 const char *ptr;
92
93 for (ptr = opt->key_desc; *ptr != '\0'; ptr++)
94 if (*ptr == ',')
95 n_items++;
96
97 // Only one item is not considered a tuple.
98 if (n_items == 1)
99 return 0;
100 // Only two and more items are a tuple.
101 return n_items;
102}
#define do_escape(c, escaped)
Format text for Markdown output.
void G__md_print_escaped(FILE *f, const char *str, const char *indent)
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:560