From 2c70eff9e4bd0171c19f9c1f7b787566dea0300d Mon Sep 17 00:00:00 2001 From: denny2006 Date: Mon, 27 Jun 2016 15:17:20 +0300 Subject: [PATCH] Allowing to extract parts of the post as a preview --- build.sbt | 2 +- .../generator/SiteGenerator.scala | 25 +++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/build.sbt b/build.sbt index fd5bc07..42b9e0b 100644 --- a/build.sbt +++ b/build.sbt @@ -1,6 +1,6 @@ name := "s2gen" -version := "0.2.2" +version := "0.2.3" scalaVersion := "2.11.8" diff --git a/src/main/scala/com/appliedscala/generator/SiteGenerator.scala b/src/main/scala/com/appliedscala/generator/SiteGenerator.scala index 4aa2d61..8f53c3c 100644 --- a/src/main/scala/com/appliedscala/generator/SiteGenerator.scala +++ b/src/main/scala/com/appliedscala/generator/SiteGenerator.scala @@ -275,6 +275,20 @@ object SiteGenerator { logger.info("The archive page was generated") } + val PreviewSplitter = """\[\/\/\]\: \# \"__PREVIEW__\"""" + + private def extractPreview(contentMd: String): Option[String] = { + val contentLength = contentMd.length + val previewParts = contentMd.split(PreviewSplitter) + if (previewParts.length > 1 && previewParts(1).trim.length > 0) { + Some(previewParts(1)) + } else if (previewParts.nonEmpty && previewParts(0).trim.length > 0 && previewParts(0).length < contentLength) { + Some(previewParts(0)) + } else { + None + } + } + private def processMdFile(mdFile: File, mdGenerator: PegDownProcessor, linkRenderer: LinkRenderer): Map[String, String] = { val postContent = Source.fromFile(mdFile).getLines().toList @@ -290,16 +304,23 @@ object SiteGenerator { } }.toMap val mdContent = contentLines.mkString("\n") + val mdPreview = extractPreview(mdContent) val renderedMdContent = mdGenerator.markdownToHtml(mdContent, linkRenderer) + val htmlPreview = mdPreview.map { preview => mdGenerator.markdownToHtml(preview, linkRenderer) } val simpleFilename = Paths.get(mdFile.getParentFile.getName, mdFile.getName).toString - val contentObj = contentPropertyMap ++ Map( + val mapBuilder = Map.newBuilder[String, String] + mapBuilder ++= contentPropertyMap + mapBuilder ++= Map( "body" -> renderedMdContent, "sourceDirectoryPath" -> mdFile.getParentFile.getAbsolutePath, "sourceFilename" -> simpleFilename, "uri" -> contentPropertyMap("link") ) - contentObj + htmlPreview.foreach { preview => + mapBuilder += "preview" -> preview + } + mapBuilder.result() } private def generateSingleBlogFile(siteCommonData: Map[String, String], contentObj: Map[String, String],