Blackfriday: a markdown processor for Go
Go to file
Russ Ross cd5e4957ce Merge pull request #2 from kjk/markdown-tests
integrate tests for markdown 1.0.3 test files by comparing them with refe
2011-06-01 17:24:11 -07:00
example rudimentary latex backend, additional cleanup 2011-05-30 11:06:20 -06:00
upskirtref integrate tests for markdown 1.0.3 test files by comparing them with reference files rendered with upskirt (no extensions) 2011-06-01 16:47:32 -07:00
.gitignore starting inline unit tests, fix a few minor bugs they exposed 2011-06-01 12:17:17 -06:00
block.go comments, minor cleanups 2011-05-31 16:28:07 -06:00
html.go gofmt 2011-05-31 11:49:49 -06:00
inline_test.go starting inline unit tests, fix a few minor bugs they exposed 2011-06-01 12:17:17 -06:00
inline.go starting inline unit tests, fix a few minor bugs they exposed 2011-06-01 12:17:17 -06:00
latex.go export all names from Renderer struct 2011-05-30 21:44:52 -06:00
Makefile rudimentary latex backend, additional cleanup 2011-05-30 11:06:20 -06:00
markdown.go tab expansion fixed to handle multibyte unicode characters 2011-05-31 12:04:58 -06:00
README.md readme updates 2011-05-31 16:31:36 -06:00
smartypants.go allocate new buffers on stack; mild speed improvement 2011-05-31 11:11:04 -06:00
upskirtref_test.go integrate tests for markdown 1.0.3 test files by comparing them with reference files rendered with upskirt (no extensions) 2011-06-01 16:47:32 -07:00

Black Friday

This is an implementation of John Gruber's markdown in Go. It is a translation of the upskirt library written in C with a few minor changes. It retains the paranoia of the original (it is careful not to trust its input, and as such it should be safe to feed it arbitrary user-supplied inputs). It also retains the emphasis on high performance, and the source is almost as ugly as the original.

HTML output is currently supported, along with Smartypants extensions. An experimental LaTeX output engine is also included.

Installation

Assuming you have recent version of Go installed, along with git:

goinstall github.com/russross/blackfriday

will download, compile, and install the package into $GOROOT/src/pkg/github.com/russross/blackfriday.

Check out example/main.go for an example of how to use it. Run gomake in that directory to build a simple command-line markdown tool:

cd $GOROOT/src/pkg/github.com/russross/blackfriday/example
gomake

will build the binary markdown in the example directory.

Features

All features of upskirt are supported, including:

  • The Markdown v1.0.3 test suite passes with the --tidy option. Without --tidy, the differences appear to be bugs/dubious features in the original.

  • Common extensions, including table support, fenced code blocks, autolinks, strikethroughs, non-strict emphasis, etc.

  • Paranoid parsing, making it safe to feed untrusted used input without fear of bad things happening. There are still some corner cases that are untested, but it is already more strict than upskirt (Go's bounds-checking uncovered a few off-by-one errors that were present in the C code).

  • Good performance. I have not done rigorous benchmarking, but informal testing suggests it is around 3.5x slower than upskirt. This is an ugly, direct translation from the C code, so the difference is unlikely to be related to differences in coding style. There is a lot of bounds checking that is duplicated (by user code for the application and again by code the compiler generates) and there is some additional memory management overhead, since I allocate and garbage collect buffers instead of explicitly managing them as upskirt does.

  • Minimal dependencies. blackfriday only depends on standard library packages in Go. The source code is pretty self-contained, so it is easy to add to any project.

Extensions

In addition to the extensions offered by upskirt, this package implements two additional Smartypants options:

  • LaTeX-style dash parsing, where -- is translated into –, and --- is translated into —
  • Generic fractions, where anything that looks like a fraction is translated into suitable HTML (instead of just a few special cases). For example, 4/5 becomes <sup>4</sup>&frasl;<sub>5</sub>

LaTeX Output

A rudimentary LaTeX rendering backend is also included. To see an example of its usage, comment out this link in main.go:

renderer := blackfriday.HtmlRenderer(html_flags)

and uncomment this line:

renderer := blackfriday.LatexRenderer(0)

It renders some basic documents, but is only experimental at this point.

Todo

  • Code cleanup
  • Better code documentation
  • Markdown pretty-printer output engine