IPFS works and renders to screen!

master
Melroy van den Berg 2020-12-05 02:51:40 +01:00
parent 1674c8d91d
commit e97a40907c
11 changed files with 88 additions and 59 deletions

View File

@ -73,6 +73,7 @@
"forward_list": "cpp",
"unordered_set": "cpp",
"regex": "cpp",
"shared_mutex": "cpp"
"shared_mutex": "cpp",
"*.inc": "cpp"
}
}

View File

@ -33,6 +33,8 @@ endif()
set(SOURCES
main.cc
file.cc
file.h
mainwindow.cc
mainwindow.h
menu.cc

22
src/file.cc Normal file
View File

@ -0,0 +1,22 @@
#include "file.h"
#include <cmark-gfm.h>
File::File() {}
cmark_node * File::read(const std::string& path)
{
return parser.parseFile(path);
}
cmark_node * File::fetch(const std::string& path)
{
std::stringstream contents;
network.fetchFile(path, &contents);
return parser.parseStream(contents);
}
void File::free(cmark_node *node)
{
cmark_node_free(node);
}

25
src/file.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef FILE_H
#define FILE_H
#include "md-parser.h"
#include "network.h"
#include <string>
/**
* \class File
* \brief Fetch markdown file from disk or IPFS network and parse to AST
*/
class File
{
public:
File();
cmark_node * read(const std::string& path); /*!< Read file from disk */
cmark_node * fetch(const std::string& path); /*!< Fetch file from IPFS network */
void free(cmark_node *node);
private:
Parser parser;
Network network;
};
#endif

View File

@ -1,7 +1,6 @@
#include "mainwindow.h"
#include <gtkmm/application.h>
int main(int argc, char *argv[])
{
auto app = Gtk::Application::create(argc, argv, "org.melroy.browser");

View File

@ -27,28 +27,25 @@ MainWindow::MainWindow() : m_vbox(Gtk::ORIENTATION_VERTICAL, 0)
add(m_vbox);
show_all_children();
// Get demo file
getFile();
demo();
}
void MainWindow::getFile()
void MainWindow::demo()
{
// Just an IPFS test! Fetch a resource from the IPFS network
// Assuming you already running a IPFS deamon
std::stringstream contents;
network.fetchFile("QmQzhn6hEfbYdCfwzYFsSt3eWpubVKA1dNqsgUwci5vHwq", &contents);
parser.parseStream(contents);
/*
// From disk
std::string exePath = n_fs::current_path().string();
std::string filePath = exePath.append("/../../test.md");
printf("Path: %s\n", filePath.c_str());
cmark_node *root_node = parser.parseFile(filePath);
if (root_node != NULL) {
/*std::string html = parser->renderHTML(root_node);
printf("HTML %s\n\n", html.c_str());*/
// process AST, which can then be drawed on render/drawing area
m_renderArea.processDocument(root_node);
cmark_node_free(root_node);
cmark_node *readDoc = file.read(filePath);
if (readDoc != NULL) {
m_renderArea.processDocument(readDoc);
file.free(readDoc);
}
*/
// From IPFS
cmark_node *fetchDoc = file.fetch("QmQzhn6hEfbYdCfwzYFsSt3eWpubVKA1dNqsgUwci5vHwq");
if (fetchDoc != NULL) {
m_renderArea.processDocument(fetchDoc);
file.free(fetchDoc);
}
}

View File

@ -7,8 +7,7 @@
#include <gtkmm/scrolledwindow.h>
#include "render-area.h"
#include "menu.h"
#include "md-parser.h"
#include "network.h"
#include "file.h"
class MainWindow : public Gtk::Window
{
@ -26,10 +25,9 @@ protected:
Gtk::ScrolledWindow m_scrolledWindow;
RenderArea m_renderArea;
private:
Parser parser;
Network network;
File file;
void getFile();
void demo();
};
#endif

View File

@ -1,7 +1,6 @@
#include "md-parser.h"
#include <string>
#include <iostream>
#include <cmark-gfm-core-extensions.h>
#include <node.h>
@ -19,51 +18,40 @@ cmark_node * Parser::parseFile(const std::string &filePath)
FILE *file;
if( ( file = fopen(filePath.c_str(), "r" ) ) != NULL )
{
cmark_node *root_node;
cmark_node *doc;
// TODO: Copy/paste cmark_parse_file() content to here, allowing me to add extensions to the parser.
root_node = cmark_parse_file(file, options);
doc = cmark_parse_file(file, options);
fclose(file);
return root_node;
return doc;
}
return NULL;
}
/**
* Parse markdown file by stringstream
* @return AST structure (of type cmark_node)
*/
cmark_node * Parser::parseStream(const std::stringstream &stream)
{
//cmark_node *doc;
// Parse to AST with cmark
cmark_parser *parser = cmark_parser_new(options);
// mark_parser *parser = cmark_parser_new(options);
// Add extensions
//addMarkdownExtension(parser, "strikethrough");
//addMarkdownExtension(parser, "table");
//const char buffer[4096];
size_t bytes;
cmark_node *document;
// Print to console
std::cout << stream.str() << std::endl;
// stream.read() ..?
/*
while ((bytes = fread(buffer, 1, sizeof(buffer), f)) > 0) {
bool eof = bytes < sizeof(buffer);
cmark_parser_feed(parser, buffer, bytes);
if (eof) {
break;
}
}*/
document = cmark_parser_finish(parser);
cmark_parser_free(parser);
return document;
const std::string tmp = stream.str();
const char *data = tmp.c_str();
// TODO: Copy cmark_parse_document() to be able to add extensions to the parser
return cmark_parse_document(data, strlen(data), options);
}
/**
* Built-in cmark parser to HTML
*/
std::string const Parser::renderHTML(cmark_node *node)
{
char *tmp = cmark_render_html(node, options, NULL);

View File

@ -2,15 +2,12 @@
#include <sstream>
// Connect to IPFS daemon
Network::Network()
: client("localhost", 5001)
{
}
Network::~Network() {
}
void Network::fetchFile(const std::string& path, std::iostream* response) {
// Demo file
client.FilesGet(path, response);
}

View File

@ -13,7 +13,6 @@ class Network
{
public:
Network();
virtual ~Network();
void fetchFile(const std::string& path, std::iostream* response);
private:

View File

@ -1,3 +1,4 @@
missingIncludeSystem
missingInclude:lib/*
unusedFunction
unusedFunction
unusedPrivateFunction