Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add NIF code and make tasks

  • Loading branch information...
commit 2766e36265922d88c38d27cc30b12ae37b333b7b 1 parent b743b40
@asaaki authored
Showing with 82 additions and 4 deletions.
  1. +19 −4 Makefile
  2. +1 −0  mix.exs
  3. +62 −0 src/markdown_nif.c
View
23 Makefile
@@ -10,6 +10,9 @@ ifeq ($(shell uname),Darwin)
OPTIONS=-dynamiclib -undefined dynamic_lookup
endif
+NIF_SRC=\
+ src/markdown_nif.c
+
DISCOUNT_OBJS=\
discount_src/mkdio.o \
discount_src/markdown.o \
@@ -31,8 +34,17 @@ DISCOUNT_OBJS=\
discount_src/html5.o \
discount_src/flags.o
+DISCOUNT_LIB=discount_src/libmarkdown.a
+
all: discount_ex
+priv/markdown.so: ${DISCOUNT_LIB} ${NIF_SRC}
+ mkdir -p priv && \
+ $(CC) $(CFLAGS) $(ERLANG_FLAGS) -shared $(OPTIONS) \
+ $(DISCOUNT_OBJS) \
+ $(NIF_SRC) \
+ -o $@ 2>&1 >/dev/null
+
discount_ex:
mix deps.get
mix compile
@@ -40,7 +52,7 @@ discount_ex:
cbin/markdown: discount_src/libmarkdown.a
mkdir -p cbin && cp discount_src/markdown cbin/
-discount_src/libmarkdown.a: discount_src/configure.sh
+$(DISCOUNT_LIB): discount_src/configure.sh
cd discount_src && \
CFLAGS="$(CFLAGS_DISCOUNT)" ./configure.sh \
--with-dl=Both \
@@ -54,7 +66,7 @@ discount_src/configure.sh:
git submodule update --init
discount_src-clean:
- test ! -f discount_src/libmarkdown.a || \
+ test ! -f $(DISCOUNT_LIB) || \
(cd discount_src && $(MAKE) clean)
discount_src-distclean:
@@ -66,8 +78,11 @@ discount_src-distclean:
discount_ex-clean:
rm -rf $(EBIN_DIR) test/tmp cbin share/*
-clean: discount_src-clean discount_ex-clean
+discount_nif-clean:
+ rm -rf priv/markdown.*
+
+clean: discount_src-clean discount_ex-clean discount_nif-clean
-distclean: discount_src-distclean discount_ex-clean
+distclean: discount_src-distclean discount_ex-clean discount_nif-clean
.PHONY: all discount_ex clean distclean discount_src-clean discount_src-distclean
View
1  mix.exs
@@ -2,6 +2,7 @@ defmodule Mix.Tasks.Compile.Discount do
@shortdoc "Compiles discount library"
def run(_) do
Mix.shell.info System.cmd("make cbin/markdown")
+ Mix.shell.info System.cmd("make priv/markdown.so")
end
end
View
62 src/markdown_nif.c
@@ -0,0 +1,62 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+#include <time.h>
+#include <ctype.h>
+
+#include "erl_nif.h"
+
+#include "config.h"
+#include "cstring.h"
+#include "tags.h"
+#include "mkdio.h"
+
+static ERL_NIF_TERM markdown_to_html_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
+
+ ErlNifBinary markdown_binary;
+ ErlNifBinary output_binary;
+ mkd_flag_t flags = 0 | MKD_AUTOLINK | MKD_TABSTOP| MKD_NOHEADER ;
+ MMIOT *doc;
+
+ if (argc != 1) {
+ return enif_make_badarg(env);
+ }
+
+ if(!enif_inspect_binary(env, argv[0], &markdown_binary)){
+ return enif_make_badarg(env);
+ }
+
+ if (markdown_binary.size <= 0){
+ const char *empty_string = "";
+ const int empty_len = strlen(empty_string);
+ enif_alloc_binary(empty_len, &output_binary);
+ strncpy((char*)output_binary.data, empty_string, empty_len);
+ return enif_make_binary(env, &output_binary);
+ }
+
+ doc = gfm_string((char*)markdown_binary.data, markdown_binary.size, flags);
+
+ if(doc && mkd_compile(doc, flags)) {
+ char *output_text;
+ int output_len = mkd_document(doc, &output_text);
+
+ enif_alloc_binary(output_len, &output_binary);
+ strncpy((char*)output_binary.data, output_text, output_len);
+
+ mkd_cleanup(doc);
+ enif_release_binary(&markdown_binary);
+ return enif_make_binary(env, &output_binary);
+ }
+
+ // everything else is an error
+ enif_release_binary(&markdown_binary);
+ enif_release_binary(&output_binary);
+ return enif_make_badarg(env);
+}
+
+static ErlNifFunc nif_funcs[] = {
+ { "nif_to_html", 1, markdown_to_html_nif }
+};
+
+ERL_NIF_INIT(Elixir.Discount.Markdown, nif_funcs, NULL, NULL, NULL, NULL);
Please sign in to comment.
Something went wrong with that request. Please try again.