Skip to content

Commit

Permalink
feat: disable paragraph support when indentUnit is undefined or 0
Browse files Browse the repository at this point in the history
closes #6
  • Loading branch information
cbeyls committed Oct 19, 2024
1 parent 36da547 commit 370feea
Showing 1 changed file with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ internal class AnnotatedStringHtmlHandler(
private var preformattedLevel = 0
private var boldLevel = 0
private var skippedTagsLevel = 0
private var blockLevel = 0
private var blockLevel = if (isParagraphSupportDisabled) -1 else 0
private var blockIndentLevel = 0
private var paragraphStartIndex = -1

private val isParagraphSupportDisabled: Boolean
get() = style.indentUnit.let { it.value.isNaN() || it.value == 0f }

private fun pushPendingSpanStyles() {
val size = pendingSpanStyles.size
if (size != 0) {
Expand Down Expand Up @@ -134,12 +137,16 @@ internal class AnnotatedStringHtmlHandler(
}

private fun handleBlockStart(prefixNewLineCount: Int, indent: Boolean) {
val currentIndex = builder.length
addPendingParagraph(currentIndex)
paragraphStartIndex = currentIndex
blockLevel++
if (indent) {
blockIndentLevel++
var level = blockLevel
if (level >= 0) {
val currentIndex = builder.length
addPendingParagraph(currentIndex)
paragraphStartIndex = currentIndex
level++
blockLevel = level
if (indent) {
blockIndentLevel++
}
}
textWriter.markBlockBoundary(if (compactMode) 1 else prefixNewLineCount, 0)
}
Expand Down Expand Up @@ -262,19 +269,22 @@ internal class AnnotatedStringHtmlHandler(
}

private fun handleBlockEnd(suffixNewLineCount: Int, indent: Boolean) {
val currentIndex = builder.length
// Paragraph will only be added if non-empty
val hasTrailingParagraph = addPendingParagraph(currentIndex)
val level = blockLevel - 1
blockLevel = level
if (indent) {
blockIndentLevel--
}
paragraphStartIndex = if (level == 0) {
// Encode the end position of the trailing paragraph as a negative value using bit inversion
if (hasTrailingParagraph) currentIndex.inv() else -1
} else {
currentIndex
var level = blockLevel
if (level >= 0) {
val currentIndex = builder.length
// Paragraph will only be added if non-empty
val hasTrailingParagraph = addPendingParagraph(currentIndex)
level--
blockLevel = level
if (indent) {
blockIndentLevel--
}
paragraphStartIndex = if (level == 0) {
// Encode the end position of the trailing paragraph as a negative value using bit inversion
if (hasTrailingParagraph) currentIndex.inv() else -1
} else {
currentIndex
}
}
textWriter.markBlockBoundary(if (compactMode) 1 else suffixNewLineCount, 0)
}
Expand Down

0 comments on commit 370feea

Please sign in to comment.