Filesystem based article implementation
authorTomas Zeman <tzeman@volny.cz>
Tue, 11 Dec 2018 15:03:57 +0100
changeset 15 1e1b0a1a4d63
parent 14 7c4b05467630
child 16 4556852613a9
Filesystem based article implementation
base/src/sqwl/cms/FsArticle.scala
example/src/sqwl/cms/Content.scala
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/base/src/sqwl/cms/FsArticle.scala	Tue Dec 11 15:03:57 2018 +0100
@@ -0,0 +1,22 @@
+package sqwl.cms
+
+import java.nio.file.Path
+
+import scala.io.Source
+
+trait FsArticle extends iArticle {
+
+  protected def contentPath: Path
+  protected def encoding: String = "UTF-8"
+
+  override def htmlContent: String = {
+    val src = Source.fromFile(contentPath.toFile, encoding)
+    try {
+      src.getLines.mkString
+    } catch {
+      case _: Throwable => ""
+    } finally {
+      src.close()
+    }
+  }
+}
--- a/example/src/sqwl/cms/Content.scala	Thu Dec 06 17:01:32 2018 +0100
+++ b/example/src/sqwl/cms/Content.scala	Tue Dec 11 15:03:57 2018 +0100
@@ -43,18 +43,10 @@
   val category: Option[iCategory],
   val tags: Seq[iTag],
 
-) extends EnumEntry with iArticle with Hyphencase {
-  override def htmlContent: String = {
-    val src = Source.fromFile(s"example/content/${entryName}.html", "UTF-8")
-    try {
-      src.getLines.mkString
-    } catch {
-      case _: Throwable => ""
-    } finally {
-      src.close()
-    }
-  }
+) extends EnumEntry with FsArticle with Hyphencase {
 
+  override protected def contentPath: Path = Paths.get(
+    s"example/content/${entryName}.html")
   override def assets: Path = Paths.get(s"example/content/${entryName}")
   override def pathSegment: String = entryName
 }