task 6: HTML export
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package ch.hevs.isc.slopify_v2
|
||||
|
||||
import java.io.{File, FileInputStream, FileOutputStream, ObjectInput, ObjectInputStream, ObjectOutputStream}
|
||||
import java.io._
|
||||
import scala.collection.immutable.HashMap
|
||||
import scala.io.{BufferedSource, Source}
|
||||
|
||||
object DataBaseHelper {
|
||||
def create(directory:String) : DataBase = {
|
||||
@@ -66,16 +68,99 @@ object DataBaseHelper {
|
||||
return db
|
||||
}
|
||||
|
||||
def exportHTML(directory: String, dataBase: DataBase): Unit = {
|
||||
var artists: Array[Artist] = dataBase.getArtists().sortBy(_.name)
|
||||
|
||||
var artistRows: String = ""
|
||||
for (artist: Artist <- artists) {
|
||||
val artistFileName: String = formatName(artist.name)
|
||||
val artistUrl: String = s"./$artistFileName.html"
|
||||
artistRows += s"<tr><td><a href='$artistUrl'>${artist.name}</a></td></tr>\n"
|
||||
|
||||
val albums: Array[Album] = artist.getAlbums().sortBy(_.name)
|
||||
var albumRows: String = ""
|
||||
for (album: Album <- albums) {
|
||||
val albumFileName: String = artistFileName + "_" + formatName(album.name)
|
||||
val albumUrl: String = s"./$albumFileName.html"
|
||||
albumRows += s"<tr><td><a href='$albumUrl'>${album.name}</a></td></tr>\n"
|
||||
|
||||
val songs: Array[Song] = album.getSongs().sortBy(_.number)
|
||||
var songRows: String = ""
|
||||
for (song: Song <- songs) {
|
||||
val songFileName: String = albumFileName + "_" + formatName(song.number + "-" + song.title)
|
||||
val songUrl: String = s"./$songFileName.html"
|
||||
songRows += s"<tr><td>${song.number}</td><td><a href='$songUrl'>${song.title}</a></td></tr>\n"
|
||||
|
||||
formatTemplate("res/template/song.html", HashMap(
|
||||
"artist.name" -> artist.name,
|
||||
"artist.page" -> artistUrl,
|
||||
"album.name" -> album.name,
|
||||
"album.page" -> albumUrl,
|
||||
"song.title" -> song.title,
|
||||
"song.number" -> song.number.toString
|
||||
), directory + "/" + songFileName + ".html")
|
||||
}
|
||||
|
||||
formatTemplate("res/template/album.html", HashMap(
|
||||
"songs" -> songRows,
|
||||
"artist.name" -> artist.name,
|
||||
"artist.page" -> artistUrl,
|
||||
"album.name" -> album.name
|
||||
), directory + "/" + albumFileName + ".html")
|
||||
}
|
||||
|
||||
formatTemplate("res/template/artist.html", HashMap(
|
||||
"albums" -> albumRows,
|
||||
"artist.name" -> artist.name
|
||||
), directory + "/" + artistFileName + ".html")
|
||||
}
|
||||
formatTemplate("res/template/index.html", HashMap(
|
||||
"artists" -> artistRows
|
||||
), directory + "/" + "index.html")
|
||||
|
||||
copyFile("res/template/base.js", directory + "/base.js")
|
||||
copyFile("res/template/base.css", directory + "/base.css")
|
||||
copyFile("res/template/index.css", directory + "/index.css")
|
||||
copyFile("res/template/artist.css", directory + "/artist.css")
|
||||
copyFile("res/template/album.css", directory + "/album.css")
|
||||
copyFile("res/template/song.css", directory + "/song.css")
|
||||
}
|
||||
|
||||
def copyFile(from: String, to: String): Unit = {
|
||||
val fis: FileInputStream = new FileInputStream(from)
|
||||
val fos: FileOutputStream = new FileOutputStream(to)
|
||||
|
||||
fos.write(fis.readAllBytes())
|
||||
|
||||
fis.close()
|
||||
fos.close()
|
||||
}
|
||||
|
||||
def formatName(name: String): String = {
|
||||
var formatted: String = name.toLowerCase
|
||||
formatted = formatted.replace(" ", "_")
|
||||
formatted = formatted.replace("'", "-")
|
||||
formatted = formatted.replaceAll("[^a-zA-Z_0-9\\-().]", "")
|
||||
return formatted
|
||||
}
|
||||
|
||||
def formatTemplate(templateName: String, variables: Map[String, String], outPath: String): Unit = {
|
||||
val source: BufferedSource = Source.fromFile(templateName)
|
||||
var template: String = source.getLines().mkString("\n")
|
||||
source.close()
|
||||
|
||||
for ((key: String, value: String) <- variables) {
|
||||
template = template.replace(s"{{$key}}", value)
|
||||
}
|
||||
|
||||
val fos: FileOutputStream = new FileOutputStream(outPath)
|
||||
fos.write(template.getBytes)
|
||||
fos.close()
|
||||
}
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
val db: DataBase = create("res/songs")
|
||||
println(db)
|
||||
val artists: Array[Artist] = db.getArtists()
|
||||
println(artists(0))
|
||||
|
||||
save("res/db.bin", db)
|
||||
val db2: DataBase = load("res/db.bin")
|
||||
println(db2)
|
||||
val artists2: Array[Artist] = db2.getArtists()
|
||||
println(artists2(0))
|
||||
exportHTML("/tmp/html", db)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user