Created
February 28, 2012 18:44
-
-
Save rodrigoflores/1934255 to your computer and use it in GitHub Desktop.
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
flores in /Users/flores/Code/sum/sum/mdown-erlang with 1.9.3-p125 on (master *) | |
% erl | |
Erlang R15B (erts-5.9) [source] [64-bit] [smp:2:2] [async-threads:0] [hipe] [kernel-poll:false] | |
Eshell V5.9 (abort with ^G) | |
1> c(markdown). | |
{ok,markdown} | |
2> markdown:to_html(<<"# OK #">>). | |
"<h1>OK</h1>\n" | |
3> markdown:to_html(<<"# OK #\n * OK">>). | |
"<h1>OK</h1>\n\n<ul>\n<li>OK</li>\n</ul>\n" | |
4> markdown:to_html(<<"# OK #\n * OK\n* Not ok\n">>). | |
"<h1>OK</h1>\n\n<ul>\n<li>OK\n\n<ul>\n<li>Not ok</li>\n</ul></li>\n</ul\b" | |
5> markdown:to_html(<<"# OK #\n * OK\n* Not ok\n">>). | |
"<h1>OK</h1>\n\n<ul>\n<li>OK\n\n<ul>\n<li>Not ok</li>\n</ul></li>\n</ul\b" | |
6> markdown:to_html(<<"# OK #\n * OK\n* Not ok\n">>). | |
"<h1>OK</h1>\n\n<ul>\n<li>OK\n\n<ul>\n<li>Not ok</li>\n</ul></li>\n</ul\n" | |
7> markdown:to_html(<<"# OK #\n * OK\n* Not ok\n">>). | |
[60,104,49,62,79,75,60,47,104,49,62,10,10,60,117,108,62,10, | |
60,108,105,62,79,75,10,10,60,117,108|...] | |
8> markdown:to_html(<<"# OK #\n * OK\n* Not ok\n">>). | |
[60,104,49,62,79,75,60,47,104,49,62,10,10,60,117,108,62,10, | |
60,108,105,62,79,75,10,10,60,117,108|...] | |
9> |
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
-module(markdown). | |
-export([to_html/1]). | |
-on_load(init/0). | |
init() -> | |
ok = erlang:load_nif("./markdown",1). | |
to_html(_) -> | |
exit(nif_library_not_loaded). |
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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include "erl_nif.h" | |
#include "markdown.h" | |
#include "html.h" | |
#include "buffer.h" | |
#define OUTPUT_SIZE 64 | |
struct sd_callbacks callbacks; | |
struct sd_markdown *markdown; | |
struct html_renderopt options; | |
static ERL_NIF_TERM to_markdown_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { | |
ErlNifBinary markdown_binary; | |
struct buf *input, *output; | |
char * html_output; | |
if(!enif_inspect_binary(env, argv[0], &markdown_binary)){ | |
enif_make_badarg(env); | |
} | |
input = bufnew(markdown_binary.size); | |
bufputs(input, markdown_binary.data); | |
output = bufnew(OUTPUT_SIZE); | |
sdhtml_renderer(&callbacks, &options, 0); | |
markdown = sd_markdown_new(0, 16, &callbacks, &options); | |
sd_markdown_render(output, input->data, input->size, markdown); | |
html_output = malloc(sizeof(char)*output->size+10); | |
strcpy(html_output, output->data); | |
bufrelease(input); | |
bufrelease(output); | |
return enif_make_string(env, html_output, ERL_NIF_LATIN1); | |
} | |
static ErlNifFunc nif_funcs[] = | |
{ | |
{"to_markdown", 1, to_markdown_nif} | |
}; | |
ERL_NIF_INIT(markdown,nif_funcs,NULL,NULL,NULL,NULL); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment