Skip to content

Commit

Permalink
Trim common indentation from inline arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Sep 20, 2024
1 parent 07eafb7 commit c9f3f17
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import eu.iamgio.quarkdown.lexer.tokens.PlainTextToken
/**
* Beginning of a function argument.
*/
private const val ARG_DELIMITER_OPEN = '{'
const val ARG_DELIMITER_OPEN = '{'

/**
* End of a function argument.
*/
private const val ARG_DELIMITER_CLOSE = '}'
const val ARG_DELIMITER_CLOSE = '}'

/**
* End of named argument's name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import eu.iamgio.quarkdown.lexer.tokens.ParagraphToken
import eu.iamgio.quarkdown.lexer.tokens.SetextHeadingToken
import eu.iamgio.quarkdown.lexer.tokens.TableToken
import eu.iamgio.quarkdown.lexer.tokens.UnorderedListToken
import eu.iamgio.quarkdown.lexer.walker.ARG_DELIMITER_CLOSE
import eu.iamgio.quarkdown.lexer.walker.ARG_DELIMITER_OPEN
import eu.iamgio.quarkdown.util.iterator
import eu.iamgio.quarkdown.util.nextOrNull
import eu.iamgio.quarkdown.util.takeUntilLastOccurrence
Expand Down Expand Up @@ -405,13 +407,14 @@ class BlockTokenParser(private val context: MutableContext) : BlockTokenVisitor<
groups.forEachRemaining { arg ->
// If this group contains the name of a named argument,
// it is applied to the very next argument.
if (arg.firstOrNull() != '{' && arg.lastOrNull() != '}') {
if (arg.firstOrNull() != ARG_DELIMITER_OPEN && arg.lastOrNull() != ARG_DELIMITER_CLOSE) {
argName = arg
return@forEachRemaining
}

// Regular argument wrapped in brackets, which are stripped off.
val argContent = arg.trimDelimiters().trim()
// Common indentation is also removed.
val argContent = arg.trimDelimiters().trimIndent().trim()

// An expression from the raw string is created.
ValueFactory.expression(argContent, context)?.let {
Expand Down
14 changes: 14 additions & 0 deletions test/src/test/kotlin/eu/iamgio/quarkdown/test/FullPipelineTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,20 @@ class FullPipelineTest {
assertEquals("<p>18</p>", it)
}

execute(
"""
.divide {
.cos {.pi}
} by:{
.sin {
1
}
}
""".trimIndent(),
) {
assertEquals("<p>-1.1883951</p>", it)
}

execute("$ 4 - 2 = $ .subtract {4} {2}") {
assertEquals("<p>__QD_INLINE_MATH__$4 - 2 =\$__QD_INLINE_MATH__ 2</p>", it)
assertTrue(attributes.hasMath)
Expand Down

0 comments on commit c9f3f17

Please sign in to comment.