Skip to content

Commit fcce5ca

Browse files
authored
chore: Add script to automatically sort members in docs/api-reference (#3200)
1 parent 5838dde commit fcce5ca

File tree

10 files changed

+69
-10
lines changed

10 files changed

+69
-10
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ repos:
4747
entry: python -m utils.check_api_reference
4848
language: python
4949
additional_dependencies: [polars]
50+
- id: sort-api-reference
51+
name: sort-api-reference
52+
pass_filenames: false
53+
entry: python -m utils.sort_api_reference
54+
language: python
5055
- id: imports-are-banned
5156
name: import are banned (use `get_pandas` instead of `import pandas`)
5257
entry: python utils/import_check.py

docs/api-reference/dataframe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@
4444
- shape
4545
- sort
4646
- tail
47-
- top_k
4847
- to_arrow
4948
- to_dict
5049
- to_native
5150
- to_numpy
5251
- to_pandas
5352
- to_polars
53+
- top_k
5454
- unique
5555
- unpivot
5656
- with_columns

docs/api-reference/expr_dt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
- replace_time_zone
1919
- second
2020
- timestamp
21+
- to_string
2122
- total_microseconds
2223
- total_milliseconds
2324
- total_minutes
2425
- total_nanoseconds
2526
- total_seconds
26-
- to_string
2727
- truncate
2828
- weekday
2929
- year

docs/api-reference/lazyframe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
- sink_parquet
2626
- sort
2727
- tail
28-
- top_k
2928
- to_native
29+
- top_k
3030
- unique
3131
- unpivot
3232
- with_columns

docs/api-reference/narwhals.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ Here are the top-level functions available in Narwhals.
4444
- read_parquet
4545
- scan_csv
4646
- scan_parquet
47+
- show_versions
4748
- sum
4849
- sum_horizontal
49-
- show_versions
5050
- to_native
5151
- to_py_scalar
5252
- when

docs/api-reference/schema.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
handler: python
55
options:
66
members:
7-
- names
87
- dtypes
98
- from_arrow
109
- from_native
1110
- from_pandas_like
1211
- from_polars
1312
- len
13+
- names
1414
- to_arrow
1515
- to_pandas
1616
- to_polars

docs/api-reference/series.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@
5959
- median
6060
- min
6161
- mode
62-
- name
6362
- n_unique
63+
- name
6464
- null_count
6565
- pipe
6666
- quantile
@@ -76,8 +76,8 @@
7676
- scatter
7777
- shape
7878
- shift
79-
- sort
8079
- skew
80+
- sort
8181
- sqrt
8282
- std
8383
- sum
@@ -86,10 +86,10 @@
8686
- to_dummies
8787
- to_frame
8888
- to_list
89+
- to_native
8990
- to_numpy
9091
- to_pandas
9192
- to_polars
92-
- to_native
9393
- unique
9494
- value_counts
9595
- var

docs/api-reference/series_dt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
- replace_time_zone
1919
- second
2020
- timestamp
21+
- to_string
2122
- total_microseconds
2223
- total_milliseconds
2324
- total_minutes
2425
- total_nanoseconds
2526
- total_seconds
26-
- to_string
2727
- truncate
2828
- weekday
2929
- year

docs/api-reference/utils.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
handler: python
55
options:
66
members:
7-
- parse_version
87
- Implementation
8+
- parse_version
99
show_root_heading: false
1010
show_source: false
1111
show_bases: false

utils/sort_api_reference.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""Script to automatically sort members lists in API reference markdown files."""
2+
# ruff: noqa: T201
3+
4+
from __future__ import annotations
5+
6+
import re
7+
import sys
8+
from pathlib import Path
9+
10+
11+
def sort_members_in_markdown(file_path: Path) -> int:
12+
"""Sort members lists in a markdown file alphabetically.
13+
14+
Returns:
15+
1 if the file was modified, 0 if no changes were needed.
16+
"""
17+
content = file_path.read_text(encoding="utf-8")
18+
19+
# Pattern matches "members:" followed by list items (lines starting with "- ")
20+
pattern = r"(members:)((?:\n\s+-[^\n]+)+)"
21+
22+
def sort_list(match: re.Match[str]) -> str:
23+
"""Sort a matched members list section."""
24+
prefix = match.group(1) # "members:"
25+
items = match.group(2) # All list items with newlines and indentation
26+
27+
item_pattern = r"(\n\s+-)([^\n]+)" # Extract individual items: (\n + spaces + -) and (content)
28+
matches = re.findall(item_pattern, items)
29+
30+
# Sort alphabetically by the item content (excluding indentation and dash)
31+
sorted_items = sorted(matches, key=lambda x: x[1].strip())
32+
33+
# Reconstruct: "members:" + sorted items with their indentation preserved
34+
return prefix + "".join(f"{indent}{content}" for indent, content in sorted_items)
35+
36+
sorted_content = re.sub(pattern, sort_list, content)
37+
if sorted_content == content:
38+
return 0
39+
40+
file_path.write_text(sorted_content, encoding="utf-8")
41+
print(f"Sorting members in {file_path}")
42+
return 1
43+
44+
45+
PATH = Path("docs") / "api-reference"
46+
FILES_TO_SKIP = {"dtypes", "typing"}
47+
48+
ret = max(
49+
sort_members_in_markdown(file_path=file_path)
50+
for file_path in PATH.glob("*.md")
51+
if file_path.stem not in FILES_TO_SKIP
52+
)
53+
54+
sys.exit(ret)

0 commit comments

Comments
 (0)