Skip to content

Commit df38d59

Browse files
author
e16din
committed
Merge remote-tracking branch 'Softwee/master'
# Conflicts: # codeview/src/main/java/io/github/kbiakov/codeview/CodeView.kt # codeview/src/main/java/io/github/kbiakov/codeview/adapters/AbstractCodeA dapter.kt # example/src/main/java/io/github/kbiakov/codeviewexample/ListingsActivity .java
2 parents 5f232a4 + 4fc7fb6 commit df38d59

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+5060
-159
lines changed

README.md

Lines changed: 92 additions & 18 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.0.3'
30+
compile 'com.github.softwee:codeview-android:1.1.2'
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,39 +72,112 @@ 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 MyCodeAdapter adapter = new MyCodeAdapter(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+
adapter.addFooterEntity(16, new MyModel(getString(R.string.py_addition_16), true));
141+
// and this a deletion diff
142+
adapter.addFooterEntity(11, new MyModel(getString(R.string.py_deletion_11), false));
143+
```
144+
<br>
145+
6. You can also add a multiple diff entities:
146+
```java
147+
AbstractCodeAdapter<MyModel>.addFooterEntities(HashMap<Int, List<MyModel>> myEntities)
148+
```
149+
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.
150+
<br>
151+
152+
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.
153+
94154
## How it looks in app
95155
See <a href="https://github.com/Softwee/codeview-android/blob/master/example/src/main/java/io/github/kbiakov/codeviewexample/ListingsActivity.java">example</a>.<br>
96156

97-
[![CodeView_Android_Screenshot.png](https://s10.postimg.org/ckzv9xmm1/Code_View_Android_Screenshot.png)](https://postimg.org/image/6wtkj1i9h/)
157+
[![CodeView_Android_Solarized_light](https://s10.postimg.org/vx3u6q0l5/Screen_Shot_2016_08_31_at_18_41_31.png)](https://s10.postimg.org/vx3u6q0l5/Screen_Shot_2016_08_31_at_18_41_31.png)
158+
[![CodeView_Android_Monokau](https://s10.postimg.org/rmkkxe649/Screen_Shot_2016_08_31_at_18_45_05.png)](https://s10.postimg.org/rmkkxe649/Screen_Shot_2016_08_31_at_18_45_05.png)
159+
[![CodeView_Android_Default](https://s10.postimg.org/u2meb8o6x/Screen_Shot_2016_08_31_at_18_49_33.png)](https://s10.postimg.org/u2meb8o6x/Screen_Shot_2016_08_31_at_18_49_33.png)
98160

99161
## List of available languages & their extensions
100-
C/C++/Objective-C (```"c"```, ```"cc"```, ```"cpp"```, ```"cxx"```, ```"cyc"```, ```"m"```), C# (```"cs"```), Java (```"java"```),Bash (```"bash"```, ```"bsh"```, ```"csh"```, ```"sh"```), Python (```"cv"```, ```"py"```, ```"python"```), Perl (```"perl"```, ```"pl"```, ```"pm"```), Ruby (```"rb"```, ```"ruby"```), JavaScript (```"javascript"```, ```"js"```), CoffeeScript (```"coffee"```), Rust (```"rc"```, ```"rs"```, ```"rust"```), Appollo (```"apollo"```, ```"agc"```, ```"aea"```), Basic (```"basic"```, ```"cbm"```), Clojure (```"clj"```), Css (```"css"```), Dart (```"dart"```), Erlang (```"erlang"```, ```"erl"```), Go (```"go"```), Haskell (```"hs"```), Lisp (```"cl"```, ```"el"```, ```"lisp"```, ```"lsp"```, ```"scm"```, ```"ss"```, ```"rkt"```), Llvm (```"llvm"```, ```"ll"```), Lua (```"lua"```), Matlab (```"matlab"```), ML (OCaml, SML, F#, etc) (```"fs"```, ```"ml"```), Mumps (```"mumps"```), N (```"n"```, ```"nemerle"```), Pascal (```"pascal"```), R (```"r"```, ```"s"```, ```"R"```, ```"S"```, ```"Splus"```), Rd (```"Rd"```, ```"rd"```), Scala (```"scala"```), SQL (```"sql"```), Tex (```"latex"```, ```"tex"```), VB (```"vb"```, ```"vbs"```), VHDL (```"vhdl"```, ```"vhd"```), Tcl (```"tcl"```), Wiki (```"wiki.meta"```), XQuery (```"xq"```, ```"xquery"```), YAML (```"yaml"```, ```"yml"```), formats (```"json"```, ```"xml"```, ```"proto"```), ```"regex"```
162+
C/C++/Objective-C (```"c"```, ```"cc"```, ```"cpp"```, ```"cxx"```, ```"cyc"```, ```"m"```), C# (```"cs"```), Java (```"java"```),Bash (```"bash"```, ```"bsh"```, ```"csh"```, ```"sh"```), Python (```"cv"```, ```"py"```, ```"python"```), Perl (```"perl"```, ```"pl"```, ```"pm"```), Ruby (```"rb"```, ```"ruby"```), JavaScript (```"javascript"```, ```"js"```), CoffeeScript (```"coffee"```), Rust (```"rc"```, ```"rs"```, ```"rust"```), Appollo (```"apollo"```, ```"agc"```, ```"aea"```), Basic (```"basic"```, ```"cbm"```), Clojure (```"clj"```), Css (```"css"```), Dart (```"dart"```), Erlang (```"erlang"```, ```"erl"```), Go (```"go"```), Haskell (```"hs"```), Lisp (```"cl"```, ```"el"```, ```"lisp"```, ```"lsp"```, ```"scm"```, ```"ss"```, ```"rkt"```), Llvm (```"llvm"```, ```"ll"```), Lua (```"lua"```), Matlab (```"matlab"```), ML (OCaml, SML, F#, etc) (```"fs"```, ```"ml"```), Mumps (```"mumps"```), N (```"n"```, ```"nemerle"```), Pascal (```"pascal"```), R (```"r"```, ```"s"```, ```"R"```, ```"S"```, ```"Splus"```), Rd (```"Rd"```, ```"rd"```), Scala (```"scala"```), SQL (```"sql"```), Tex (```"latex"```, ```"tex"```), VB (```"vb"```, ```"vbs"```), VHDL (```"vhdl"```, ```"vhd"```), Tcl (```"tcl"```), Wiki (```"wiki.meta"```), XQuery (```"xq"```, ```"xquery"```), YAML (```"yaml"```, ```"yml"```), Markdown (```"md"```, ```"markdown"```), formats (```"json"```, ```"xml"```, ```"proto"```), ```"regex"```
101163

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

166+
## List of available themes
167+
1. Default (simple light theme)
168+
2. Solarized Light
169+
3. Monokai
170+
171+
## Contribute
172+
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>
173+
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>
174+
3. Various adapters also welcome, customization is unlimited.
175+
104176
## Author
105-
### <a href="https://github.com/kbiakov">Kirill Biakov</a>
177+
### [Kirill Biakov](https://github.com/kbiakov)
106178

107179
## License MIT
180+
```
108181
Copyright (c) 2016 Softwee
109182
110183
Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -124,3 +197,4 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
124197
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
125198
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
126199
SOFTWARE.
200+
```

codeview/build.gradle

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ dependencies {
2626
compile fileTree(dir: 'libs', include: ['*.jar'])
2727
testCompile 'junit:junit:4.12'
2828

29-
compile 'com.android.support:appcompat-v7:24.1.1'
30-
compile 'com.android.support:recyclerview-v7:24.1.1'
31-
compile 'com.github.twalcari:java-prettify:1.2.2'
3229
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
30+
31+
compile 'com.android.support:appcompat-v7:24.2.0'
32+
compile 'com.android.support:recyclerview-v7:24.2.0'
3333
}
3434
repositories {
3535
mavenCentral()
3636
}
37+
buildscript {
38+
}

0 commit comments

Comments
 (0)