Skip to content

Commit 4062719

Browse files
authored
Update README.md
1 parent f68c1e7 commit 4062719

File tree

1 file changed

+82
-16
lines changed

1 file changed

+82
-16
lines changed

README.md

Lines changed: 82 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@ CodeView helps to show code content with syntax highlighting in native way.
88
## Description
99
CodeView contains 3 core parts to implement necessary logic:<br>
1010

11-
1. <b>CodeClassifier</b> is trying to define what language presented in code snippet. It built upon <a href="https://github.com/ptnplanet/Java-Naive-Bayes-Classifier">Naive Bayes classifier</a>. There is no need to work with this class directly & you must just follow instructions below. (Experimental module, may not work properly!)<br>
11+
1. <b>CodeClassifier</b> is trying to define what language presented in code snippet. It built upon [Naive Bayes classifier](https://github.com/ptnplanet/Java-Naive-Bayes-Classifier). There is no need to work with this class directly & you must just follow instructions below. (Experimental module, may not work properly!)<br>
1212

13-
2. For highlighting it uses <b>CodeHighlighter</b>, just highlights your code & returns formatted content. It based on Google Prettify and <a href="https://github.com/twalcari/java-prettify">their fork</a>.<br>
13+
2. For highlighting it uses <b>CodeHighlighter</b>, just highlights your code & returns formatted content. It based on [Google Prettify](https://github.com/google/code-prettify) and their Java implementation & [fork](https://github.com/google/code-prettify).<br>
1414

15-
3. <b>CodeView</b> & related adapter.<br>
15+
3. <b>CodeView</b> & related abstract adapter to provide customization (see below).<br>
1616

1717
## Download
1818
Add it in your root ```build.gradle``` at the end of repositories:
1919
```groovy
2020
allprojects {
21-
repositories {
22-
...
23-
maven { url "https://jitpack.io" }
24-
}
21+
repositories {
22+
...
23+
maven { url "https://jitpack.io" }
24+
}
2525
}
2626
```
2727

2828
Add the dependency:
2929
```groovy
30-
compile 'com.github.softwee:codeview-android:1.1.0'
30+
compile 'com.github.softwee:codeview-android:1.1.1'
3131
```
3232

3333
## Usage
@@ -40,9 +40,9 @@ CodeProcessor.init(this);
4040
Add view for your layout:
4141
```xml
4242
<io.github.kbiakov.codeview.CodeView
43-
android:id="@+id/code_view"
44-
android:layout_width="wrap_content"
45-
android:layout_height="wrap_content"/>
43+
android:id="@+id/code_view"
44+
android:layout_width="wrap_content"
45+
android:layout_height="wrap_content"/>
4646
```
4747

4848
Use chaining syntax when build view:
@@ -72,25 +72,81 @@ or eplixit (see available extensions below):
7272
codeView.highlightCode("js"); // it will work fast!
7373
```
7474

75-
Extend default color theme or create your own (don't forget to open PR with this stuff!):
75+
Extend default color theme:
7676
```java
7777
int myColor = ContextCompat.getColor(this, R.color.code_content_background);
7878
codeView.setColorTheme(ColorTheme.SOLARIZED_LIGHT.withBgContent(myColor));
7979
```
80+
or provide your own (don't forget to open PR with this stuff!)
8081
```java
81-
codeView.setColorTheme(new ColorThemeData(new SyntaxColors(...)));
82+
codeView.setColorTheme(new ColorThemeData(new SyntaxColors(...), ...));
8283
```
8384

8485
Handle user clicks on code lines:
8586
```java
8687
codeView.setCodeListener(new OnCodeLineClickListener() {
8788
@Override
88-
public void onCodeLineClicked(int n) {
89-
// your logic here
89+
public void onCodeLineClicked(int n, @NotNull String line) {
90+
Log.i("ListingsActivity", "On " + (n + 1) + " line clicked");
9091
}
9192
});
9293
```
9394

95+
Enable shadows to hide scrolled content:
96+
```java
97+
codeView.setShadowsEnabled(true);
98+
```
99+
100+
## Adapter customization
101+
Sometimes you may want to add some content under line. You can create your own implementation as follows:
102+
103+
1. Create your model to store data, for example some ```MyModel``` class.<br>
104+
2. Extend ```AbstractCodeAdapter<MyModel>``` typed by your model class.<br>
105+
3. Implement necessary methods in obtained ```MyCodeAdapter<MyModel>```:
106+
```kotlin
107+
// Kotlin
108+
class MyCodeAdapter : AbstractCodeAdapter<MyModel> {
109+
constructor(context: Context, content: String) : super(context, content)
110+
111+
override fun createFooter(context: Context, entity: MyModel, isFirst: Boolean) =
112+
/* init & return your view here */
113+
}
114+
```
115+
```java
116+
// Java
117+
public class MyCodeAdapter extends AbstractCodeAdapter<MyModel> {
118+
public CustomAdapter(@NotNull Context context, @NotNull String content) {
119+
// @see params in AbstractCodeAdapter
120+
super(context, content, true, 10, context.getString(R.string.show_all), null);
121+
}
122+
123+
@NotNull
124+
@Override
125+
public View createFooter(@NotNull Context context, CustomModel entity, boolean isFirst) {
126+
return /* init your view here */;
127+
}
128+
}
129+
```
130+
<br>
131+
4. Set custom adapter to your code view:
132+
```java
133+
final CodeWithDiffsAdapter diffsAdapter = new CodeWithDiffsAdapter(this, getString(R.string.listing_py));
134+
codeView.setAdapter(diffsAdapter);
135+
```
136+
<br>
137+
5. Init footer entities to provide mapper from your view to model:
138+
```java
139+
// it will add an addition diff to code line
140+
diffsAdapter.addFooterEntity(16, new DiffModel(getString(R.string.py_addition_16), true));
141+
// and this a deletion diff
142+
diffsAdapter.addFooterEntity(11, new DiffModel(getString(R.string.py_deletion_11), false));
143+
```
144+
<br>
145+
6. You can also add a multiple diff entities, see ```AbstractCodeAdapter<MyModel>.addFooterEntities(HashMap<Int, List<MyModel>>)``` method). Here you must provide a map from code line numbers (started from 0) to list of line entities. It will be mapped by adapter to specified footer views.
146+
<br>
147+
148+
See [Github diff](https://github.com/Softwee/codeview-android/blob/master/codeview/src/main/java/io/github/kbiakov/codeview/adapters/CodeWithDiffsAdapter.kt) as example of my "best practice" implementation.
149+
94150
## How it looks in app
95151
See <a href="https://github.com/Softwee/codeview-android/blob/master/example/src/main/java/io/github/kbiakov/codeviewexample/ListingsActivity.java">example</a>.<br>
96152

@@ -101,8 +157,18 @@ C/C++/Objective-C (```"c"```, ```"cc"```, ```"cpp"```, ```"cxx"```, ```"cyc"```,
101157

102158
Didn't found yours? Please, open issue to show your interest & I try to add this language in next releases.
103159

160+
## List of available themes
161+
1. Default (simple light theme)
162+
2. Solarized Light
163+
3. Monokai
164+
165+
## Contribute
166+
1. You can add your theme (see [ColorTheme](https://github.com/Softwee/codeview-android/blob/master/codeview/src/main/java/io/github/kbiakov/codeview/highlight/CodeHighlighter.kt) class). Try to add some classic color themes or create your own if it looks cool. You can find many of them in different open-source text editors.<br>
167+
2. If you are strong in a regex add missed language as shown [here](https://github.com/Softwee/codeview-android/blob/master/codeview/src/main/java/io/github/kbiakov/codeview/highlight/prettify/lang/LangScala.java). You can find existing regex for some language in different sources of js-libraries, etc, which plays the same role.<br>
168+
3. Various adapters also welcome, many use cases are impossible to cover.
169+
104170
## Author
105-
### <a href="https://github.com/kbiakov">Kirill Biakov</a>
171+
### [Kirill Biakov](https://github.com/kbiakov)
106172

107173
## License MIT
108174
Copyright (c) 2016 Softwee

0 commit comments

Comments
 (0)