Skip to content

Commit ba7e0fc

Browse files
atermenjikevinsawicki
authored andcommitted
Add markdown processing of source code files
Closes pockethub#306 Closes pockethub#310
1 parent 4cb5b4c commit ba7e0fc

File tree

5 files changed

+102
-13
lines changed

5 files changed

+102
-13
lines changed

app/src/main/java/com/github/mobile/ui/BaseActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
import android.os.Bundle;
1919

2020
import com.github.kevinsawicki.wishlist.ViewFinder;
21-
import com.github.rtyley.android.sherlock.roboguice.activity.RoboSherlockActivity;
21+
import com.github.rtyley.android.sherlock.roboguice.activity.RoboSherlockFragmentActivity;
2222

2323
import java.io.Serializable;
2424

2525
/**
2626
* Base sherlock activity
2727
*/
28-
public class BaseActivity extends RoboSherlockActivity {
28+
public class BaseActivity extends RoboSherlockFragmentActivity {
2929

3030
/**
3131
* Finder bound to this activity's view

app/src/main/java/com/github/mobile/ui/MarkdownLoader.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public class MarkdownLoader extends AuthenticatedUserLoader<CharSequence> {
4343

4444
private final String raw;
4545

46+
private boolean encode;
47+
4648
@Inject
4749
private MarkdownService service;
4850

@@ -51,14 +53,16 @@ public class MarkdownLoader extends AuthenticatedUserLoader<CharSequence> {
5153
* @param repository
5254
* @param raw
5355
* @param imageGetter
56+
* @param encode
5457
*/
5558
public MarkdownLoader(Context context, IRepositoryIdProvider repository,
56-
String raw, ImageGetter imageGetter) {
59+
String raw, ImageGetter imageGetter, boolean encode) {
5760
super(context);
5861

5962
this.repository = repository;
6063
this.raw = raw;
6164
this.imageGetter = imageGetter;
65+
this.encode = encode;
6266
}
6367

6468
@Override
@@ -74,7 +78,11 @@ public CharSequence load(Account account) {
7478
html = service.getRepositoryHtml(repository, raw);
7579
else
7680
html = service.getHtml(raw, MODE_GFM);
77-
return HtmlUtils.encode(html, imageGetter);
81+
82+
if (encode)
83+
return HtmlUtils.encode(html, imageGetter);
84+
else
85+
return html;
7886
} catch (IOException e) {
7987
Log.d(TAG, "Loading rendered markdown failed", e);
8088
return null;

app/src/main/java/com/github/mobile/ui/comment/RenderedCommentFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public Loader<CharSequence> onCreateLoader(int loader, Bundle args) {
9797
final IRepositoryIdProvider repo = (IRepositoryIdProvider) args
9898
.getSerializable(ARG_REPO);
9999
return new MarkdownLoader(getActivity(), repo, raw.toString(),
100-
imageGetter);
100+
imageGetter, true);
101101
}
102102

103103
@Override

app/src/main/java/com/github/mobile/ui/ref/BranchFileViewActivity.java

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import static com.github.mobile.util.PreferenceUtils.WRAP;
2323
import android.content.Intent;
2424
import android.os.Bundle;
25+
import android.support.v4.app.LoaderManager;
26+
import android.support.v4.content.Loader;
2527
import android.util.Log;
2628
import android.webkit.WebView;
2729
import android.widget.ProgressBar;
@@ -38,23 +40,37 @@
3840
import com.github.mobile.core.code.RefreshBlobTask;
3941
import com.github.mobile.core.commit.CommitUtils;
4042
import com.github.mobile.ui.BaseActivity;
43+
import com.github.mobile.ui.MarkdownLoader;
4144
import com.github.mobile.util.AvatarLoader;
45+
import com.github.mobile.util.HttpImageGetter;
4246
import com.github.mobile.util.PreferenceUtils;
4347
import com.github.mobile.util.ShareUtils;
4448
import com.github.mobile.util.SourceEditor;
4549
import com.github.mobile.util.ToastUtils;
4650
import com.google.inject.Inject;
4751

52+
import java.io.Serializable;
53+
4854
import org.eclipse.egit.github.core.Blob;
55+
import org.eclipse.egit.github.core.IRepositoryIdProvider;
4956
import org.eclipse.egit.github.core.Repository;
57+
import org.eclipse.egit.github.core.util.EncodingUtils;
5058

5159
/**
5260
* Activity to view a file on a branch
5361
*/
54-
public class BranchFileViewActivity extends BaseActivity {
62+
public class BranchFileViewActivity extends BaseActivity implements
63+
LoaderManager.LoaderCallbacks<CharSequence> {
5564

5665
private static final String TAG = "BranchFileViewActivity";
5766

67+
private static final String ARG_TEXT = "text";
68+
69+
private static final String ARG_REPO = "repo";
70+
71+
private static final String[] MARKDOWN_EXTENSIONS =
72+
{"md", "mkdn", "mdwn", "mdown", "markdown"};
73+
5874
/**
5975
* Create intent to show file in commit
6076
*
@@ -93,6 +109,9 @@ public static Intent createIntent(Repository repository, String branch,
93109
@Inject
94110
private AvatarLoader avatars;
95111

112+
@Inject
113+
private HttpImageGetter imageGetter;
114+
96115
@Override
97116
protected void onCreate(Bundle savedInstanceState) {
98117
super.onCreate(savedInstanceState);
@@ -155,6 +174,30 @@ public boolean onOptionsItemSelected(MenuItem item) {
155174
}
156175
}
157176

177+
@Override
178+
public Loader<CharSequence> onCreateLoader(int loader, Bundle args) {
179+
final String raw = args.getString(ARG_TEXT);
180+
final IRepositoryIdProvider repo = (IRepositoryIdProvider) args
181+
.getSerializable(ARG_REPO);
182+
return new MarkdownLoader(this, repo, raw, imageGetter, false);
183+
}
184+
185+
@Override
186+
public void onLoadFinished(Loader<CharSequence> loader,
187+
CharSequence rendered) {
188+
if (rendered == null)
189+
ToastUtils.show(this, string.error_rendering_markdown);
190+
191+
ViewUtils.setGone(loadingBar, true);
192+
ViewUtils.setGone(codeView, false);
193+
194+
editor.setMarkdown(true).setSource(file, rendered.toString(), false);
195+
}
196+
197+
@Override
198+
public void onLoaderReset(Loader<CharSequence> loader) {
199+
}
200+
158201
private void shareFile() {
159202
String id = repo.generateId();
160203
startActivity(ShareUtils.create(path + " at " + branch + " on " + id,
@@ -168,10 +211,19 @@ private void loadContent() {
168211
protected void onSuccess(Blob blob) throws Exception {
169212
super.onSuccess(blob);
170213

171-
ViewUtils.setGone(loadingBar, true);
172-
ViewUtils.setGone(codeView, false);
173-
174-
editor.setSource(file, blob);
214+
if (isMarkdown(file)) {
215+
String markdown = new String(EncodingUtils.fromBase64(blob.getContent()));
216+
Bundle args = new Bundle();
217+
args.putCharSequence(ARG_TEXT, markdown);
218+
if (repo instanceof Serializable)
219+
args.putSerializable(ARG_REPO, (Serializable) repo);
220+
getSupportLoaderManager().restartLoader(0, args, BranchFileViewActivity.this);
221+
} else {
222+
ViewUtils.setGone(loadingBar, true);
223+
ViewUtils.setGone(codeView, false);
224+
225+
editor.setMarkdown(false).setSource(file, blob);
226+
}
175227
}
176228

177229
@Override
@@ -187,4 +239,12 @@ protected void onException(Exception e) throws RuntimeException {
187239
}
188240
}.execute();
189241
}
242+
243+
private boolean isMarkdown(String name) {
244+
for (int i = 0; i < MARKDOWN_EXTENSIONS.length; i++)
245+
if (name.endsWith(MARKDOWN_EXTENSIONS[i]))
246+
return true;
247+
248+
return false;
249+
}
190250
}

app/src/main/java/com/github/mobile/util/SourceEditor.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public class SourceEditor {
4848

4949
private boolean encoded;
5050

51+
private boolean markdown;
52+
5153
/**
5254
* Create source editor using given web view
5355
*
@@ -124,8 +126,23 @@ public boolean getWrap() {
124126
*/
125127
public SourceEditor setWrap(final boolean wrap) {
126128
this.wrap = wrap;
127-
if (name != null && content != null)
128-
view.loadUrl(URL_PAGE);
129+
if (name != null && content != null) {
130+
if (markdown)
131+
view.loadData(content, "text/html", null);
132+
else
133+
view.loadUrl(URL_PAGE);
134+
}
135+
return this;
136+
}
137+
138+
/**
139+
* Sets whether the content is a markdown file
140+
*
141+
* @param markdown
142+
* @return this editor
143+
*/
144+
public SourceEditor setMarkdown(final boolean markdown) {
145+
this.markdown = markdown;
129146
return this;
130147
}
131148

@@ -142,7 +159,11 @@ public SourceEditor setSource(final String name, final String content,
142159
this.name = name;
143160
this.content = content;
144161
this.encoded = encoded;
145-
view.loadUrl(URL_PAGE);
162+
if (markdown)
163+
view.loadData(content, "text/html", null);
164+
else
165+
view.loadUrl(URL_PAGE);
166+
146167
return this;
147168
}
148169

0 commit comments

Comments
 (0)