LibreWeb-Browser/src/md-parser.cc

87 lines
2.2 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 <cmark-gfm-core-extensions.h>
2022-01-21 15:39:53 -08:00
#include <filesystem>
2020-11-14 17:39:05 -08:00
#include <node.h>
2022-01-21 15:39:53 -08:00
#include <stdexcept>
2020-11-14 17:39:05 -08:00
#include <syntax_extension.h>
2020-11-12 19:31:05 -08:00
static const int Options = CMARK_OPT_STRIKETHROUGH_DOUBLE_TILDE;
2020-12-14 10:40:08 -08:00
/// 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
*/
Parser& Parser::get_instance()
2021-02-12 12:46:41 -08:00
{
2022-01-21 15:39:53 -08:00
static Parser instance;
return instance;
2020-11-15 16:10:54 -08:00
}
2020-12-04 17:51:40 -08:00
/**
2022-01-21 15:39:53 -08:00
* \brief Parse markdown file from string content.
* Note: Do not forgot to execute: cmark_node_free(document); when you are done with the doc.
2022-01-21 15:39:53 -08:00
* \param content Content as string
* \return AST structure (of type cmark_node)
2020-12-04 17:51:40 -08:00
*/
cmark_node* Parser::parse_content(const Glib::ustring& content)
{
2022-01-21 15:39:53 -08:00
const char* data = content.c_str();
2022-01-21 15:39:53 -08:00
cmark_gfm_core_extensions_ensure_registered();
2022-01-21 15:39:53 -08:00
// Modified version of cmark_parse_document() in blocks.c
cmark_parser* parser = cmark_parser_new(Options);
2022-01-21 15:39:53 -08:00
cmark_node* document;
// Add extensions
add_markdown_extension(parser, "strikethrough");
add_markdown_extension(parser, "highlight");
add_markdown_extension(parser, "superscript");
add_markdown_extension(parser, "subscript");
// add_markdown_extension(parser, "table");
2022-01-21 15:39:53 -08:00
cmark_parser_feed(parser, data, strlen(data));
document = cmark_parser_finish(parser);
cmark_parser_free(parser);
return document;
}
2020-12-04 17:51:40 -08:00
/**
2022-01-21 15:39:53 -08:00
* \brief Built-in cmark parser to HTML
* \return HTML as string
2020-12-04 17:51:40 -08:00
*/
Glib::ustring Parser::render_html(cmark_node* node)
2020-11-15 16:10:54 -08:00
{
char* tmp = cmark_render_html(node, Options, NULL);
2022-01-21 15:39:53 -08:00
Glib::ustring output = Glib::ustring(tmp);
free(tmp);
return output;
2020-12-11 21:02:17 -08:00
}
2021-03-05 09:13:06 -08:00
/**
2022-01-21 15:39:53 -08:00
* \brief Built-in cmark parser to markdown (again)
* \return return markdown as string
2021-03-05 09:13:06 -08:00
*/
Glib::ustring Parser::render_markdown(cmark_node* node)
2021-03-05 09:13:06 -08:00
{
char* tmp = cmark_render_commonmark(node, Options, 600);
2022-01-21 15:39:53 -08:00
Glib::ustring output = Glib::ustring(tmp);
free(tmp);
return output;
2021-03-05 09:13:06 -08:00
}
/**
* This is a function that will make enabling extensions easier
*/
void Parser::add_markdown_extension(cmark_parser* parser, const char* ext_name)
2021-02-12 12:46:41 -08:00
{
cmark_syntax_extension* ext = cmark_find_syntax_extension(ext_name);
2022-01-21 15:39:53 -08:00
if (ext)
cmark_parser_attach_syntax_extension(parser, ext);
}