Skip to content

Commit

Permalink
Merge pull request #48 from hossain-khan/develop
Browse files Browse the repository at this point in the history
Sync `main`
  • Loading branch information
hossain-khan authored Jan 2, 2024
2 parents fe1b5fe + c05d579 commit 21dcc54
Show file tree
Hide file tree
Showing 51 changed files with 390 additions and 257 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: set up JDK 1.8
uses: actions/setup-java@v1
- uses: actions/checkout@v4
# https://github.com/actions/setup-java
- uses: actions/setup-java@v4
with:
java-version: 1.8
distribution: 'zulu' # See 'Supported distributions' for available options
java-version: '17'
- name: Build with Gradle
run: ./gradlew build
6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/migrations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions .idea/runConfigurations.xml

This file was deleted.

36 changes: 30 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# android-syntax-highlighter
yet another android syntax highlighter (YAASH)
yet _NOT_ another android syntax highlighter (YNAASH)

### Objective
Explore well established web based syntax highlighter like [PrismJS](https://prismjs.com/) and [highlight.js](https://highlightjs.org/),
Expand All @@ -9,25 +9,49 @@ and showcase how anybody can quickly incorporate these into their project by fol
> The intention is **NOT** to create another library project that gets abandoned over time.
Feel free to copy parts of code that is necessary for you to add syntax highlighting support to your app.

* Try out the latest [release](https://github.com/amardeshbd/android-syntax-highlighter/releases) - `syntax-highlighter-example-v1.1.apk` _(2.5 MB)_ [[Download](https://github.com/amardeshbd/android-syntax-highlighter/releases/download/v1.1/syntax-highlighter-example-v1.1.apk)]


## Existing Syntax Highlighting Libraries

If you need a library, you may look into following existing projects

1. [CodeView-Android](https://github.com/kbiakov/CodeView-Android) - Display code with syntax highlighting ✨ in native way.
703 :star:, Last updated: Jan 24, 2019
845 :star:, Last updated: Jan 24, 2019
1. [highlightjs-android](https://github.com/PDDStudio/highlightjs-android) - A view for source code syntax highlighting on Android.
283 :star:, Last updated: Sep 8, 2018
310 :star:, Last updated: Aug 19, 2020
1. [Syntax-View-Android](https://github.com/Badranh/Syntax-View-Android) - Beautiful Android Syntax View with line counter it will automatically highlight the code.
42 :star:, Last updated: Mar 24, 2020
56 :star:, Last updated: Mar 24, 2020
1. [KodeEditor](https://github.com/markusressel/KodeEditor) - A simple code editor with syntax highlighting and pinch to zoom.
72 :star:, Last updated: May 19, 2023
1. [HighlightJs View - Android](https://github.com/PDDStudio/highlightjs-android) - A view for source code syntax highlighting on Android.
310 :star:, Last updated: Aug 19, 2020
1. [synta kt s](https://github.com/wingio/syntakts) - Simple to use text parser and syntax highlighter for Kotlin Multiplatform.
7 :star:, Last updated: Nov 11, 2023 _(Actively beind developed with KMP focus)_

> _NOTE: The 'Last updated' and :star: data was taken as of July 16th, 2020_
> _NOTE: The 'Last updated' and :star: data was taken as of Nov 13th, 2023_
------------------------


- [Under the hood](#under-the-hood)
- [1. Choose JS Library](#1-choose-js-library)
- [2. Use HTML+CSS+JS Asset](#2-use-htmlcssjs-asset)
- [3. Load the static HTML on `WebView`](#3-load-the-static-html-on-webview)
- [Example App Screenshots](#screenshot)
- [Building your own Fragment or Custom View](#building-your-own-fragment-or-custom-view)
- [Custom View](#custom-view)
- [PrismJS Template Function](#prismjs-template-function)
- [Creating custom syntax highlighter WebView](#creating-custom-syntax-highlighter-webview)
- [Use custom view from Fragment or Activity](#use-custom-view-from-fragment-or-activity)
- [Fragment](#fragment)
- [Create custom Syntax Highlighter Fragment](#create-custom-syntax-highlighter-fragment)
- [Using the Syntax Highlighter Fragment](#using-the-syntax-highlighter-fragment)

## Under the hood
Here is how you would have syntax highlighting using any modern JavaScript library.

> ps. I also ✍️ wrote a [short blog](https://medium.com/@hossainkhan/source-code-syntax-highlighting-on-android-taking-full-control-b704fd4bd8ee) summarizing the process on Medium.com
### 1. Choose JS Library
There are several popular syntax highlighters. Here I have used Prism JS because it's light weight and one of the popular one.

Expand Down
14 changes: 7 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.3.72"
ext.kotlin_version = "1.9.22"
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.0"
classpath 'com.android.tools.build:gradle:8.2.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand All @@ -17,7 +17,7 @@ buildscript {
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}

Expand All @@ -30,9 +30,9 @@ task clean(type: Delete) {
ext {
// Sdk and tools
// --------------------------------------------------
androidMinSdkVersion = 21 // Lollipop 5.0
androidTargetSdkVersion = 29 // Android 10
androidCompileSdkVersion = 29
androidMinSdkVersion = 29 // Android 10
androidTargetSdkVersion = 34 // Android 14
androidCompileSdkVersion = 34
// Android SDK Build Tools - Versions: https://developer.android.com/studio/releases/build-tools.html
buildToolsVersion = '29.0.3'

Expand Down
30 changes: 18 additions & 12 deletions example/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
plugins {
id 'com.android.application'
id 'kotlin-android'
}

android {
compileSdkVersion rootProject.ext.androidCompileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion

buildFeatures {
viewBinding true
buildConfig true
}

defaultConfig {
applicationId "dev.hossain.yaash.example"
applicationId "dev.hossain.ynaash.example"
minSdkVersion rootProject.ext.androidMinSdkVersion
targetSdkVersion rootProject.ext.androidTargetSdkVersion
versionCode 2
Expand All @@ -30,16 +35,17 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
namespace 'dev.hossain.ynaash.example'
}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation project(':highlighter')

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.core:core-ktx:1.12.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

// https://developer.android.com/jetpack/androidx/releases/cardview
implementation "androidx.cardview:cardview:1.0.0"
Expand All @@ -48,13 +54,13 @@ dependencies {
// - https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView
// - https://developer.android.com/guide/topics/ui/layout/recyclerview
// - https://developer.android.com/jetpack/androidx/releases/recyclerview
implementation "androidx.recyclerview:recyclerview:1.1.0"
implementation "androidx.recyclerview:recyclerview:1.3.2"

// Timber for logging
implementation "com.jakewharton.timber:timber:4.7.1"

testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.hossain.yaash.example
package dev.hossain.ynaash.example

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
Expand All @@ -19,6 +19,6 @@ class ExampleInstrumentedTest {
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("dev.hossain.yaash.example", appContext.packageName)
assertEquals("dev.hossain.ynaash.example", appContext.packageName)
}
}
6 changes: 3 additions & 3 deletions example/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dev.hossain.yaash.example">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:name=".SampleApp"
Expand All @@ -10,7 +9,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ui.MainActivity">
<activity android:name=".ui.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.hossain.yaash.example
package dev.hossain.ynaash.example

import android.app.Application
import timber.log.Timber
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package dev.hossain.yaash.example.ui
package dev.hossain.ynaash.example.ui

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import dev.hossain.yaash.example.R
import dev.hossain.yaash.example.ui.demohighlightjs.HighlightJsDemoActivity
import dev.hossain.yaash.example.ui.demoprismjs.PrismJsDemoActivity
import dev.hossain.ynaash.example.R
import dev.hossain.ynaash.example.ui.demohighlightjs.HighlightJsDemoActivity
import dev.hossain.ynaash.example.ui.demoprismjs.PrismJsDemoActivity

/**
* Main activity to showcase both fragment based and custom view based syntax highlighting.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.hossain.yaash.example.ui.common
package dev.hossain.ynaash.example.ui.common

import android.content.Context
import android.content.Intent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.hossain.yaash.example.ui.common
package dev.hossain.ynaash.example.ui.common

object SampleSourceCode {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package dev.hossain.yaash.example.ui.demohighlightjs
package dev.hossain.ynaash.example.ui.demohighlightjs

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import dev.hossain.yaash.example.R
import dev.hossain.yaash.example.ui.common.SampleSourceCode
import dev.hossain.yaash.highlightjs.SyntaxHighlighterFragment
import dev.hossain.yaash.highlightjs.SyntaxHighlighterWebView
import dev.hossain.ynaash.example.R
import dev.hossain.ynaash.example.ui.common.SampleSourceCode
import dev.hossain.ynaash.highlightjs.SyntaxHighlighterFragment
import dev.hossain.ynaash.highlightjs.SyntaxHighlighterWebView

/**
* Demo activity to showcase both fragment based and custom view based
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package dev.hossain.yaash.example.ui.demoprismjs
package dev.hossain.ynaash.example.ui.demoprismjs

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import dev.hossain.yaash.example.R
import dev.hossain.yaash.example.ui.common.SampleSourceCode
import dev.hossain.yaash.prismjs.SyntaxHighlighterFragment
import dev.hossain.yaash.prismjs.SyntaxHighlighterWebView
import dev.hossain.ynaash.example.R
import dev.hossain.ynaash.example.ui.common.SampleSourceCode
import dev.hossain.ynaash.prismjs.SyntaxHighlighterFragment
import dev.hossain.ynaash.prismjs.SyntaxHighlighterWebView

/**
* Demo activity to showcase both fragment based and custom view based
Expand Down
2 changes: 1 addition & 1 deletion example/src/main/res/layout/activity_demo_highlightjs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
android:layout_marginTop="4dp"
android:text="Syntax Highlighting using Custom View" />

<dev.hossain.yaash.highlightjs.SyntaxHighlighterWebView
<dev.hossain.ynaash.highlightjs.SyntaxHighlighterWebView
android:id="@+id/syntax_highlighter_webview"
android:layout_width="match_parent"
android:layout_height="0dp"
Expand Down
2 changes: 1 addition & 1 deletion example/src/main/res/layout/activity_demo_prismjs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
android:layout_marginTop="4dp"
android:text="Syntax Highlighting using Custom View" />

<dev.hossain.yaash.prismjs.SyntaxHighlighterWebView
<dev.hossain.ynaash.prismjs.SyntaxHighlighterWebView
android:id="@+id/syntax_highlighter_webview"
android:layout_width="match_parent"
android:layout_height="0dp"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.hossain.yaash.example
package dev.hossain.ynaash.example

import org.junit.Assert.assertEquals
import org.junit.Test
Expand Down
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
kotlin.code.style=official
android.nonTransitiveRClass=false
android.nonFinalResIds=false
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 1 addition & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Thu Jul 16 13:06:36 EDT 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
Loading

0 comments on commit 21dcc54

Please sign in to comment.