<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>

#include "erl_nif.h"
#include "cmark.h"

static ERL_NIF_TERM to_html_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
  ErlNifBinary  markdown_binary;
  ErlNifBinary  output_binary;
  char         *html;
  size_t        html_len;
  int           options;

  if (argc != 2) {
    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);
  }

  enif_get_int(env, argv[1], &options);

  html = cmark_markdown_to_html((const char *)markdown_binary.data, markdown_binary.size, options);
  html_len = strlen(html);
  enif_release_binary(&markdown_binary);

  enif_alloc_binary(html_len, &output_binary);
  strncpy((char*)output_binary.data, html, html_len);

  return enif_make_binary(env, &output_binary);
};

static ErlNifFunc nif_funcs[] = {
  { "to_html", 2, to_html_nif }
};

ERL_NIF_INIT(Elixir.Cmark.Nif, nif_funcs, NULL, NULL, NULL, NULL);
<commit_msg>Prepare for multi format outputs<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>

#include "erl_nif.h"
#include "cmark.h"

static ERL_NIF_TERM to_html_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
  ErlNifBinary  markdown_binary;
  ErlNifBinary  output_binary;
  cmark_node   *doc;
  char         *html;
  size_t        html_len;
  int           options;

  if (argc != 2) {
    return enif_make_badarg(env);
  }

  if(!enif_inspect_binary(env, argv[0], &markdown_binary)){
    return enif_make_badarg(env);
  }

  if (markdown_binary.size <= 0){
    enif_release_binary(&markdown_binary);
    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);
  }

  enif_get_int(env, argv[1], &options);

  doc = cmark_parse_document(
    (const char *)markdown_binary.data,
    markdown_binary.size,
    options
  );
  html = cmark_render_html(doc, options);
  html_len = strlen(html);
  enif_release_binary(&markdown_binary);

  enif_alloc_binary(html_len, &output_binary);
  strncpy((char*)output_binary.data, html, html_len);

  free(html);
  cmark_node_free(doc);

  return enif_make_binary(env, &output_binary);
};

static ErlNifFunc nif_funcs[] = {
  { "to_html", 2, to_html_nif }
};

ERL_NIF_INIT(Elixir.Cmark.Nif, nif_funcs, NULL, NULL, NULL, NULL);

{'fraction_non_alphanumeric': 0.12875388332758025, 'fraction_numerical': 0.0034518467380048328, 'mean_word_length': 4.583815028901734, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3776620', 'n_tokens_mistral': 1159, 'n_tokens_neox': 1118, 'n_words': 203}