Skip to content

Commit

Permalink
Add support for including sections based on <hr/> tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
wspringer committed Oct 21, 2012
1 parent f5c0a36 commit 0b6db3f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import org.fusesource.scalate.{Binding, Template, TemplateEngine}
import org.fusesource.scalate.util.{ResourceLoader => ScalateResourceLoader}
import org.fusesource.scalate.support.URLTemplateSource

case class MonkeymanConfiguration(sourceDir: File, layoutDir: File) {
case class MonkeymanConfiguration(sourceDir: File, layoutDir: File, sections: Boolean = false) {

private val layoutFileName = "layout"

Expand Down Expand Up @@ -106,7 +106,7 @@ case class MonkeymanConfiguration(sourceDir: File, layoutDir: File) {
new LessDecorator,
new ZussDecorator,
new YamlFrontmatterDecorator(),
new MarkdownDecorator(),
new MarkdownDecorator(sections),
new SnippetDecorator(layoutResolver, templateEngine, allResources _),
new ScalateDecorator(templateEngine, allResources _),
PermalinkDecorator
Expand Down
5 changes: 4 additions & 1 deletion src/main/scala/nl/flotsam/monkeyman/MonkeymanTool.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ abstract class MonkeymanTool(toolName: String) {
else new File(name)
}

val sections = parser.flag[Boolean]("sections", false, "Interpret markdown horizontal rules as section breaks.")

def main(args: Array[String]) {
try {
parser.parse(args)
Expand All @@ -63,7 +65,8 @@ abstract class MonkeymanTool(toolName: String) {
else {
val config = new MonkeymanConfiguration(
sourceDir = sourceDir.value.getOrElse(new File(workingDir, "source")),
layoutDir = layoutDir.value.getOrElse(new File(workingDir, "layout"))
layoutDir = layoutDir.value.getOrElse(new File(workingDir, "layout")),
sections = sections.value.getOrElse(false)
)
try {
if (!config.sourceDir.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import nl.flotsam.monkeyman.Resource
import nl.flotsam.monkeyman.decorator.ResourceDecoration
import nl.flotsam.monkeyman.util.Closeables._
import org.apache.commons.io.{FilenameUtils, IOUtils}
import org.pegdown.ast.{TextNode, HeaderNode}
import org.pegdown.ast.{SimpleNode, TextNode, HeaderNode}
import org.pegdown.{LinkRenderer, ToHtmlSerializer, PegDownProcessor}
import nl.flotsam.monkeyman.util.Logging

case class MarkdownDecoration(resource: Resource)
case class MarkdownDecoration(resource: Resource, sections: Boolean)
extends ResourceDecoration(resource) with Logging {

lazy val (extractedTitle, html) = {
Expand All @@ -37,7 +37,9 @@ case class MarkdownDecoration(resource: Resource)
val processor = new PegDownProcessor
val rootNode = processor.parseMarkdown(markdown.toCharArray)
val visitor = new TitleExtractingToHtmlSerializer(new LinkRenderer)
val html = visitor.toHtml(rootNode)
val html =
if (sections) "<section>" + visitor.toHtml(rootNode) + "</section>"
else visitor.toHtml(rootNode)
val title = visitor.title
(title, html)
}
Expand Down Expand Up @@ -74,6 +76,16 @@ case class MarkdownDecoration(resource: Resource)
super.visit(node)
}
}

override def visit(node: SimpleNode) {
node.getType match {
case SimpleNode.Type.HRule if (sections) =>
printer.println.print("</section><section>")
case _ => super.visit(node)
}
}


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ package nl.flotsam.monkeyman.decorator.markdown

import nl.flotsam.monkeyman.{Resource, ResourceDecorator}

class MarkdownDecorator extends ResourceDecorator {
class MarkdownDecorator(sections: Boolean) extends ResourceDecorator {

def decorate(resource: Resource) = {
if (resource.contentType == "text/x-web-markdown" || resource.path.endsWith(".md"))
new MarkdownDecoration(resource)
new MarkdownDecoration(resource, sections)
else resource
}

Expand Down

0 comments on commit 0b6db3f

Please sign in to comment.