LibreWeb-Browser/src/md-parser.cc

66 lines
1.6 KiB
C++
Raw Normal View History

2020-11-16 10:31:58 -08:00
#include "md-parser.h"
2020-11-12 14:27:47 -08:00
#include <string>
2020-12-10 10:01:11 -08:00
#include <stdexcept>
#include <cmark-gfm-core-extensions.h>
2020-11-14 17:39:05 -08:00
#include <node.h>
#include <syntax_extension.h>
2020-12-10 10:01:11 -08:00
#include <filesystem>
2020-11-12 19:31:05 -08:00
2020-12-14 10:40:08 -08:00
static const int OPTIONS = CMARK_OPT_DEFAULT;
/// Meyers Singleton
2021-02-12 12:46:41 -08:00
Parser::Parser() = default;
2020-12-14 10:40:08 -08:00
/// Destructor
2021-02-12 12:46:41 -08:00
Parser::~Parser() = default;
2020-11-12 15:35:14 -08:00
2020-11-15 16:10:54 -08:00
/**
2020-12-14 10:40:08 -08:00
* \brief Get singleton instance
* \return Helper reference (singleton)
2020-11-15 16:10:54 -08:00
*/
2021-02-12 12:46:41 -08:00
Parser &Parser::getInstance()
{
static Parser instance;
return instance;
2020-11-15 16:10:54 -08:00
}
2020-12-04 17:51:40 -08:00
/**
2020-12-14 10:40:08 -08:00
* Parse markdown file from string content
2020-12-04 17:51:40 -08:00
* @return AST structure (of type cmark_node)
*/
2021-02-12 12:46:41 -08:00
cmark_node *Parser::parseContent(const std::string &content)
{
2020-12-04 17:51:40 -08:00
//cmark_node *doc;
// Parse to AST with cmark
2020-12-14 10:40:08 -08:00
// mark_parser *parser = cmark_parser_new(OPTIONS);
// Add extensions
//addMarkdownExtension(parser, "strikethrough");
//addMarkdownExtension(parser, "table");
2021-02-12 12:46:41 -08:00
const char *data = content.c_str();
2020-12-04 17:51:40 -08:00
// TODO: Copy cmark_parse_document() to be able to add extensions to the parser
2020-12-14 10:40:08 -08:00
return cmark_parse_document(data, strlen(data), OPTIONS);
}
2020-12-04 17:51:40 -08:00
/**
* Built-in cmark parser to HTML
*/
2020-11-29 18:54:21 -08:00
std::string const Parser::renderHTML(cmark_node *node)
2020-11-15 16:10:54 -08:00
{
2020-12-14 10:40:08 -08:00
char *tmp = cmark_render_html(node, OPTIONS, NULL);
2020-12-11 21:02:17 -08:00
std::string output = std::string(tmp);
free(tmp);
return output;
}
/**
* This is a function that will make enabling extensions easier
*/
2021-02-12 12:46:41 -08:00
void Parser::addMarkdownExtension(cmark_parser *parser, const char *extName)
{
cmark_syntax_extension *ext = cmark_find_syntax_extension(extName);
if (ext)
cmark_parser_attach_syntax_extension(parser, ext);
}