mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-17 11:22:55 +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
|
||||
pub fn render_markdown(&self) -> String {
|
||||
let trimmed = documentation_trim(&self.0);
|
||||
// TODO: now actually parse it and "render" it to markdown.
|
||||
//
|
||||
// For now we just wrap the content in a plaintext codeblock
|
||||
// to avoid the contents erroneously being interpreted as markdown.
|
||||
format!("```text\n{trimmed}\n```")
|
||||
|
||||
// Try to parse and render the contents as markdown,
|
||||
// and if we fail, wrap it in a codeblock and display it raw.
|
||||
try_render_markdown(&trimmed).unwrap_or_else(|| format!("```text\n{trimmed}\n```"))
|
||||
}
|
||||
|
||||
/// Extract parameter documentation from popular docstring formats.
|
||||
|
|
@ -153,6 +152,26 @@ fn documentation_trim(docs: &str) -> String {
|
|||
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.
|
||||
fn extract_google_style_params(docstring: &str) -> HashMap<String, String> {
|
||||
let mut param_docs = HashMap::new();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue