crosspoint-reader/lib/StarDict/DictHtmlParser.h
cottongin 8fa01bc83a
Some checks failed
CI / build (push) Failing after 2m16s
fix: prevent Serial.printf from blocking when USB disconnected
On ESP32-C3 with USB CDC, Serial.printf() blocks indefinitely when USB
is not connected. This caused device freezes when booted without USB.

Solution: Call Serial.setTxTimeoutMs(0) after Serial.begin() to make
all Serial output non-blocking.

Also added if (Serial) guards to high-traffic logging paths in
EpubReaderActivity as belt-and-suspenders protection.

Includes documentation of the debugging process and Serial call inventory.

Also applies clang-format to fix pre-existing formatting issues.
2026-01-28 16:02:13 -05:00

65 lines
2.0 KiB
C++

#pragma once
#include <Epub/blocks/TextBlock.h>
#include <functional>
#include <memory>
#include <string>
class GfxRenderer;
/**
* DictHtmlParser parses HTML dictionary definitions into ParsedText.
*
* Supports:
* - Bold: <b>, <strong>
* - Italic: <i>, <em>
* - Underline: <u>, <ins>
* - Lists: <ol>, <li> with numbering/bullets
* - Block elements: <p>, <br>, <hr>, </html> (entry separator)
* - HTML entities: numeric (&#NNN;, &#xHHH;) and named (&amp;, etc.)
* - Superscript: <sup> rendered as ^text
*/
class DictHtmlParser {
public:
/**
* Parse HTML definition and populate ParsedText with styled words.
* Each paragraph/block creates a separate ParsedText via the callback.
*
* @param html The HTML definition text
* @param fontId Font ID for text width calculations
* @param renderer Reference to renderer for layout
* @param onParagraph Callback invoked for each paragraph/block of text
*/
static void parse(const std::string& html, int fontId, const GfxRenderer& renderer, uint16_t viewportWidth,
const std::function<void(std::shared_ptr<TextBlock>)>& onTextBlock);
private:
// Decode HTML entity at position i (starting with '&')
static std::string decodeEntity(const std::string& html, size_t& i);
// Extract tag name from position (after '<')
static std::string extractTagName(const std::string& html, size_t start, bool& isClosing);
// Check if tag is a block-level element
static bool isBlockTag(const std::string& tagName);
// Check if tag starts/ends bold
static bool isBoldTag(const std::string& tagName);
// Check if tag starts/ends italic
static bool isItalicTag(const std::string& tagName);
// Check if tag starts/ends underline
static bool isUnderlineTag(const std::string& tagName);
// Check if tag is superscript
static bool isSuperscriptTag(const std::string& tagName);
// Check if tag is list item
static bool isListItemTag(const std::string& tagName);
// Check if tag starts ordered list
static bool isOrderedListTag(const std::string& tagName);
};