Update sample-custome-extension to use plugin
This commit is contained in:
		
							parent
							
								
									f97c852c8a
								
							
						
					
					
						commit
						6a82b75aba
					
				| @ -0,0 +1,58 @@ | ||||
| package ru.noties.markwon.sample.extension; | ||||
| 
 | ||||
| import android.support.annotation.NonNull; | ||||
| import android.text.TextUtils; | ||||
| 
 | ||||
| import org.commonmark.parser.Parser; | ||||
| 
 | ||||
| import ru.noties.markwon.AbstractMarkwonPlugin; | ||||
| import ru.noties.markwon.MarkwonVisitor; | ||||
| 
 | ||||
| public class IconPlugin extends AbstractMarkwonPlugin { | ||||
| 
 | ||||
|     @NonNull | ||||
|     public static IconPlugin create(@NonNull IconSpanProvider iconSpanProvider) { | ||||
|         return new IconPlugin(iconSpanProvider); | ||||
|     } | ||||
| 
 | ||||
|     private final IconSpanProvider iconSpanProvider; | ||||
| 
 | ||||
|     IconPlugin(@NonNull IconSpanProvider iconSpanProvider) { | ||||
|         this.iconSpanProvider = iconSpanProvider; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void configureParser(@NonNull Parser.Builder builder) { | ||||
|         builder.customDelimiterProcessor(IconProcessor.create()); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void configureVisitor(@NonNull MarkwonVisitor.Builder builder) { | ||||
|         builder.on(IconNode.class, new MarkwonVisitor.NodeVisitor<IconNode>() { | ||||
|             @Override | ||||
|             public void visit(@NonNull MarkwonVisitor visitor, @NonNull IconNode iconNode) { | ||||
| 
 | ||||
|                 final String name = iconNode.name(); | ||||
|                 final String color = iconNode.color(); | ||||
|                 final String size = iconNode.size(); | ||||
| 
 | ||||
|                 if (!TextUtils.isEmpty(name) | ||||
|                         && !TextUtils.isEmpty(color) | ||||
|                         && !TextUtils.isEmpty(size)) { | ||||
| 
 | ||||
|                     final int length = visitor.length(); | ||||
| 
 | ||||
|                     visitor.builder().append(name); | ||||
|                     visitor.setSpans(length, iconSpanProvider.provide(name, color, size)); | ||||
|                     visitor.builder().append(' '); | ||||
|                 } | ||||
|             } | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     @NonNull | ||||
|     @Override | ||||
|     public String processMarkdown(@NonNull String markdown) { | ||||
|         return IconProcessor.prepare(markdown); | ||||
|     } | ||||
| } | ||||
| @ -1,62 +0,0 @@ | ||||
| package ru.noties.markwon.sample.extension; | ||||
| 
 | ||||
| import android.support.annotation.NonNull; | ||||
| import android.text.TextUtils; | ||||
| 
 | ||||
| import org.commonmark.node.CustomNode; | ||||
| 
 | ||||
| import ru.noties.markwon.SpannableBuilder; | ||||
| import ru.noties.markwon.MarkwonConfiguration; | ||||
| import ru.noties.markwon.renderer.SpannableMarkdownVisitor; | ||||
| 
 | ||||
| @SuppressWarnings("WeakerAccess") | ||||
| public class IconVisitor extends SpannableMarkdownVisitor { | ||||
| 
 | ||||
|     private final SpannableBuilder builder; | ||||
| 
 | ||||
|     private final IconSpanProvider iconSpanProvider; | ||||
| 
 | ||||
|     public IconVisitor( | ||||
|             @NonNull MarkwonConfiguration configuration, | ||||
|             @NonNull SpannableBuilder builder, | ||||
|             @NonNull IconSpanProvider iconSpanProvider | ||||
|     ) { | ||||
|         super(configuration, builder); | ||||
|         this.builder = builder; | ||||
|         this.iconSpanProvider = iconSpanProvider; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void visit(CustomNode customNode) { | ||||
|         if (!visitIconNode(customNode)) { | ||||
|             super.visit(customNode); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private boolean visitIconNode(@NonNull CustomNode customNode) { | ||||
| 
 | ||||
|         if (customNode instanceof IconNode) { | ||||
| 
 | ||||
|             final IconNode node = (IconNode) customNode; | ||||
| 
 | ||||
|             final String name = node.name(); | ||||
|             final String color = node.color(); | ||||
|             final String size = node.size(); | ||||
| 
 | ||||
|             if (!TextUtils.isEmpty(name) | ||||
|                     && !TextUtils.isEmpty(color) | ||||
|                     && !TextUtils.isEmpty(size)) { | ||||
| 
 | ||||
|                 final int length = builder.length(); | ||||
| 
 | ||||
|                 builder.append(name); | ||||
|                 builder.setSpan(iconSpanProvider.provide(name, color, size), length); | ||||
|                 builder.append(' '); | ||||
|                  | ||||
|                 return true; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         return false; | ||||
|     } | ||||
| } | ||||
| @ -3,19 +3,12 @@ package ru.noties.markwon.sample.extension; | ||||
| import android.app.Activity; | ||||
| import android.graphics.Typeface; | ||||
| import android.os.Bundle; | ||||
| import android.support.annotation.NonNull; | ||||
| import android.widget.TextView; | ||||
| 
 | ||||
| import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension; | ||||
| import org.commonmark.ext.gfm.tables.TablesExtension; | ||||
| import org.commonmark.node.Node; | ||||
| import org.commonmark.parser.Parser; | ||||
| 
 | ||||
| import java.util.Arrays; | ||||
| 
 | ||||
| import ru.noties.markwon.MarkwonConfiguration; | ||||
| import ru.noties.markwon.SpannableBuilder; | ||||
| import ru.noties.markwon.AbstractMarkwonPlugin; | ||||
| import ru.noties.markwon.Markwon; | ||||
| import ru.noties.markwon.spans.MarkwonTheme; | ||||
| import ru.noties.markwon.tasklist.TaskListExtension; | ||||
| 
 | ||||
| public class MainActivity extends Activity { | ||||
| 
 | ||||
| @ -27,49 +20,19 @@ public class MainActivity extends Activity { | ||||
| 
 | ||||
|         final TextView textView = findViewById(R.id.text_view); | ||||
| 
 | ||||
|         // obtain an instance of parser | ||||
|         final Parser parser = new Parser.Builder() | ||||
|                 // we will register all known to Markwon extensions | ||||
|                 .extensions(Arrays.asList( | ||||
|                         StrikethroughExtension.create(), | ||||
|                         TablesExtension.create(), | ||||
|                         TaskListExtension.create() | ||||
|                 )) | ||||
|                 // this is the handler for custom icons | ||||
|                 .customDelimiterProcessor(IconProcessor.create()) | ||||
|         final Markwon markwon = Markwon.builder(this) | ||||
|                 .use(IconPlugin.create(IconSpanProvider.create(this, 0))) | ||||
|                 .use(new AbstractMarkwonPlugin() { | ||||
|                     @Override | ||||
|                     public void configureTheme(@NonNull MarkwonTheme.Builder builder) { | ||||
|                         final float[] textSizeMultipliers = new float[]{3f, 2f, 1.5f, 1f, .5f, .25f}; | ||||
|                         builder | ||||
|                                 .headingTypeface(Typeface.MONOSPACE) | ||||
|                                 .headingTextSizeMultipliers(textSizeMultipliers); | ||||
|                     } | ||||
|                 }) | ||||
|                 .build(); | ||||
| 
 | ||||
|         // we process input to wrap icon definitions with `@` on both ends | ||||
|         // if your input already does it, there is not need for `IconProcessor.prepare()` call. | ||||
|         final String markdown = IconProcessor.prepare(getString(R.string.input)); | ||||
| 
 | ||||
|         final Node node = parser.parse(markdown); | ||||
| 
 | ||||
|         final SpannableBuilder builder = new SpannableBuilder(); | ||||
| 
 | ||||
|         // please note that here I am passing `0` as fallback it means that if toMarkdown references | ||||
|         // unknown icon, it will try to load fallback one and will fail with ResourceNotFound. It's | ||||
|         // better to provide a valid fallback option | ||||
|         final IconSpanProvider spanProvider = IconSpanProvider.create(this, 0); | ||||
| 
 | ||||
|         final float[] textSizeMultipliers = new float[]{3f, 2f, 1.5f, 1f, .5f, .25f}; | ||||
|         MarkwonConfiguration configuration = MarkwonConfiguration.builder(this) | ||||
|                 .theme(MarkwonTheme.builder() | ||||
|                         .headingTypeface(Typeface.MONOSPACE) | ||||
|                         .headingTextSizeMultipliers(textSizeMultipliers) | ||||
|                         .build()) | ||||
|                 .build(); | ||||
|         // create an instance of visitor to process parsed toMarkdown | ||||
|         final IconVisitor visitor = new IconVisitor( | ||||
|                 configuration, | ||||
|                 builder, | ||||
|                 spanProvider | ||||
|         ); | ||||
| 
 | ||||
|         // trigger visit | ||||
|         node.accept(visitor); | ||||
| 
 | ||||
|         // apply | ||||
|         textView.setText(builder.text()); | ||||
|         markwon.setMarkdown(textView, getString(R.string.input)); | ||||
|     } | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Dimitry Ivanov
						Dimitry Ivanov