From 1674c8d91d38606e86455094dd01afe6ef6e4b0d Mon Sep 17 00:00:00 2001 From: Melroy van den Berg Date: Sat, 5 Dec 2020 01:29:39 +0100 Subject: [PATCH] Some refactoring + introduce parseStream() --- src/mainwindow.cc | 26 ++++++++++---------------- src/mainwindow.h | 8 +++----- src/md-parser.cc | 44 +++++++++++++++++++++++++++++++++++--------- src/md-parser.h | 3 ++- src/network.cc | 9 +++------ src/network.h | 4 +++- 6 files changed, 56 insertions(+), 38 deletions(-) diff --git a/src/mainwindow.cc b/src/mainwindow.cc index 536c750..c9e59e6 100644 --- a/src/mainwindow.cc +++ b/src/mainwindow.cc @@ -1,5 +1,4 @@ #include "mainwindow.h" -#include "md-parser.h" #include #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());*/ diff --git a/src/mainwindow.h b/src/mainwindow.h index 47eb46c..7ef06b0 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -7,15 +7,13 @@ #include #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 \ No newline at end of file diff --git a/src/md-parser.cc b/src/md-parser.cc index a98d797..e3023b2 100644 --- a/src/md-parser.cc +++ b/src/md-parser.cc @@ -1,6 +1,7 @@ #include "md-parser.h" #include +#include #include #include @@ -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); diff --git a/src/md-parser.h b/src/md-parser.h index 633d1a8..c31df96 100644 --- a/src/md-parser.h +++ b/src/md-parser.h @@ -4,7 +4,7 @@ #include #include #include - +#include /** * \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: diff --git a/src/network.cc b/src/network.cc index 7312f52..3cf0d1b 100644 --- a/src/network.cc +++ b/src/network.cc @@ -1,6 +1,5 @@ #include "network.h" -#include #include 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); } \ No newline at end of file diff --git a/src/network.h b/src/network.h index fe8d427..25341f7 100644 --- a/src/network.h +++ b/src/network.h @@ -2,6 +2,8 @@ #define NETWORK_H #include +#include +#include /** * \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; };