Skip to content

Commit 071bfbc

Browse files
authored
Merge pull request #13 from amardeshbd/develop
Ready for release 1.0 tagging
2 parents f81cbcc + d19d583 commit 071bfbc

File tree

6 files changed

+154
-18
lines changed

6 files changed

+154
-18
lines changed

example/src/main/java/dev/hossain/yaash/example/MainActivity.kt

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,27 @@ package dev.hossain.yaash.example
22

33
import android.os.Bundle
44
import androidx.appcompat.app.AppCompatActivity
5-
import dev.hossain.yaash.prismjs.ShowSourceCodeFragment
5+
import dev.hossain.yaash.prismjs.SyntaxHighlighterFragment
6+
import dev.hossain.yaash.prismjs.SyntaxHighlighterWebView
67

8+
/**
9+
* Main activity to showcase both fragment based and custom view based syntax highlighting.
10+
*
11+
* @see SyntaxHighlighterFragment
12+
* @see SyntaxHighlighterWebView
13+
*/
714
class MainActivity : AppCompatActivity() {
815
override fun onCreate(savedInstanceState: Bundle?) {
916
super.onCreate(savedInstanceState)
1017
setContentView(R.layout.activity_main)
1118

1219
loadSourceCodeFragment()
20+
loadSourceCodeCustomView()
1321
}
1422

1523
private fun loadSourceCodeFragment() {
16-
val fragment = ShowSourceCodeFragment.newInstance(
17-
formattedSourceCode = sourceCode,
24+
val fragment = SyntaxHighlighterFragment.newInstance(
25+
formattedSourceCode = fragmentSourceCode,
1826
language = "kotlin",
1927
showLineNumbers = true
2028
)
@@ -25,8 +33,21 @@ class MainActivity : AppCompatActivity() {
2533
fragmentTransaction.commit()
2634
}
2735

28-
// Sample code for syntax-highlight preview
29-
private val sourceCode = """
36+
private fun loadSourceCodeCustomView() {
37+
val syntaxHighlighter: SyntaxHighlighterWebView = findViewById(R.id.syntax_highlighter_webview)
38+
39+
syntaxHighlighter.bindSyntaxHighlighter(
40+
formattedSourceCode = customViewSourceCode,
41+
language = "kotlin",
42+
showLineNumbers = true
43+
)
44+
}
45+
46+
//
47+
// Sample codes for syntax-highlight preview
48+
//
49+
50+
private val fragmentSourceCode = """
3051
|@SuppressLint("SetJavaScriptEnabled")
3152
|override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
3253
| super.onViewCreated(view, savedInstanceState)
@@ -52,4 +73,25 @@ class MainActivity : AppCompatActivity() {
5273
| }
5374
|}
5475
""".trimMargin()
76+
77+
private val customViewSourceCode = """
78+
|@SuppressLint("SetJavaScriptEnabled")
79+
|fun bindSyntaxHighlighter(
80+
| formattedSourceCode: String,
81+
| language: String,
82+
| showLineNumbers: Boolean = false
83+
|) {
84+
| settings.javaScriptEnabled = true
85+
| webChromeClient = WebViewChromeClient()
86+
| webViewClient = AppWebViewClient()
87+
|
88+
| loadDataWithBaseURL(
89+
| ANDROID_ASSETS_PATH /* baseUrl */,
90+
| prismJsHtmlContent(formattedSourceCode, language, showLineNumbers) /* html-data */,
91+
| "text/html" /* mimeType */,
92+
| "utf-8" /* encoding */,
93+
| "" /* failUrl */
94+
| )
95+
|}
96+
""".trimMargin()
5597
}
Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:app="http://schemas.android.com/apk/res-auto"
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
43
xmlns:tools="http://schemas.android.com/tools"
54
android:layout_width="match_parent"
65
android:layout_height="match_parent"
6+
android:orientation="vertical"
77
tools:context=".MainActivity">
88

9+
<TextView
10+
android:id="@+id/title_fragment_sample"
11+
style="@style/TextAppearance.AppCompat.Title"
12+
android:layout_width="411dp"
13+
android:layout_height="wrap_content"
14+
android:text="Syntax Highlighting using Fragment" />
15+
916
<FrameLayout
1017
android:id="@+id/fragment_container"
11-
android:layout_width="0dp"
18+
android:layout_width="match_parent"
1219
android:layout_height="0dp"
13-
app:layout_constraintBottom_toBottomOf="parent"
14-
app:layout_constraintLeft_toLeftOf="parent"
15-
app:layout_constraintRight_toRightOf="parent"
16-
app:layout_constraintTop_toTopOf="parent" />
20+
android:layout_weight="1"
21+
tools:background="#cad" />
22+
23+
<TextView
24+
android:id="@+id/title_custom_view_sample"
25+
style="@style/TextAppearance.AppCompat.Title"
26+
android:layout_width="match_parent"
27+
android:layout_height="wrap_content"
28+
android:layout_marginTop="4dp"
29+
android:text="Syntax Highlighting using Custom View" />
1730

18-
</androidx.constraintlayout.widget.ConstraintLayout>
31+
<dev.hossain.yaash.prismjs.SyntaxHighlighterWebView
32+
android:id="@+id/syntax_highlighter_webview"
33+
android:layout_width="match_parent"
34+
android:layout_height="0dp"
35+
android:layout_weight="1"
36+
tools:foreground="#6ad" />
37+
</LinearLayout>

highlighter/src/main/assets/www/main.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ body {
44

55
/* Some basic reset remove any default margin */
66
html, body, div, span, pre, code {
7-
margin: 0;
7+
margin: 0!important;
88
padding: 0;
99
border: 0;
1010
}

highlighter/src/main/java/dev/hossain/yaash/prismjs/ShowStaticSourceCodeFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import dev.hossain.yaash.webclient.WebViewChromeClient
1616
/**
1717
* Fragment that loads hard coded static HTML file with syntax highlighting.
1818
*
19-
* @see ShowSourceCodeFragment
19+
* @see SyntaxHighlighterFragment
2020
*/
2121
class ShowStaticSourceCodeFragment : Fragment() {
2222
companion object {

highlighter/src/main/java/dev/hossain/yaash/prismjs/ShowSourceCodeFragment.kt renamed to highlighter/src/main/java/dev/hossain/yaash/prismjs/SyntaxHighlighterFragment.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,29 @@ import androidx.fragment.app.Fragment
1010
import androidx.webkit.WebSettingsCompat
1111
import androidx.webkit.WebViewFeature
1212
import dev.hossain.yaash.R
13-
import dev.hossain.yaash.prismjs.ShowSourceCodeFragment.Companion.newInstance
13+
import dev.hossain.yaash.prismjs.SyntaxHighlighterFragment.Companion.newInstance
1414
import dev.hossain.yaash.webclient.AppWebViewClient
1515
import dev.hossain.yaash.webclient.WebViewChromeClient
1616

1717
/**
1818
* Fragment that shows syntax highlighted source code.
1919
* Use [newInstance] to provide source code and other configuration parameters.
20+
*
21+
* Here is an example of how to add the fragment from an activity:
22+
* ```
23+
* val fragment = SyntaxHighlighterFragment.newInstance(
24+
* formattedSourceCode = "data class Student(val name: String)",
25+
* language = "kotlin",
26+
* showLineNumbers = true
27+
* )
28+
*
29+
* val fragmentManager = supportFragmentManager
30+
* val fragmentTransaction = fragmentManager.beginTransaction()
31+
* fragmentTransaction.add(R.id.fragment_container, fragment)
32+
* fragmentTransaction.commit()
33+
* ```
2034
*/
21-
class ShowSourceCodeFragment : Fragment() {
35+
class SyntaxHighlighterFragment : Fragment() {
2236
companion object {
2337
private const val ANDROID_ASSETS_PATH = "file:///android_asset/"
2438

@@ -33,7 +47,7 @@ class ShowSourceCodeFragment : Fragment() {
3347
formattedSourceCode: String,
3448
language: String,
3549
showLineNumbers: Boolean = false
36-
) = ShowSourceCodeFragment().apply {
50+
) = SyntaxHighlighterFragment().apply {
3751
arguments = Bundle(4).apply {
3852
putString(ARG_KEY_SOURCE_CODE_CONTENT, formattedSourceCode)
3953
putString(ARG_KEY_CODE_LANGUAGE, language)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package dev.hossain.yaash.prismjs
2+
3+
import android.annotation.SuppressLint
4+
import android.content.Context
5+
import android.util.AttributeSet
6+
import android.webkit.WebView
7+
import dev.hossain.yaash.webclient.AppWebViewClient
8+
import dev.hossain.yaash.webclient.WebViewChromeClient
9+
10+
/**
11+
* Custom view that shows provided source code on custom view.
12+
*
13+
* Here is an example how to load syntax highlighting using this custom view:
14+
*
15+
* ```
16+
* <!-- Add the view in your Activity or Fragment layout -->
17+
* <dev.hossain.yaash.prismjs.SyntaxHighlighterWebView
18+
* android:id="@+id/syntax_highlighter_webview"
19+
* android:layout_width="match_parent"
20+
* android:layout_height="match_parent" />
21+
* ```
22+
*
23+
* Once the Activity or Fragment is ready, binding the `SyntaxHighlighterWebView` with source coded and config.
24+
* ```
25+
* val highlighter: SyntaxHighlighterWebView = findViewById(R.id.syntax_highlighter_webview)
26+
*
27+
* highlighter.bindSyntaxHighlighter(
28+
* formattedSourceCode = "data class Student(val name: String)",
29+
* language = "kotlin",
30+
* showLineNumbers = true
31+
* )
32+
* ```
33+
*/
34+
class SyntaxHighlighterWebView @JvmOverloads constructor(
35+
context: Context,
36+
attrs: AttributeSet? = null,
37+
defStyleAttr: Int = 0
38+
) : WebView(context, attrs, defStyleAttr) {
39+
companion object {
40+
private const val ANDROID_ASSETS_PATH = "file:///android_asset/"
41+
}
42+
43+
@SuppressLint("SetJavaScriptEnabled")
44+
fun bindSyntaxHighlighter(
45+
formattedSourceCode: String,
46+
language: String,
47+
showLineNumbers: Boolean = false
48+
) {
49+
settings.javaScriptEnabled = true
50+
webChromeClient = WebViewChromeClient()
51+
webViewClient = AppWebViewClient()
52+
53+
loadDataWithBaseURL(
54+
ANDROID_ASSETS_PATH /* baseUrl */,
55+
prismJsHtmlContent(formattedSourceCode, language, showLineNumbers) /* html-data */,
56+
"text/html" /* mimeType */,
57+
"utf-8" /* encoding */,
58+
"" /* failUrl */
59+
)
60+
}
61+
}

0 commit comments

Comments
 (0)