sample, add one line and css style parser samples
This commit is contained in:
parent
79cd43bc9c
commit
3e1db2abbe
@ -1,7 +1,31 @@
|
||||
[
|
||||
{
|
||||
"javaClassName": "io.noties.markwon.app.samples.html.HtmlCssStyleParserSample",
|
||||
"id": "20210118155530",
|
||||
"title": "CSS attributes in HTML",
|
||||
"description": "Parse CSS attributes of HTML tags with `CssInlineStyleParser`",
|
||||
"artifacts": [
|
||||
"HTML"
|
||||
],
|
||||
"tags": [
|
||||
"HTML"
|
||||
]
|
||||
},
|
||||
{
|
||||
"javaClassName": "io.noties.markwon.app.samples.basics.OneLineNoMarkdownSample",
|
||||
"id": "20210118154116",
|
||||
"title": "One line text",
|
||||
"description": "Single line text without markdown markup",
|
||||
"artifacts": [
|
||||
"CORE"
|
||||
],
|
||||
"tags": [
|
||||
"rendering"
|
||||
]
|
||||
},
|
||||
{
|
||||
"javaClassName": "io.noties.markwon.app.samples.tasklist.TaskListMutateNestedSample",
|
||||
"id": "20211228120444",
|
||||
"id": "20201228120444",
|
||||
"title": "Task list mutate nested",
|
||||
"description": "Task list mutation with nested items",
|
||||
"artifacts": [
|
||||
|
@ -0,0 +1,29 @@
|
||||
package io.noties.markwon.app.samples.basics;
|
||||
|
||||
import io.noties.markwon.Markwon;
|
||||
import io.noties.markwon.app.sample.Tags;
|
||||
import io.noties.markwon.app.sample.ui.MarkwonTextViewSample;
|
||||
import io.noties.markwon.sample.annotations.MarkwonArtifact;
|
||||
import io.noties.markwon.sample.annotations.MarkwonSampleInfo;
|
||||
|
||||
@MarkwonSampleInfo(
|
||||
id = "20210118154116",
|
||||
title = "One line text",
|
||||
description = "Single line text without markdown markup",
|
||||
artifacts = MarkwonArtifact.CORE,
|
||||
tags = Tags.rendering
|
||||
)
|
||||
public class OneLineNoMarkdownSample extends MarkwonTextViewSample {
|
||||
@Override
|
||||
public void render() {
|
||||
|
||||
textView.setBackgroundColor(0x40ff0000);
|
||||
|
||||
final String md = " Demo text ";
|
||||
|
||||
final Markwon markwon = Markwon.builder(context)
|
||||
.build();
|
||||
|
||||
markwon.setMarkdown(textView, md);
|
||||
}
|
||||
}
|
@ -0,0 +1,99 @@
|
||||
package io.noties.markwon.app.samples.html;
|
||||
|
||||
import android.graphics.Color;
|
||||
import android.text.TextUtils;
|
||||
import android.text.style.BackgroundColorSpan;
|
||||
import android.text.style.ForegroundColorSpan;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import io.noties.debug.Debug;
|
||||
import io.noties.markwon.Markwon;
|
||||
import io.noties.markwon.MarkwonConfiguration;
|
||||
import io.noties.markwon.RenderProps;
|
||||
import io.noties.markwon.app.sample.Tags;
|
||||
import io.noties.markwon.app.sample.ui.MarkwonTextViewSample;
|
||||
import io.noties.markwon.html.CssInlineStyleParser;
|
||||
import io.noties.markwon.html.CssProperty;
|
||||
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;
|
||||
|
||||
@MarkwonSampleInfo(
|
||||
id = "20210118155530",
|
||||
title = "CSS attributes in HTML",
|
||||
description = "Parse CSS attributes of HTML tags with `CssInlineStyleParser`",
|
||||
artifacts = MarkwonArtifact.HTML,
|
||||
tags = Tags.html
|
||||
)
|
||||
public class HtmlCssStyleParserSample extends MarkwonTextViewSample {
|
||||
@Override
|
||||
public void render() {
|
||||
|
||||
final String md = "# CSS\n\n" +
|
||||
"<span style=\"background-color: #ff0000;\">this has red background</span> and then\n\n" +
|
||||
"this <span style=\"color: #00ff00;\">is green</span>";
|
||||
|
||||
final Markwon markwon = Markwon.builder(context)
|
||||
.usePlugin(HtmlPlugin.create(plugin -> plugin.addHandler(new SpanTagHandler())))
|
||||
.build();
|
||||
|
||||
markwon.setMarkdown(textView, md);
|
||||
}
|
||||
|
||||
private static class SpanTagHandler extends SimpleTagHandler {
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Object getSpans(@NonNull MarkwonConfiguration configuration, @NonNull RenderProps renderProps, @NonNull HtmlTag tag) {
|
||||
final String style = tag.attributes().get("style");
|
||||
if (TextUtils.isEmpty(style)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int color = 0;
|
||||
int backgroundColor = 0;
|
||||
|
||||
for (CssProperty property : CssInlineStyleParser.create().parse(style)) {
|
||||
switch (property.key()) {
|
||||
|
||||
case "color":
|
||||
color = Color.parseColor(property.value());
|
||||
break;
|
||||
|
||||
case "background-color":
|
||||
backgroundColor = Color.parseColor(property.value());
|
||||
break;
|
||||
|
||||
default:
|
||||
Debug.i("unexpected CSS property: %s", property);
|
||||
}
|
||||
}
|
||||
|
||||
final List<Object> spans = new ArrayList<>(3);
|
||||
|
||||
if (color != 0) {
|
||||
spans.add(new ForegroundColorSpan(color));
|
||||
}
|
||||
if (backgroundColor != 0) {
|
||||
spans.add(new BackgroundColorSpan(backgroundColor));
|
||||
}
|
||||
|
||||
return spans.toArray();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Collection<String> supportedTags() {
|
||||
return Collections.singleton("span");
|
||||
}
|
||||
}
|
||||
}
|
@ -25,7 +25,7 @@ import org.commonmark.node.SoftLineBreak
|
||||
import org.commonmark.node.Text
|
||||
|
||||
@MarkwonSampleInfo(
|
||||
id = "20211228120444",
|
||||
id = "20201228120444",
|
||||
title = "Task list mutate nested",
|
||||
description = "Task list mutation with nested items",
|
||||
artifacts = [MarkwonArtifact.EXT_TASKLIST],
|
||||
|
@ -1 +1 @@
|
||||
io.noties.markwon.sample.processor.MarkwonSampleProcessor,isolating
|
||||
io.noties.markwon.sample.processor.MarkwonSampleProcessor,aggregating
|
Loading…
x
Reference in New Issue
Block a user