PageRenderTime 22ms CodeModel.GetById 14ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

/scalate-pegdown/src/main/scala/org/fusesource/scalate/filter/PegDownFilter.scala

http://github.com/scalate/scalate
Scala | 57 lines | 26 code | 5 blank | 26 comment | 0 complexity | 863d97a8951c79dbbc4bc70425b1d7b2 MD5 | raw file
 1/*
 2 * Copyright (C) 2009-2011 the original author or authors.
 3 * See the notice.md file distributed with this work for additional
 4 * information regarding copyright ownership.
 5 *
 6 * Licensed under the Apache License, Version 2.0 (the "License");
 7 * you may not use this file except in compliance with the License.
 8 * You may obtain a copy of the License at
 9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18package org.fusesource.scalate.filter
19
20import org.fusesource.scalate.{TemplateEngineAddOn, RenderContext, TemplateEngine}
21import org.fusesource.scalate.filter.Filter
22import org.pegdown.{Extensions, PegDownProcessor}
23
24/**
25 * Renders markdown syntax with multi-markdown like extras.
26 *
27 * @author <a href="mailto:stuart.roebuck@gmail.com">Stuart Roebuck</a>
28 */
29object PegDownFilter extends Filter with TemplateEngineAddOn {
30
31  private val pegDownProcessor = new PegDownProcessor(
32    Extensions.ABBREVIATIONS |
33      Extensions.AUTOLINKS |
34      Extensions.DEFINITIONS |
35      Extensions.FENCED_CODE_BLOCKS |
36      Extensions.QUOTES |
37      Extensions.SMARTS |
38      Extensions.TABLES |
39      Extensions.WIKILINKS
40  )
41
42  def filter(context: RenderContext, content: String) = {
43    synchronized {
44      // This code block is synchronized as the PegDownProcessor is not thread safe.
45      pegDownProcessor.markdownToHtml(content).stripLineEnd
46    }
47  }
48
49  /**
50   * Add the markdown filter to the template engine.
51   */
52  def apply(te: TemplateEngine) = {
53    te.filters += "multimarkdown" -> PegDownFilter
54    te.pipelines += "mmd" -> List(PegDownFilter)
55    te.pipelines += "multimarkdown" -> List(PegDownFilter)
56  }
57}