Skip to content

Commit fe9759a

Browse files
authored
Improve docs for TextDocument example (#4008)
### What * Closes #3904 * Closes #3905 This enabled the API-docs for the `TextDocument` example. To make sure it renders correctly I needed to: * Remove the ```-code block (for proper embedding in the website inside of a ``` markdown block) * Replace `""""` with `''''` in the python example, so it can be embedded in a `"""`-docstring in the Python API docs #### Python API docs: ![image](https://github.com/rerun-io/rerun/assets/1148717/e9d2b814-1aa6-4018-bc76-c53aa6dbc829) #### Rust API docs: ![image](https://github.com/rerun-io/rerun/assets/1148717/9b013213-41c1-4692-bfb9-e29b0c592c9f) ### Checklist * [x] I have read and agree to [Contributor Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and the [Code of Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md) * [x] I've included a screenshot or gif (if applicable) * [x] I have tested [demo.rerun.io](https://demo.rerun.io/pr/4008) (if applicable) * [x] The PR title and labels are set such as to maximize their usefulness for the next release's CHANGELOG - [PR Build Summary](https://build.rerun.io/pr/4008) - [Docs preview](https://rerun.io/preview/a8947d56d39d866f14791d6bb57fafad32323c5b/docs) <!--DOCS-PREVIEW--> - [Examples preview](https://rerun.io/preview/a8947d56d39d866f14791d6bb57fafad32323c5b/examples) <!--EXAMPLES-PREVIEW--> - [Recent benchmark results](https://ref.rerun.io/dev/bench/) - [Wasm size tracking](https://ref.rerun.io/dev/sizes/)
1 parent 7f7f0f3 commit fe9759a

12 files changed

Lines changed: 282 additions & 32 deletions

File tree

crates/re_types/definitions/rerun/archetypes/text_document.fbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace rerun.archetypes;
1111
///
1212
/// Supports raw text and markdown.
1313
///
14-
/// \example text_document !api title="Markdown text document" image="https://static.rerun.io/textdocument/babda19558ee32ed8d730495b595aee7a5e2c174/1200w.png"
14+
/// \example text_document title="Markdown text document" image="https://static.rerun.io/textdocument/babda19558ee32ed8d730495b595aee7a5e2c174/1200w.png"
1515
table TextDocument (
1616
"attr.rust.derive": "PartialEq, Eq"
1717
) {

crates/re_types/src/archetypes/text_document.rs

Lines changed: 69 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/re_types_builder/src/codegen/cpp/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,6 +2068,10 @@ fn lines_from_docs(docs: &Docs) -> Vec<String> {
20682068
..
20692069
} = &example.base;
20702070

2071+
for line in &example.lines {
2072+
assert!(!line.contains("```"), "Example {name:?} contains ``` in it, so we can't embed it in the C++ API docs.");
2073+
}
2074+
20712075
if let Some(title) = title {
20722076
lines.push(format!("### {title}"));
20732077
} else {

crates/re_types_builder/src/codegen/docs/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,11 @@ fn object_page(reporter: &Reporter, object: &Object, object_map: &ObjectMap) ->
192192
putln!(page);
193193
write_used_by(&mut page, reporter, object, object_map);
194194
}
195-
ObjectKind::Blueprint | ObjectKind::Archetype => {}
195+
ObjectKind::Blueprint | ObjectKind::Archetype => {
196+
if examples.is_empty() {
197+
reporter.warn(&object.virtpath, &object.fqname, "No examples");
198+
}
199+
}
196200
}
197201

198202
page
@@ -259,7 +263,9 @@ fn write_used_by(o: &mut String, reporter: &Reporter, object: &Object, object_ma
259263
// reference other tables, but they are unwrapped in the codegen.
260264
// So for instance: `union Angle` uses `rerun.datatypes.Float32` in
261265
// `angle.fbs`, but in the generated code that datatype is unused.
262-
reporter.warn(&object.virtpath, &object.fqname, "Unused object");
266+
if false {
267+
reporter.warn(&object.virtpath, &object.fqname, "Unused object");
268+
}
263269
} else {
264270
putln!(o, "## Used by");
265271
putln!(o);

crates/re_types_builder/src/codegen/python.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,13 +875,41 @@ fn quote_examples(examples: Vec<Example<'_>>, lines: &mut Vec<String>) {
875875
name, title, image, ..
876876
} = &example.base;
877877

878+
let mut example_lines = example.lines.clone();
879+
880+
if let Some(first_line) = example_lines.first() {
881+
if first_line.starts_with("\"\"\"")
882+
&& first_line.ends_with("\"\"\"")
883+
&& first_line.len() > 6
884+
{
885+
// Remove one-line docstring, otherwise we can't embed this.
886+
example_lines.remove(0);
887+
}
888+
}
889+
890+
// Remove leading blank lines:
891+
while example_lines.first() == Some(&String::default()) {
892+
example_lines.remove(0);
893+
}
894+
895+
for line in &example_lines {
896+
assert!(
897+
!line.contains("```"),
898+
"Example {name:?} contains ``` in it, so we can't embed it in the Python API docs."
899+
);
900+
assert!(
901+
!line.contains("\"\"\""),
902+
"Example {name:?} contains \"\"\" in it, so we can't embed it in the Python API docs."
903+
);
904+
}
905+
878906
if let Some(title) = title {
879907
lines.push(format!("### {title}:"));
880908
} else {
881909
lines.push(format!("### `{name}`:"));
882910
}
883911
lines.push("```python".into());
884-
lines.extend(example.lines.into_iter());
912+
lines.extend(example_lines.into_iter());
885913
lines.push("```".into());
886914
if let Some(image) = &image {
887915
lines.extend(image.image_stack());
@@ -930,6 +958,13 @@ fn quote_doc_lines(lines: Vec<String>) -> String {
930958
return String::new();
931959
}
932960

961+
for line in &lines {
962+
assert!(
963+
!line.contains("\"\"\""),
964+
"Cannot put triple quotes in Python docstrings"
965+
);
966+
}
967+
933968
// NOTE: Filter out docstrings within docstrings, it just gets crazy otherwise…
934969
let lines: Vec<String> = lines
935970
.into_iter()

crates/re_types_builder/src/codegen/rust/api.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,14 @@ impl quote::ToTokens for ObjectFieldTokenizer<'_> {
452452
}
453453

454454
fn quote_field_docs(reporter: &Reporter, field: &ObjectField) -> TokenStream {
455-
let lines = doc_as_lines(reporter, &field.virtpath, &field.fqname, &field.docs);
455+
let require_example = false;
456+
let lines = doc_as_lines(
457+
reporter,
458+
&field.virtpath,
459+
&field.fqname,
460+
&field.docs,
461+
require_example,
462+
);
456463

457464
let require_field_docs = false;
458465
if require_field_docs && lines.is_empty() && !field.is_testing() {
@@ -463,7 +470,14 @@ fn quote_field_docs(reporter: &Reporter, field: &ObjectField) -> TokenStream {
463470
}
464471

465472
fn quote_obj_docs(reporter: &Reporter, obj: &Object) -> TokenStream {
466-
let mut lines = doc_as_lines(reporter, &obj.virtpath, &obj.fqname, &obj.docs);
473+
let require_example = obj.kind == ObjectKind::Archetype;
474+
let mut lines = doc_as_lines(
475+
reporter,
476+
&obj.virtpath,
477+
&obj.fqname,
478+
&obj.docs,
479+
require_example,
480+
);
467481

468482
// Prefix first line with `**Datatype**: ` etc:
469483
if let Some(first) = lines.first_mut() {
@@ -475,14 +489,24 @@ fn quote_obj_docs(reporter: &Reporter, obj: &Object) -> TokenStream {
475489
quote_doc_lines(&lines)
476490
}
477491

478-
fn doc_as_lines(reporter: &Reporter, virtpath: &str, fqname: &str, docs: &Docs) -> Vec<String> {
492+
fn doc_as_lines(
493+
reporter: &Reporter,
494+
virtpath: &str,
495+
fqname: &str,
496+
docs: &Docs,
497+
require_example: bool,
498+
) -> Vec<String> {
479499
let mut lines = crate::codegen::get_documentation(docs, &["rs", "rust"]);
480500

481501
let examples = collect_examples_for_api_docs(docs, "rs", true)
482502
.map_err(|err| reporter.error(virtpath, fqname, err))
483503
.unwrap_or_default();
484504

485-
if !examples.is_empty() {
505+
if examples.is_empty() {
506+
if require_example {
507+
reporter.warn(virtpath, fqname, "Missing example");
508+
}
509+
} else {
486510
lines.push(Default::default());
487511
let section_title = if examples.len() == 1 {
488512
"Example"
@@ -497,14 +521,27 @@ fn doc_as_lines(reporter: &Reporter, virtpath: &str, fqname: &str, docs: &Docs)
497521
name, title, image, ..
498522
} = &example.base;
499523

524+
for line in &example.lines {
525+
if line.contains("```") {
526+
reporter.error(
527+
virtpath,
528+
fqname,
529+
format!("Example {name:?} contains ``` in it, so we can't embed it in the Rust API docs."),
530+
);
531+
continue;
532+
}
533+
}
534+
500535
if let Some(title) = title {
501536
lines.push(format!("### {title}"));
502537
} else {
503538
lines.push(format!("### `{name}`:"));
504539
}
540+
505541
lines.push("```ignore".into());
506542
lines.extend(example.lines.into_iter());
507543
lines.push("```".into());
544+
508545
if let Some(image) = &image {
509546
lines.extend(image.image_stack().into_iter());
510547
}

docs/code-examples/text_document.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@ Basic formatting:
2929
3030
----------------------------------
3131
32-
Some code:
33-
```rs
34-
fn main() {
35-
println!("Hello, world!");
36-
}
37-
```
38-
3932
## Support
4033
- [x] [Commonmark](https://commonmark.org/help/) support
4134
- [x] GitHub-style strikethrough, tables, and checkboxes

docs/code-examples/text_document.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python3
21
"""Log a `TextDocument`."""
32

43
import rerun as rr
@@ -10,7 +9,7 @@
109
rr.log(
1110
"markdown",
1211
rr.TextDocument(
13-
"""
12+
'''
1413
# Hello Markdown!
1514
[Click here to see the raw text](recording://markdown.Text).
1615
@@ -26,13 +25,6 @@
2625
2726
----------------------------------
2827
29-
Some code:
30-
```rs
31-
fn main() {
32-
println!("Hello, world!");
33-
}
34-
```
35-
3628
## Support
3729
- [x] [Commonmark](https://commonmark.org/help/) support
3830
- [x] GitHub-style strikethrough, tables, and checkboxes
@@ -51,7 +43,7 @@
5143
5244
## Image
5345
![A random image](https://picsum.photos/640/480)
54-
""".strip(),
46+
'''.strip(),
5547
media_type=rr.MediaType.MARKDOWN,
5648
),
5749
)

docs/code-examples/text_document.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ Basic formatting:
2828
2929
----------------------------------
3030
31-
Some code:
32-
```rs
33-
fn main() {
34-
println!("Hello, world!");
35-
}
36-
```
37-
3831
## Support
3932
- [x] [Commonmark](https://commonmark.org/help/) support
4033
- [x] GitHub-style strikethrough, tables, and checkboxes

rerun_cpp/src/rerun/archetypes/text_document.hpp

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)