@@ -61,7 +61,7 @@ pub struct Input {
61
61
pub struct Module {
62
62
mod_type : ContentType ,
63
63
title : String ,
64
- id : String ,
64
+ anchor : String ,
65
65
pub file_name : String ,
66
66
pub include_statement : String ,
67
67
includes : Option < Vec < String > > ,
@@ -108,7 +108,7 @@ impl Input {
108
108
/// let options = Options::default();
109
109
/// let input = Input::new(mod_type, title, &options);
110
110
///
111
- /// assert_eq!("con_a -test-with-problematic-characters", input.id());
111
+ /// assert_eq!("a -test-with-problematic-characters", input.id());
112
112
/// ```
113
113
#[ must_use]
114
114
pub fn id ( & self ) -> String {
@@ -188,34 +188,99 @@ impl Input {
188
188
title_with_replacements = title_with_replacements[ ..len - 1 ] . to_string ( ) ;
189
189
}
190
190
191
- let prefix = self . prefix ( ) ;
192
-
193
- format ! ( "{}{}" , prefix, title_with_replacements)
191
+ title_with_replacements
194
192
}
195
193
196
194
/// Prepare the file name for the generated file.
197
195
///
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
+ /// ```
199
221
#[ must_use]
200
222
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
+
201
232
let suffix = ".adoc" ;
202
233
203
- self . id ( ) + suffix
234
+ [ prefix , & id , suffix] . join ( "" )
204
235
}
205
236
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 ( )
216
267
} else {
217
- // If prefixes are disabled, use an empty string for the prefix
218
268
""
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_" ,
219
284
}
220
285
}
221
286
@@ -285,7 +350,7 @@ impl From<Input> for Module {
285
350
let module = Module {
286
351
mod_type : input. mod_type ,
287
352
title : input. title . clone ( ) ,
288
- id : input. id ( ) ,
353
+ anchor : input. anchor ( ) ,
289
354
file_name : input. file_name ( ) ,
290
355
include_statement : input. include_statement ( ) ,
291
356
includes : input. includes . clone ( ) ,
@@ -294,7 +359,7 @@ impl From<Input> for Module {
294
359
295
360
log:: debug!( "Generated module properties:" ) ;
296
361
log:: debug!( "Type: {:?}" , & module. mod_type) ;
297
- log:: debug!( "ID : {}" , & module. id ) ;
362
+ log:: debug!( "Anchor : {}" , & module. anchor ) ;
298
363
log:: debug!( "File name: {}" , & module. file_name) ;
299
364
log:: debug!( "Include statement: {}" , & module. include_statement) ;
300
365
log:: debug!(
@@ -328,7 +393,8 @@ mod tests {
328
393
fn basic_options ( ) -> Options {
329
394
Options {
330
395
comments : false ,
331
- prefixes : true ,
396
+ file_prefixes : true ,
397
+ anchor_prefixes : false ,
332
398
examples : true ,
333
399
target_dir : PathBuf :: from ( "." ) ,
334
400
verbosity : Verbosity :: Default ,
@@ -338,7 +404,8 @@ mod tests {
338
404
fn path_options ( ) -> Options {
339
405
Options {
340
406
comments : false ,
341
- prefixes : true ,
407
+ file_prefixes : true ,
408
+ anchor_prefixes : false ,
342
409
examples : true ,
343
410
target_dir : PathBuf :: from ( "repo/modules/topic/" ) ,
344
411
verbosity : Verbosity :: Default ,
@@ -360,8 +427,8 @@ mod tests {
360
427
"A testing assembly with /special-characters*"
361
428
) ;
362
429
assert_eq ! (
363
- assembly. id ,
364
- "assembly_a -testing-assembly-with-special-characters"
430
+ assembly. anchor ,
431
+ "a -testing-assembly-with-special-characters"
365
432
) ;
366
433
assert_eq ! (
367
434
assembly. file_name,
0 commit comments