welder
0.1.0
Bindings for annotated C++ types, from C++26 reflection
Toggle main menu visibility
Loading...
Searching...
No Matches
doc_style.hpp
Go to the documentation of this file.
1
#pragma once
2
#include <span>
3
#include <string>
4
#include <string_view>
5
6
#include <
welder/doc.hpp
>
// detail::function_doc / detail::param_doc / doc_style concept
7
39
40
namespace
welder::inline v0::
rods::python
{
41
42
namespace
detail
{
43
48
constexpr
void
append_indented
(std::string& out,
const
char
* text,
49
std::string_view indent) {
50
for
(
const
char
* c{text}; *c; ++c) {
51
out += *c;
52
if
(*c ==
'\n'
&& c[1] !=
'\0'
)
53
out += indent;
54
}
55
}
56
60
constexpr
void
blank_line
(std::string& out) {
61
if
(!out.empty()) {
62
if
(out.back() !=
'\n'
)
63
out +=
'\n'
;
64
out +=
'\n'
;
65
}
66
}
67
70
constexpr
bool
any_param_doc
(const ::welder::detail::function_doc& d) {
71
for
(
const
auto
& p : d.params)
72
if
(p.text)
73
return
true
;
74
return
false
;
75
}
76
77
}
// namespace detail
78
92
struct
google_style
{
96
static
constexpr
std::string
format
(const ::welder::detail::function_doc& d) {
97
std::string out{};
98
if
(d.summary)
99
out += d.summary;
100
101
if
(
detail::any_param_doc
(d)) {
102
detail::blank_line
(out);
103
out +=
"Args:\n"
;
104
for
(
const
auto
& p : d.params)
105
if
(p.text) {
106
out +=
" "
;
107
out += p.name ? p.name :
"?"
;
108
out +=
": "
;
109
detail::append_indented
(out, p.text,
" "
);
110
out +=
'\n'
;
111
}
112
}
113
114
if
(d.returns) {
115
detail::blank_line
(out);
116
out +=
"Returns:\n "
;
117
detail::append_indented
(out, d.returns,
" "
);
118
}
119
return
out;
120
}
121
};
122
123
static_assert
(::welder::doc_style<google_style>);
124
139
struct
numpy_style
{
143
static
constexpr
std::string
format
(const ::welder::detail::function_doc& d) {
144
std::string out{};
145
// Append an underlined section header (`title` then a rule of matching
146
// length), separated from prior content by a blank line.
147
auto
section = [&out](std::string_view title) {
148
detail::blank_line
(out);
149
out += title;
150
out +=
'\n'
;
151
out.append(title.size(),
'-'
);
152
out +=
'\n'
;
153
};
154
155
if
(d.summary)
156
out += d.summary;
157
158
if
(
detail::any_param_doc
(d)) {
159
section(
"Parameters"
);
160
for
(
const
auto
& p : d.params)
161
if
(p.text) {
162
out += p.name ? p.name :
"?"
;
163
out +=
"\n "
;
164
detail::append_indented
(out, p.text,
" "
);
165
out +=
'\n'
;
166
}
167
}
168
169
if
(d.returns) {
170
section(
"Returns"
);
171
detail::append_indented
(out, d.returns,
""
);
172
}
173
return
out;
174
}
175
};
176
177
static_assert
(::welder::doc_style<numpy_style>);
178
190
struct
sphinx_style
{
194
static
constexpr
std::string
format
(const ::welder::detail::function_doc& d) {
195
std::string out{};
196
if
(d.summary)
197
out += d.summary;
198
199
const
bool
any_field{
detail::any_param_doc
(d) || d.returns};
200
if
(any_field)
201
detail::blank_line
(out);
202
203
for
(
const
auto
& p : d.params)
204
if
(p.text) {
205
out +=
":param "
;
206
out += p.name ? p.name :
"?"
;
207
out +=
": "
;
208
detail::append_indented
(out, p.text,
" "
);
209
out +=
'\n'
;
210
}
211
212
if
(d.returns) {
213
out +=
":returns: "
;
214
detail::append_indented
(out, d.returns,
" "
);
215
}
216
return
out;
217
}
218
};
219
220
static_assert
(::welder::doc_style<sphinx_style>);
221
222
}
// namespace welder::rods::python
doc.hpp
Language-agnostic documentation layer: read [[=welder::doc(...)]] annotations off reflected entities ...
welder::rods::python::detail
Definition
doc_style.hpp:42
welder::rods::python::detail::any_param_doc
constexpr bool any_param_doc(const ::welder::detail::function_doc &d)
Whether d has at least one documented parameter (so a params block is worth opening).
Definition
doc_style.hpp:70
welder::rods::python::detail::append_indented
constexpr void append_indented(std::string &out, const char *text, std::string_view indent)
Append text to out, indenting every continuation line by indent so a multiline param/returns doc stay...
Definition
doc_style.hpp:48
welder::rods::python::detail::blank_line
constexpr void blank_line(std::string &out)
Separate a new block from preceding content in out by exactly one blank line, whether or not that con...
Definition
doc_style.hpp:60
welder::rods::python
Definition
doc_style.hpp:40
welder::rods::python::google_style
Google-style docstring assembly (the default).
Definition
doc_style.hpp:92
welder::rods::python::google_style::format
static constexpr std::string format(const ::welder::detail::function_doc &d)
Assemble d into a Google-style docstring.
Definition
doc_style.hpp:96
welder::rods::python::numpy_style
NumPy-style docstring assembly (numpydoc).
Definition
doc_style.hpp:139
welder::rods::python::numpy_style::format
static constexpr std::string format(const ::welder::detail::function_doc &d)
Assemble d into a NumPy-style docstring.
Definition
doc_style.hpp:143
welder::rods::python::sphinx_style
Sphinx-style docstring assembly (reStructuredText field lists).
Definition
doc_style.hpp:190
welder::rods::python::sphinx_style::format
static constexpr std::string format(const ::welder::detail::function_doc &d)
Assemble d into a Sphinx (reStructuredText) docstring.
Definition
doc_style.hpp:194
src
welder
rods
python
doc_style.hpp
Generated by
1.17.0