Created
September 22, 2012 09:28
-
-
Save mikaelhg/3765660 to your computer and use it in GitHub Desktop.
Scalate PlantUML + ditaa macro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package scalate | |
import org.fusesource.scalamd.{MacroDefinition, Markdown} | |
import java.util.regex.Matcher | |
import org.fusesource.scalate.wikitext.Pygmentize | |
import org.fusesource.scalate._ | |
import util.Log | |
import net.sourceforge.plantuml.{FileFormat, FileFormatOption, SourceStringReader} | |
import java.io.ByteArrayOutputStream | |
import java.nio.charset.StandardCharsets | |
import javax.xml.bind.DatatypeConverter.{printBase64Binary => base64} | |
import org.fusesource.scalamd.MacroDefinition | |
import org.stathissideris.ascii2image.core.ConversionOptions | |
import org.stathissideris.ascii2image.text.TextGrid | |
import org.stathissideris.ascii2image.graphics.{BitmapRenderer, Diagram} | |
import javax.imageio.ImageIO | |
object Boot extends Log; import Boot._ | |
class Boot(engine: TemplateEngine) { | |
def run: Unit = { | |
def filter(m : Matcher) : String = { | |
val filter_name = m.group(1) | |
val body = m.group(2) | |
engine.filter(filter_name) match { | |
case Some(filter) => filter.filter(RenderContext(), body) | |
case None => "<div class=\"macro error\"><p>filter not found: %s</p><pre>%s</pre></div>".format(filter_name, body) | |
} | |
} | |
def pygmentize(m : Matcher) : String = Pygmentize.pygmentize(m.group(2), m.group(1)) | |
def plantuml(m : Matcher) : String = { | |
val baos = new ByteArrayOutputStream() | |
println(m) | |
val reader = new SourceStringReader(m.group(2)) | |
m.group(1) match { | |
case "svg" => { | |
reader.generateImage(baos, new FileFormatOption(FileFormat.SVG)) | |
new String(baos.toByteArray, StandardCharsets.UTF_8) | |
} | |
case "png" => { | |
reader.generateImage(baos, new FileFormatOption(FileFormat.PNG)) | |
"<img src=\"data:image/png;base64," + base64(baos.toByteArray) + "\"/>" | |
} | |
} | |
} | |
def ditaa(m : Matcher) : String = { | |
val options = new ConversionOptions() | |
val grid = new TextGrid() | |
grid.initialiseWithText(m.group(1), options.processingOptions) | |
val image = new BitmapRenderer().renderToImage(new Diagram(grid, options), options.renderingOptions) | |
val baos = new ByteArrayOutputStream() | |
ImageIO.write(image, "png", baos) | |
"<img src=\"data:image/png;base64," + base64(baos.toByteArray) + "\"/>" | |
} | |
// add some macros to markdown. | |
Markdown.macros :::= List( | |
MacroDefinition("""\{filter::(.*?)\}(.*?)\{filter\}""", "s", filter, true), | |
MacroDefinition("""\{pygmentize::(.*?)\}(.*?)\{pygmentize\}""", "s", pygmentize, true), | |
MacroDefinition("""\{pygmentize\_and\_compare::(.*?)\}(.*?)\{pygmentize\_and\_compare\}""", "s", pygmentize, true), | |
MacroDefinition("""\{plantuml::(.*?)\}(.*?)\{plantuml\}""", "s", plantuml, true), | |
MacroDefinition("""\{ditaa\}(.*?)\{ditaa\}""", "s", ditaa, true) | |
) | |
// add some more recognized pipelines. | |
for( ssp <- engine.filter("ssp"); md <- engine.filter("markdown") ) { | |
engine.pipelines += "ssp.md"-> List(ssp, md) | |
engine.pipelines += "ssp.markdown"-> List(ssp, md) | |
} | |
info("Bootstrapped sitegen") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment