-- SPDX-FileCopyrightText: © 2022—2026 Vladimir Zorin -- SPDX-License-Identifier: LicenseRef-OWL-1.0-or-later -- Licensed under OWL v1.0+. See LICENSE. --[==[ Callback-based event emitter for the markdown parser. Invokes handlers immediately as events occur, enabling true streaming rendering. Events carry type, tag, optional text, and optional attrs fields. ]==] -- Emit an event to the callback local emit = function(self, event) if self.__state.callback then self.__state.callback(event) end end -- Emit a block start event local emit_block_start = function(self, tag, attrs) self:emit({ type = "block_start", tag = tag, attrs = attrs }) end -- Emit a block end event local emit_block_end = function(self, tag) self:emit({ type = "block_end", tag = tag }) end -- Emit a text event local emit_text = function(self, text) if text and text ~= "" then self:emit({ type = "text", text = text }) end end -- Emit an inline start event local emit_inline_start = function(self, tag, attrs) self:emit({ type = "inline_start", tag = tag, attrs = attrs }) end -- Emit an inline end event local emit_inline_end = function(self, tag) self:emit({ type = "inline_end", tag = tag }) end -- Emit a softbreak event local emit_softbreak = function(self) self:emit({ type = "softbreak" }) end -- Change the callback function local set_callback = function(self, fn) self.__state.callback = fn end ---! Create a new event emitter ---@ new(`on_event`{.func .opt}) -> `emitter`{.tbl} local new = function(on_event) return { __state = { callback = on_event, }, emit = emit, emit_block_start = emit_block_start, emit_block_end = emit_block_end, emit_inline_start = emit_inline_start, emit_inline_end = emit_inline_end, emit_text = emit_text, emit_softbreak = emit_softbreak, set_callback = set_callback, } end return { new = new }