Skip to content

Commit ac0a81e

Browse files
author
Marek Suchánek
authored
Merge pull request #34 from redhat-documentation/no-id-prefixes-21
Separate options for the module type prefix in IDs (anchors) and file names
2 parents 685b115 + 6afbf5d commit ac0a81e

17 files changed

+158
-75
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
The following is a summary of changes in each `newdoc` release, which is also a Git tag by the same name in this repository.
44

5+
## v2.11.0
6+
7+
* Separate options for the module type prefix in IDs (anchors) and file names.
8+
59
## v2.10.6
610

711
* A prettier confirmation prompt when overwriting a file.

Cargo.lock

+13-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[package]
22
name = "newdoc"
3-
version = "2.10.6"
3+
version = "2.11.0"
44
description = "Generate pre-populated module files formatted with AsciiDoc that are used in Red Hat and Fedora documentation."
55
authors = ["Marek Suchánek <[email protected]>"]
66
license = "GPL-3.0-or-later"
77
edition = "2021"
88
# Check the Rust version using `cargo msrv verify`.
9-
rust-version = "1.59"
9+
rust-version = "1.60"
1010
documentation = "https://docs.rs/newdoc"
1111
readme = "README.md"
1212
repository = "https://github.com/mrksu/newdoc"

docs/using-newdoc.adoc

+4-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,10 @@ $ newdoc --validate modules/con_proper-module.adoc
280280

281281
* To generate the file without the example, placeholder content, add the `--no-examples` or `-E` option when creating documents.
282282

283-
* To create the file without the module type prefix in the ID and the file name, add the `--no-prefixes` or `-P` option.
283+
* By default, the content type prefix appears in the generated file name and not in the ID (anchor). To change this behavior, use the following options:
284+
+
285+
`--no-file-prefixes` or `-P`:: Disables the file-name prefix.
286+
`--anchor-prefixes` or `-A`:: Enables the ID (anchor) prefix.
284287

285288
* To specify the directory where `newdoc` saves the generated file, add the `--target-dir=<directory>` or `-T <directory>` option.
286289

