Skip to content

Commit

Permalink
Allowing to extract parts of the post as a preview
Browse files Browse the repository at this point in the history
  • Loading branch information
denisftw committed Jun 27, 2016
1 parent 51112c6 commit 2c70eff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name := "s2gen"

version := "0.2.2"
version := "0.2.3"

scalaVersion := "2.11.8"

Expand Down
25 changes: 23 additions & 2 deletions src/main/scala/com/appliedscala/generator/SiteGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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],
Expand Down

0 comments on commit 2c70eff

Please sign in to comment.