Skip to content

Commit

Permalink
add inline code rule
Browse files Browse the repository at this point in the history
  • Loading branch information
lytefast committed Oct 27, 2020
1 parent 63234ee commit af5f644
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,11 @@ object CustomMarkdownRules {
}
}
}

fun <RC, S : BlockQuoteState<S>> createCodeInlineRule(context: Context): Rule<RC, Node<RC>, S> {
return CodeRules.createInlineCodeRule(
{ listOf(TextAppearanceSpan(context, R.style.Code_TextAppearance)) },
{ listOf(BackgroundColorSpan(Color.DKGRAY)) },
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class MainActivity : AppCompatActivity() {
listOf(R.style.Demo_Header_1, R.style.Demo_Header_2, R.style.Demo_Header_3),
listOf(R.style.Demo_Header_1_Add, R.style.Demo_Header_1_Remove, R.style.Demo_Header_1_Fix)))
.addRule(CustomMarkdownRules.createCodeRule(this))
.addRule(CustomMarkdownRules.createCodeInlineRule(this))
.addRules(SimpleMarkdownRules.createSimpleMarkdownRules())

resultText.text = SimpleRenderer.render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object SampleTexts {
const val TEXT = """
Some really long **introduction** text that goes on **__forever__** explaining __something__.
single newline above
single `newline` above. `test` sentence
"""

const val HEADERS = """
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.discord.simpleast.code

import android.text.SpannableStringBuilder
import android.text.Spanned
import com.discord.simpleast.core.node.Node
import com.discord.simpleast.core.node.StyleNode
import com.discord.simpleast.core.node.TextNode
Expand Down Expand Up @@ -37,6 +39,9 @@ object CodeRules {
val PATTERN_CODE_BLOCK: Pattern =
Pattern.compile("""^```(?:([A-z0-9_+\-.]+))?(\s*)([^\n].*?)\n*```""", Pattern.DOTALL)

val PATTERN_CODE_INLINE: Pattern =
Pattern.compile("""^`(?:\s*)([^\n].*?)\n*`""", Pattern.DOTALL)

private const val CODE_BLOCK_LANGUAGE_GROUP = 1
private const val CODE_BLOCK_WS_PREFIX = 2
private const val CODE_BLOCK_BODY_GROUP = 3
Expand Down Expand Up @@ -216,4 +221,32 @@ object CodeRules {
}
}
}

fun <R, S> createInlineCodeRule(
textStyleProvider: StyleNode.SpanProvider<R>,
bgStyleProvider: StyleNode.SpanProvider<R>,

): Rule<R, Node<R>, S> {
return object : Rule<R, Node<R>, S>(PATTERN_CODE_INLINE) {
override fun parse(matcher: Matcher, parser: Parser<R, in Node<R>, S>, state: S)
: ParseSpec<R, Node<R>, S> {
val codeBody = matcher.group(1).orEmpty()

val content = CodeNode.Content.Raw(codeBody)

val codeNode = CodeNode(content, null, textStyleProvider)
// We can't use a StyleNode here as we can't share background spans.
val node = object : Node.Parent<R>(codeNode) {
override fun render(builder: SpannableStringBuilder, renderContext: R) {
val startIndex = builder.length
super.render(builder, renderContext)
bgStyleProvider.get(renderContext).forEach {
builder.setSpan(it, startIndex, builder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
}
}
return ParseSpec.createTerminal(node, state)
}
}
}
}

0 comments on commit af5f644

Please sign in to comment.