src/cmd_line.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,13 @@ pub struct Cli {
7777
#[arg(short = 'E', long = "no-examples", alias = "expert-mode")]
7878
pub no_examples: bool,
7979

80-
/// Do not use module type prefixes (such as `proc_`) in IDs and file names
81-
#[arg(short = 'P', long = "no-prefixes")]
82-
pub no_prefixes: bool,
80+
/// Do not use module type prefixes (such as `proc_`) in file names
81+
#[arg(short = 'P', long, alias = "no-prefixes")]
82+
pub no_file_prefixes: bool,
83+
84+
/// Add use module type prefixes (such as `proc_`) in AsciiDoc anchors
85+
#[arg(short = 'A', long)]
86+
pub anchor_prefixes: bool,
8387

8488
/// Save the generated files in this directory
8589
#[arg(short = 'T', long = "target-dir", value_name = "DIRECTORY")]

src/lib.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ const REGEX_ERROR: &str = "Failed to construct a regular expression. Please repo
5757
#[derive(Debug, Clone)]
5858
pub struct Options {
5959
pub comments: bool,
60-
pub prefixes: bool,
60+
pub file_prefixes: bool,
61+
pub anchor_prefixes: bool,
6162
pub examples: bool,
6263
pub target_dir: PathBuf,
6364
pub verbosity: Verbosity,
@@ -83,7 +84,8 @@ impl Options {
8384
// on the command line. If the no-comments or no-prefixes option is passed,
8485
// the feature is disabled, so the option is set to false.
8586
comments: !cli.no_comments,
86-
prefixes: !cli.no_prefixes,
87+
file_prefixes: !cli.no_file_prefixes,
88+
anchor_prefixes: cli.anchor_prefixes,
8789
examples: !cli.no_examples,
8890
// Set the target directory as specified or fall back on the current directory
8991
target_dir: cli.target_dir.clone().unwrap_or_else(|| PathBuf::from(".")),
@@ -96,7 +98,8 @@ impl Default for Options {
9698
fn default() -> Self {
9799
Self {
98100
comments: true,
99-
prefixes: true,
101+
file_prefixes: true,
102+
anchor_prefixes: false,
100103
examples: true,
101104
target_dir: PathBuf::from("."),
102105
verbosity: Verbosity::Default,

src/module.rs

+91-24
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub struct Input {
6161
pub struct Module {
6262
mod_type: ContentType,
6363
title: String,
64-
id: String,
64+
anchor: String,
6565
pub file_name: String,
6666
pub include_statement: String,
6767
includes: Option<Vec<String>>,
@@ -108,7 +108,7 @@ impl Input {
108108
/// let options = Options::default();
109109
/// let input = Input::new(mod_type, title, &options);
110110
///
111-
/// assert_eq!("con_a-test-with-problematic-characters", input.id());
111+
/// assert_eq!("a-test-with-problematic-characters", input.id());
112112
/// ```
113113
#[must_use]
114114
pub fn id(&self) -> String {
@@ -188,34 +188,99 @@ impl Input {
188188
title_with_replacements = title_with_replacements[..len - 1].to_string();
189189
}
190190

191-
let prefix = self.prefix();
192-
193-
format!("{}{}", prefix, title_with_replacements)
191+
title_with_replacements
194192
}
195193

196194
/// Prepare the file name for the generated file.
197195
///
198-
/// The file name is based on the module ID, with the `.adoc` extension.
196+
/// The file name is based on the module ID,
197+
/// with an optional prefix and the `.adoc` extension.
198+
///
199+
/// # Examples
200+
///
201+
/// ```
202+
/// use newdoc::{ContentType, Input, Options};
203+
///
204+
/// let mod_type = ContentType::Concept;
205+
/// let title = "Default file name configuration";
206+
/// let options = Options::default();
207+
/// let input = Input::new(mod_type, title, &options);
208+
///
209+
/// assert_eq!("con_default-file-name-configuration.adoc", input.file_name());
210+
///
211+
/// let mod_type = ContentType::Concept;
212+
/// let title = "No prefix file name configuration";
213+
/// let options = Options {
214+
/// file_prefixes: false,
215+
/// ..Default::default()
216+
/// };
217+
/// let input = Input::new(mod_type, title, &options);
218+
///
219+
/// assert_eq!("no-prefix-file-name-configuration.adoc", input.file_name());
220+
/// ```
199221
#[must_use]
200222
pub fn file_name(&self) -> String {
223+
// Add a prefix only if they're enabled.
224+
let prefix = if self.options.file_prefixes {
225+
self.prefix()
226+
} else {
227+
""
228+
};
229+
230+
let id = self.id();
231+
201232
let suffix = ".adoc";
202233

203-
self.id() + suffix
234+
[prefix, &id, suffix].join("")
204235
}
205236

206-
fn prefix(&self) -> &'static str {
207-
if self.options.prefixes {
208-
// If prefixes are enabled, pick the right file prefix
209-
match self.mod_type {
210-
ContentType::Assembly => "assembly_",
211-
ContentType::Concept => "con_",
212-
ContentType::Procedure => "proc_",
213-
ContentType::Reference => "ref_",
214-
ContentType::Snippet => "snip_",
215-
}
237+
/// Prepare the AsciiDoc anchor or ID.
238+
///
239+
/// The anchor is based on the module ID, with an optional prefix.
240+
///
241+
/// # Examples
242+
///
243+
/// ```
244+
/// use newdoc::{ContentType, Input, Options};
245+
///
246+
/// let mod_type = ContentType::Concept;
247+
/// let title = "Default anchor configuration";
248+
/// let options = Options::default();
249+
/// let input = Input::new(mod_type, title, &options);
250+
///
251+
/// assert_eq!("default-anchor-configuration", input.anchor());
252+
///
253+
/// let mod_type = ContentType::Concept;
254+
/// let title = "Prefix anchor configuration";
255+
/// let options = Options {
256+
/// anchor_prefixes: true,
257+
/// ..Default::default()
258+
/// };
259+
/// let input = Input::new(mod_type, title, &options);
260+
///
261+
/// assert_eq!("con_prefix-anchor-configuration", input.anchor());
262+
#[must_use]
263+
pub fn anchor(&self) -> String {
264+
// Add a prefix only if they're enabled.
265+
let prefix = if self.options.anchor_prefixes {
266+
self.prefix()
216267
} else {
217-
// If prefixes are disabled, use an empty string for the prefix
218268
""
269+
};
270+
271+
let id = self.id();
272+
273+
[prefix, &id].join("")
274+
}
275+
276+
/// Pick the right file and ID prefix depending on the content type.
277+
fn prefix(&self) -> &'static str {
278+
match self.mod_type {
279+
ContentType::Assembly => "assembly_",
280+
ContentType::Concept => "con_",
281+
ContentType::Procedure => "proc_",
282+
ContentType::Reference => "ref_",
283+
ContentType::Snippet => "snip_",
219284
}
220285
}
221286

@@ -285,7 +350,7 @@ impl From<Input> for Module {
285350
let module = Module {
286351
mod_type: input.mod_type,
287352
title: input.title.clone(),
288-
id: input.id(),
353+
anchor: input.anchor(),
289354
file_name: input.file_name(),
290355
include_statement: input.include_statement(),
291356
includes: input.includes.clone(),
@@ -294,7 +359,7 @@ impl From<Input> for Module {
294359

295360
log::debug!("Generated module properties:");
296361
log::debug!("Type: {:?}", &module.mod_type);
297-
log::debug!("ID: {}", &module.id);
362+
log::debug!("Anchor: {}", &module.anchor);
298363
log::debug!("File name: {}", &module.file_name);
299364
log::debug!("Include statement: {}", &module.include_statement);
300365
log::debug!(
@@ -328,7 +393,8 @@ mod tests {
328393
fn basic_options() -> Options {
329394
Options {
330395
comments: false,
331-
prefixes: true,
396+
file_prefixes: true,
397+
anchor_prefixes: false,
332398
examples: true,
333399
target_dir: PathBuf::from("."),
334400
verbosity: Verbosity::Default,
@@ -338,7 +404,8 @@ mod tests {
338404
fn path_options() -> Options {
339405
Options {
340406
comments: false,
341-
prefixes: true,
407+
file_prefixes: true,
408+
anchor_prefixes: false,
342409
examples: true,
343410
target_dir: PathBuf::from("repo/modules/topic/"),
344411
verbosity: Verbosity::Default,
@@ -360,8 +427,8 @@ mod tests {
360427
"A testing assembly with /special-characters*"
361428
);
362429
assert_eq!(
363-
assembly.id,
364-
"assembly_a-testing-assembly-with-special-characters"
430+
assembly.anchor,
431+
"a-testing-assembly-with-special-characters"
365432
);
366433
assert_eq!(
367434
assembly.file_name,

0 commit comments

Comments
 (0)