16
16
17
17
package laika .rewrite .nav
18
18
19
- import laika .config .{
20
- Config ,
21
- ConfigBuilder ,
22
- ConfigDecoder ,
23
- ConfigEncoder ,
24
- DefaultKey ,
25
- Key ,
26
- LaikaKeys ,
27
- ValidationError
28
- }
19
+ import laika .config .*
29
20
30
21
/** Configuration for autonumbering of documents and sections.
31
22
*/
32
- case class AutonumberConfig (
33
- documents : Boolean = true ,
34
- sections : Boolean = true ,
35
- maxDepth : Int = Int .MaxValue
36
- )
23
+ sealed abstract class AutonumberConfig {
37
24
38
- private [nav] sealed trait Scope
25
+ /** Applies autonumbering to directories and documents and their titles. */
26
+ def documents : Boolean
39
27
40
- private [nav] object Scope {
41
- case object Documents extends Scope
42
- case object Sections extends Scope
43
- case object All extends Scope
44
- case object None extends Scope
28
+ /** Applies autonumber to section header within documents. */
29
+ def sections : Boolean
45
30
46
- implicit val decoder : ConfigDecoder [Scope ] = ConfigDecoder .string.flatMap {
47
- case " documents" => Right (Documents )
48
- case " sections" => Right (Sections )
49
- case " all" => Right (All )
50
- case " none" => Right (None )
51
- case other => Left (ValidationError (s " Invalid value for autonumbering.scope: $other" ))
52
- }
31
+ /** Specifies how many levels deep the autonumbering should be applied.
32
+ * Any documents or section headers beyond this limit will be ignored.
33
+ *
34
+ * The default is unlimited depth.
35
+ */
36
+ def maxDepth : Int
53
37
38
+ /** Specifies how many levels deep the autonumbering should be applied.
39
+ * Any documents or section headers beyond this limit will be ignored.
40
+ */
41
+ def withMaxDepth (value : Int ): AutonumberConfig
54
42
}
55
43
56
44
object AutonumberConfig {
57
45
46
+ private final case class Impl (documents : Boolean , sections : Boolean , maxDepth : Int )
47
+ extends AutonumberConfig {
48
+ override def productPrefix : String = " AutonumberConfig"
49
+ def withMaxDepth (value : Int ): AutonumberConfig = copy(maxDepth = value)
50
+ }
51
+
58
52
private val scopeKey = Key (" scope" )
59
53
private val depthKey = Key (" depth" )
60
54
@@ -71,7 +65,7 @@ object AutonumberConfig {
71
65
case Scope .All => (true , true )
72
66
case Scope .None => (false , false )
73
67
}
74
- AutonumberConfig (documents, sections, depth)
68
+ Impl (documents, sections, depth)
75
69
}
76
70
}
77
71
@@ -92,7 +86,9 @@ object AutonumberConfig {
92
86
/** Disables section numbering for the specified config instance.
93
87
* Retains the existing value for auto-numbering of documents.
94
88
*/
95
- def withoutSectionNumbering (config : Config )(builder : ConfigBuilder ): ConfigBuilder = {
89
+ private [laika] def withoutSectionNumbering (
90
+ config : Config
91
+ )(builder : ConfigBuilder ): ConfigBuilder = {
96
92
val key = LaikaKeys .autonumbering.child(scopeKey)
97
93
config.get[Scope ](key).toOption.fold(builder) {
98
94
case Scope .Documents => builder
@@ -102,10 +98,43 @@ object AutonumberConfig {
102
98
}
103
99
}
104
100
105
- /** The defaults for autonumbering with section
106
- * and document numbering both switched off.
101
+ /** Section numbering within documents switched on, but document and directory numbering switched off.
102
+ */
103
+ def sectionsEnabled : AutonumberConfig =
104
+ Impl (documents = false , sections = true , maxDepth = Int .MaxValue )
105
+
106
+ /** Document and directory numbering switched on, but section numbering within documents switched off.
107
+ */
108
+ def documentsEnabled : AutonumberConfig =
109
+ Impl (documents = true , sections = false , maxDepth = Int .MaxValue )
110
+
111
+ /** Section and document numbering both switched on.
107
112
*/
108
- def defaults : AutonumberConfig =
109
- AutonumberConfig (documents = false , sections = false , maxDepth = 0 )
113
+ def allEnabled : AutonumberConfig =
114
+ Impl (documents = true , sections = true , maxDepth = Int .MaxValue )
115
+
116
+ /** Section and document numbering both switched off.
117
+ */
118
+ def disabled : AutonumberConfig =
119
+ Impl (documents = false , sections = false , maxDepth = 0 )
120
+
121
+ }
122
+
123
+ private [nav] sealed trait Scope
124
+
125
+ private [nav] object Scope {
126
+
127
+ case object Documents extends Scope
128
+ case object Sections extends Scope
129
+ case object All extends Scope
130
+ case object None extends Scope
131
+
132
+ implicit val decoder : ConfigDecoder [Scope ] = ConfigDecoder .string.flatMap {
133
+ case " documents" => Right (Documents )
134
+ case " sections" => Right (Sections )
135
+ case " all" => Right (All )
136
+ case " none" => Right (None )
137
+ case other => Left (ValidationError (s " Invalid value for autonumbering.scope: $other" ))
138
+ }
110
139
111
140
}
0 commit comments