Skip to content

Commit f81cbcc

Browse files
authored
Merge pull request #11 from amardeshbd/develop
Develop -> Main
2 parents 7126b48 + 379c61d commit f81cbcc

File tree

70 files changed

+1683
-0
lines changed

Some content is hidden

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

70 files changed

+1683
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,4 @@ lint/generated/
8383
lint/outputs/
8484
lint/tmp/
8585
# lint/reports/
86+
.DS_Store

.idea/.name

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/codeStyles/Project.xml

Lines changed: 123 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/codeStyles/codeStyleConfig.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/jarRepositories.xml

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations.xml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,58 @@ If you need a library, you may look into following existing projects
1919
42 :star:, Last updated: Mar 24, 2020
2020

2121
> _NOTE: The 'Last updated' and :star: data was taken as of July 16th, 2020_
22+
23+
------------------------
24+
25+
## Under the hood
26+
Here is how you would have syntax highlighting using any modern JavaScript library.
27+
28+
### 1. Choose JS Library
29+
There are several popular syntax highlighters. Here I have used Prism JS because it's light weight and one of the popular one.
30+
31+
Follow their documentation to download the library with the plugins you need. For example, showing line number is a plugin, that is how they can keep the library so light weight like `2KB` core.
32+
33+
### 2. Use HTML+CSS+JS Asset
34+
Move downloaded `prism.js` and `prism.css` to `assets` resource directory. For example, here I have moved them to "[assets/www](https://github.com/amardeshbd/android-syntax-highlighter/tree/develop/highlighter/src/main/assets/www)" folder.
35+
36+
Write plain HTML that loads these assets and your source code.
37+
38+
For example:
39+
```html
40+
<!DOCTYPE html>
41+
<html>
42+
<head>
43+
<meta name="viewport" content="width=device-width, initial-scale=1">
44+
<link href="www/prism.css" rel="stylesheet"/>
45+
<script src="www/prism.js"></script>
46+
</head>
47+
<body>
48+
<h1>Demo Syntax Highlight</h1>
49+
<p>Description about the code.</p>
50+
<pre class="line-numbers">
51+
<code class="language-kotlin">class MainActivity : AppCompatActivity() { /* ... */ }
52+
</code>
53+
</pre>
54+
</body>
55+
</html>
56+
```
57+
58+
> NOTE: For most cases, hard coding sample code for each sample-code is not ideal.
59+
> Soon, we will explore how to make the HTML file as template and inject source code from Activity or Fragment.
60+
61+
### 3. Load the static HTML on `WebView`
62+
Finally on your Activity or Fragment, once view is loaded initialize `WebView` with local html file from `assets`.
63+
64+
```kotlin
65+
webView.apply {
66+
settings.javaScriptEnabled = true
67+
webChromeClient = WebViewChromeClient()
68+
webViewClient = AppWebViewClient()
69+
loadUrl("file:///android_asset/code-highlight.html")
70+
}
71+
```
72+
73+
#### Screenshot
74+
Here is a screenshot taken from a demo static html page that has syntax highlighting using Prism JS.
75+
76+
<img width="200" src="https://user-images.githubusercontent.com/99822/87729492-809b4200-c793-11ea-8bfd-810359a11663.png">

build.gradle

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2+
buildscript {
3+
ext.kotlin_version = "1.3.72"
4+
repositories {
5+
google()
6+
jcenter()
7+
}
8+
dependencies {
9+
classpath "com.android.tools.build:gradle:4.0.0"
10+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
11+
12+
// NOTE: Do not place your application dependencies here; they belong
13+
// in the individual module build.gradle files
14+
}
15+
}
16+
17+
allprojects {
18+
repositories {
19+
google()
20+
jcenter()
21+
}
22+
}
23+
24+
task clean(type: Delete) {
25+
delete rootProject.buildDir
26+
}
27+
28+
29+
// Define different versions in a single place
30+
ext {
31+
// Sdk and tools
32+
// --------------------------------------------------
33+
androidMinSdkVersion = 21 // Lollipop 5.0
34+
androidTargetSdkVersion = 29 // Android 10
35+
androidCompileSdkVersion = 29
36+
// Android SDK Build Tools - Versions: https://developer.android.com/studio/releases/build-tools.html
37+
buildToolsVersion = '29.0.3'
38+
39+
// Google Products & Support library dependencies
40+
// --------------------------------------------------
41+
// https://developer.android.com/topic/libraries/support-library/revisions.html
42+
appCompatVersion = '1.1.0'
43+
coreKtxVersion = '1.3.0'
44+
// https://developer.android.com/reference/android/support/constraint/ConstraintLayout
45+
constraintLayoutVersion = '1.1.3'
46+
// https://developer.android.com/topic/libraries/architecture/adding-components
47+
// https://developer.android.com/jetpack/androidx/releases/lifecycle
48+
archComponentVersion = '2.0.0'
49+
50+
51+
// Unit test dependencies
52+
// --------------------------------------------------
53+
junitVersion = '4.13'
54+
espressoCoreVersion = '3.2.0'
55+
56+
// 3rd party library dependencies
57+
// --------------------------------------------------
58+
timberLibraryVersion = '4.7.1' // https://github.com/JakeWharton/timber
59+
}

example/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

0 commit comments

Comments
 (0)