base/src/sqwl/cms/package.scala
author Tomas Zeman <tomas@functionals.cz>
Tue, 24 Nov 2020 11:17:26 +0100
changeset 31 5c4364d6e726
parent 23 afcfbdeb3b88
child 34 775d6da26993
permissions -rw-r--r--
SQWL#2003 Studie -> Projekt

package sqwl

import java.nio.file.Path

import scala.io.Source
import scala.util.{Failure, Success, Try}
import scalatags.Text.all._

package object cms {

  def readSourceFull(src: Source): Try[String] = {
    try {
      Success(src.getLines.mkString)
    } catch {
      case t: Throwable => Failure(t)
    } finally {
      src.close()
    }
  }

  def readFile(f: Path, enc: String = "UTF-8"): Try[String] =
    readSourceFull(Source.fromFile(f.toFile, enc))

  def readFile(f: String): String =
    readSourceFull(Source.fromFile(f, "UTF-8")) getOrElse ""

  def lnk(name: String, customUrl: String = ""): Frag = {
    val usedUrl = if (customUrl == "") name else customUrl
    a(name, href := usedUrl)
  }
}