Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion crates/ruff_markdown/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static MARKDOWN_CODE_FENCE: LazyLock<Regex> = LazyLock::new(|| {
^
(?<indent>\s*)
(?<fence>(?:```+|~~~+))\s*
(?<language>(?:\w+)?)\s*
\{?(?<language>(?:\w+)?)\}?\s*
(?<info>(?:.*))\s*
$
",
Expand Down Expand Up @@ -411,4 +411,30 @@ def bar(): ...
}
), @"Unchanged");
}

#[test]
fn format_code_blocks_quarto() {
let code = r#"
```{py}
print( 'hello' )
```

~~~{pyi}
def foo(): ...


def bar(): ...
~~~
"#;
assert_snapshot!(format_code_blocks(code, None, &FormatterSettings::default()), @r#"
```{py}
print("hello")
```

~~~{pyi}
def foo(): ...
def bar(): ...
~~~
"#);
}
}
2 changes: 1 addition & 1 deletion crates/ruff_python_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<P: AsRef<Path>> From<P> for SourceType {
Some(filename) if filename == "poetry.lock" => Self::Toml(TomlSourceType::Poetry),
_ => match path.as_ref().extension() {
Some(ext) if ext == "toml" => Self::Toml(TomlSourceType::Unrecognized),
Some(ext) if ext == "md" => Self::Markdown,
Some(ext) if ext == "md" || ext == "qmd" => Self::Markdown,
_ => Self::Python(PySourceType::from(path)),
},
}
Expand Down
22 changes: 21 additions & 1 deletion docs/formatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,27 @@ reformatted code would produce an invalid Python program.

Code blocks marked as `python`, `py`, `python3`, or `py3` will be formatted with
the normal Python code formatting style, while any code blocks marked with
`pyi` will be formatted like Python type stub files.
`pyi` will be formatted like Python type stub files:

````markdown
```py
print("hello")
```

```pyi
def foo(): ...
def bar(): ...
```
````

Ruff also supports [Quarto](https://quarto.org/) style executable code blocks
with curly braces surrounding the language name:

````markdown
```{python}
print("hello")
```
````

While [formatting suppression](#format-suppression) comments will be handled as
usual within code blocks, the formatter will also skip formatting any code block
Expand Down
Loading