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};
122
123static_assert(::welder::doc_style<google_style>);
124
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) {
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
177static_assert(::welder::doc_style<numpy_style>);
178
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)
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
220static_assert(::welder::doc_style<sphinx_style>);
221
222} // 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(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(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.