Skip to content

Commit

Permalink
feat: add support for <hr> tag
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeyls committed Jan 21, 2024
1 parent 8756c6a commit fa66a4f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` (<sup>supertext</sup>), `sub` (<sub>subtext</sub>)
- 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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
}
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit fa66a4f

Please sign in to comment.