1
0
mirror of https://github.com/exaloop/codon.git synced 2025-06-03 15:03:52 +08:00

Add mimetype support [wip]

This commit is contained in:
Ibrahim Numanagić 2021-12-29 13:11:56 -08:00
parent fa994600b1
commit 682b97676d
3 changed files with 33 additions and 4 deletions

View File

@ -209,7 +209,7 @@ set(CODON_HPPFILES
codon/util/toml++/toml_node.h
codon/util/toml++/toml_parser.hpp
codon/util/toml++/toml_utf8_streams.h
extra/jupyter/src/codon.h)
extra/jupyter/jupyter.h)
set(CODON_CPPFILES
codon/compiler/compiler.cpp
codon/compiler/debug_listener.cpp
@ -288,7 +288,7 @@ set(CODON_CPPFILES
codon/sir/var.cpp
codon/util/common.cpp
codon/util/fmt/format.cpp
extra/jupyter/src/codon.cpp)
extra/jupyter/jupyter.cpp)
add_library(codonc SHARED ${CODON_HPPFILES})
target_sources(codonc PRIVATE ${CODON_CPPFILES} codon_rules.cpp omp_rules.cpp)
if(CODON_JUPYTER)

View File

@ -1,9 +1,11 @@
#include "codon.h"
#include "jupyter.h"
#ifdef CODON_JUPYTER
#include <codecvt>
#include <dirent.h>
#include <fcntl.h>
#include <iostream>
#include <locale>
#include <nlohmann/json.hpp>
#include <unistd.h>
#include <xeus/xhelper.hpp>
@ -50,8 +52,35 @@ nl::json CodonJupyter::execute_request_impl(int execution_counter, const string
ast::join(backtrace, " \n"));
});
if (failed.empty()) {
std::string msg = *result;
nl::json pub_data;
pub_data["text/plain"] = *result;
if (ast::startswith(msg, "\x00\x00__codon/mime__\x00")) {
std::string mime = "";
int i = 17;
for (; i < msg.size() && msg[i]; i++)
mime += msg[i];
if (i < msg.size() && !msg[i]) {
i += 1;
} else {
mime = "text/plain";
i = 0;
}
std::string out;
out.reserve(msg.size() * 1.5);
for (; i < msg.size(); i++) {
uint8_t c = msg[i];
if (c <= 127) {
out.push_back(c);
} else {
out.push_back((c >> 6) | 0xC0);
out.push_back((c & 0x3F) | 0x80);
}
}
pub_data[mime] = out;
} else {
pub_data["text/plain"] = msg;
}
publish_execution_result(execution_counter, move(pub_data), nl::json::object());
return nl::json{{"status", "ok"},
{"payload", nl::json::array()},