-
Notifications
You must be signed in to change notification settings - Fork 44
/
ThemeLink.scala
339 lines (257 loc) · 10.5 KB
/
ThemeLink.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package laika.helium.config
import cats.data.NonEmptyList
import laika.ast.RelativePath.CurrentDocument
import laika.ast.*
import laika.config.{ LaikaKeys, Versions }
import laika.helium.internal.config.HeliumStyles
import laika.parse.{ SourceCursor, SourceFragment }
/** A Helium link type available for navigation bars and the landing page.
*/
sealed trait ThemeLink extends Unresolved {
val source: SourceFragment = SourceCursor.Generated
type Self <: ThemeLink
def unresolvedMessage: String = s"Unresolved theme link: $this"
def runsIn(phase: RewritePhase): Boolean = phase.isInstanceOf[RewritePhase.Render]
}
sealed trait ThemeLinkSpan extends ThemeLink with SpanResolver {
type Self <: ThemeLinkSpan
}
sealed trait ThemeLinkBlock extends ThemeLink with BlockResolver {
type Self <: ThemeLinkBlock
}
sealed trait SingleTargetLink extends ThemeLinkSpan {
/** The target of the link, either internal or external. */
def target: Target
def resolve(cursor: DocumentCursor): Span = target match {
case et: ExternalTarget => createLink(et)
case it: InternalTarget => cursor.validateAndRecover(createLink(it), source)
}
protected def createLink(target: Target): Link
}
sealed trait MultiTargetLink extends ThemeLink {
def links: Seq[SingleTargetLink]
}
/** A link consisting of an icon and optional text.
*/
sealed abstract case class IconLink(
target: Target,
icon: Icon,
text: Option[String] = None,
options: Options = Options.empty
) extends SingleTargetLink {
type Self = IconLink
protected def createLink(target: Target): Link = {
val iconTypeStyle = icon match {
case _: IconGlyph => "glyph-link"
case _: IconStyle => "style-link"
case _: InlineSVGIcon => "svg-link"
case _: SVGSymbolIcon => "svg-link"
}
val allOptions = HeliumStyles.iconLink + Styles(iconTypeStyle) + options
SpanLink(icon +: text.map(Text(_)).toSeq, target, options = allOptions)
}
def withOptions(newOptions: Options): IconLink = new IconLink(target, icon, text, newOptions) {}
}
object IconLink {
/** Creates an icon link to an external target, consisting of an icon and optional text. */
def external(
url: String,
icon: Icon,
text: Option[String] = None,
options: Options = Options.empty
): IconLink =
new IconLink(ExternalTarget(url), icon, text, options) {}
/** Creates an icon link to an internal target, consisting of an icon and optional text. */
def internal(
path: Path,
icon: Icon,
text: Option[String] = None,
options: Options = Options.empty
): IconLink =
new IconLink(InternalTarget(path), icon, text, options) {}
}
/** A link consisting of text and an optional icon, by default rendered in a rounded rectangle.
*/
sealed abstract case class ButtonLink(
target: Target,
text: String,
icon: Option[Icon] = None,
options: Options = Options.empty
) extends SingleTargetLink {
type Self = ButtonLink
protected def createLink(target: Target): Link =
SpanLink(icon.toSeq :+ Text(text), target, options = HeliumStyles.buttonLink + options)
def withOptions(newOptions: Options): ButtonLink =
new ButtonLink(target, text, icon, newOptions) {}
}
object ButtonLink {
/** Creates a button link to an external target, consisting of text and an optional icon rendered in a rounded rectangle. */
def external(
url: String,
text: String,
icon: Option[Icon] = None,
options: Options = Options.empty
): ButtonLink =
new ButtonLink(ExternalTarget(url), text, icon, options) {}
/** Creates a button link to an internal target, consisting of text and an optional icon rendered in a rounded rectangle. */
def internal(
path: Path,
text: String,
icon: Option[Icon] = None,
options: Options = Options.empty
): ButtonLink =
new ButtonLink(InternalTarget(path), text, icon, options) {}
}
/** A simple text link.
*/
sealed abstract case class TextLink(target: Target, text: String, options: Options = Options.empty)
extends SingleTargetLink {
type Self = TextLink
protected def createLink(target: Target): Link =
SpanLink(Seq(Text(text)), target, options = HeliumStyles.textLink + options)
def withOptions(newOptions: Options): TextLink = new TextLink(target, text, newOptions) {}
}
object TextLink {
/** Creates a simple text link to an external target. */
def external(url: String, text: String, options: Options = Options.empty): TextLink =
new TextLink(ExternalTarget(url), text, options) {}
/** Creates a simple text link to an internal target. */
def internal(path: Path, text: String, options: Options = Options.empty): TextLink =
new TextLink(InternalTarget(path), text, options) {}
}
/** A simple image link.
*/
sealed abstract case class ImageLink(target: Target, image: Image, options: Options = Options.empty)
extends SingleTargetLink {
type Self = ImageLink
protected def createLink(target: Target): Link =
SpanLink(Seq(image), target, options = HeliumStyles.imageLink + options)
def withOptions(newOptions: Options): ImageLink =
new ImageLink(target, image, newOptions) {}
}
object ImageLink {
/** Creates a simple image link to an external target. */
def external(url: String, image: Image, options: Options = Options.empty): ImageLink =
new ImageLink(ExternalTarget(url), image, options) {}
/** Creates a simple image link to an internal target. */
def internal(path: Path, image: Image, options: Options = Options.empty): ImageLink =
new ImageLink(InternalTarget(path), image, options) {}
}
/** A home link that inserts a link to the title page of the root document tree (if available)
* or otherwise an invalid element.
*/
final case class DynamicHomeLink(options: Options = Options.empty) extends ThemeLinkSpan {
type Self = DynamicHomeLink
def resolve(cursor: DocumentCursor): Span = {
cursor.root.tree.titleDocument match {
case Some(homePage) =>
IconLink.internal(homePage.path, HeliumIcon.home).resolve(cursor)
case None =>
val message =
"No target for home link found - for options see 'Theme Settings / Top Navigation Bar' in the manual"
InvalidSpan(message, SourceCursor.Generated)
}
}
def withOptions(newOptions: Options): DynamicHomeLink = copy(options = newOptions)
}
object DynamicHomeLink {
val default: DynamicHomeLink = DynamicHomeLink()
}
/** A generic group of theme links.
*
* Can be used to create structures like a row of icon links in a vertical column of text links.
*/
sealed abstract case class LinkGroup(links: Seq[SingleTargetLink], options: Options = Options.empty)
extends ThemeLinkSpan with MultiTargetLink {
type Self = LinkGroup
def resolve(cursor: DocumentCursor): Span =
SpanSequence(links.map(_.resolve(cursor)), HeliumStyles.linkRow + options)
def withOptions(newOptions: Options): LinkGroup = new LinkGroup(links, newOptions) {}
}
object LinkGroup {
def create(link: SingleTargetLink, links: SingleTargetLink*): LinkGroup = new LinkGroup(
link +: links
) {}
}
/** A menu for the top navigation bar or the landing page.
*/
sealed abstract case class Menu(
label: Seq[Span],
links: Seq[SingleTargetLink],
options: Options = Options.empty
) extends ThemeLinkBlock with MultiTargetLink {
type Self = Menu
def resolve(cursor: DocumentCursor): Block = {
val toggle = SpanLink(label, InternalTarget(CurrentDocument()))
.withOptions(HeliumStyles.textLink + HeliumStyles.menuToggle)
val navLinks = links.map(_.resolve(cursor)).collect { case sl: SpanLink =>
NavigationItem(SpanSequence(sl.content), Nil, Some(NavigationLink(sl.target)))
.withOptions(Style.level(1))
}
val content = BlockSequence(NavigationList(navLinks)).withOptions(HeliumStyles.menuContent)
BlockSequence(SpanSequence(toggle), content).withOptions(HeliumStyles.menuContainer + options)
}
def withOptions(newOptions: Options): Menu = new Menu(label, links, newOptions) {}
}
object Menu {
def create(label: String, link: SingleTargetLink, links: SingleTargetLink*): Menu =
new Menu(Seq(Text(label)), link +: links) {}
def create(label: Seq[Span], link: SingleTargetLink, links: SingleTargetLink*): Menu =
new Menu(label, link +: links) {}
}
sealed abstract case class VersionMenu(
versionedLabelPrefix: String,
unversionedLabel: String,
links: Seq[SingleTargetLink],
options: Options = Options.empty
) extends ThemeLinkBlock with MultiTargetLink {
type Self = VersionMenu
def resolve(cursor: DocumentCursor): Block = {
cursor.config.get[Versions].toOption.fold[Block](BlockSequence.empty) { versions =>
val isVersioned = cursor.config.get[Boolean](LaikaKeys.versioned).getOrElse(false)
val labelText =
if (isVersioned) s"$versionedLabelPrefix ${versions.currentVersion.displayValue}"
else unversionedLabel
val menu = new Menu(Seq(Text(labelText)), links, HeliumStyles.versionMenu) {}
menu.resolve(cursor)
}
}
def withOptions(newOptions: Options): VersionMenu =
new VersionMenu(versionedLabelPrefix, unversionedLabel, links, newOptions) {}
}
object VersionMenu {
def create(
versionedLabelPrefix: String = default.versionedLabelPrefix,
unversionedLabel: String = default.unversionedLabel,
additionalLinks: Seq[SingleTargetLink] = Nil
): VersionMenu =
new VersionMenu(versionedLabelPrefix, unversionedLabel, additionalLinks) {}
val default: VersionMenu = create("Version", "Choose Version")
}
sealed abstract class ThemeNavigationSection {
def title: String
def links: NonEmptyList[TextLink]
}
object ThemeNavigationSection {
private final case class Impl(title: String, links: NonEmptyList[TextLink])
extends ThemeNavigationSection {
override def productPrefix = "ThemeNavigationSection"
}
def apply(title: String, link: TextLink, links: TextLink*): ThemeNavigationSection =
Impl(title, NonEmptyList.of(link, links *))
}