4
0
Fork 0
mirror of https://github.com/Anvilcraft/modpacktools synced 2024-05-19 20:04:07 +02:00

improve modlist

add BuilderContainerTag to make html more kotliny
This commit is contained in:
LordMZTE 2020-08-16 19:44:42 +02:00
parent 74b0d80c33
commit ec2468aaac
3 changed files with 138 additions and 66 deletions

View file

@ -1,19 +1,5 @@
package ley.anvil.modpacktools.commands
import j2html.TagCreator.a
import j2html.TagCreator.b
import j2html.TagCreator.body
import j2html.TagCreator.each
import j2html.TagCreator.head
import j2html.TagCreator.html
import j2html.TagCreator.img
import j2html.TagCreator.li
import j2html.TagCreator.p
import j2html.TagCreator.style
import j2html.TagCreator.table
import j2html.TagCreator.td
import j2html.TagCreator.tr
import j2html.TagCreator.ul
import j2html.utils.CSSMin.compressCss
import ley.anvil.addonscript.wrapper.MetaData
import ley.anvil.modpacktools.MPJH
@ -22,6 +8,7 @@ import ley.anvil.modpacktools.command.AbstractCommand
import ley.anvil.modpacktools.command.CommandReturn
import ley.anvil.modpacktools.command.CommandReturn.Companion.success
import ley.anvil.modpacktools.command.LoadCommand
import ley.anvil.modpacktools.util.BuilderContainerTag.Companion.html
import ley.anvil.modpacktools.util.arg
import ley.anvil.modpacktools.util.fPrintln
import net.sourceforge.argparse4j.impl.Arguments.storeTrue
@ -102,10 +89,9 @@ object CreateModlist : AbstractCommand("CreateModlist") {
private fun doHtml(outFile: File, all: Boolean, sorting: Comparator<MetaData>): CommandReturn {
fPrintln("Making HTML file $outFile", TERMC.green)
val writer = FileWriter(outFile)
val html = html(
head(
style(
//Fancy css!
val html = html {
"head" {
"style"(
compressCss(
IOUtils.toString(
ClassLoader.getSystemResourceAsStream("commands/createmodlist/style.css"),
@ -113,55 +99,91 @@ object CreateModlist : AbstractCommand("CreateModlist") {
)
)
)
),
body(
table(
tr(
td(),
td(b("Name")),
td(b("Contributors")),
td(b("Description"))
),
each(getMods(all, sorting)) {
fPrintln("Writing relation ${it.name}", TERMC.blue)
tr(
td(
if(it.icon != null) a(
img().withSrc(it.icon)
.withClass("img")
).withHref(it.website) else null
),
td(
run {
val a = a(it.name)
//Open in new tab
.withRel("noopener noreferrer")
.withTarget("_blank")
if(it.website != null)
a.withHref(it.website)
a
}
),
td(
ul(
each(it.contributors) {contr ->
li(contr.key)
//for contributor colors
.withClass("contributor_${contr.value.getOrElse(0) {""}}")
}
)
),
td(
each(it.description?.asList() ?: listOf()) {d: String ->
p(d)
}
)
.withClass("description")
)
}
"body" {
"div" {
val meta = MPJH.asWrapper!!.json.meta
withId("header")
"div" {
withClass("img")
meta.icon?.let {
"img" {
withSrc(it)
}
}
}
)
)
).render()
"div" {
"p" {
withHref(meta.website)
"b"("Name")
}
"p"(meta.name ?: "")
}
"div" {
"p" {"b"("Contributors")}
"ul" {
for(con in meta.contributors)
"li"(con.name) {
//for contributor colors
withClass("contributor_${con.roles.getOrElse(0) {""}}")
}
}
}
"div" {
"p" {"b"("Description")}
"p"(meta.description?.joinToString("\n") ?: "")
}
}
"table" {
"tr" {
"td"()
"td" {"b"("Name")}
"td" {"b"("Contributors")}
"td" {"b"("Description")}
}
for(mod in getMods(all, sorting)) {
fPrintln("Writing relation ${mod.name}", TERMC.blue)
"tr" {
"td" {
mod.icon?.let {
"a" {
"img" {
withSrc(it)
withClass("img")
}
withHref(mod.website)
}
}
}
"td" {
"a"(mod.name!!) {
//Open in new tab
withRel("noopener noreferrer")
withTarget("_blank")
mod.website?.let {withHref(it)}
}
}
"td" {
"ul" {
for(con in mod.contributors)
"li"(con.key) {
//for contributor colors
withClass("contributor_${con.value.getOrElse(0) {""}}")
}
}
}
"td" {
for(desc in mod.description ?: arrayOf(""))
"p"(desc)
withClass("description")
}
}
}
}
}
}.render()
writer.write(html)
writer.close()

View file

@ -0,0 +1,32 @@
package ley.anvil.modpacktools.util
import j2html.tags.ContainerTag
import j2html.tags.DomContent
class BuilderContainerTag(tagName: String?) : ContainerTag(tagName) {
companion object {
fun html(block: BuilderContainerTag.() -> Unit) = BuilderContainerTag("html").apply {block()}
}
override fun with(child: DomContent?): BuilderContainerTag {
super.with(child)
return this
}
override fun withText(text: String?): BuilderContainerTag {
super.withText(text)
return this
}
@JvmOverloads
operator fun String.invoke(block: (BuilderContainerTag.() -> Unit)? = null): BuilderContainerTag =
BuilderContainerTag(this).apply {block?.invoke(this)}
.apply {this@BuilderContainerTag.with(this)}
operator fun String.invoke(content: String, block: (BuilderContainerTag.() -> Unit)? = null): BuilderContainerTag =
BuilderContainerTag(this).withText(content)
.apply {
block?.invoke(this)
this@BuilderContainerTag.with(this)
}
}

View file

@ -1,30 +1,48 @@
a:link {
color: #f55;
}
a:visited {
color: #c5c;
}
body {
background-color: #333;
color: #fff;
font-family: sans-serif;
}
.img {
width: 100px;
}
td {
border: #999 3px;
border-style: solid;
}
.description {
width: 100%;
}
.contributor_owner {
color: #f55;
}
.contributor_author {
color: orange;
}
.contributor_contributor {
color: green;
}
#header {
width: 100%;
overflow: hidden;
}
#header div {
margin-right: 10px;
float: left;
}