Skip to content

Commit 3712175

Browse files
authored
Merge pull request #122 from hsjobeki/feat/prefix
Allow empty prefix
2 parents 4da4347 + e89961f commit 3712175

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# Changelog
22

3+
## Version 3.0.7
4+
5+
Add support for empty prefix flags.
6+
Allows for improved generic usage in nixpkgs/lib and other projects.
7+
8+
Empty prefix is now possible.
9+
Issue: https://github.com/nix-community/nixdoc/issues/119 by @roberth
10+
11+
by @hsjobeki;
12+
13+
in https://github.com/nix-community/nixdoc/pull/122.
14+
315
## Version 3.0.6
416

517
Exposes the package recipe under `recipes.default` so it can easily be re-used.

src/commonmark.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,35 @@ pub struct ManualEntry {
127127
}
128128

129129
impl ManualEntry {
130+
/// Generate the identifier and title for CommonMark.
131+
/// title is the human-readable name of the function.
132+
/// ident is used as URL Encoded link to the function and has thus stricter rules (i.e. "' " in "lib.map' " is not allowed).
133+
pub(crate) fn get_ident_title(&self) -> (String, String) {
134+
let name_prime = self.name.replace('\'', "-prime");
135+
136+
let ident = vec![&self.prefix, &self.category, &name_prime]
137+
.into_iter()
138+
.filter(|x| !x.is_empty())
139+
.cloned()
140+
.collect::<Vec<String>>()
141+
.join(".");
142+
143+
let title = vec![&self.prefix, &self.category, &self.name]
144+
.into_iter()
145+
.filter(|x| !x.is_empty())
146+
.cloned()
147+
.collect::<Vec<String>>()
148+
.join(".");
149+
150+
(ident, title)
151+
}
130152
/// Write a single CommonMark entry for a documented Nix function.
131153
pub fn write_section<W: Write>(
132154
self,
133155
locs: &HashMap<String, String>,
134156
writer: &mut W,
135157
) -> Result<()> {
136-
let title = format!("{}.{}.{}", self.prefix, self.category, self.name);
137-
let ident = format!(
138-
"{}.{}.{}",
139-
self.prefix,
140-
self.category,
141-
self.name.replace('\'', "-prime")
142-
);
143-
158+
let (ident, title) = self.get_ident_title();
144159
writeln!(writer, "## `{}` {{#function-library-{}}}\n", title, ident)?;
145160

146161
// <subtitle> (type signature)

src/test.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fs;
33

44
use std::io::Write;
55

6-
use crate::{collect_entries, format::shift_headings, retrieve_description};
6+
use crate::{collect_entries, format::shift_headings, retrieve_description, ManualEntry};
77

88
#[test]
99
fn test_main() {
@@ -209,3 +209,21 @@ fn test_doc_comment_no_duplicate_arguments() {
209209

210210
insta::assert_snapshot!(output);
211211
}
212+
213+
#[test]
214+
fn test_empty_prefix() {
215+
let test_entry = ManualEntry {
216+
args: vec![],
217+
category: "test".to_string(),
218+
description: vec![],
219+
example: None,
220+
fn_type: None,
221+
name: "mapSimple'".to_string(),
222+
prefix: "".to_string(),
223+
};
224+
225+
let (ident, title) = test_entry.get_ident_title();
226+
227+
assert_eq!(ident, "test.mapSimple-prime");
228+
assert_eq!(title, "test.mapSimple'");
229+
}

0 commit comments

Comments
 (0)