-
Notifications
You must be signed in to change notification settings - Fork 44
/
SlugBuilder.scala
53 lines (46 loc) · 1.95 KB
/
SlugBuilder.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
/*
* 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.api.bundle
/** Default implementation for the logic that transforms section titles, document names
* and user-provided ids to a slug that is compatible with HTML/XML ids, URLs and file names.
*
* @author Jens Halm
*/
object SlugBuilder {
/** Default slug builder that turns the specified string into a slug suitable
* as HTML or XML id, as a path segment in a URL and as a file name.
*
* Iterates over the input text one Unicode code point at a time.
* All code points that are letters or numbers or underscore are considered valid characters.
* They are mapped to lower case, and included in the output.
* All other code points are considered invalid characters,
* and any sequence of such code points will be replaced by a single dash character (`-`),
* unless the sequence starts or ends the string in which case it is simply dropped.
*/
def default(text: String): String = {
val slug = text.foldLeft(("", true)) { case ((res, inWord), char) =>
if (Character.isLetter(char) || Character.isDigit(char)) (res + char.toLower, true)
else if (char == '_') (res + char, true)
else if (inWord) (res + "-", false)
else (res, false)
}
._1
.stripPrefix("-")
.stripSuffix("-")
if (slug.headOption.exists(Character.isLetter)) slug
else "_" + slug
}
}