Skip to content

Commit 0c1c709

Browse files
authored
Merge pull request #24 from Softwee/develop
Revised API & improved performance
2 parents b91f141 + 3531978 commit 0c1c709

File tree

15 files changed

+453
-491
lines changed

15 files changed

+453
-491
lines changed

README.md

+60-45
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
[![Release](https://jitpack.io/v/softwee/codeview-android.svg)](https://jitpack.io/#softwee/codeview-android)
55
[![Build Status](https://travis-ci.org/Softwee/codeview-android.svg?branch=master)](https://travis-ci.org/Softwee/codeview-android)
66

7-
CodeView helps to show code content with syntax highlighting in native way.
7+
<b>CodeView</b> helps to show code content with syntax highlighting in native way.
88

99
## Description
10-
CodeView contains 3 core parts to implement necessary logic:<br>
10+
<b>CodeView</b> contains 3 core parts to implement necessary logic:<br>
1111

12-
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>
12+
1. <b>CodeView</b> & related abstract adapter to provide options & customization (see below).<br>
1313

1414
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>
1515

16-
3. <b>CodeView</b> & related abstract adapter to provide customization (see below).<br>
16+
3. <b>CodeClassifier</b> is trying to define what language presented in code snippet. It built using [Naive Bayes classifier](https://en.wikipedia.org/wiki/Naive_Bayes_classifier) upon found open-source [implementation](https://github.com/ptnplanet/Java-Naive-Bayes-Classifier), which I rewrote in Kotlin. There is no need to work with this class directly & you must just follow instructions below. (Experimental module, may not work properly!)<br>
1717

1818
## Download
1919
Add it in your root ```build.gradle``` at the end of repositories:
@@ -28,7 +28,7 @@ allprojects {
2828

2929
Add the dependency:
3030
```groovy
31-
compile 'com.github.softwee:codeview-android:1.1.2'
31+
compile 'com.github.softwee:codeview-android:1.2.0'
3232
```
3333

3434
## Usage
@@ -38,72 +38,87 @@ If you want to use code classifier to auto language recognizing just add to your
3838
CodeProcessor.init(this);
3939
```
4040

41-
Add view for your layout:
41+
Having done ones on app start you can classify language for different snippets more faster, because algorithm needs time for training on sets for presented listings of languages which library has.
42+
43+
Add view to your layout & bind as usual:
4244
```xml
4345
<io.github.kbiakov.codeview.CodeView
4446
android:id="@+id/code_view"
4547
android:layout_width="wrap_content"
4648
android:layout_height="wrap_content"/>
4749
```
48-
49-
Use chaining syntax when build view:
5050
```java
5151
CodeView codeView = (CodeView) findViewById(R.id.code_view);
52-
53-
codeView.highlightCode("js")
54-
.setColorTheme(ColorTheme.SOLARIZED_LIGHT.withBgContent(myColor))
55-
.setCodeContent(getString(R.string.listing_js));
5652
```
5753

58-
And perform actions sequentially when view built:
54+
So now you can set code using implicit form:
5955
```java
60-
codeView.setCodeContent(getString(R.string.listing_java));
61-
codeView.highlightCode("java");
56+
// auto language recognition
57+
codeView.setCode(getString(R.string.listing_js));
6258
```
6359

64-
You can use both forms for build & built view, but note: ```setCodeContent(String)``` is final step when you build your view, otherwise not. If you firstly highlight and then set code content, code will not be highlighted if view was not built yet. Instructions above helps you to avoid errors. View has state to handle this behavior.
65-
66-
## Customizing
67-
Use implicit form to code highlighting:
60+
Or explicit (see available extensions below):
6861
```java
69-
codeView.highlightCode();
62+
// will work faster!
63+
codeView.setCode(getString(R.string.listing_py), "py");
7064
```
71-
or eplixit (see available extensions below):
65+
66+
## Customization
67+
When you call ```setCode(...)``` view will prepared with default params if view was not initialized before. So if you want some customization, it can be done using options and/or adapter.
68+
69+
### Initialization
70+
You can initialize view with options:
7271
```java
73-
codeView.highlightCode("js"); // it will work fast!
72+
codeView.setOptions(Options.Default.get(this)
73+
.withLanguage("python")
74+
.withCode(R.string.listing_py)
75+
.withTheme(ColorTheme.MONOKAI));
7476
```
7577

76-
Use default color theme:
78+
Or using adapter (see <b>Adapter</b> or example for more details):
7779
```java
78-
codeView.setColorTheme(ColorTheme.SOLARIZED_LIGHT);
80+
final CustomAdapter myAdapter = new CustomAdapter(this, getString(R.string.listing_md));
81+
codeView.setAdapter(myAdapter);
7982
```
80-
or extend default:
83+
84+
<b>Note:</b> Each <b>CodeView</b> has adapter and each adapter has options. When calling ```setOptions(...)``` or ```setAdapter(...)``` current adapter "flushed" with current options. If you want to save the state and just update options saving adapter or set adapter saving options you must call ```updateOptions(...)``` or ```updateAdapter(...)``` accordingly.
85+
86+
### Options
87+
Options helps to easily set necessary params, such as code & language, color theme, shortcut params (max lines, note), code line click listener. Some params are unnecessary.
88+
89+
When view initialized (options or adapter are set) you can manipulate options in various ways:
8190
```java
82-
int myColor = ContextCompat.getColor(this, R.color.my_color);
83-
codeView.setColorTheme(ColorTheme.MONOKAI.withBgContent(myColor));
91+
codeView.getOptions()
92+
.withCode(R.string.listing_java)
93+
.withLanguage("java")
94+
.withTheme(ColorTheme.MONOKAI);
8495
```
85-
or provide your own (don't forget to open PR with this stuff!)
96+
97+
### Color theme
98+
There are some default themes (see full list below):
8699
```java
87-
codeView.setColorTheme(new ColorThemeData(new SyntaxColors(...), ...));
100+
codeView.getOptions().setTheme(ColorTheme.SOLARIZED_LIGHT);
88101
```
89102

90-
Handle user clicks on code lines:
103+
But you can build your own from existing one:
91104
```java
92-
codeView.setCodeListener(new OnCodeLineClickListener() {
93-
@Override
94-
public void onCodeLineClicked(int n, @NotNull String line) {
95-
Log.i("ListingsActivity", "On " + (n + 1) + " line clicked");
96-
}
97-
});
105+
ColorThemeData myTheme = ColorTheme.SOLARIZED_LIGHT.theme()
106+
.withBgContent(android.R.color.black)
107+
.withNoteColor(android.R.color.white);
108+
109+
codeView.getOptions().setTheme(myTheme);
98110
```
99111

100-
Enable shadows to hide scrolled content:
112+
Or create your own from scratch (don't forget to open PR with this stuff!):
101113
```java
102-
codeView.setShadowsEnabled(true);
114+
ColorThemeData customTheme = new ColorThemeData(new SyntaxColors(...), ...);
115+
codeView.getOptions().setTheme(customTheme);
103116
```
104117

105-
## Adapter customization
106-
Sometimes you may want to add some content under line. You can create your own implementation as follows:
118+
### Adapter
119+
Sometimes you may want to take code lines under your control, and that's why you need <b>Adapter</b>.
120+
121+
You can create your own implementation as follows:
107122

108123
1. Create your model to store data, for example some ```MyModel``` class.<br>
109124
2. Extend ```AbstractCodeAdapter<MyModel>``` typed by your model class.<br>
@@ -169,14 +184,14 @@ C/C++/Objective-C (```"c"```, ```"cc"```, ```"cpp"```, ```"cxx"```, ```"cyc"```,
169184
Didn't found yours? Please, open issue to show your interest & I'll try to add this language in next releases.
170185

171186
## List of available themes
172-
1. Default (simple light theme)
173-
2. Solarized Light
174-
3. Monokai
187+
1. Default (simple light theme).
188+
2. Solarized Light.
189+
3. Monokai.
175190

176191
## Contribute
177192
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>
178-
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>
179-
3. Various adapters also welcome, customization is unlimited.
193+
2. If you are strong in 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 libraries, which plays the same role.<br>
194+
3. Various adapters also welcome.
180195

181196
## Author
182197
### [Kirill Biakov](https://github.com/kbiakov)

codeview/build.gradle

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ apply plugin: 'com.android.library'
22
apply plugin: 'kotlin-android'
33

44
android {
5-
compileSdkVersion 24
6-
buildToolsVersion "24.0.2"
5+
compileSdkVersion 25
6+
buildToolsVersion "25.0.1"
77

88
defaultConfig {
99
minSdkVersion 15
10-
targetSdkVersion 24
10+
targetSdkVersion 25
1111
versionCode 1
1212
versionName "1.0"
1313
}
@@ -25,8 +25,8 @@ android {
2525
dependencies {
2626
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
2727

28-
compile 'com.android.support:appcompat-v7:24.2.0'
29-
compile 'com.android.support:recyclerview-v7:24.2.0'
28+
compile 'com.android.support:appcompat-v7:25.0.1'
29+
compile 'com.android.support:recyclerview-v7:25.0.1'
3030
}
3131
repositories {
3232
mavenCentral()

codeview/src/main/assets/training-set/javascript/new 3.txt

+16-16
Original file line numberDiff line numberDiff line change
@@ -67,32 +67,32 @@ var minExpectedLifetime = '20s';
6767
* @api public
6868
*/
6969

70-
function UpServer (server, file, opts) {
71-
if (this == global) return new UpServer(server, file, opts);
70+
function UpServer (server, file, options) {
71+
if (this == global) return new UpServer(server, file, options);
7272

7373
Distributor.call(this, server);
7474

7575
var self = this;
76-
opts = opts || {};
76+
options = options || {};
7777

7878
this.file = file;
79-
this.numWorkers = eq(opts.numWorkers || numWorkers, { cpus: cpus });
80-
this.workerTimeout = ms(null != opts.workerTimeout
81-
? opts.workerTimeout : workerTimeout);
82-
this.requires = opts.requires || [];
83-
this.assumeReady = opts.assumeReady === undefined ? true : !!opts.assumeReady;
84-
this.keepAlive = opts.keepAlive || false;
85-
this.minExpectedLifetime = ms(opts.minExpectedLifetime != null ? opts.minExpectedLifetime : minExpectedLifetime);
86-
if (false !== opts.workerPingInterval) {
87-
this.workerPingInterval = ms(opts.workerPingInterval || '1m');
79+
this.numWorkers = eq(options.numWorkers || numWorkers, { cpus: cpus });
80+
this.workerTimeout = ms(null != options.workerTimeout
81+
? options.workerTimeout : workerTimeout);
82+
this.requires = options.requires || [];
83+
this.assumeReady = options.assumeReady === undefined ? true : !!options.assumeReady;
84+
this.keepAlive = options.keepAlive || false;
85+
this.minExpectedLifetime = ms(options.minExpectedLifetime != null ? options.minExpectedLifetime : minExpectedLifetime);
86+
if (false !== options.workerPingInterval) {
87+
this.workerPingInterval = ms(options.workerPingInterval || '1m');
8888
}
8989

9090
this.workers = [];
9191
this.spawning = [];
9292
this.lastIndex = -1;
9393

94-
if (opts.title) {
95-
this.title = opts.title;
94+
if (options.title) {
95+
this.title = options.title;
9696
process.title = this.title + ' master';
9797
}
9898

@@ -290,15 +290,15 @@ function Worker (server) {
290290
this.server = server;
291291
this.readyState = 'spawning';
292292

293-
var opts = JSON.stringify({
293+
var options = JSON.stringify({
294294
file: server.file
295295
, requires: server.requires
296296
, assumeReady: server.assumeReady
297297
, pingInterval: server.workerPingInterval
298298
, title: server.title
299299
});
300300

301-
this.proc = fork(__dirname + '/worker.js', [opts], { env: process.env });
301+
this.proc = fork(__dirname + '/worker.js', [options], { env: process.env });
302302
this.proc.on('message', this.onMessage.bind(this));
303303
this.proc.on('exit', this.onExit.bind(this));
304304
this.pid = this.proc.pid;

0 commit comments

Comments
 (0)