Packages refactoring (+ created Markwon class)

This commit is contained in:
Dimitry Ivanov 2017-05-16 20:33:26 +03:00
parent 07bd7b7cd1
commit 87d03793a8
12 changed files with 79 additions and 37 deletions

View File

@ -7,23 +7,19 @@ import android.support.annotation.NonNull;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;
import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Scanner;
import ru.noties.debug.AndroidLogDebugOutput;
import ru.noties.debug.Debug;
import ru.noties.markwon.renderer.SpannableConfiguration;
import ru.noties.markwon.renderer.SpannableRenderer;
import ru.noties.markwon.spans.AsyncDrawable;
public class MainActivity extends Activity {
// markdown, mdown, mkdn, mdwn, mkd, md
// markdown, mdown, mkdn, mkd, md, text
static {
Debug.init(new AndroidLogDebugOutput(true));
}
@ -75,11 +71,8 @@ public class MainActivity extends Activity {
}
if (md != null) {
final long start = SystemClock.uptimeMillis();
final Parser parser = new Parser.Builder()
.extensions(Arrays.asList(StrikethroughExtension.create()))
.build();
final Node node = parser.parse(md);
final SpannableConfiguration configuration = SpannableConfiguration.builder(MainActivity.this)
.asyncDrawableLoader(new AsyncDrawable.Loader() {
@ -95,10 +88,7 @@ public class MainActivity extends Activity {
})
.build();
final CharSequence text = new SpannableRenderer().render(
configuration,
node
);
final CharSequence text = Markwon.markdown(configuration, md);
final long end = SystemClock.uptimeMillis();
Debug.i("Rendered: %d ms, length: %d", end - start, text.length());
@ -109,8 +99,7 @@ public class MainActivity extends Activity {
// NB! LinkMovementMethod forces frequent updates...
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(text);
SpannableRenderer.scheduleDrawables(textView);
// AsyncDrawableSpanUtils.scheduleDrawables(textView);
Markwon.scheduleDrawables(textView);
}
});
}

View File

@ -1,4 +1,4 @@
package ru.noties.markwon.renderer;
package ru.noties.markwon;
import android.support.annotation.NonNull;

View File

@ -1,4 +1,4 @@
package ru.noties.markwon.renderer;
package ru.noties.markwon;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

View File

@ -1,4 +1,4 @@
package ru.noties.markwon.renderer;
package ru.noties.markwon;
import android.content.ActivityNotFoundException;
import android.content.Context;

View File

@ -0,0 +1,57 @@
package ru.noties.markwon;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.TextView;
import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import java.util.Collections;
import ru.noties.markwon.renderer.SpannableRenderer;
@SuppressWarnings("WeakerAccess")
public abstract class Markwon {
public static void scheduleDrawables(@NonNull TextView view) {
DrawablesScheduler.schedule(view);
}
public static void unscheduleDrawables(@NonNull TextView view) {
DrawablesScheduler.unschedule(view);
}
// with default configuration
public static CharSequence markdown(@NonNull Context context, @Nullable String text) {
final CharSequence out;
if (TextUtils.isEmpty(text)) {
out = null;
} else {
final SpannableConfiguration configuration = SpannableConfiguration.create(context);
out = markdown(configuration, text);
}
return out;
}
public static CharSequence markdown(@NonNull SpannableConfiguration configuration, @Nullable String text) {
final CharSequence out;
if (TextUtils.isEmpty(text)) {
out = null;
} else {
final Parser parser = new Parser.Builder()
.extensions(Collections.singleton(StrikethroughExtension.create()))
.build();
final Node node = parser.parse(text);
final SpannableRenderer renderer = new SpannableRenderer();
out = renderer.render(configuration, node);
}
return out;
}
private Markwon() {
}
}

View File

@ -1,4 +1,4 @@
package ru.noties.markwon.renderer;
package ru.noties.markwon;
import android.content.Context;
import android.support.annotation.NonNull;
@ -8,6 +8,7 @@ import ru.noties.markwon.spans.AsyncDrawable;
import ru.noties.markwon.spans.LinkSpan;
import ru.noties.markwon.spans.SpannableTheme;
@SuppressWarnings("WeakerAccess")
public class SpannableConfiguration {
// creates default configuration

View File

@ -1,8 +1,9 @@
package ru.noties.markwon.renderer;
package ru.noties.markwon;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@SuppressWarnings("WeakerAccess")
public interface SyntaxHighlight {
@NonNull

View File

@ -1,4 +1,4 @@
package ru.noties.markwon.renderer;
package ru.noties.markwon;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

View File

@ -34,6 +34,7 @@ import java.util.ArrayDeque;
import java.util.Deque;
import ru.noties.debug.Debug;
import ru.noties.markwon.SpannableConfiguration;
import ru.noties.markwon.renderer.html.SpannableHtmlParser;
import ru.noties.markwon.spans.AsyncDrawable;
import ru.noties.markwon.spans.AsyncDrawableSpan;
@ -47,7 +48,7 @@ import ru.noties.markwon.spans.OrderedListItemSpan;
import ru.noties.markwon.spans.StrongEmphasisSpan;
import ru.noties.markwon.spans.ThematicBreakSpan;
// please do not reuse between different texts (due to the html handling)
@SuppressWarnings("WeakerAccess")
public class SpannableMarkdownVisitor extends AbstractVisitor {
private static final String HTML_CONTENT = "<%1$s>%2$s</%1$s>";
@ -120,7 +121,7 @@ public class SpannableMarkdownVisitor extends AbstractVisitor {
final int length = builder.length();
// NB, in order to provide a _padding_ feeling code is wrapped inside two unbreakable spaces
// unfortunately we cannot use this for multiline code as we cannot control there a new line break will be inserted
// unfortunately we cannot use this for multiline code as we cannot control where a new line break will be inserted
builder.append('\u00a0');
builder.append(code.getLiteral());
builder.append('\u00a0');
@ -331,7 +332,7 @@ public class SpannableMarkdownVisitor extends AbstractVisitor {
public void visit(HtmlInline htmlInline) {
final SpannableHtmlParser htmlParser = configuration.htmlParser();
final SpannableHtmlParser.Tag tag = htmlParser.parseTag(htmlInline.getLiteral());
Debug.i(tag);
if (tag != null) {
final boolean voidTag = tag.voidTag();

View File

@ -3,21 +3,14 @@ package ru.noties.markwon.renderer;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.SpannableStringBuilder;
import android.widget.TextView;
import org.commonmark.node.Node;
import ru.noties.markwon.SpannableConfiguration;
// please note that this class does not implement Renderer in order to return CharSequence (instead of String)
public class SpannableRenderer {
public static void scheduleDrawables(@NonNull TextView view) {
DrawablesScheduler.schedule(view);
}
public static void unscheduleDrawables(@NonNull TextView view) {
DrawablesScheduler.unschedule(view);
}
// todo
// * LinkDrawableSpan, that draws link whilst image is still loading (it must be clickable...)
// * Common interface for images (in markdown & inline-html)

View File

@ -4,7 +4,7 @@ import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
class ObjectsPool {
abstract class ObjectsPool {
// maybe it's premature optimization, but as all the drawing is done in one thread
// and we apply needed values before actual drawing it's (I assume) safe to reuse some frequently used objects

View File

@ -13,7 +13,7 @@ import org.commonmark.parser.Parser;
import java.util.Collections;
import ru.noties.markwon.renderer.SpannableConfiguration;
import ru.noties.markwon.SpannableConfiguration;
import ru.noties.markwon.renderer.SpannableRenderer;
public class MarkdownTextView extends TextView {