SimpleTagHandler visit children if tag is block

This commit is contained in:
Dimitry Ivanov 2020-05-28 11:54:15 +03:00
parent 6103ec0574
commit 03770cfe2d
5 changed files with 114 additions and 3 deletions

View File

@ -1,5 +1,10 @@
# Changelog # Changelog
# $nap;
* `html` - `SimpleTagHandler` visits children tags if supplied tag is block one ([#235])
[#235]: https://github.com/noties/Markwon/issues/235
# 4.4.0 # 4.4.0
* `TextViewSpan` to obtain `TextView` in which markdown is displayed (applied by `CorePlugin`) * `TextViewSpan` to obtain `TextView` in which markdown is displayed (applied by `CorePlugin`)
* `TextLayoutSpan` to obtain `Layout` in which markdown is displayed (applied by `TablePlugin`, more specifically `TableRowSpan` to propagate layout in which cell content is displayed) * `TextLayoutSpan` to obtain `Layout` in which markdown is displayed (applied by `TablePlugin`, more specifically `TableRowSpan` to propagate layout in which cell content is displayed)

View File

@ -28,6 +28,11 @@ public abstract class SimpleTagHandler extends TagHandler {
@Override @Override
public void handle(@NonNull MarkwonVisitor visitor, @NonNull MarkwonHtmlRenderer renderer, @NonNull HtmlTag tag) { public void handle(@NonNull MarkwonVisitor visitor, @NonNull MarkwonHtmlRenderer renderer, @NonNull HtmlTag tag) {
// @since $nap; check if tag is block one and visit children
if (tag.isBlock()) {
visitChildren(visitor, renderer, tag.getAsBlock());
}
final Object spans = getSpans(visitor.configuration(), visitor.renderProps(), tag); final Object spans = getSpans(visitor.configuration(), visitor.renderProps(), tag);
if (spans != null) { if (spans != null) {
SpannableBuilder.setSpans(visitor.builder(), spans, tag.start(), tag.end()); SpannableBuilder.setSpans(visitor.builder(), spans, tag.start(), tag.end());

View File

@ -0,0 +1,39 @@
package io.noties.markwon.sample.html;
import android.text.Layout;
import android.text.style.AlignmentSpan;
import android.util.Log;
import androidx.annotation.NonNull;
import java.util.Collection;
import java.util.Collections;
import io.noties.markwon.MarkwonVisitor;
import io.noties.markwon.SpannableBuilder;
import io.noties.markwon.html.HtmlTag;
import io.noties.markwon.html.MarkwonHtmlRenderer;
import io.noties.markwon.html.TagHandler;
class CenterTagHandler extends TagHandler {
@Override
public void handle(@NonNull MarkwonVisitor visitor, @NonNull MarkwonHtmlRenderer renderer, @NonNull HtmlTag tag) {
Log.e("HTML", String.format("center, isBlock: %s", tag.isBlock()));
if (tag.isBlock()) {
visitChildren(visitor, renderer, tag.getAsBlock());
}
SpannableBuilder.setSpans(
visitor.builder(),
new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER),
tag.start(),
tag.end()
);
}
@NonNull
@Override
public Collection<String> supportedTags() {
return Collections.singleton("center");
}
}

View File

@ -46,7 +46,8 @@ public class HtmlActivity extends ActivityWithMenuOptions {
.add("image", this::image) .add("image", this::image)
// .add("elegantUnderline", this::elegantUnderline) // .add("elegantUnderline", this::elegantUnderline)
.add("iframe", this::iframe) .add("iframe", this::iframe)
.add("emptyTagReplacement", this::emptyTagReplacement); .add("emptyTagReplacement", this::emptyTagReplacement)
.add("centerTag", this::centerTag);
} }
private TextView textView; private TextView textView;
@ -354,4 +355,65 @@ public class HtmlActivity extends ActivityWithMenuOptions {
markwon.setMarkdown(textView, md); markwon.setMarkdown(textView, md);
} }
private void centerTag() {
final String html = "<html>\n" +
"\n" +
"<head></head>\n" +
"\n" +
"<body>\n" +
" <p></p>\n" +
" <h3>LiSA's Sword Art Online: Alicization OP Song \"ADAMAS\" Certified Platinum with 250,000 Downloads</h3>\n" +
" <p></p>\n" +
" <h5>The upper tune was already certified Gold one month after its digital release</h5>\n" +
" <p>According to The Recording Industry Association of Japan (RIAJ)'s monthly report for April 2020, one of the <span\n" +
" style=\"color: #ff9900;\"><strong><a href=\"http://www.lxixsxa.com/\" target=\"_blank\"><span\n" +
" style=\"color: #ff9900;\">LiSA</span></a></strong></span>'s 14th single songs,\n" +
" <strong>\"ADAMAS\"</strong>&nbsp;(the first OP theme for the TV anime <a href=\"/sword-art-online\"\n" +
" target=\"_blank\"><span style=\"color: #ff9900;\"><strong><em>Sword Art Online:\n" +
" Alicization</em></strong></span></a>) has been certified <strong>Platinum</strong> for\n" +
" surpassing 250,000 downloads.</p>\n" +
" <p>&nbsp;</p>\n" +
" <p>As a double A-side single with <strong>\"Akai Wana (who loves it?),\"</strong> <strong>\"ADAMAS\"</strong> was\n" +
" released from SACRA Music in Japan on December 12, 2018. Its CD single ranked second in Oricon's weekly single\n" +
" chart by selling 35,000 copies in its first week. Meanwhile, the song was released digitally two months prior to\n" +
" its CD release, October 8, then reached Gold (100,000 downloads) in the following month.</p>\n" +
" <p>&nbsp;</p>\n" +
" <p>&nbsp;</p>\n" +
" <center>\n" +
" <p><strong>\"ADAMAS\"</strong> MV YouTube EDIT ver.:</p>\n" +
" <p><iframe src=\"https://www.youtube.com/embed/UeEIl4JlE-g\" frameborder=\"0\" width=\"640\" height=\"360\"></iframe>\n" +
" </p>\n" +
" <p>&nbsp;</p>\n" +
" <p>Standard edition CD jacket:</p>\n" +
" <p><img src=\"https://img1.ak.crunchyroll.com/i/spire2/d7b1d6bc7563224388ef5ffc04a967581589950464_full.jpg\"\n" +
" alt=\"\" width=\"640\" height=\"635\"></p>\n" +
" </center>\n" +
" <p>&nbsp;&nbsp;</p>\n" +
" <hr>\n" +
" <p>&nbsp;</p>\n" +
" <p>Source: RIAJ press release</p>\n" +
" <p>&nbsp;</p>\n" +
" <p><em>©SACRA MUSIC</em></p>\n" +
" <p>&nbsp;</p>\n" +
" <p style=\"text-align: center;\"><a href=\"https://got.cr/PremiumTrial-NewsBanner4\"><em><img\n" +
" src=\"https://img1.ak.crunchyroll.com/i/spire4/78f5441d927cf160a93e037b567c2b1f1559091520_full.png\"\n" +
" alt=\"\" width=\"640\" height=\"43\"></em></a></p>\n" +
"</body>\n" +
"\n" +
"</html>";
final Markwon markwon = Markwon.builder(this)
.usePlugin(HtmlPlugin.create(new HtmlPlugin.HtmlConfigure() {
@Override
public void configureHtml(@NonNull HtmlPlugin plugin) {
plugin.addHandler(new CenterTagHandler());
}
}))
.usePlugin(new IFrameHtmlPlugin())
.usePlugin(ImagesPlugin.create())
.build();
markwon.setMarkdown(textView, html);
}
} }

View File

@ -34,7 +34,7 @@ public class IFrameHtmlPlugin extends AbstractMarkwonPlugin {
public Object getSpans(@NonNull MarkwonConfiguration configuration, @NonNull RenderProps renderProps, @NonNull HtmlTag tag) { public Object getSpans(@NonNull MarkwonConfiguration configuration, @NonNull RenderProps renderProps, @NonNull HtmlTag tag) {
final ImageSize imageSize = new ImageSize(new ImageSize.Dimension(640, "px"), new ImageSize.Dimension(480, "px")); final ImageSize imageSize = new ImageSize(new ImageSize.Dimension(640, "px"), new ImageSize.Dimension(480, "px"));
ImageProps.IMAGE_SIZE.set(renderProps, imageSize); ImageProps.IMAGE_SIZE.set(renderProps, imageSize);
ImageProps.DESTINATION.set(renderProps, "https://hey.com/1.png"); ImageProps.DESTINATION.set(renderProps, "https://img1.ak.crunchyroll.com/i/spire2/d7b1d6bc7563224388ef5ffc04a967581589950464_full.jpg");
return configuration.spansFactory().require(Image.class) return configuration.spansFactory().require(Image.class)
.getSpans(configuration, renderProps); .getSpans(configuration, renderProps);
} }