-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.sbt
56 lines (50 loc) · 2 KB
/
build.sbt
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
organization := "io.github.gitbucket"
name := "gitbucket-emoji-plugin"
version := "4.6.0"
scalaVersion := "2.13.15"
gitbucketVersion := "4.41.0"
val donwloadImagesKey = TaskKey[Unit]("downloadImages")
donwloadImagesKey := {
import java.io.File
import java.net.URL
import java.nio.charset.StandardCharsets
import org.jsoup.Jsoup
import scala.jdk.CollectionConverters._
val baseUrl = "https://www.webfx.com/tools/emoji-cheat-sheet/"
val assetsDir = Keys.sourceDirectory.value / "main" / "resources" / "gitbucket" / "emoji" / "assets"
val existingFiles = assetsDir.listFiles().map(_.getName())
val doc = Jsoup.connect(baseUrl).get()
doc.select(".emojis > li .emoji[data-src]").asScala.foreach { e =>
val name = e.nextElementSibling().text()
if (!existingFiles.contains(s"${name}.png")) {
val image = e.dataset().get("src")
IO.transfer(new URL(s"${baseUrl}${image}").openStream(), new File(assetsDir, s"${name}.png"))
}
}
val files = assetsDir.listFiles().map(_.getName().split("\\.")(0)).sorted
val emojis = files.map { file => s""""${file}"""" }.mkString(",\n ")
val source = s"""package gitbucket.emoji
|
|/**
| * Converts an Emoji like :smiley: to its related image
| */
|object EmojiUtil {
|
| val emojis = Set(
| ${emojis}
| )
|
| private val emojiPattern = ${"\"\"\""}\\:[a-z0-9_\\-\\+]+\\:${"\"\"\""}.r
|
| def convertEmojis(text: String)(implicit context: gitbucket.core.controller.Context): String =
| emojiPattern.replaceAllIn(text, e => {
| val emoji = e.group(0).replaceAll(":", "")
| if (!emojis.contains(emoji)) s":$$emoji:"
| else s${"\"\"\""}<img src="$${context.baseUrl}/plugin-assets/emoji/$${emoji}.png" alt=":$$emoji:" class="emoji" />${"\"\"\""}
| })
|
|}
|""".stripMargin
val sourceFile = Keys.sourceDirectory.value / "main" / "scala" / "gitbucket" / "emoji" / "EmojiUtil.scala"
IO.write(sourceFile, source, StandardCharsets.UTF_8)
}