Skip to content

Commit 2def30e

Browse files
authored
Merge pull request #97 from Janik-Haag/master
feat: support custom prefixes
2 parents 4615290 + 1cd046a commit 2def30e

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## 2.7.0
4+
5+
- Added support to customise the attribute set prefix, which was previously hardcoded to `lib`.
6+
The default is still `lib`, but you can pass `--prefix` now to use something else like `utils`.
7+
8+
By @Janik-Haag in https://github.com/nix-community/nixdoc/pull/97
9+
310
## 2.6.0
411

512
- After doing a great job of maintaining the project for this year, @asymmetric is passing on the torch to @infinisil!

src/commonmark.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ fn handle_indentation(raw: &str) -> String {
102102
/// Represents a single manual section describing a library function.
103103
#[derive(Clone, Debug)]
104104
pub struct ManualEntry {
105+
/// Prefix for the category (e.g. 'lib' or 'utils').
106+
pub prefix: String,
107+
105108
/// Name of the function category (e.g. 'strings', 'trivial', 'attrsets')
106109
pub category: String,
107110

@@ -130,9 +133,10 @@ impl ManualEntry {
130133
locs: &HashMap<String, String>,
131134
writer: &mut W,
132135
) -> Result<()> {
133-
let title = format!("lib.{}.{}", self.category, self.name);
136+
let title = format!("{}.{}.{}", self.prefix, self.category, self.name);
134137
let ident = format!(
135-
"lib.{}.{}",
138+
"{}.{}.{}",
139+
self.prefix,
136140
self.category,
137141
self.name.replace('\'', "-prime")
138142
);

src/main.rs

+26-12
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ use std::path::PathBuf;
4343
#[derive(Debug, Parser)]
4444
#[command(author, version, about)]
4545
struct Options {
46+
/// Prefix for the category (e.g. 'lib' or 'utils').
47+
#[arg(short, long, default_value_t = String::from("lib"))]
48+
prefix: String,
49+
4650
/// Name of the function category (e.g. 'strings', 'attrsets').
4751
#[arg(short, long)]
4852
category: String,
@@ -80,8 +84,9 @@ struct DocItem {
8084
}
8185

8286
impl DocItem {
83-
fn into_entry(self, category: &str) -> ManualEntry {
87+
fn into_entry(self, prefix: &str, category: &str) -> ManualEntry {
8488
ManualEntry {
89+
prefix: prefix.to_string(),
8590
category: category.to_string(),
8691
name: self.name,
8792
description: self
@@ -329,6 +334,7 @@ fn collect_entry_information(entry: AttrpathValue) -> Option<DocItem> {
329334
// - as inherits
330335
fn collect_bindings(
331336
node: &SyntaxNode,
337+
prefix: &str,
332338
category: &str,
333339
scope: HashMap<String, ManualEntry>,
334340
) -> Vec<ManualEntry> {
@@ -339,7 +345,8 @@ fn collect_bindings(
339345
for child in n.children() {
340346
if let Some(apv) = AttrpathValue::cast(child.clone()) {
341347
entries.extend(
342-
collect_entry_information(apv).map(|di| di.into_entry(category)),
348+
collect_entry_information(apv)
349+
.map(|di| di.into_entry(prefix, category)),
343350
);
344351
} else if let Some(inh) = Inherit::cast(child) {
345352
// `inherit (x) ...` needs much more handling than we can
@@ -366,7 +373,7 @@ fn collect_bindings(
366373

367374
// Main entrypoint for collection
368375
// TODO: document
369-
fn collect_entries(root: rnix::Root, category: &str) -> Vec<ManualEntry> {
376+
fn collect_entries(root: rnix::Root, prefix: &str, category: &str) -> Vec<ManualEntry> {
370377
// we will look into the top-level let and its body for function docs.
371378
// we only need a single level of scope for this.
372379
// since only the body can export a function we don't need to implement
@@ -376,16 +383,17 @@ fn collect_entries(root: rnix::Root, category: &str) -> Vec<ManualEntry> {
376383
WalkEvent::Enter(n) if n.kind() == SyntaxKind::NODE_LET_IN => {
377384
return collect_bindings(
378385
LetIn::cast(n.clone()).unwrap().body().unwrap().syntax(),
386+
prefix,
379387
category,
380388
n.children()
381389
.filter_map(AttrpathValue::cast)
382390
.filter_map(collect_entry_information)
383-
.map(|di| (di.name.to_string(), di.into_entry(category)))
391+
.map(|di| (di.name.to_string(), di.into_entry(prefix, category)))
384392
.collect(),
385393
);
386394
}
387395
WalkEvent::Enter(n) if n.kind() == SyntaxKind::NODE_ATTR_SET => {
388-
return collect_bindings(&n, category, Default::default());
396+
return collect_bindings(&n, prefix, category, Default::default());
389397
}
390398
_ => (),
391399
}
@@ -424,7 +432,7 @@ fn main() {
424432
// TODO: move this to commonmark.rs
425433
writeln!(output, "{}", description).expect("Failed to write header");
426434

427-
for entry in collect_entries(nix, &opts.category) {
435+
for entry in collect_entries(nix, &opts.prefix, &opts.category) {
428436
entry
429437
.write_section(&locs, &mut output)
430438
.expect("Failed to write section")
@@ -438,6 +446,7 @@ fn test_main() {
438446
let locs = serde_json::from_str(&fs::read_to_string("test/strings.json").unwrap()).unwrap();
439447
let nix = rnix::Root::parse(&src).ok().expect("failed to parse input");
440448
let desc = "string manipulation functions";
449+
let prefix = "lib";
441450
let category = "strings";
442451

443452
// TODO: move this to commonmark.rs
@@ -448,7 +457,7 @@ fn test_main() {
448457
)
449458
.expect("Failed to write header");
450459

451-
for entry in collect_entries(nix, category) {
460+
for entry in collect_entries(nix, prefix, category) {
452461
entry
453462
.write_section(&locs, &mut output)
454463
.expect("Failed to write section")
@@ -464,11 +473,12 @@ fn test_description_of_lib_debug() {
464473
let mut output = Vec::new();
465474
let src = fs::read_to_string("test/lib-debug.nix").unwrap();
466475
let nix = rnix::Root::parse(&src).ok().expect("failed to parse input");
476+
let prefix = "lib";
467477
let category = "debug";
468478
let desc = retrieve_description(&nix, &"Debug", category);
469479
writeln!(output, "{}", desc).expect("Failed to write header");
470480

471-
for entry in collect_entries(nix, category) {
481+
for entry in collect_entries(nix, prefix, category) {
472482
entry
473483
.write_section(&Default::default(), &mut output)
474484
.expect("Failed to write section")
@@ -484,9 +494,10 @@ fn test_arg_formatting() {
484494
let mut output = Vec::new();
485495
let src = fs::read_to_string("test/arg-formatting.nix").unwrap();
486496
let nix = rnix::Root::parse(&src).ok().expect("failed to parse input");
497+
let prefix = "lib";
487498
let category = "options";
488499

489-
for entry in collect_entries(nix, category) {
500+
for entry in collect_entries(nix, prefix, category) {
490501
entry
491502
.write_section(&Default::default(), &mut output)
492503
.expect("Failed to write section")
@@ -502,9 +513,10 @@ fn test_inherited_exports() {
502513
let mut output = Vec::new();
503514
let src = fs::read_to_string("test/inherited-exports.nix").unwrap();
504515
let nix = rnix::Root::parse(&src).ok().expect("failed to parse input");
516+
let prefix = "lib";
505517
let category = "let";
506518

507-
for entry in collect_entries(nix, category) {
519+
for entry in collect_entries(nix, prefix, category) {
508520
entry
509521
.write_section(&Default::default(), &mut output)
510522
.expect("Failed to write section")
@@ -520,9 +532,10 @@ fn test_line_comments() {
520532
let mut output = Vec::new();
521533
let src = fs::read_to_string("test/line-comments.nix").unwrap();
522534
let nix = rnix::Root::parse(&src).ok().expect("failed to parse input");
535+
let prefix = "lib";
523536
let category = "let";
524537

525-
for entry in collect_entries(nix, category) {
538+
for entry in collect_entries(nix, prefix, category) {
526539
entry
527540
.write_section(&Default::default(), &mut output)
528541
.expect("Failed to write section")
@@ -538,9 +551,10 @@ fn test_multi_line() {
538551
let mut output = Vec::new();
539552
let src = fs::read_to_string("test/multi-line.nix").unwrap();
540553
let nix = rnix::Root::parse(&src).ok().expect("failed to parse input");
554+
let prefix = "lib";
541555
let category = "let";
542556

543-
for entry in collect_entries(nix, category) {
557+
for entry in collect_entries(nix, prefix, category) {
544558
entry
545559
.write_section(&Default::default(), &mut output)
546560
.expect("Failed to write section")

0 commit comments

Comments
 (0)