Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ac69dba

Browse files
committed
完成大部分功能
1 parent e7038d1 commit ac69dba

File tree

22 files changed

+964
-186
lines changed

22 files changed

+964
-186
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
##### 开发中。。。
66

77
_by zzhoujay_
8+
9+
10+
*[link]()*

app/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ android {
2020
}
2121

2222
dependencies {
23-
compile fileTree(dir: 'libs', include: ['*.jar'])
23+
compile fileTree(include: ['*.jar'], dir: 'libs')
2424
testCompile 'junit:junit:4.12'
2525
compile 'com.android.support:appcompat-v7:23.4.0'
26+
compile project(':markdown')
2627
}
Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,98 @@
11
package com.zzhoujay.markdowndemo;
22

33
import android.graphics.Color;
4+
import android.graphics.Typeface;
45
import android.support.v7.app.AppCompatActivity;
56
import android.os.Bundle;
67
import android.text.SpannableStringBuilder;
78
import android.text.Spanned;
9+
import android.text.method.LinkMovementMethod;
810
import android.text.style.ForegroundColorSpan;
11+
import android.text.style.StyleSpan;
912
import android.util.Log;
13+
import android.widget.TextView;
1014

15+
import com.zzhoujay.markdown.parser.MarkDownParser;
16+
import com.zzhoujay.markdown.parser.StyleBuilderImpl;
17+
import com.zzhoujay.markdown.spanneds.TestSpan;
18+
19+
import java.io.BufferedReader;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.io.InputStreamReader;
1123
import java.util.regex.Matcher;
1224
import java.util.regex.Pattern;
1325

