Skip to content

Commit

Permalink
V1.0.6 (#46)
Browse files Browse the repository at this point in the history
* Fix bullet list item size (depend on text size and not top-bottom arguments)

* Add SNAPSHOT publishing

* Add ability to specify MovementMethod when applying markdown to a TextView

* Add information about new method signature to README (Markwon.setText)

* Fix README links reference

* Add new section to README (applications using markwon)

* Markdown images size is also resolved via ImageSizeResolver
  • Loading branch information
noties authored Jun 26, 2018
1 parent ddb8989 commit cb66618
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 27 deletions.
39 changes: 36 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,34 @@

## Installation
```groovy
compile 'ru.noties:markwon:1.0.5'
compile 'ru.noties:markwon-image-loader:1.0.5' // optional
compile 'ru.noties:markwon-view:1.0.5' // optional
compile 'ru.noties:markwon:1.0.6'
compile 'ru.noties:markwon-image-loader:1.0.6' // optional
compile 'ru.noties:markwon-view:1.0.6' // optional
```

### Snapshot
![markwon-snapshot](https://img.shields.io/nexus/s/https/oss.sonatype.org/ru.noties/markwon.svg?label=markwon)

In order to use latest `SNAPSHOT` version add snapshot repository to your root project's `build.gradle` file:

```groovy
allprojects {
repositories {
jcenter()
google()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}
}
```

and then in your module `build.gradle`:

```groovy
implementation 'ru.noties:markwon:1.0.6-SNAPSHOT'
```

Please note that `markwon-image-loader` and `markwon-view` are also present in `SNAPSHOT` repository and share the same version as main `markwon` artifact.

## Supported markdown features:
* Emphasis (`*`, `_`)
* Strong emphasis (`**`, `__`)
Expand Down Expand Up @@ -111,6 +134,11 @@ Markwon.scheduleDrawables(textView);
Markwon.scheduleTableRows(textView);
```

Please note that if you are having trouble with `LinkMovementMethod` you can use
`Markwon.setText(textView, markdown, movementMethod)` method (`@since 1.0.6`) to specify _no_ movement
method (aka `null`) or own implementation. As an alternative to the system `LinkMovementMethod`
you can use [Better-Link-Movement-Method][better-link-movement-method].

Please refer to [SpannableConfiguration] document for more info

---
Expand Down Expand Up @@ -269,6 +297,10 @@ Underscores (`_`)

---

## Applications using Markwon

* [FairNote Notepad](https://play.google.com/store/apps/details?id=com.rgiskard.fairnote)


## License

Expand All @@ -292,6 +324,7 @@ Underscores (`_`)
[commonmark-java]: https://github.com/atlassian/commonmark-java/blob/master/README.md
[cheatsheet]: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet
[SpannableConfiguration]: ./docs/SpannableConfiguration.md
[better-link-movement-method]: https://github.com/saket/Better-Link-Movement-Method

[arbitrary case-insensitive reference text]: https://www.mozilla.org
[1]: http://slashdot.org
Expand Down
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies {
implementation project(':library-image-loader')

implementation 'ru.noties:debug:3.0.0@jar'
implementation 'me.saket:better-link-movement-method:2.2.0'

implementation OK_HTTP

Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/ru/noties/markwon/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import javax.inject.Inject;

import me.saket.bettermovementmethod.BetterLinkMovementMethod;
import ru.noties.debug.Debug;

public class MainActivity extends Activity {
Expand Down Expand Up @@ -64,14 +67,15 @@ public void apply(final String text) {
markdownRenderer.render(MainActivity.this, uri(), text, new MarkdownRenderer.MarkdownReadyListener() {
@Override
public void onMarkdownReady(CharSequence markdown) {
Markwon.setText(textView, markdown);
Markwon.setText(textView, markdown, BetterLinkMovementMethod.getInstance());
Views.setVisible(progress, false);
}
});
}
});
}

@NonNull
private AppBarItem.State appBarState() {

final String title;
Expand Down Expand Up @@ -100,6 +104,7 @@ private void checkUri() {
}
}

@Nullable
private Uri uri() {
final Intent intent = getIntent();
return intent != null
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ org.gradle.configureondemand=true
android.enableBuildCache=true
android.buildCacheDir=build/pre-dex-cache

VERSION_NAME=1.0.5
VERSION_NAME=1.0.6

GROUP=ru.noties
POM_DESCRIPTION=Markwon
Expand Down
27 changes: 24 additions & 3 deletions library/src/main/java/ru/noties/markwon/Markwon.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.widget.TextView;

import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension;
Expand Down Expand Up @@ -67,20 +69,39 @@ public static void setMarkdown(

/**
* Helper method to apply parsed markdown.
* <p>
* Since 1.0.6 redirects it\'s call to {@link #setText(TextView, CharSequence, MovementMethod)}
* with LinkMovementMethod as an argument to preserve current API.
*
* @param view {@link TextView} to set markdown into
* @param text parsed markdown
* @see #scheduleDrawables(TextView)
* @see #scheduleTableRows(TextView)
* @see #setText(TextView, CharSequence, MovementMethod)
* @since 1.0.0
*/
public static void setText(@NonNull TextView view, CharSequence text) {
setText(view, text, LinkMovementMethod.getInstance());
}

/**
* Helper method to apply parsed markdown with additional argument of a MovementMethod. Used
* to workaround problems that occur when using system LinkMovementMethod (for example:
* https://issuetracker.google.com/issues/37068143). As a better alternative to it consider
* using: https://github.com/saket/Better-Link-Movement-Method
*
* @param view TextView to set markdown into
* @param text parsed markdown
* @param movementMethod an implementation if MovementMethod or null
* @see #scheduleDrawables(TextView)
* @see #scheduleTableRows(TextView)
* @since 1.0.6
*/
public static void setText(@NonNull TextView view, CharSequence text, @Nullable MovementMethod movementMethod) {

unscheduleDrawables(view);
unscheduleTableRows(view);

// update movement method (for links to be clickable)
view.setMovementMethod(LinkMovementMethod.getInstance());
view.setMovementMethod(movementMethod);
view.setText(text);

// schedule drawables (dynamic drawables that can change bounds/animate will be correctly updated)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import android.content.Context;
import android.support.annotation.NonNull;

import ru.noties.markwon.renderer.html.ImageSizeResolver;
import ru.noties.markwon.renderer.html.ImageSizeResolverDef;
import ru.noties.markwon.renderer.ImageSizeResolver;
import ru.noties.markwon.renderer.ImageSizeResolverDef;
import ru.noties.markwon.renderer.html.SpannableHtmlParser;
import ru.noties.markwon.spans.AsyncDrawable;
import ru.noties.markwon.spans.LinkSpan;
Expand All @@ -30,6 +30,7 @@ public static Builder builder(@NonNull Context context) {
private final LinkSpan.Resolver linkResolver;
private final UrlProcessor urlProcessor;
private final SpannableHtmlParser htmlParser;
private final ImageSizeResolver imageSizeResolver;

private SpannableConfiguration(@NonNull Builder builder) {
this.theme = builder.theme;
Expand All @@ -38,6 +39,7 @@ private SpannableConfiguration(@NonNull Builder builder) {
this.linkResolver = builder.linkResolver;
this.urlProcessor = builder.urlProcessor;
this.htmlParser = builder.htmlParser;
this.imageSizeResolver = builder.imageSizeResolver;
}

@NonNull
Expand Down Expand Up @@ -70,6 +72,11 @@ public SpannableHtmlParser htmlParser() {
return htmlParser;
}

@NonNull
public ImageSizeResolver imageSizeResolver() {
return imageSizeResolver;
}

@SuppressWarnings("unused")
public static class Builder {

Expand Down Expand Up @@ -154,12 +161,11 @@ public SpannableConfiguration build() {
urlProcessor = new UrlProcessorNoOp();
}

if (htmlParser == null) {

if (imageSizeResolver == null) {
imageSizeResolver = new ImageSizeResolverDef();
}
if (imageSizeResolver == null) {
imageSizeResolver = new ImageSizeResolverDef();
}

if (htmlParser == null) {
htmlParser = SpannableHtmlParser.create(theme, asyncDrawableLoader, urlProcessor, linkResolver, imageSizeResolver);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.noties.markwon.renderer.html;
package ru.noties.markwon.renderer;

import android.support.annotation.Nullable;

Expand Down Expand Up @@ -34,4 +34,12 @@ public ImageSize(@Nullable Dimension width, @Nullable Dimension height) {
this.width = width;
this.height = height;
}

@Override
public String toString() {
return "ImageSize{" +
"width=" + width +
", height=" + height +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.noties.markwon.renderer.html;
package ru.noties.markwon.renderer;

import android.graphics.Rect;
import android.support.annotation.NonNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.noties.markwon.renderer.html;
package ru.noties.markwon.renderer;

import android.graphics.Rect;
import android.support.annotation.NonNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,9 @@ public void visit(Image image) {
configuration.theme(),
new AsyncDrawable(
destination,
configuration.asyncDrawableLoader()
configuration.asyncDrawableLoader(),
configuration.imageSizeResolver(),
null
),
AsyncDrawableSpan.ALIGN_BOTTOM,
link
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.Map;

import ru.noties.markwon.UrlProcessor;
import ru.noties.markwon.renderer.ImageSize;
import ru.noties.markwon.renderer.ImageSizeResolver;
import ru.noties.markwon.spans.AsyncDrawable;
import ru.noties.markwon.spans.AsyncDrawableSpan;
import ru.noties.markwon.spans.SpannableTheme;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import ru.noties.markwon.LinkResolverDef;
import ru.noties.markwon.UrlProcessor;
import ru.noties.markwon.UrlProcessorNoOp;
import ru.noties.markwon.renderer.ImageSizeResolver;
import ru.noties.markwon.renderer.ImageSizeResolverDef;
import ru.noties.markwon.spans.AsyncDrawable;
import ru.noties.markwon.spans.LinkSpan;
import ru.noties.markwon.spans.SpannableTheme;
Expand Down
11 changes: 7 additions & 4 deletions library/src/main/java/ru/noties/markwon/spans/AsyncDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import ru.noties.markwon.renderer.html.ImageSize;
import ru.noties.markwon.renderer.html.ImageSizeResolver;
import ru.noties.markwon.renderer.ImageSize;
import ru.noties.markwon.renderer.ImageSizeResolver;

public class AsyncDrawable extends Drawable {

Expand All @@ -33,6 +33,10 @@ public interface Loader {
private int canvasWidth;
private float textSize;

/**
* @deprecated 1.0.6 markdown images are also processed with {@link ImageSizeResolver}
*/
@Deprecated
public AsyncDrawable(@NonNull String destination, @NonNull Loader loader) {
this(destination, loader, null, null);
}
Expand Down Expand Up @@ -176,8 +180,7 @@ private Rect resolveBounds() {

final Rect rect;

if (imageSizeResolver == null
|| imageSize == null) {
if (imageSizeResolver == null) {

// @since 1.0.5
final Rect bounds = result.getBounds();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int ba
try {

final int width = theme.getBlockMargin();
final int height = bottom - top;

final int side = theme.getBulletWidth(bottom - top);
// @since 1.0.6 we no longer rely on (bottom-top) calculation in order to detect line height
// it lead to bad rendering as first & last lines received different results even
// if text size is the same (first line received greater amount and bottom line -> less)
final int textLineHeight = (int) (paint.descent() - paint.ascent() + .5F);

final int side = theme.getBulletWidth(textLineHeight);

final int marginLeft = (width - side) / 2;
final int marginTop = (height - side) / 2;

// in order to support RTL
final int l;
Expand All @@ -64,7 +67,8 @@ public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int ba
l = Math.min(left, right);
r = Math.max(left, right);
}
final int t = top + marginTop;

final int t = baseline + (int) ((paint.descent() + paint.ascent()) / 2.F + .5F) - (side / 2);
final int b = t + side;

if (level == 0
Expand Down

0 comments on commit cb66618

Please sign in to comment.