From 205ae7b47a66d7bd33a0b21c7c9105929cd8a117 Mon Sep 17 00:00:00 2001 From: Dimitry Ivanov Date: Wed, 24 Feb 2021 12:36:11 +0300 Subject: [PATCH] sample, add reddit superscript --- app-sample/samples.json | 13 +++ .../samples/parser/RedditSuperscriptSample.kt | 91 +++++++++++++++++++ .../markwon/sample/annotations/Tag.java | 1 + 3 files changed, 105 insertions(+) create mode 100644 app-sample/src/main/java/io/noties/markwon/app/samples/parser/RedditSuperscriptSample.kt diff --git a/app-sample/samples.json b/app-sample/samples.json index c6c5938d..ea8bf2d5 100644 --- a/app-sample/samples.json +++ b/app-sample/samples.json @@ -1,4 +1,17 @@ [ + { + "javaClassName": "io.noties.markwon.app.samples.parser.RedditSuperscriptSample", + "id": "20210224091506", + "title": "Reddit superscript", + "description": "", + "artifacts": [ + "HTML" + ], + "tags": [ + "html", + "reddit" + ] + }, { "javaClassName": "io.noties.markwon.app.samples.image.ImageSizeResolverSample", "id": "20210201165512", diff --git a/app-sample/src/main/java/io/noties/markwon/app/samples/parser/RedditSuperscriptSample.kt b/app-sample/src/main/java/io/noties/markwon/app/samples/parser/RedditSuperscriptSample.kt new file mode 100644 index 00000000..70dc705a --- /dev/null +++ b/app-sample/src/main/java/io/noties/markwon/app/samples/parser/RedditSuperscriptSample.kt @@ -0,0 +1,91 @@ +package io.noties.markwon.app.samples.parser + +import android.text.TextPaint +import android.text.style.MetricAffectingSpan +import io.noties.markwon.AbstractMarkwonPlugin +import io.noties.markwon.Markwon +import io.noties.markwon.MarkwonConfiguration +import io.noties.markwon.RenderProps +import io.noties.markwon.app.sample.ui.MarkwonTextViewSample +import io.noties.markwon.html.HtmlPlugin +import io.noties.markwon.html.HtmlTag +import io.noties.markwon.html.tag.SimpleTagHandler +import io.noties.markwon.sample.annotations.MarkwonArtifact +import io.noties.markwon.sample.annotations.MarkwonSampleInfo +import io.noties.markwon.sample.annotations.Tag.html +import io.noties.markwon.sample.annotations.Tag.reddit +import java.util.regex.Pattern + +@MarkwonSampleInfo( + id = "20210224091506", + title = "Reddit superscript", + artifacts = [MarkwonArtifact.HTML], + tags = [html, reddit] +) +class RedditSuperscriptSample : MarkwonTextViewSample() { + override fun render() { + val md = """ + The greatest thing you'll ever learn is just to ^reddit and be ^(reddited here) in ^return and ^(hey hey hye) something something^? daaagw + """.trimIndent() + + val markwon = Markwon.builder(context) + .usePlugin(HtmlPlugin.create { + it.addHandler(SmallerSuperScriptTagHandler()) + }) + .usePlugin(ProcessRedditSuperscript) + .build() + + markwon.setMarkdown(textView, md) + } + + object ProcessRedditSuperscript : AbstractMarkwonPlugin() { + + val re = Pattern.compile("(?:\\^\\((.+?)\\))|(?:\\^(\\S+))") + + override fun processMarkdown(markdown: String): String { + val builder = StringBuilder() + val matcher = re.matcher(markdown) + + var start = 0 + + while (matcher.find()) { + // if one of them, otherwise matcher should not report a match + val match = matcher.group(1) ?: matcher.group(2)!! + builder + .append(markdown.substring(start, matcher.start())) + .append("") + .append(match) + .append("") + start = matcher.end() + } + + // append the rest of markwon + if (start < markdown.length) { + builder.append(markdown.substring(start)) + } + + // if no match is found (thus builder is empty), then return original + return if (builder.isEmpty()) markdown else builder.toString() + } + } + + class SmallerSuperScriptSpan(val ratio: Float) : MetricAffectingSpan() { + override fun updateDrawState(tp: TextPaint?): Unit = tp?.let(::update) ?: Unit + override fun updateMeasureState(textPaint: TextPaint) = update(textPaint) + + fun update(paint: TextPaint) = paint.run { + textSize *= ratio + baselineShift += (ascent() / 2F).toInt() + } + } + + class SmallerSuperScriptTagHandler : SimpleTagHandler() { + override fun supportedTags(): Collection = setOf("sup") + + override fun getSpans(configuration: MarkwonConfiguration, renderProps: RenderProps, tag: HtmlTag): Any { + // 0.5 makes half the original textSize + // default value (for library SuperscriptSpan is `HtmlPlugin.SCRIPT_DEF_TEXT_SIZE_RATIO` + return SmallerSuperScriptSpan(0.5F) + } + } +} \ No newline at end of file diff --git a/sample-utils/annotations/io/noties/markwon/sample/annotations/Tag.java b/sample-utils/annotations/io/noties/markwon/sample/annotations/Tag.java index 58039316..5352241c 100644 --- a/sample-utils/annotations/io/noties/markwon/sample/annotations/Tag.java +++ b/sample-utils/annotations/io/noties/markwon/sample/annotations/Tag.java @@ -35,4 +35,5 @@ public enum Tag { precomputedText, cache, spanFactory, + reddit }