Merge pull request #104 from noties/v2.0.2

# v2.0.2

* Extend task list parsing (#99) 
   Thanks @Tunous
* Fix deep nested bullet lists for Nougat
This commit is contained in:
Dimitry 2019-03-14 18:03:30 +03:00 committed by GitHub
commit 550165402a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 6 deletions

View File

@ -6,7 +6,7 @@ org.gradle.configureondemand=true
android.enableBuildCache=true android.enableBuildCache=true
android.buildCacheDir=build/pre-dex-cache android.buildCacheDir=build/pre-dex-cache
VERSION_NAME=2.0.1 VERSION_NAME=2.0.2-SNAPSHOT
GROUP=ru.noties GROUP=ru.noties
POM_DESCRIPTION=Markwon POM_DESCRIPTION=Markwon

View File

@ -58,14 +58,28 @@ public class BulletListItemSpan implements LeadingMarginSpan {
final int marginLeft = (width - side) / 2; final int marginLeft = (width - side) / 2;
// @since 2.0.2
// There is a bug in Android Nougat, when this span receives an `x` that
// doesn't correspond to what it should be (text is placed correctly though).
// Let's make this a general rule -> manually calculate difference between expected/actual
// and add this difference to resulting left/right values. If everything goes well
// we do not encounter a bug -> this `diff` value will be 0
final int diff;
if (dir < 0) {
// rtl
diff = x - (layout.getWidth() - (width * level));
} else {
diff = (width * level) - x;
}
// in order to support RTL // in order to support RTL
final int l; final int l;
final int r; final int r;
{ {
final int left = x + (dir * marginLeft); final int left = x + (dir * marginLeft);
final int right = left + (dir * side); final int right = left + (dir * side);
l = Math.min(left, right); l = Math.min(left, right) + (dir * diff);
r = Math.max(left, right); r = Math.max(left, right) + (dir * diff);
} }
final int t = baseline + (int) ((paint.descent() + paint.ascent()) / 2.F + .5F) - (side / 2); final int t = baseline + (int) ((paint.descent() + paint.ascent()) / 2.F + .5F) - (side / 2);

View File

@ -23,7 +23,7 @@ import java.util.regex.Pattern;
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
class TaskListBlockParser extends AbstractBlockParser { class TaskListBlockParser extends AbstractBlockParser {
private static final Pattern PATTERN = Pattern.compile("\\s*-\\s+\\[(x|X|\\s)\\]\\s+(.*)"); private static final Pattern PATTERN = Pattern.compile("\\s*([-*+]|\\d{1,9}[.)])\\s+\\[(x|X|\\s)]\\s+(.*)");
private final TaskListBlock block = new TaskListBlock(); private final TaskListBlock block = new TaskListBlock();
@ -88,9 +88,9 @@ class TaskListBlockParser extends AbstractBlockParser {
continue; continue;
} }
listItem = new TaskListItem() listItem = new TaskListItem()
.done(isDone(matcher.group(1))) .done(isDone(matcher.group(2)))
.indent(item.indent / 2); .indent(item.indent / 2);
inlineParser.parse(matcher.group(2), listItem); inlineParser.parse(matcher.group(3), listItem);
block.appendChild(listItem); block.appendChild(listItem);
} }
} }

View File

@ -0,0 +1,57 @@
input: |-
- [ ] First
- [x] Second
- [X] Third
* [ ] First star
* [x] Second star
* [X] Third star
+ [ ] First plus
+ [x] Second plus
+ [X] Third plus
1. [x] Number with dot
3) [ ] Number
output:
- task-list: "First"
blockIdent: 1
done: false
- text: "\n"
- task-list: "Second"
blockIdent: 1
done: true
- text: "\n"
- task-list: "Third"
blockIdent: 1
done: true
- text: "\n"
- task-list: "First star"
blockIdent: 1
done: false
- text: "\n"
- task-list: "Second star"
blockIdent: 1
done: true
- text: "\n"
- task-list: "Third star"
blockIdent: 1
done: true
- text: "\n"
- task-list: "First plus"
blockIdent: 1
done: false
- text: "\n"
- task-list: "Second plus"
blockIdent: 1
done: true
- text: "\n"
- task-list: "Third plus"
blockIdent: 1
done: true
- text: "\n"
- task-list: "Number with dot"
blockIdent: 1
done: true
- text: "\n"
- task-list: "Number"
blockIdent: 1
done: false