Added the ability to script the rendering of the MarkdownRenderer. Also added a new render() method equivalent to render_to_html().

This commit is contained in:
Relintai 2024-04-26 17:31:42 +02:00
parent b3eab569ea
commit fb3e87a9e3
4 changed files with 1297 additions and 1 deletions

View File

@ -63,6 +63,7 @@ def get_doc_classes():
"BBCodeParser", "BBCodeParser",
"MarkdownRenderer", "MarkdownRenderer",
"MarkdownRendererCustomRendererCallback",
"StaticWebPage", "StaticWebPage",
"StaticWebPageFile", "StaticWebPageFile",

File diff suppressed because it is too large Load Diff

View File

@ -36,6 +36,149 @@
#include "core/object/reference.h" #include "core/object/reference.h"
#include "libs/hoedown/document.h"
// So there is no need to add ~20 virtual methods
class MarkdownRendererCustomRendererCallback : public Reference {
GDCLASS(MarkdownRendererCustomRendererCallback, Reference);
public:
enum CallbackType {
CALLBACK_BLOCKCODE = 0,
CALLBACK_BLOCKQUOTE,
CALLBACK_HEADER,
CALLBACK_HRULE,
CALLBACK_LIST,
CALLBACK_LISTITEM,
CALLBACK_PARAGRAPH,
CALLBACK_TABLE,
CALLBACK_TABLE_HEADER,
CALLBACK_TABLE_BODY,
CALLBACK_TABLE_ROW,
CALLBACK_TABLE_CELL,
CALLBACK_FOOTNOTES,
CALLBACK_FOOTNOTE_DEF,
CALLBACK_BLOCKHTML,
CALLBACK_AUTOLINK,
CALLBACK_CODESPAN,
CALLBACK_DOUBLE_EMPHASIS,
CALLBACK_EMPHASIS,
CALLBACK_UNDERLINE,
CALLBACK_HIGHLIGHT,
CALLBACK_QUOTE,
CALLBACK_IMAGE,
CALLBACK_LINEBREAK,
CALLBACK_LINK,
CALLBACK_TRIPLE_EMPHASIS,
CALLBACK_STRIKETHROUGH,
CALLBACK_SUPERSCRIPT,
CALLBACK_FOOTNOTE_REF,
CALLBACK_MATH,
CALLBACK_RAW_HTML,
CALLBACK_ENTITY,
CALLBACK_NORMAL_TEXT,
CALLBACK_DOC_HEADER,
CALLBACK_DOC_FOOTER,
};
enum ListFlags {
LIST_FLAG_ORDERED = (1 << 0),
LIST_FLAG_LI_BLOCK = (1 << 1)
};
enum TableFlags {
TABLE_FLAG_ALIGN_LEFT = 1,
TABLE_FLAG_ALIGN_RIGHT = 2,
TABLE_FLAG_ALIGN_CENTER = 3,
TABLE_FLAG_ALIGNMASK = 3,
TABLE_FLAG_HEADER = 4
};
enum AutolinkType {
AUTOLINK_TYPE_NONE = 0, /* used internally when it is not an autolink*/
AUTOLINK_TYPE_NORMAL, /* normal http/http/ftp/mailto/etc link */
AUTOLINK_TYPE_EMAIL /* e-mail link without explit mailto: */
};
CallbackType get_callback_type() const;
void set_callback_type(const CallbackType val);
String get_text() const;
void set_text(const String &val);
String get_lang() const;
void set_lang(const String &val);
String get_content() const;
void set_content(const String &val);
int get_level() const;
void set_level(const int val);
int get_list_flags() const;
void set_list_flags(const int val);
int get_table_flags() const;
void set_table_flags(const int val);
String get_link() const;
void set_link(const String &val);
AutolinkType get_auto_link_type() const;
void set_auto_link_type(const AutolinkType val);
String get_title() const;
void set_title(const String &val);
String get_alt() const;
void set_alt(const String &val);
uint32_t get_num() const;
void set_num(const uint32_t val);
int get_display_mode() const;
void set_display_mode(const int val);
int get_inline_render() const;
void set_inline_render(const int val);
String get_result() const;
void set_result(const String &val);
int get_result_code() const;
void set_result_code(const int val);
MarkdownRendererCustomRendererCallback();
virtual ~MarkdownRendererCustomRendererCallback();
protected:
static void _bind_methods();
protected:
CallbackType _callback_type;
String _text;
String _lang;
String _content;
int _level;
int _list_flags;
int _table_flags;
String _link;
AutolinkType _auto_link_type;
String _title;
String _alt;
uint32_t _num;
int _display_mode;
int _inline_render;
String _result;
int _result_code;
};
class MarkdownRenderer : public Reference { class MarkdownRenderer : public Reference {
GDCLASS(MarkdownRenderer, Reference); GDCLASS(MarkdownRenderer, Reference);
@ -43,6 +186,7 @@ public:
enum RenderType { enum RenderType {
RENDER_TYPE_HTML = 0, RENDER_TYPE_HTML = 0,
RENDER_TYPE_HTML_TOC = 1, RENDER_TYPE_HTML_TOC = 1,
RENDERER_TYPE_CUSTOM = 2,
}; };
enum HTMLFlags { enum HTMLFlags {
@ -92,11 +236,15 @@ public:
void set_toc_level(const int val); void set_toc_level(const int val);
String render_to_html(const String &markdown); String render_to_html(const String &markdown);
String render(const String &markdown);
MarkdownRenderer(); MarkdownRenderer();
virtual ~MarkdownRenderer(); virtual ~MarkdownRenderer();
protected: protected:
void renderer_callback(Ref<MarkdownRendererCustomRendererCallback> data);
virtual void _renderer_callback(Ref<MarkdownRendererCustomRendererCallback> data);
static void _bind_methods(); static void _bind_methods();
RenderType _render_type; RenderType _render_type;
@ -105,10 +253,63 @@ protected:
int _max_nesting; int _max_nesting;
int _toc_level; int _toc_level;
bool _use_smartypants; bool _use_smartypants;
protected:
struct hoedown_custom_renderer_state {
void *self;
};
/* block level callbacks - NULL skips the block */
static void hoedown_cb_blockcode(hoedown_buffer *ob, const hoedown_buffer *text, const hoedown_buffer *lang, const hoedown_renderer_data *data);
static void hoedown_cb_blockquote(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
static void hoedown_cb_header(hoedown_buffer *ob, const hoedown_buffer *content, int level, const hoedown_renderer_data *data);
static void hoedown_cb_hrule(hoedown_buffer *ob, const hoedown_renderer_data *data);
static void hoedown_cb_list(hoedown_buffer *ob, const hoedown_buffer *content, hoedown_list_flags flags, const hoedown_renderer_data *data);
static void hoedown_cb_listitem(hoedown_buffer *ob, const hoedown_buffer *content, hoedown_list_flags flags, const hoedown_renderer_data *data);
static void hoedown_cb_paragraph(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
static void hoedown_cb_table(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
static void hoedown_cb_table_header(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
static void hoedown_cb_table_body(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
static void hoedown_cb_table_row(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
static void hoedown_cb_table_cell(hoedown_buffer *ob, const hoedown_buffer *content, hoedown_table_flags flags, const hoedown_renderer_data *data);
static void hoedown_cb_footnotes(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
static void hoedown_cb_footnote_def(hoedown_buffer *ob, const hoedown_buffer *content, unsigned int num, const hoedown_renderer_data *data);
static void hoedown_cb_blockhtml(hoedown_buffer *ob, const hoedown_buffer *text, const hoedown_renderer_data *data);
/* span level callbacks - NULL or return 0 prints the span verbatim */
static int hoedown_cb_autolink(hoedown_buffer *ob, const hoedown_buffer *link, hoedown_autolink_type type, const hoedown_renderer_data *data);
static int hoedown_cb_codespan(hoedown_buffer *ob, const hoedown_buffer *text, const hoedown_renderer_data *data);
static int hoedown_cb_double_emphasis(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
static int hoedown_cb_emphasis(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
static int hoedown_cb_underline(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
static int hoedown_cb_highlight(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
static int hoedown_cb_quote(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
static int hoedown_cb_image(hoedown_buffer *ob, const hoedown_buffer *link, const hoedown_buffer *title, const hoedown_buffer *alt, const hoedown_renderer_data *data);
static int hoedown_cb_linebreak(hoedown_buffer *ob, const hoedown_renderer_data *data);
static int hoedown_cb_link(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_buffer *link, const hoedown_buffer *title, const hoedown_renderer_data *data);
static int hoedown_cb_triple_emphasis(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
static int hoedown_cb_strikethrough(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
static int hoedown_cb_superscript(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data);
static int hoedown_cb_footnote_ref(hoedown_buffer *ob, unsigned int num, const hoedown_renderer_data *data);
static int hoedown_cb_math(hoedown_buffer *ob, const hoedown_buffer *text, int displaymode, const hoedown_renderer_data *data);
static int hoedown_cb_raw_html(hoedown_buffer *ob, const hoedown_buffer *text, const hoedown_renderer_data *data);
static void hoedown_cb_entity(hoedown_buffer *ob, const hoedown_buffer *text, const hoedown_renderer_data *data);
static void hoedown_cb_normal_text(hoedown_buffer *ob, const hoedown_buffer *text, const hoedown_renderer_data *data);
static void hoedown_cb_doc_header(hoedown_buffer *ob, int inline_render, const hoedown_renderer_data *data);
static void hoedown_cb_doc_footer(hoedown_buffer *ob, int inline_render, const hoedown_renderer_data *data);
hoedown_renderer *create_custom_hoedown_renderer();
}; };
VARIANT_ENUM_CAST(MarkdownRenderer::RenderType); VARIANT_ENUM_CAST(MarkdownRenderer::RenderType);
VARIANT_ENUM_CAST(MarkdownRenderer::HTMLFlags); VARIANT_ENUM_CAST(MarkdownRenderer::HTMLFlags);
VARIANT_ENUM_CAST(MarkdownRenderer::MarkdownExtensions); VARIANT_ENUM_CAST(MarkdownRenderer::MarkdownExtensions);
VARIANT_ENUM_CAST(MarkdownRendererCustomRendererCallback::CallbackType);
VARIANT_ENUM_CAST(MarkdownRendererCustomRendererCallback::ListFlags);
VARIANT_ENUM_CAST(MarkdownRendererCustomRendererCallback::TableFlags);
VARIANT_ENUM_CAST(MarkdownRendererCustomRendererCallback::AutolinkType);
#endif #endif

View File

@ -122,6 +122,7 @@ void register_web_types(ModuleRegistrationLevel p_level) {
ClassDB::register_class<BBCodeParser>(); ClassDB::register_class<BBCodeParser>();
ClassDB::register_class<MarkdownRenderer>(); ClassDB::register_class<MarkdownRenderer>();
ClassDB::register_class<MarkdownRendererCustomRendererCallback>();
ClassDB::register_class<FileCache>(); ClassDB::register_class<FileCache>();