PijulTag.kt
/**
* Dracon - An IntelliJ-Pijul integration.
* Copyright 2021 JonathanxD <jhrldev@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.github.jonathanxd.dracon.log
import com.github.jonathanxd.dracon.push.parseDate
import org.tomlj.Toml
import java.time.ZonedDateTime
import java.util.*
data class PijulTag(
val hash: String,
val authors: List<Author>,
val date: ZonedDateTime,
val state: String,
val message: String
)
val PijulTag.simpleMessage get() =
if (this.message.isEmpty()) ""
else this.message.split("\n").first { it.trim().isNotEmpty() }.trim()
val AUTHOR_REGEX = Regex("Author \\{ name: (\"([^\"]+)\"|None), full_name: (Some\\(\"([^\"]+)\"\\)|None), email: (Some\\(\"([^\"]+)\"\\)|None) }")
fun String.parseTags(): List<PijulTag> {
if (this.isEmpty()) return emptyList()
val lines = this.lines().listIterator()
val tags = mutableListOf<PijulTag>()
while (lines.hasNext()) {
var hash: String? = null
val authors = mutableListOf<Author>()
var date: ZonedDateTime? = null
var state: String? = null
val message = StringJoiner("\n")
var messageStarted = false
while (lines.hasNext()) {
val line = lines.next()
if (line.startsWith("Tag")) {
hash = line.substring("Tag ".length)
} else if (line.startsWith("Author: ")) {
val finds = AUTHOR_REGEX.findAll(line)
for (find in finds) {
val groups = find.groups
val name: String? = groups[2]?.value
val fullName: String? = groups[4]?.value
val email: String? = groups[6]?.value
authors += Author(name, fullName, email)
}
} else if (line.startsWith("Date: ")) {
val dateString = line.substring("Date: ".length)
date = dateString.parseDate()
} else if (line.startsWith("State: ")) {
state = line.substring("State: ".length)
} else {
if (line.startsWith("Tag")) {
lines.previous()
break;
} else if (line.trim().isEmpty() && state != null && !messageStarted) {
messageStarted = true
} else {
if (line.startsWith(" ")) {
message.add(line.substring(" ".length))
} else {
message.add(line)
}
}
}
}
tags += PijulTag(hash!!, authors, date!!, state!!, message.toString().removeTrailingLineJump())
}
return tags
}
private val TRAILING_LINE_JUMP = Regex("[\n]+$")
fun String.removeTrailingLineJump() =
this.replace(TRAILING_LINE_JUMP, "")