welder 0.1.0
Bindings for annotated C++ types, from C++26 reflection
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
40namespace welder::inline v0::rods::python {
41
42namespace detail {
43
48constexpr 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
60constexpr void blank_line(std::string& out) {
61 if (!out.empty()) {
62 if (out.back() != '\n')
63 out += '\n';
64 out += '\n';
65 }
66}
67
70constexpr 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
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)) {
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) {
116 out += "Returns:\n ";
117 detail::append_indented(out, d.returns, " ");
118 }
119 return out;
120 }
121
128 static constexpr std::string format_enum(const ::welder::detail::enum_doc& e) {
129 std::string out{};
130 if (e.summary)
131 out += e.summary;
132
133 if (!e.members.empty()) {
135 out += "Attributes:\n";
136 for (const auto& m : e.members) {
137 out += " ";
138 out += m.name;
139 out += ": ";
140 detail::append_indented(out, m.text, " ");
141 out += '\n';
142 }
143 }
144 return out;
145 }
146};
147
148static_assert(::welder::doc_style<google_style>);
149
168 static constexpr std::string format(const ::welder::detail::function_doc& d) {
169 std::string out{};
170 // Append an underlined section header (`title` then a rule of matching
171 // length), separated from prior content by a blank line.
172 auto section = [&out](std::string_view title) {
174 out += title;
175 out += '\n';
176 out.append(title.size(), '-');
177 out += '\n';
178 };
179
180 if (d.summary)
181 out += d.summary;
182
183 if (detail::any_param_doc(d)) {
184 section("Parameters");
185 for (const auto& p : d.params)
186 if (p.text) {
187 out += p.name ? p.name : "?";
188 out += "\n ";
189 detail::append_indented(out, p.text, " ");
190 out += '\n';
191 }
192 }
193
194 if (d.returns) {
195 section("Returns");
196 detail::append_indented(out, d.returns, "");
197 }
198 return out;
199 }
200
207 static constexpr std::string format_enum(const ::welder::detail::enum_doc& e) {
208 std::string out{};
209 if (e.summary)
210 out += e.summary;
211
212 if (!e.members.empty()) {
214 out += "Attributes\n";
215 out.append(std::string_view{"Attributes"}.size(), '-');
216 out += '\n';
217 for (const auto& m : e.members) {
218 out += m.name;
219 out += "\n ";
220 detail::append_indented(out, m.text, " ");
221 out += '\n';
222 }
223 }
224 return out;
225 }
226};
227
228static_assert(::welder::doc_style<numpy_style>);
229
245 static constexpr std::string format(const ::welder::detail::function_doc& d) {
246 std::string out{};
247 if (d.summary)
248 out += d.summary;
249
250 const bool any_field{detail::any_param_doc(d) || d.returns};
251 if (any_field)
253
254 for (const auto& p : d.params)
255 if (p.text) {
256 out += ":param ";
257 out += p.name ? p.name : "?";
258 out += ": ";
259 detail::append_indented(out, p.text, " ");
260 out += '\n';
261 }
262
263 if (d.returns) {
264 out += ":returns: ";
265 detail::append_indented(out, d.returns, " ");
266 }
267 return out;
268 }
269
276 static constexpr std::string format_enum(const ::welder::detail::enum_doc& e) {
277 std::string out{};
278 if (e.summary)
279 out += e.summary;
280
281 if (!e.members.empty())
283
284 for (const auto& m : e.members) {
285 out += ":var ";
286 out += m.name;
287 out += ": ";
288 detail::append_indented(out, m.text, " ");
289 out += '\n';
290 }
291 return out;
292 }
293};
294
295static_assert(::welder::doc_style<sphinx_style>);
296
297} // namespace welder::rods::python
Language-agnostic documentation layer: read [[=welder::doc(...)]] annotations off reflected entities ...
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
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
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
Google-style docstring assembly (the default).
Definition doc_style.hpp:92
static constexpr std::string format_enum(const ::welder::detail::enum_doc &e)
Assemble enum e into a Google-style docstring: the summary, then an Attributes: block listing each do...
static constexpr std::string format(const ::welder::detail::function_doc &d)
Assemble d into a Google-style docstring.
Definition doc_style.hpp:96
NumPy-style docstring assembly (numpydoc).
static constexpr std::string format_enum(const ::welder::detail::enum_doc &e)
Assemble enum e into a NumPy-style docstring: the summary, then an underlined Attributes section list...
static constexpr std::string format(const ::welder::detail::function_doc &d)
Assemble d into a NumPy-style docstring.
Sphinx-style docstring assembly (reStructuredText field lists).
static constexpr std::string format(const ::welder::detail::function_doc &d)
Assemble d into a Sphinx (reStructuredText) docstring.
static constexpr std::string format_enum(const ::welder::detail::enum_doc &e)
Assemble enum e into a Sphinx (reStructuredText) docstring: the summary, then a :var Name: text field...