mirror of
https://github.com/MatomoCamp/matomocamp-companion-android.git
synced 2024-09-19 16:13:46 +02:00
Added multi-level lists support to HTML parser; fixes #14
This commit is contained in:
parent
9fd6c6c374
commit
9ff63fb011
2 changed files with 78 additions and 17 deletions
|
@ -14,7 +14,6 @@ import android.support.v4.app.LoaderManager;
|
||||||
import android.support.v4.app.LoaderManager.LoaderCallbacks;
|
import android.support.v4.app.LoaderManager.LoaderCallbacks;
|
||||||
import android.support.v4.app.ShareCompat;
|
import android.support.v4.app.ShareCompat;
|
||||||
import android.support.v4.content.Loader;
|
import android.support.v4.content.Loader;
|
||||||
import android.text.Html;
|
|
||||||
import android.text.Spannable;
|
import android.text.Spannable;
|
||||||
import android.text.SpannableString;
|
import android.text.SpannableString;
|
||||||
import android.text.SpannableStringBuilder;
|
import android.text.SpannableStringBuilder;
|
||||||
|
@ -173,7 +172,7 @@ public class EventDetailsFragment extends Fragment {
|
||||||
if (TextUtils.isEmpty(text)) {
|
if (TextUtils.isEmpty(text)) {
|
||||||
textView.setVisibility(View.GONE);
|
textView.setVisibility(View.GONE);
|
||||||
} else {
|
} else {
|
||||||
textView.setText(StringUtils.trimEnd(Html.fromHtml(text)));
|
textView.setText(StringUtils.parseHtml(text));
|
||||||
textView.setMovementMethod(linkMovementMethod);
|
textView.setMovementMethod(linkMovementMethod);
|
||||||
}
|
}
|
||||||
textView = (TextView) view.findViewById(R.id.description);
|
textView = (TextView) view.findViewById(R.id.description);
|
||||||
|
@ -181,7 +180,7 @@ public class EventDetailsFragment extends Fragment {
|
||||||
if (TextUtils.isEmpty(text)) {
|
if (TextUtils.isEmpty(text)) {
|
||||||
textView.setVisibility(View.GONE);
|
textView.setVisibility(View.GONE);
|
||||||
} else {
|
} else {
|
||||||
textView.setText(StringUtils.trimEnd(Html.fromHtml(text)));
|
textView.setText(StringUtils.parseHtml(text));
|
||||||
textView.setMovementMethod(linkMovementMethod);
|
textView.setMovementMethod(linkMovementMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -339,8 +338,7 @@ public class EventDetailsFragment extends Fragment {
|
||||||
if (TextUtils.isEmpty(description)) {
|
if (TextUtils.isEmpty(description)) {
|
||||||
description = event.getDescription();
|
description = event.getDescription();
|
||||||
}
|
}
|
||||||
// Strip HTML
|
description = StringUtils.stripHtml(description);
|
||||||
description = StringUtils.trimEnd(Html.fromHtml(description)).toString();
|
|
||||||
// Add speaker info if available
|
// Add speaker info if available
|
||||||
if (personsCount > 0) {
|
if (personsCount > 0) {
|
||||||
description = String.format("%1$s: %2$s\n\n%3$s", getResources().getQuantityString(R.plurals.speakers, personsCount), event.getPersonsSummary(),
|
description = String.format("%1$s: %2$s\n\n%3$s", getResources().getQuantityString(R.plurals.speakers, personsCount), event.getPersonsSummary(),
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
package be.digitalia.fosdem.utils;
|
package be.digitalia.fosdem.utils;
|
||||||
|
|
||||||
|
import android.text.Editable;
|
||||||
|
import android.text.Html;
|
||||||
|
|
||||||
|
import org.xml.sax.XMLReader;
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -20,8 +25,7 @@ public class StringUtils {
|
||||||
/**
|
/**
|
||||||
* Returns string without diacritics - 7 bit approximation.
|
* Returns string without diacritics - 7 bit approximation.
|
||||||
*
|
*
|
||||||
* @param source
|
* @param source string to convert
|
||||||
* string to convert
|
|
||||||
* @return corresponding string without diacritics
|
* @return corresponding string without diacritics
|
||||||
*/
|
*/
|
||||||
public static String removeDiacritics(String source) {
|
public static String removeDiacritics(String source) {
|
||||||
|
@ -65,9 +69,6 @@ public class StringUtils {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes all non-alphanumeric chars at the beginning and end of source.
|
* Removes all non-alphanumeric chars at the beginning and end of source.
|
||||||
*
|
|
||||||
* @param source
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
private static String trimNonAlpha(String source) {
|
private static String trimNonAlpha(String source) {
|
||||||
int st = 0;
|
int st = 0;
|
||||||
|
@ -84,14 +85,19 @@ public class StringUtils {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transforms a name to a slug identifier to be used in a FOSDEM URL.
|
* Transforms a name to a slug identifier to be used in a FOSDEM URL.
|
||||||
*
|
|
||||||
* @param source
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
public static String toSlug(String source) {
|
public static String toSlug(String source) {
|
||||||
return replaceNonAlphaGroups(trimNonAlpha(removeDiacritics(source)), '_').toLowerCase(Locale.US);
|
return replaceNonAlphaGroups(trimNonAlpha(removeDiacritics(source)), '_').toLowerCase(Locale.US);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String stripHtml(String html) {
|
||||||
|
return trimEnd(Html.fromHtml(html)).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CharSequence parseHtml(String html) {
|
||||||
|
return trimEnd(Html.fromHtml(html, null, new ListsTagHandler()));
|
||||||
|
}
|
||||||
|
|
||||||
public static CharSequence trimEnd(CharSequence source) {
|
public static CharSequence trimEnd(CharSequence source) {
|
||||||
int pos = source.length() - 1;
|
int pos = source.length() - 1;
|
||||||
while ((pos >= 0) && Character.isWhitespace(source.charAt(pos))) {
|
while ((pos >= 0) && Character.isWhitespace(source.charAt(pos))) {
|
||||||
|
@ -104,8 +110,6 @@ public class StringUtils {
|
||||||
/**
|
/**
|
||||||
* Converts a room name to a local drawable resource name, by stripping non-alpha chars and converting to lower case. Any letter following a digit will be
|
* Converts a room name to a local drawable resource name, by stripping non-alpha chars and converting to lower case. Any letter following a digit will be
|
||||||
* ignored, along with the rest of the string.
|
* ignored, along with the rest of the string.
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
public static String roomNameToResourceName(String roomName) {
|
public static String roomNameToResourceName(String roomName) {
|
||||||
StringBuilder builder = new StringBuilder(ROOM_DRAWABLE_PREFIX.length() + roomName.length());
|
StringBuilder builder = new StringBuilder(ROOM_DRAWABLE_PREFIX.length() + roomName.length());
|
||||||
|
@ -126,4 +130,63 @@ public class StringUtils {
|
||||||
}
|
}
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class ListsTagHandler implements Html.TagHandler {
|
||||||
|
|
||||||
|
private int level = 0;
|
||||||
|
private int liStart = -1;
|
||||||
|
|
||||||
|
private static void trimStart(final int start, Editable output) {
|
||||||
|
int end = start;
|
||||||
|
final int length = output.length();
|
||||||
|
while ((end < length) && Character.isWhitespace(output.charAt(end))) {
|
||||||
|
end++;
|
||||||
|
}
|
||||||
|
if (start < end) {
|
||||||
|
output.delete(start, end);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void trimEnd(Editable output) {
|
||||||
|
final int end = output.length();
|
||||||
|
int start = end - 1;
|
||||||
|
while ((start >= 0) && Character.isWhitespace(output.charAt(start))) {
|
||||||
|
start--;
|
||||||
|
}
|
||||||
|
start++;
|
||||||
|
if (start < end) {
|
||||||
|
output.delete(start, end);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
|
||||||
|
switch (tag) {
|
||||||
|
case "li":
|
||||||
|
case "LI":
|
||||||
|
if (liStart != -1) {
|
||||||
|
trimStart(liStart, output);
|
||||||
|
}
|
||||||
|
if (opening) {
|
||||||
|
level++;
|
||||||
|
for (int i = 0; i < level; ++i) {
|
||||||
|
output.append('\t');
|
||||||
|
}
|
||||||
|
output.append("• ");
|
||||||
|
liStart = output.length();
|
||||||
|
} else {
|
||||||
|
level--;
|
||||||
|
trimEnd(output);
|
||||||
|
output.append('\n');
|
||||||
|
if (liStart == -1) {
|
||||||
|
// End of list; moving one level up
|
||||||
|
output.append('\n');
|
||||||
|
} else {
|
||||||
|
liStart = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue