Some refactoring + introduce parseStream()

master
Melroy van den Berg 2020-12-05 01:29:39 +01:00
parent bedc7a0dd8
commit 1674c8d91d
6 changed files with 56 additions and 38 deletions

View File

@ -1,5 +1,4 @@
#include "mainwindow.h"
#include "md-parser.h"
#include <gtkmm/menuitem.h>
#ifdef LEGACY_CXX
@ -28,28 +27,23 @@ MainWindow::MainWindow() : m_vbox(Gtk::ORIENTATION_VERTICAL, 0)
add(m_vbox);
show_all_children();
// Get demo file
getFile();
}
void MainWindow::getFile()
{
// Just an IPFS test! Fetch a resource from the IPFS network
// Assuming you already running a IPFS deamon
network.FetchReadme();
// Setup parser
setupParser();
}
MainWindow::~MainWindow()
{
delete parser;
}
void MainWindow::setupParser()
{
parser = new Parser();
std::stringstream contents;
network.fetchFile("QmQzhn6hEfbYdCfwzYFsSt3eWpubVKA1dNqsgUwci5vHwq", &contents);
parser.parseStream(contents);
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);
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());*/

View File

@ -7,15 +7,13 @@
#include <gtkmm/scrolledwindow.h>
#include "render-area.h"
#include "menu.h"
#include "md-parser.h"
#include "network.h"
class Parser;
class MainWindow : public Gtk::Window
{
public:
MainWindow();
virtual ~MainWindow();
protected:
// Signal handlers:
@ -28,10 +26,10 @@ protected:
Gtk::ScrolledWindow m_scrolledWindow;
RenderArea m_renderArea;
private:
Parser *parser;
Parser parser;
Network network;
void setupParser();
void getFile();
};
#endif

View File

@ -1,6 +1,7 @@
#include "md-parser.h"
#include <string>
#include <iostream>
#include <cmark-gfm-core-extensions.h>
#include <node.h>
@ -14,15 +15,6 @@ Parser::Parser(): options(CMARK_OPT_DEFAULT) {}
*/
cmark_node * Parser::parseFile(const std::string &filePath)
{
// Modified version of cmark_parse_document in blocks.c
cmark_parser *parser = cmark_parser_new(options);
// Add extensions
addMarkdownExtension(parser, "strikethrough");
addMarkdownExtension(parser, "table");
cmark_parser_free(parser);
// Parse to AST with cmark
FILE *file;
if( ( file = fopen(filePath.c_str(), "r" ) ) != NULL )
@ -38,6 +30,40 @@ cmark_node * Parser::parseFile(const std::string &filePath)
return NULL;
}
cmark_node * Parser::parseStream(const std::stringstream &stream)
{
// Parse to AST with cmark
cmark_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;
}
std::string const Parser::renderHTML(cmark_node *node)
{
char *tmp = cmark_render_html(node, options, NULL);

View File

@ -4,7 +4,7 @@
#include <string>
#include <cmark-gfm.h>
#include <render.h>
#include <sstream>
/**
* \class Parser Markdown parser class, parse the content to an AST model
*/
@ -13,6 +13,7 @@ class Parser
public:
Parser();
cmark_node * parseFile(const std::string &filePath);
cmark_node * parseStream(const std::stringstream &stream);
std::string const renderHTML(cmark_node *node);
private:

View File

@ -1,6 +1,5 @@
#include "network.h"
#include <iostream>
#include <sstream>
Network::Network()
@ -11,9 +10,7 @@ Network::Network()
Network::~Network() {
}
void Network::FetchReadme() {
// Demo ...
std::stringstream contents;
client.FilesGet("QmQzhn6hEfbYdCfwzYFsSt3eWpubVKA1dNqsgUwci5vHwq", &contents);
std::cout << contents.str() << std::endl;
void Network::fetchFile(const std::string& path, std::iostream* response) {
// Demo file
client.FilesGet(path, response);
}

View File

@ -2,6 +2,8 @@
#define NETWORK_H
#include <ipfs/client.h>
#include <iostream>
#include <string>
/**
* \class Network
@ -13,7 +15,7 @@ public:
Network();
virtual ~Network();
void FetchReadme();
void fetchFile(const std::string& path, std::iostream* response);
private:
ipfs::Client client;
};