Switched to flexmark as markdown processor

This commit is contained in:
William Brawner 2017-07-27 12:43:55 -05:00
parent 90ae21724a
commit 9a493fb536
5 changed files with 66 additions and 7 deletions

View file

@ -1,6 +1,13 @@
apply plugin: 'com.android.application'
android {
packagingOptions {
exclude 'META-INF/LICENSE-LGPL-2.1.txt'
exclude 'META-INF/LICENSE-LGPL-3.txt'
exclude 'META-INF/LICENSE-W3C-TEST'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
}
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
@ -27,9 +34,9 @@ dependencies {
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:26.+'
compile 'us.feras.mdv:markdownview:1.1.0'
compile 'com.jakewharton:butterknife:8.7.0'
compile 'com.android.support:support-v4:26.+'
compile 'com.vladsch.flexmark:flexmark-all:0.22.16'
testCompile 'junit:junit:4.12'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.7.0'
}

View file

@ -3,6 +3,7 @@
package="com.wbrawner.simplemarkdown">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"

View file

@ -212,7 +212,7 @@ public class MainActivity extends AppCompatActivity {
public class EditPagerAdapter extends FragmentPagerAdapter {
private static final int FRAGMENT_EDIT = 0;
private static final int FRAGMENT_PREVIEW = 1;
public static final int FRAGMENT_PREVIEW = 1;
private static final int NUM_PAGES = 2;
public EditPagerAdapter(FragmentManager fm) {
@ -249,4 +249,12 @@ public class MainActivity extends AppCompatActivity {
return getString(stringId);
}
}
@Override
public void onBackPressed() {
if (pager.getCurrentItem() == EditPagerAdapter.FRAGMENT_EDIT)
super.onBackPressed();
else
pager.setCurrentItem(EditPagerAdapter.FRAGMENT_EDIT);
}
}

View file

@ -1,27 +1,45 @@
package com.wbrawner.simplemarkdown;
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import com.vladsch.flexmark.ast.Node;
import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughExtension;
import com.vladsch.flexmark.ext.tables.TablesExtension;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.parser.ParserEmulationProfile;
import com.vladsch.flexmark.util.options.MutableDataHolder;
import com.vladsch.flexmark.util.options.MutableDataSet;
import java.util.Arrays;
import butterknife.BindView;
import butterknife.ButterKnife;
import us.feras.mdv.MarkdownView;
public class PreviewFragment extends Fragment {
private static final String TAG = PreviewFragment.class.getSimpleName();
private static final int INTERNET_REQUEST = 0;
private WebView mMarkdownView;
@BindView(R.id.markdown_view)
MarkdownView markdownView;
WebView markdownView;
public static final String PREVIEW_ACTION = "com.wbrawner.simplemarkdown.preview";
@ -38,6 +56,17 @@ public class PreviewFragment extends Fragment {
new MarkdownBroadcastSender(),
filter
);
checkPermissions();
}
private void checkPermissions() {
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.INTERNET)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.INTERNET},
INTERNET_REQUEST);
}
}
@Override
@ -46,6 +75,10 @@ public class PreviewFragment extends Fragment {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_preview, container, false);
ButterKnife.bind(this, view);
mMarkdownView = markdownView;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
}
return view;
}
@ -55,9 +88,19 @@ public class PreviewFragment extends Fragment {
if (intent.hasExtra("markdownData")) {
String data = intent.getStringExtra("markdownData");
Log.d(TAG, "Markdown Data: " + data);
markdownView.loadMarkdown(data);
markdown(data);
}
}
}
private void markdown(String text) {
MutableDataHolder options = new MutableDataSet();
options.setFrom(ParserEmulationProfile.MARKDOWN);
options.set(Parser.EXTENSIONS, Arrays.asList(TablesExtension.create(), StrikethroughExtension.create()));
Parser parser = Parser.builder(options).build();
Node document = parser.parse(text);
HtmlRenderer renderer = HtmlRenderer.builder(options).build();
String html = renderer.render(document);
mMarkdownView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
}
}

View file

@ -4,7 +4,7 @@
android:layout_height="match_parent"
tools:context="com.wbrawner.simplemarkdown.PreviewFragment">
<us.feras.mdv.MarkdownView
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/markdown_view" />