Skip to content

Commit 7844ad1

Browse files
author
Federico Iosue
committed
Fixed tags parsing and removal (fix #640)
1 parent 77942a9 commit 7844ad1

File tree

4 files changed

+49
-23
lines changed

4 files changed

+49
-23
lines changed

omniNotes/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ dependencies {
185185
implementation ('com.github.federicoiosue:checklistview:3.2.1') {
186186
transitive=false
187187
}
188-
implementation 'com.github.federicoiosue:pixlui:2.6'
188+
implementation 'com.github.federicoiosue:pixlui:3.0.0'
189189

190190
// Flavors specific dependencies
191191
playImplementation 'io.nlopez.smartlocation:library:3.2.4'

omniNotes/src/main/java/it/feio/android/omninotes/utils/ConstantsBase.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ public interface ConstantsBase {
116116
String TIMESTAMP_UNIX_EPOCH = "0";
117117
String TIMESTAMP_UNIX_EPOCH_FAR = "18464193800000";
118118

119+
String TAG_SPECIAL_CHARS_TO_REMOVE = "[<>\\[\\],-\\.\\(\\)\\[\\]\\{\\}\\!\\?]";
120+
119121
int MENU_SORT_GROUP_ID = 11998811;
120122

121123
String MERGED_NOTES_SEPARATOR = "----------------------";

omniNotes/src/main/java/it/feio/android/omninotes/utils/TagsHelper.java

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,21 @@
1818
package it.feio.android.omninotes.utils;
1919

2020
import android.support.v4.util.Pair;
21-
import it.feio.android.omninotes.db.DbHelper;
22-
import it.feio.android.omninotes.models.Note;
23-
import it.feio.android.omninotes.models.Tag;
24-
import it.feio.android.pixlui.links.RegexPatternsConstants;
21+
22+
import org.apache.commons.lang.StringUtils;
2523

2624
import java.util.ArrayList;
2725
import java.util.Arrays;
2826
import java.util.HashMap;
2927
import java.util.List;
30-
import java.util.regex.Matcher;
28+
29+
import it.feio.android.omninotes.db.DbHelper;
30+
import it.feio.android.omninotes.models.Note;
31+
import it.feio.android.omninotes.models.Tag;
32+
import it.feio.android.pixlui.links.UrlCompleter;
33+
import rx.Observable;
34+
35+
import static it.feio.android.omninotes.utils.ConstantsBase.TAG_SPECIAL_CHARS_TO_REMOVE;
3136

3237

3338
public class TagsHelper {
@@ -40,10 +45,12 @@ public static List<Tag> getAllTags() {
4045

4146
public static HashMap<String, Integer> retrieveTags(Note note) {
4247
HashMap<String, Integer> tagsMap = new HashMap<>();
43-
for (String token : (note.getTitle() + " " + note.getContent()).replaceAll("\n", " ").trim().split(" ")) {
44-
if (RegexPatternsConstants.HASH_TAG.matcher(token).matches()) {
45-
int count = tagsMap.get(token) == null ? 0 : tagsMap.get(token);
46-
tagsMap.put(token, ++count);
48+
String[] words = (note.getTitle() + " " + note.getContent()).replaceAll("\n", " ").trim().split(" ");
49+
for (String word: words) {
50+
String parsedHashtag = UrlCompleter.parseHashtag(word);
51+
if (StringUtils.isNotEmpty(parsedHashtag)) {
52+
int count = tagsMap.get(parsedHashtag) == null ? 0 : tagsMap.get(parsedHashtag);
53+
tagsMap.put(parsedHashtag, ++count);
4754
}
4855
}
4956
return tagsMap;
@@ -87,9 +94,23 @@ private static boolean mapContainsTag(HashMap<String, Integer> tagsMap, Tag tag)
8794
public static Pair<String, String> removeTag(String noteTitle, String noteContent, List<Tag> tagsToRemove) {
8895
String title = noteTitle, content = noteContent;
8996
for (Tag tagToRemove : tagsToRemove) {
90-
String tagRegex = tagToRemove.getText() + "(\\s)|" + tagToRemove.getText() + "$";
91-
title = title.replaceAll(tagRegex, "");
92-
content = content.replaceAll(tagRegex, "");
97+
if (StringUtils.isNotEmpty(title)) {
98+
title = Observable.from(title.replaceAll(TAG_SPECIAL_CHARS_TO_REMOVE, " ").split("\\s"))
99+
.map(String::trim)
100+
.filter(s -> !s.matches(tagToRemove.getText()))
101+
.reduce((s, s2) -> s + " " + s2)
102+
.toBlocking()
103+
.singleOrDefault("");
104+
}
105+
if (StringUtils.isNotEmpty(content)) {
106+
content = Observable.from(content.replaceAll(TAG_SPECIAL_CHARS_TO_REMOVE, " ").split("\\s"))
107+
.map(String::trim)
108+
.filter(s -> !s.matches(tagToRemove.getText()))
109+
.reduce((s, s2) -> s + " " + s2)
110+
.toBlocking()
111+
.singleOrDefault("");
112+
}
113+
93114
}
94115
return new Pair<>(title, content);
95116
}

omniNotes/src/test/java/it/feio/android/omninotes/utils/TagsHelperTest.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@
1818
package it.feio.android.omninotes.utils;
1919

2020
import android.support.v4.util.Pair;
21-
import it.feio.android.omninotes.models.Note;
22-
import it.feio.android.omninotes.models.Tag;
21+
2322
import org.junit.Before;
2423
import org.junit.Test;
2524

2625
import java.util.ArrayList;
2726
import java.util.HashMap;
2827
import java.util.List;
2928

29+
import it.feio.android.omninotes.models.Note;
30+
import it.feio.android.omninotes.models.Tag;
31+
32+
import static java.util.Collections.singletonList;
3033
import static org.junit.Assert.assertEquals;
3134
import static org.junit.Assert.assertFalse;
3235
import static org.junit.Assert.assertTrue;
@@ -37,25 +40,25 @@ public class TagsHelperTest {
3740
private static String TAG1 = "#mixed";
3841
private static String TAG2 = "#123numbered";
3942
private static String TAG3 = "#tags";
43+
private static String TAG4 = "#tag";
44+
private static String TAG5 = "#numberedAfter123";
4045

4146
private Note note;
4247

4348

4449
@Before
4550
public void setup() {
4651
note = new Note();
47-
note.setContent("Random content with " + TAG1 + " " + TAG2 + " " + TAG3);
52+
note.setContent("Random content with " + TAG1 + " " + TAG2 + " " + TAG3 + "(and another with similar prefix: " + TAG4 + ") and " + TAG5);
4853
}
4954

5055

5156
@Test
5257
public void retrievesTagsFromNote() {
5358
HashMap<String, Integer> tags = TagsHelper.retrieveTags(note);
54-
assertEquals(tags.size(), 3);
55-
assertTrue(tags.containsKey(TAG1));
56-
assertTrue(tags.containsKey(TAG2));
57-
assertTrue(tags.containsKey(TAG3));
58-
assertFalse(tags.containsKey("#nonExistingTag"));
59+
assertEquals(tags.size(), 4);
60+
assertTrue(tags.containsKey(TAG1) && tags.containsKey(TAG3) && tags.containsKey(TAG4) && tags.containsKey(TAG5));
61+
assertFalse(tags.containsKey(TAG2));
5962
}
6063

6164

@@ -73,14 +76,14 @@ public void retrievesTagsFromNoteMultilanguage() {
7376

7477
@Test
7578
public void removesTagsFromNote() {
76-
Pair<String, String> pair = TagsHelper.removeTag(note.getTitle(), note.getContent(), java.util.Collections
77-
.singletonList(new Tag(TAG2, 4)));
79+
Pair<String, String> pair = TagsHelper.removeTag(note.getTitle(), note.getContent(), singletonList(new Tag(TAG4, 4)));
7880
note.setTitle(pair.first);
7981
note.setContent(pair.second);
8082
HashMap<String, Integer> tags = TagsHelper.retrieveTags(note);
8183
assertTrue(tags.containsKey(TAG1));
8284
assertFalse(tags.containsKey(TAG2));
8385
assertTrue(tags.containsKey(TAG3));
86+
assertFalse(tags.containsKey(TAG4));
8487
}
8588

8689

0 commit comments

Comments
 (0)