Home Code Other Software

Table of Contents

Introduction

The MarkWrap library (pronounced “mark wrap” or “more crap”, depending on your preference) is a unified Scala API for various underlying lightweight markup APIs. Currently, it supports:

Installation

MarkWrap is published to the Bintray Maven repository, which is automatically linked to Bintray’s JCenter repository. (From JCenter, it’s eventually pushed to the automatically sync’d with the Maven Central Repository.

Installing for Maven

If you’re using Maven, just specify the artifact, and Maven will do the rest for you:

For example:

<dependency>
  <groupId>org.clapper</groupId>
  <artifactId>markwrap_2.10</artifactId>
  <version>1.2.0</version>
</dependency>

If you cannot resolve the artifact, then add the JCenter repository:

<repositories>
  <repository>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
    <id>central</id>
    <name>bintray</name>
    <url>http://jcenter.bintray.com</url>
  </repository>
  ...
</repositories>

For more information on using Maven and Scala, see Josh Suereth’s Scala Maven Guide.

Using with SBT

Using with SBT

Just add the following line to your build.sbt:

libraryDependencies += "org.clapper" %% "markwrap" % "1.2.0"

Building from Source

Source Code Repository

The source code for the MarkWrap library is maintained on GitHub. To clone the repository, run this command:

git clone git://github.com/bmc/markwrap.git

Build Requirements

Building the MarkWrap library requires SBT 0.13.x. Install SBT, as described at the SBT web site.

Building MarkWrap

Assuming you have an sbt shell script (or .BAT file, for Windows), run:

sbt compile test package

The resulting jar file will be in the top-level target directory.

Using MarkWrap

MarkWrap provides a simple, unified API for various lightweight markup languages. There are two steps to use the API:

Getting the Desired Parser

To obtain a converter, use the org.clapper.markwrap.MarkWrap object’s converterFor() method, passing it either:

Converting Markdown

To get a Markdown converter, call MarkWrap.converterFor() with:

Markdown is parsed with flexmark-java, using the CommonMark dialect, with some these extensions:

Tables

Supports GitHub-flavored Markdown table syntax.

Strikethrough

~~text~~

renders “text” as strikethrough:

<p><del>text</del></p>

Subscript

~text~

renders “text” as subscript:

<p><sub>text</sub></p>

Definition lists

Orange
:   The fruit of an evergreen tree of the genus Citrus.

renders as:

<dl>
<dt>Orange</dt>
<dd>The fruit of an evergreen tree of the genus Citrus.</dd>
</dl>

Converting Textile

To get a Textile converter, call MarkWrap.converterFor() with:

Textile is parsed with the Eclipse Mylyn wikitext parser API.

“Converting” raw HTML

To get a passthrough converter for HTML or XHTML, call MarkWrap.converterFor() with:

A passthrough HTML converter simply passes the HTML straight through, unmodified.

Handling plain text

To get a plaintext-to-HTML converter, call MarkWrap.converterFor() with:

The resulting plain text is simply wrapped in <pre> and </pre> tags.

Examples

Getting a converter via type

If you call converterFor() with a MarkupType value, you get the converter directly:

import org.clapper.markwrap._

val markdown = MarkWrap.converterFor(MarkupType.Markdown)
val textile = MarkWrap.converterFor(MarkupType.Markdown)
val html = MarkWrap.converterFor(MarkupType.HTML) // or MarkupType.XHTML
val plain = MarkWrap.converterFor(MarkupType.PlainText)

markdown.parseToHTML("...")

Getting a converter via MIME type

You can pass a (string) MIME type to converterFor(). The following MIME types are supported:

Passing any other MIME type causes an error.

Because errors can occur, this version of converterFor() returns a scala.util.Try.

import org.clapper.markwrap._

val mimeType = "text/markdown"
MarkWrap.converterFor(mimeType) match {
  case Success(converter) =>  converter.parseToHTML("...")
  case Failure(exception) =>  // handle error
}

// Or, monadically:
MarkWrap.converterFor(mimeType).map { converter =>
  converter.parseToHTML("...")
} // you now have a Try[String]. If Success, it contains the HTML.

Getting a converter via filename

You can pass a java.io.File object to converterFor(). MarkWrap will convert the file name to a MIME type (using the file’s extension); then, it’ll use the resulting MIME type to get the converter.

The following extensions are supported:

extension MIME type markup
.md, .markdown text/markdown Markdown
.textile text/textile Textile
.htm, .html text/html HTML
.xhtm, .xhtml text/xhtml XHTML
.txt, .TXT, .text, .TEXT, .cfg, .conf, .properties text/plain plain text

Passing any other MIME type causes an error.

Because errors can occur, this version of converterFor() returns a scala.util.Try.

import org.clapper.markwrap._

MarkWrap.converterFor(new java.io.File(pathToFile)) match {
  case Success(converter) =>  converter.parseToHTML("...")
  case Failure(exception) =>  // handle error
}

// Or, monadically:
MarkWrap.converterFor(mimeType).map { converter =>
  converter.parseToHTML("...")
} // you now have a Try[String]. If Success, it contains the HTML.

Converting the Markup

Once you have the correct converter, you can parse the markup using one of the parsing methods.

Converting to an HTML fragment

The parseToHTML() methods produce HTML fragments, not complete HTML documents. (There’s another parseToHTMLDocument() method that produces a complete document; see below for details.)

Reading from a Scala Source

Example 1:

import org.clapper.markwrap._
import scala.io.Source
import java.io.File

val file = new File("/path/to/markup.md")
val t: Try[String] = MarkWrap.converterFor(file).map { converter =>
   converter.parseToHTML(Source.fromFile(file))
}

Example 2:

import org.clapper.markwrap._
import scala.io.Source

val markup = """This is some *Markdown* text"""
val converter = MarkWrap.converterFor(MarkupType.Markdown)
val html = converter.parseToHTML(Source.fromString(markup))

Producing a complete HTML document

The parseToHTMLDocument() method produces a complete HTML document, with <html>, <head>, and <body> sections, as well as an optional cascading style sheet. The method’s signature looks like this:

def parseToHTMLDocument(markupSource: Source,
                        title: String,
                        cssSource: Option[Source] = None,
                        encoding: String  = "UTF-8"): String =

This method is just a simple convenience method; there are features it does not support (for instance, <link> tags, for external style sheets or Javascript files). You’re certainly free to use your own replacement for it.

API Documentation

The Scaladoc-generated the API documentation is available locally. In addition, you can generate your own version with:

sbt doc

Change log

The change log for all releases is here.

Author

Brian M. Clapper, bmc@clapper.org

Copyright and License

MarkWrap is copyright © 2009-2018 Brian M. Clapper and is released under a BSD License.

Patches

I gladly accept patches from their original authors. Feel free to email patches to me or to fork the GitHub repository and send me a pull request. Along with any patch you send: