From fa66a4f65a99cf2e43ddfc2c6cf83a5156266947 Mon Sep 17 00:00:00 2001 From: Christophe Beyls Date: Sun, 21 Jan 2024 17:45:19 +0100 Subject: [PATCH] feat: add support for
tag --- README.md | 1 + .../htmlconverter/internal/AnnotatedStringHtmlHandler.kt | 8 +++++++- .../compose/htmlconverter/internal/StringHtmlHandler.kt | 8 +++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 861a9b0..80b4e11 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,7 @@ The default implementation uses the [KtXml](https://github.com/kobjects/ktxml) m - Inline tags with styling: `strong`, `b` (**bold**), `em`, `cite`, `dfn`, `i` (*italic*), `big` (bigger text), `small` (smaller text), `tt`, `code` (`monospace font`), `a` ([hyperlink](#supported-html-tags)), `u` (underline), `del`, `s`, `strike` (~~strikethrough~~), `sup` (supertext), `sub` (subtext) - Block tags (paragraphs): `p`, `blockquote`, `pre` (including `monospace font`), `div`, `header`, `footer`, `main`, `nav`, `aside`, `section`, `article`, `address`, `figure`, `figcaption`, `video`, `audio` (no player shown, only inline text) +- Horizontal rule: `hr` (no line drawn, but marks a new paragraph) - Lists: `ul`, `ol`, `li`, `dl`, `dt`, `dd` - Section headings: `h1`, `h2`, `h3`, `h4`, `h5`, `h6` - Line break: `br` diff --git a/htmlconverter/src/commonMain/kotlin/be/digitalia/compose/htmlconverter/internal/AnnotatedStringHtmlHandler.kt b/htmlconverter/src/commonMain/kotlin/be/digitalia/compose/htmlconverter/internal/AnnotatedStringHtmlHandler.kt index 3d01e77..87e1fb8 100644 --- a/htmlconverter/src/commonMain/kotlin/be/digitalia/compose/htmlconverter/internal/AnnotatedStringHtmlHandler.kt +++ b/htmlconverter/src/commonMain/kotlin/be/digitalia/compose/htmlconverter/internal/AnnotatedStringHtmlHandler.kt @@ -61,6 +61,7 @@ internal class AnnotatedStringHtmlHandler( override fun onOpenTag(name: String, attributes: (String) -> String?) { when (name) { "br" -> handleLineBreakStart() + "hr" -> handleHorizontalRuleStart() "p" -> handleBlockStart(2, false) "blockquote" -> handleBlockStart(2, true) "div", "header", "footer", "main", "nav", "aside", "section", "article", @@ -91,6 +92,10 @@ internal class AnnotatedStringHtmlHandler( textWriter.writeLineBreak() } + private fun handleHorizontalRuleStart() { + textWriter.markBlockBoundary(if (compactMode) 1 else 2, 0) + } + /** * Add a pending paragraph, if any, and return the current index. */ @@ -210,7 +215,8 @@ internal class AnnotatedStringHtmlHandler( override fun onCloseTag(name: String) { when (name) { - "br" -> {} + "br", + "hr" -> {} "p" -> handleBlockEnd(2, false) "blockquote" -> handleBlockEnd(2, true) "div", "header", "footer", "main", "nav", "aside", "section", "article", diff --git a/htmlconverter/src/commonMain/kotlin/be/digitalia/compose/htmlconverter/internal/StringHtmlHandler.kt b/htmlconverter/src/commonMain/kotlin/be/digitalia/compose/htmlconverter/internal/StringHtmlHandler.kt index 3de5868..22c7dc3 100644 --- a/htmlconverter/src/commonMain/kotlin/be/digitalia/compose/htmlconverter/internal/StringHtmlHandler.kt +++ b/htmlconverter/src/commonMain/kotlin/be/digitalia/compose/htmlconverter/internal/StringHtmlHandler.kt @@ -34,6 +34,7 @@ internal class StringHtmlHandler( override fun onOpenTag(name: String, attributes: (String) -> String?) { when (name) { "br" -> handleLineBreakStart() + "hr" -> handleHorizontalRuleStart() "p", "blockquote" -> handleBlockStart(2, 0) "div", "header", "footer", "main", "nav", "aside", "section", "article", "address", "figure", "figcaption", @@ -53,6 +54,10 @@ internal class StringHtmlHandler( textWriter.writeLineBreak() } + private fun handleHorizontalRuleStart() { + textWriter.markBlockBoundary(if (compactMode) 1 else 2, 0) + } + private fun handleBlockStart(prefixNewLineCount: Int, indentCount: Int) { textWriter.markBlockBoundary(if (compactMode) 1 else prefixNewLineCount, indentCount) } @@ -113,7 +118,8 @@ internal class StringHtmlHandler( override fun onCloseTag(name: String) { when (name) { - "br" -> {} + "br", + "hr" -> {} "p", "blockquote" -> handleBlockEnd(2) "div", "header", "footer", "main", "nav", "aside", "section", "article", "address", "figure", "figcaption",