1426
public class MainActivity extends AppCompatActivity {
1527

28+
private static final String test_str = "# MarkDown\n" +
29+
"\n" +
30+
"> Android平台的Markdown解析器\n" +
31+
"\n" +
32+
"##### 开发中。。。\n" +
33+
"\n" +
34+
"_by zzhoujay_\n";
35+
1636
@Override
1737
protected void onCreate(Bundle savedInstanceState) {
1838
super.onCreate(savedInstanceState);
1939
setContentView(R.layout.activity_main);
2040

41+
final TextView textView = (TextView) findViewById(R.id.textView);
42+
assert textView != null;
43+
textView.setMovementMethod(LinkMovementMethod.getInstance());
44+
45+
// textView.post(new Runnable() {
46+
// @Override
47+
// public void run() {
48+
// SpannableStringBuilder sb = new SpannableStringBuilder();
49+
// sb.append("hello world zzhoujay");
50+
// StyleSpan styleSpan = new StyleSpan(Typeface.ITALIC);
51+
// StyleSpan styleSpan1 = new StyleSpan(Typeface.BOLD);
52+
// sb.setSpan(styleSpan,0,sb.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
53+
// sb.setSpan(styleSpan1,4,10,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
54+
// textView.setText(sb);
55+
// }
56+
// });
57+
2158

22-
Pattern pattern = Pattern.compile("#\\s+(.*)");
23-
String test = "# hello";
24-
SpannableStringBuilder builder = new SpannableStringBuilder(test);
25-
builder.setSpan(new ForegroundColorSpan(Color.RED), 1, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
26-
Matcher matcher = pattern.matcher(builder);
27-
if (matcher.find()) {
28-
Log.i("find", matcher.group(1));
29-
System.out.println(matcher.group(1));
30-
} else {
31-
Log.i("find", matcher.group(1));
32-
System.out.println("gg");
59+
InputStream stream = getResources().openRawResource(R.raw.test);
60+
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
61+
final StringBuilder sb = new StringBuilder();
62+
String line;
63+
try {
64+
while ((line = bufferedReader.readLine()) != null) {
65+
sb.append(line).append('\n');
66+
}
67+
} catch (IOException e) {
68+
e.printStackTrace();
3369
}
70+
71+
textView.post(new Runnable() {
72+
@Override
73+
public void run() {
74+
MarkDownParser markDownParser = new MarkDownParser(sb.toString(), new StyleBuilderImpl(textView));
75+
try {
76+
textView.setText(markDownParser.parser());
77+
} catch (IOException e) {
78+
e.printStackTrace();
79+
}
80+
81+
}
82+
});
83+
84+
//
85+
// Pattern pattern = Pattern.compile("#\\s+(.*)");
86+
// String test = "# hello";
87+
// SpannableStringBuilder builder = new SpannableStringBuilder(test);
88+
// builder.setSpan(new ForegroundColorSpan(Color.RED), 1, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
89+
// Matcher matcher = pattern.matcher(builder);
90+
// if (matcher.find()) {
91+
// Log.i("find", matcher.group(1));
92+
// System.out.println(matcher.group(1));
93+
// } else {
94+
// Log.i("find", matcher.group(1));
95+
// System.out.println("gg");
96+
// }
3497
}
3598
}

app/src/main/res/layout/activity_main.xml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@
22
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools"
44
android:layout_width="match_parent"
5+
android:background="@android:color/white"
56
android:layout_height="match_parent"
67
android:paddingBottom="@dimen/activity_vertical_margin"
78
android:paddingLeft="@dimen/activity_horizontal_margin"
89
android:paddingRight="@dimen/activity_horizontal_margin"
910
android:paddingTop="@dimen/activity_vertical_margin"
1011
tools:context="com.zzhoujay.markdowndemo.MainActivity">
1112

12-
<TextView
13-
android:layout_width="wrap_content"
14-
android:layout_height="wrap_content"
15-
android:text="Hello World!" />
13+
<ScrollView
14+
android:layout_width="match_parent"
15+
android:layout_height="match_parent">
16+
17+
<TextView
18+
android:id="@+id/textView"
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content" />
21+
</ScrollView>
22+
1623
</RelativeLayout>

app/src/main/res/raw/test.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,34 @@ H6
4343

4444
> ## H2
4545
46-
hello`code`world
46+
hello`code`world`zz`
4747

4848
```
4949
this code block
50-
```
50+
block 2
51+
block 3
52+
```
53+
54+
55+
code block1
56+
code block2
57+
code block3
58+
59+
60+
[_baidu_](http://www.baidu.com "baidu")
61+
62+
[jd][id]
63+
64+
[id]:http://jd.com "jd"
65+
66+
~~*delete*~~
67+
68+
_this a **italic** line_
69+
70+
**~~start~~**``__fj__``__italic__
71+
72+
**em**hh**asdfasd**
73+
74+
1. 4534
75+
2. 235
76+
5. 2342

markdown/src/main/java/com/zzhoujay/markdown/Pair.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,12 @@
33
/**
44
* Created by zhou on 16-7-1.
55
*/
6-
public class Pair {
6+
public class Pair<K, T> {
7+
public final K first;
8+
public final T last;
9+
10+
public Pair(K first, T last) {
11+
this.first = first;
12+
this.last = last;
13+
}
714
}

markdown/src/main/java/com/zzhoujay/markdown/parser/Line.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
11
package com.zzhoujay.markdown.parser;
22

3-
import android.text.SpannableStringBuilder;
4-
53
/**
64
* Created by zhou on 16-6-28.
75
*/
86
public class Line {
97

8+
public static final int LINE_NORMAL = 0;
9+
public static final int LINE_TYPE_QUOTA = 1;
10+
public static final int LINE_TYPE_UL = 2;
11+
public static final int LINE_TYPE_OL = 3;
12+
public static final int LINE_TYPE_H1 = 4;
13+
public static final int LINE_TYPE_H2 = 5;
14+
public static final int LINE_TYPE_H3 = 6;
15+
public static final int LINE_TYPE_H4 = 7;
16+
public static final int LINE_TYPE_H5 = 8;
17+
public static final int LINE_TYPE_H6 = 9;
18+
public static final int LINE_TYPE_CODE_BLOCK_2 = 10;
19+
public static final int LINE_TYPE_CODE_BLOCK_1 = 11;
20+
1021
public final int lineNum;
1122
private String source;
12-
1323
private boolean codeBlock;
14-
1524
private CharSequence builder;
25+
private int type;
26+
private int typeCount;
1627

1728
public Line(int lineNum, String source) {
1829
this.lineNum = lineNum;
1930
this.source = source;
2031
codeBlock = false;
32+
typeCount = 1;
33+
type = LINE_NORMAL;
2134
}
2235

2336
public CharSequence getBuilder() {
@@ -43,4 +56,20 @@ public boolean isCodeBlock() {
4356
public void setCodeBlock(boolean codeBlock) {
4457
this.codeBlock = codeBlock;
4558
}
59+
60+
public int getType() {
61+
return type;
62+
}
63+
64+
public void setType(int type) {
65+
this.type = type;
66+
}
67+
68+
public int getTypeCount() {
69+
return typeCount;
70+
}
71+
72+
public void setTypeCount(int typeCount) {
73+
this.typeCount = typeCount;
74+
}
4675
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,52 @@
11
package com.zzhoujay.markdown.parser;
22

3+
import java.util.LinkedList;
4+
import java.util.List;
5+
36
/**
47
* Created by zhou on 16-7-2.
58
*/
69
public class LineQueue {
10+
11+
private List<Line> lines;
12+
private int point;
13+
14+
public LineQueue(List<Line> lines) {
15+
this.lines = lines;
16+
point = 0;
17+
}
18+
19+
public boolean end() {
20+
return point == lines.size() - 1;
21+
}
22+
23+
public boolean start() {
24+
return point == 0;
25+
}
26+
27+
public Line nextLine() {
28+
if (!end()) {
29+
return lines.get(point + 1);
30+
}
31+
return null;
32+
}
33+
34+
public Line prevLine() {
35+
if (!start()) {
36+
return lines.get(point - 1);
37+
}
38+
return null;
39+
}
40+
41+
public Line get() {
42+
return lines.get(point);
43+
}
44+
45+
public boolean next() {
46+
if (end()) {
47+
return false;
48+
}
49+
point++;
50+
return true;
51+
}
752
}

0 commit comments

Comments
 (0)