Skip to content

Commit 480a87f

Browse files
bors[bot]Bromeon
andauthored
Merge #642
642: Increase readability of binding docs r=toasteater a=Bromeon Transforms some of the GDScript documentation markup to Rustdoc, using simple de-indentation and replacements. This makes bindings documentation, which has so far been a single huge code block, much more readable. Adds the `unindent` crate, which has no further dependencies. **Before:** ![before](https://user-images.githubusercontent.com/708488/101278044-f6db2800-37b8-11eb-8946-140e993bd843.png) **After:** ![after](https://user-images.githubusercontent.com/708488/101278182-0d35b380-37ba-11eb-8b77-841b9b833da7.png) This would be a first step. We could pull in the `regex` crate, to do smarter replacements such as `[constant HTTPClient.METHOD_GET]`, and possibly even link them using Rust 1.48's new feature ([doc](https://doc.rust-lang.org/rustdoc/linking-to-items-by-name.html), [announcement](https://blog.rust-lang.org/2020/11/19/Rust-1.48.html#easier-linking-in-rustdoc)). Co-authored-by: Jan Haller <[email protected]>
2 parents 09b6316 + 587ce16 commit 480a87f

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

bindings_generator/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ proc-macro2 = "1.0.6"
2020
quote = "1.0.2"
2121
syn = { version = "1.0", features = ["full", "extra-traits", "visit"] }
2222
miniserde = "=0.1.13"
23+
unindent = "0.1.7"

bindings_generator/src/class_docs.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,12 @@ impl GodotXMLDocs {
110110
}
111111

112112
fn add_fn(&mut self, class: &str, method: &str, desc: &str, default_args: &[(&str, &str)]) {
113-
let doc = desc.trim();
113+
let mut doc = unindent::unindent(desc.trim());
114114

115115
if doc.is_empty() && default_args.is_empty() {
116116
return;
117117
}
118118

119-
let mut doc = if !doc.is_empty() {
120-
format!("```text\n{}\n```\n", doc)
121-
} else {
122-
String::new()
123-
};
124-
125119
if !default_args.is_empty() {
126120
doc.push_str("\n# Default Arguments");
127121

@@ -130,7 +124,29 @@ impl GodotXMLDocs {
130124
}
131125
}
132126

133-
self.class_fn_desc
134-
.insert((class.into(), method.into()), doc);
127+
self.class_fn_desc.insert(
128+
(class.into(), method.into()),
129+
Self::reformat_as_rustdoc(doc),
130+
);
131+
}
132+
133+
/// Takes the Godot documentation markup and transforms it to Rustdoc.
134+
/// Very basic approach with limitations, but already helps readability quite a bit.
135+
fn reformat_as_rustdoc(godot_doc: String) -> String {
136+
let gdscript_note = if godot_doc.contains("[codeblock]") {
137+
"_Sample code is GDScript unless otherwise noted._\n\n"
138+
} else {
139+
""
140+
};
141+
142+
let translated = godot_doc
143+
.replace("[code]", "`")
144+
.replace("[/code]", "`")
145+
.replace("[codeblock]", "```gdscript")
146+
.replace("[/codeblock]", "```")
147+
.replace("[b]", "**")
148+
.replace("[/b]", "**");
149+
150+
format!("{}{}", gdscript_note, translated)
135151
}
136152
}

bindings_generator/src/documentation.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ pub fn generate_class_documentation(api: &Api, class: &GodotClass) -> TokenStrea
5252
let official_docs = format!(
5353
r#"## Official documentation
5454
55-
See the [documentation of this class]({url}) in the Godot engine's official documentation."#,
55+
See the [documentation of this class]({url}) in the Godot engine's official documentation.
56+
The method descriptions are generated from it and typically contain code samples in GDScript, not Rust."#,
5657
url = official_doc_url(class),
5758
);
5859

0 commit comments

Comments
 (0)