Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 60 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
[![Release](https://jitpack.io/v/softwee/codeview-android.svg)](https://jitpack.io/#softwee/codeview-android)
[![Build Status](https://travis-ci.org/Softwee/codeview-android.svg?branch=master)](https://travis-ci.org/Softwee/codeview-android)

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

## Description
CodeView contains 3 core parts to implement necessary logic:<br>
<b>CodeView</b> contains 3 core parts to implement necessary logic:<br>

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

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>

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

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

Add the dependency:
```groovy
compile 'com.github.softwee:codeview-android:1.1.2'
compile 'com.github.softwee:codeview-android:1.2.0'
```

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

Add view for your layout:
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.

Add view to your layout & bind as usual:
```xml
<io.github.kbiakov.codeview.CodeView
android:id="@+id/code_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
```

Use chaining syntax when build view:
```java
CodeView codeView = (CodeView) findViewById(R.id.code_view);

codeView.highlightCode("js")
.setColorTheme(ColorTheme.SOLARIZED_LIGHT.withBgContent(myColor))
.setCodeContent(getString(R.string.listing_js));
```

And perform actions sequentially when view built:
So now you can set code using implicit form:
```java
codeView.setCodeContent(getString(R.string.listing_java));
codeView.highlightCode("java");
// auto language recognition
codeView.setCode(getString(R.string.listing_js));
```

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.

## Customizing
Use implicit form to code highlighting:
Or explicit (see available extensions below):
```java
codeView.highlightCode();
// will work faster!
codeView.setCode(getString(R.string.listing_py), "py");
```
or eplixit (see available extensions below):

## Customization
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.

### Initialization
You can initialize view with options:
```java
codeView.highlightCode("js"); // it will work fast!
codeView.setOptions(Options.Default.get(this)
.withLanguage("python")
.withCode(R.string.listing_py)
.withTheme(ColorTheme.MONOKAI));
```

Use default color theme:
Or using adapter (see <b>Adapter</b> or example for more details):
```java
codeView.setColorTheme(ColorTheme.SOLARIZED_LIGHT);
final CustomAdapter myAdapter = new CustomAdapter(this, getString(R.string.listing_md));
codeView.setAdapter(myAdapter);
```
or extend default:

<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.

### Options
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.

When view initialized (options or adapter are set) you can manipulate options in various ways:
```java
int myColor = ContextCompat.getColor(this, R.color.my_color);
codeView.setColorTheme(ColorTheme.MONOKAI.withBgContent(myColor));
codeView.getOptions()
.withCode(R.string.listing_java)
.withLanguage("java")
.withTheme(ColorTheme.MONOKAI);
```
or provide your own (don't forget to open PR with this stuff!)

### Color theme
There are some default themes (see full list below):
```java
codeView.setColorTheme(new ColorThemeData(new SyntaxColors(...), ...));
codeView.getOptions().setTheme(ColorTheme.SOLARIZED_LIGHT);
```

Handle user clicks on code lines:
But you can build your own from existing one:
```java
codeView.setCodeListener(new OnCodeLineClickListener() {
@Override
public void onCodeLineClicked(int n, @NotNull String line) {
Log.i("ListingsActivity", "On " + (n + 1) + " line clicked");
}
});
ColorThemeData myTheme = ColorTheme.SOLARIZED_LIGHT.theme()
.withBgContent(android.R.color.black)
.withNoteColor(android.R.color.white);

codeView.getOptions().setTheme(myTheme);
```

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

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

You can create your own implementation as follows:

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

## List of available themes
1. Default (simple light theme)
2. Solarized Light
3. Monokai
1. Default (simple light theme).
2. Solarized Light.
3. Monokai.

## Contribute
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>
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>
3. Various adapters also welcome, customization is unlimited.
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>
3. Various adapters also welcome.

## Author
### [Kirill Biakov](https://github.com/kbiakov)
Expand Down
10 changes: 5 additions & 5 deletions codeview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
compileSdkVersion 25
buildToolsVersion "25.0.1"

defaultConfig {
minSdkVersion 15
targetSdkVersion 24
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
Expand All @@ -25,8 +25,8 @@ android {
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:recyclerview-v7:24.2.0'
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:recyclerview-v7:25.0.1'
}
repositories {
mavenCentral()
Expand Down
32 changes: 16 additions & 16 deletions codeview/src/main/assets/training-set/javascript/new 3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,32 +67,32 @@ var minExpectedLifetime = '20s';
* @api public
*/

function UpServer (server, file, opts) {
if (this == global) return new UpServer(server, file, opts);
function UpServer (server, file, options) {
if (this == global) return new UpServer(server, file, options);

Distributor.call(this, server);

var self = this;
opts = opts || {};
options = options || {};

this.file = file;
this.numWorkers = eq(opts.numWorkers || numWorkers, { cpus: cpus });
this.workerTimeout = ms(null != opts.workerTimeout
? opts.workerTimeout : workerTimeout);
this.requires = opts.requires || [];
this.assumeReady = opts.assumeReady === undefined ? true : !!opts.assumeReady;
this.keepAlive = opts.keepAlive || false;
this.minExpectedLifetime = ms(opts.minExpectedLifetime != null ? opts.minExpectedLifetime : minExpectedLifetime);
if (false !== opts.workerPingInterval) {
this.workerPingInterval = ms(opts.workerPingInterval || '1m');
this.numWorkers = eq(options.numWorkers || numWorkers, { cpus: cpus });
this.workerTimeout = ms(null != options.workerTimeout
? options.workerTimeout : workerTimeout);
this.requires = options.requires || [];
this.assumeReady = options.assumeReady === undefined ? true : !!options.assumeReady;
this.keepAlive = options.keepAlive || false;
this.minExpectedLifetime = ms(options.minExpectedLifetime != null ? options.minExpectedLifetime : minExpectedLifetime);
if (false !== options.workerPingInterval) {
this.workerPingInterval = ms(options.workerPingInterval || '1m');
}

this.workers = [];
this.spawning = [];
this.lastIndex = -1;

if (opts.title) {
this.title = opts.title;
if (options.title) {
this.title = options.title;
process.title = this.title + ' master';
}

Expand Down Expand Up @@ -290,15 +290,15 @@ function Worker (server) {
this.server = server;
this.readyState = 'spawning';

var opts = JSON.stringify({
var options = JSON.stringify({
file: server.file
, requires: server.requires
, assumeReady: server.assumeReady
, pingInterval: server.workerPingInterval
, title: server.title
});

this.proc = fork(__dirname + '/worker.js', [opts], { env: process.env });
this.proc = fork(__dirname + '/worker.js', [options], { env: process.env });
this.proc.on('message', this.onMessage.bind(this));
this.proc.on('exit', this.onExit.bind(this));
this.pid = this.proc.pid;
Expand Down
Loading