Skip to content

Commit 57b1189

Browse files
committed
Added markdown support
1 parent b7f911a commit 57b1189

File tree

6 files changed

+57
-7
lines changed

6 files changed

+57
-7
lines changed

codeview/src/main/java/io/github/kbiakov/codeview/CodeView.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class CodeView : RelativeLayout {
111111
return rvCodeContent.adapter as AbstractCodeAdapter<*>
112112
}
113113
set(adapter) {
114-
delayed { // to prevent UI overhead & initialization inconsistency
114+
delayed { // prevent UI overhead & initialization inconsistency
115115
rvCodeContent.adapter = adapter
116116
state = ViewState.PRESENTED
117117
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.github.kbiakov.codeview.highlight.prettify.lang;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.regex.Pattern;
7+
import io.github.kbiakov.codeview.highlight.prettify.parser.Prettify;
8+
9+
/**
10+
* Registers a language handler for markdown.
11+
*
12+
* @author Kirill Biakov ([email protected])
13+
*/
14+
public class LangMd extends Lang {
15+
16+
public LangMd() {
17+
List<List<Object>> _shortcutStylePatterns = new ArrayList<List<Object>>();
18+
List<List<Object>> _fallthroughStylePatterns = new ArrayList<List<Object>>();
19+
20+
_fallthroughStylePatterns.add(Arrays.asList(new Object[]{Prettify.PR_DECLARATION, Pattern.compile("^#.*?[\\n\\r]")}));
21+
_fallthroughStylePatterns.add(Arrays.asList(new Object[]{Prettify.PR_STRING, Pattern.compile("^```[\\s\\S]*?(?:```|$)")}));
22+
23+
setShortcutStylePatterns(_shortcutStylePatterns);
24+
setFallthroughStylePatterns(_fallthroughStylePatterns);
25+
}
26+
27+
public static List<String> getFileExtensions() {
28+
return Arrays.asList(new String[]{"md", "markdown"});
29+
}
30+
}

codeview/src/main/java/io/github/kbiakov/codeview/highlight/prettify/parser/Prettify.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import io.github.kbiakov.codeview.highlight.prettify.lang.LangLlvm;
3636
import io.github.kbiakov.codeview.highlight.prettify.lang.LangLua;
3737
import io.github.kbiakov.codeview.highlight.prettify.lang.LangMatlab;
38+
import io.github.kbiakov.codeview.highlight.prettify.lang.LangMd;
3839
import io.github.kbiakov.codeview.highlight.prettify.lang.LangMl;
3940
import io.github.kbiakov.codeview.highlight.prettify.lang.LangMumps;
4041
import io.github.kbiakov.codeview.highlight.prettify.lang.LangN;
@@ -377,6 +378,7 @@ public Prettify() {
377378
register(LangLlvm.class);
378379
register(LangLua.class);
379380
register(LangMatlab.class);
381+
register(LangMd.class);
380382
register(LangMl.class);
381383
register(LangMumps.class);
382384
register(LangN.class);

codeview/src/main/res/layout/item_code_line.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
<TextView
1717
android:id="@+id/tv_line_content"
18-
android:layout_width="match_parent"
18+
android:layout_width="wrap_content"
1919
android:layout_height="24dp"
2020
android:layout_marginLeft="16dp"
2121
android:layout_marginRight="16dp"

example/src/main/java/io/github/kbiakov/codeviewexample/ListingsActivity.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
2525
CodeView codeView = (CodeView) findViewById(R.id.code_view);
2626

2727
/**
28-
* 1: Default adapter with chaining build flow
28+
* 1: default adapter with chaining build flow
2929
*/
3030

3131
// use chaining to build view with default adapter
@@ -34,7 +34,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
3434
.setCodeContent(getString(R.string.listing_js));
3535

3636
/**
37-
* 2: Updating built view
37+
* 2: updating built view
3838
*/
3939

4040
// do not use chaining for built view
@@ -50,8 +50,8 @@ public void onCodeLineClicked(int n, @NotNull String line) {
5050
});
5151

5252
/**
53-
* 3: Custom adapter with footer views
54-
*/
53+
* 3: custom adapter with footer views
54+
*
5555
5656
final CustomAdapter adapter = new CustomAdapter(this, getString(R.string.listing_py));
5757
@@ -64,5 +64,6 @@ public void onCodeLineClicked(int n, @NotNull String line) {
6464
adapter.addFooterEntity(n, new CustomAdapter.CustomModel("Line " + (n + 1), line));
6565
}
6666
});
67+
*/
6768
}
6869
}

example/src/main/res/values/strings.xml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,24 @@
6464
return \"\".join(tmp for i in range(10000))\n
6565
\n
6666
for v in range(1,5):\n
67-
print (Timer(\"func()\","from __main__ import case%s as func\" % v).timeit(200))\n
67+
print (Timer(\"func()\",\"from __main__ import case%s as func\" % v).timeit(200))\n
68+
</string>
69+
70+
<string name="listing_md">
71+
3. CodeView and related adapter.\n
72+
\n
73+
## Download\n
74+
Add it in your root ```build.gradle``` at the end of repositories:\n
75+
```groovy\n
76+
allprojects {\n
77+
repositories {\n
78+
...\n
79+
maven { url \"https://jitpack.io\" }\n
80+
}\n
81+
}\n
82+
```\n
83+
\n
84+
Add the dependency:
6885
</string>
6986

7087
<string name="listing_java">

0 commit comments

Comments
 (0)