Skip to content
Permalink
Browse files

Basic formatting and links using Markdown

  • Loading branch information...
yanokwa committed Oct 19, 2015
1 parent bc0e65c commit 55ee5a60c17e34d7b0b56270547e5e7e32f7762f
@@ -14,46 +14,71 @@

package org.odk.collect.android.utilities;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.odk.collect.android.application.Collect;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Video;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class TextUtils {
private static final String t = "TextUtils";
private static final String t = "TextUtils";

private TextUtils() {
// static methods only
}

private static String markdownToHtml(String html) {

// Regular expressions should match https://github.com/enketo/enketo-transformer/blob/master/src/markdown.js
String strongRegex1 = "__(.*?)__";
String strongRegex2 = "\\*\\*(.*?)\\*\\*";
String strongReplacement = "<strong>$1</strong>";

String emphasisRegex1 = "_([^\\s][^_\\n]*)_";
String emphasisRegex2 = "\\*([^\\s][^\\*\\n]*)\\*";
String emphasisReplacement = "<em>$1</em>";

String linkRegex = "\\[([^\\]]*)\\]\\(([^\\)]+)\\)";
String linkReplacement = "<a href=\"$2\">$1</a>";

html = html.replaceAll(strongRegex1, strongReplacement);
html = html.replaceAll(strongRegex2, strongReplacement);
html = html.replaceAll(emphasisRegex1, emphasisReplacement);
html = html.replaceAll(emphasisRegex2, emphasisReplacement);
html = html.replaceAll(linkRegex, linkReplacement);

private TextUtils() {
// static methods only
}
String headerRegex = "(#+)([^\\n]*)\\n";
StringBuffer headerOutput = new StringBuffer();
Pattern headerPattern = Pattern.compile(headerRegex);
Matcher headerMatcher = headerPattern.matcher(html);
while (headerMatcher.find()) {
headerMatcher.appendReplacement(headerOutput, createHeaderReplacement(headerMatcher));
}
headerMatcher.appendTail(headerOutput);
html = headerOutput.toString();

public static CharSequence fixHtml(String brokenHtml) {
// There's some terrible bug that displays all the text as the
return html;
}

public static String createHeaderReplacement(Matcher matcher) {
int level = matcher.group(1).length();
return "<h" + level + ">" + matcher.group(2).replaceAll("#+$", "").trim() + "</h" + level + ">\n";
}

public static CharSequence textToHtml(String text) {

// There's some terrible bug that displays all the text as the
// opening tag if a tag is the first thing in the string
// so we hack around it so it begins with something else
// when we convert it

// terrible hack, just add some chars
Spanned html = Html.fromHtml("aa" + brokenHtml);
Spanned brokenHtml = Html.fromHtml("x" + markdownToHtml(text));
// after we have the good html, remove the chars
CharSequence fixedText = html.subSequence(2, html.length());
return fixedText;
CharSequence fixedHtml = brokenHtml.subSequence(1, brokenHtml.length());

return fixedHtml;
}

}
@@ -14,13 +14,12 @@

package org.odk.collect.android.views;

import android.text.method.LinkMovementMethod;
import org.odk.collect.android.logic.HierarchyElement;
import org.odk.collect.android.utilities.MediaUtils;
import org.odk.collect.android.utilities.TextUtils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.RelativeLayout;
@@ -73,7 +72,8 @@ public HierarchyElementView(Context context, HierarchyElement it) {


public void setPrimaryText(String text) {
mPrimaryTextView.setText(TextUtils.fixHtml(text));
mPrimaryTextView.setText(TextUtils.textToHtml(text));
mPrimaryTextView.setMovementMethod(LinkMovementMethod.getInstance());
}


@@ -17,6 +17,7 @@
import java.util.ArrayList;
import java.util.List;

import android.text.method.LinkMovementMethod;
import org.javarosa.core.model.data.IAnswerData;
import org.javarosa.form.api.FormEntryPrompt;
import org.odk.collect.android.application.Collect;
@@ -208,7 +209,8 @@ protected void addQuestionText(FormEntryPrompt p) {
mQuestionText.setTypeface(null, Typeface.BOLD);
mQuestionText.setPadding(0, 0, 0, 7);
mQuestionText.setId(QuestionWidget.newUniqueId()); // assign random id
mQuestionText.setText(promptText == null ? "" : TextUtils.fixHtml(promptText));
mQuestionText.setText(promptText == null ? "" : TextUtils.textToHtml(promptText));
mQuestionText.setMovementMethod(LinkMovementMethod.getInstance());

// Wrap to the size of the parent view
mQuestionText.setHorizontallyScrolling(false);
@@ -239,7 +241,8 @@ private void addHelpText(FormEntryPrompt p) {
// wrap to the widget of view
mHelpText.setHorizontallyScrolling(false);
mHelpText.setTypeface(null, Typeface.ITALIC);
mHelpText.setText(TextUtils.fixHtml(s));
mHelpText.setText(TextUtils.textToHtml(s));
mHelpText.setMovementMethod(LinkMovementMethod.getInstance());

addView(mHelpText, mLayout);
}
@@ -17,6 +17,7 @@
import java.util.ArrayList;
import java.util.List;

import android.text.method.LinkMovementMethod;
import org.javarosa.core.model.SelectChoice;
import org.javarosa.core.model.data.IAnswerData;
import org.javarosa.core.model.data.SelectMultiData;
@@ -83,7 +84,8 @@ public SelectMultiWidget(Context context, FormEntryPrompt prompt) {
CheckBox c = new CheckBox(getContext());
c.setTag(Integer.valueOf(i));
c.setId(QuestionWidget.newUniqueId());
c.setText(TextUtils.fixHtml(prompt.getSelectChoiceText(mItems.get(i))));
c.setText(TextUtils.textToHtml(prompt.getSelectChoiceText(mItems.get(i))));
c.setMovementMethod(LinkMovementMethod.getInstance());
c.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mAnswerFontsize);
c.setFocusable(!prompt.isReadOnly());
c.setEnabled(!prompt.isReadOnly());
@@ -17,6 +17,7 @@
import java.util.ArrayList;
import java.util.List;

import android.text.method.LinkMovementMethod;
import org.javarosa.core.model.SelectChoice;
import org.javarosa.core.model.data.IAnswerData;
import org.javarosa.core.model.data.SelectOneData;
@@ -82,7 +83,8 @@ public SelectOneWidget(Context context, FormEntryPrompt prompt) {
for (int i = 0; i < mItems.size(); i++) {
RadioButton r = new RadioButton(getContext());
r.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mAnswerFontsize);
r.setText(TextUtils.fixHtml(prompt.getSelectChoiceText(mItems.get(i))));
r.setText(TextUtils.textToHtml(prompt.getSelectChoiceText(mItems.get(i))));
r.setMovementMethod(LinkMovementMethod.getInstance());
r.setTag(Integer.valueOf(i));
r.setId(QuestionWidget.newUniqueId());
r.setEnabled(!prompt.isReadOnly());

0 comments on commit 55ee5a6

Please sign in to comment.
You can’t perform that action at this time.