mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-17 19:27:11 +00:00
render signatures
This commit is contained in:
parent
42abe02eac
commit
cfd75914be
1 changed files with 24 additions and 5 deletions
|
|
@ -65,11 +65,10 @@ impl Docstring {
|
||||||
/// Render the docstring for markdown display
|
/// Render the docstring for markdown display
|
||||||
pub fn render_markdown(&self) -> String {
|
pub fn render_markdown(&self) -> String {
|
||||||
let trimmed = documentation_trim(&self.0);
|
let trimmed = documentation_trim(&self.0);
|
||||||
// TODO: now actually parse it and "render" it to markdown.
|
|
||||||
//
|
// Try to parse and render the contents as markdown,
|
||||||
// For now we just wrap the content in a plaintext codeblock
|
// and if we fail, wrap it in a codeblock and display it raw.
|
||||||
// to avoid the contents erroneously being interpreted as markdown.
|
try_render_markdown(&trimmed).unwrap_or_else(|| format!("```text\n{trimmed}\n```"))
|
||||||
format!("```text\n{trimmed}\n```")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extract parameter documentation from popular docstring formats.
|
/// Extract parameter documentation from popular docstring formats.
|
||||||
|
|
@ -153,6 +152,26 @@ fn documentation_trim(docs: &str) -> String {
|
||||||
output
|
output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn try_render_markdown(docstring: &str) -> Option<String> {
|
||||||
|
let mut output = String::new();
|
||||||
|
let mut first_line = true;
|
||||||
|
for line in docstring.lines() {
|
||||||
|
// We can assume leading whitespace has been normalized
|
||||||
|
let trimmed_line = line.trim_start_matches(' ');
|
||||||
|
let num_leading_spaces = line.len() - trimmed_line.len();
|
||||||
|
|
||||||
|
if !first_line {
|
||||||
|
output.push_str(" \n");
|
||||||
|
}
|
||||||
|
for _ in 0..num_leading_spaces {
|
||||||
|
output.push_str(" ");
|
||||||
|
}
|
||||||
|
output.push_str(trimmed_line);
|
||||||
|
first_line = false;
|
||||||
|
}
|
||||||
|
Some(output)
|
||||||
|
}
|
||||||
|
|
||||||
/// Extract parameter documentation from Google-style docstrings.
|
/// Extract parameter documentation from Google-style docstrings.
|
||||||
fn extract_google_style_params(docstring: &str) -> HashMap<String, String> {
|
fn extract_google_style_params(docstring: &str) -> HashMap<String, String> {
|
||||||
let mut param_docs = HashMap::new();
|
let mut param_docs = HashMap::new();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue