#include "Python.h"

#include <cstdio>
#include <string>
#include "itex2MML.h"

std::string buffer;

//?????
static void write(const char* b, unsigned long length) {
  if (b) {
    if (length) {
      for (unsigned long i=0;i<length;i++) {
        buffer += b[i];
      }
    } else {
      buffer += b;
    }
  }
}

static const char* itex2mml(char* s) {
  buffer = ""; ///???

  std::string itex = s;

  /*
  char* mathml = itex2MML_parse(itex.c_str(), itex.size());
  return mathml;
  */

  itex2MML_html_filter(itex.c_str(), itex.size());
  return buffer.c_str();
}

static PyObject* itex2mml_bind(PyObject* self, PyObject* args) {
  char* s;
  const char* mathml;
  if (!PyArg_ParseTuple(args, "s", &s)) {
    return NULL;
  } else {
    mathml = itex2mml(s);
    return Py_BuildValue("s", mathml);
  }
}

static PyMethodDef itex2mml_methods[] = {
  {"itex2mml", itex2mml_bind, METH_VARARGS,
   "Execute `itex2mml' function: (char *) -> char *"},
  { NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC inititex2mml(void) {
  Py_InitModule("itex2mml", itex2mml_methods);
  itex2MML_write = write;
}

