Skip to content

Commit

Permalink
add ksoup-lite variant
Browse files Browse the repository at this point in the history
  • Loading branch information
itboy87 committed Sep 14, 2024
1 parent e13b94c commit 13afa6d
Show file tree
Hide file tree
Showing 16 changed files with 231 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ jobs:
matrix:
buildType:
- "common"
- "lite"
- "kotlinx"
- "korlibs"
- "ktor2"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
{ target: windows, os: windows-latest, tasks: mingwX64Test, continueOnError: false },
{ target: linux, os: ubuntu-latest, tasks: linuxX64Test, continueOnError: false },
]
libBuildType: [ "korlibs", "kotlinx", "okio", "ktor2" ]
libBuildType: [ "lite", "korlibs", "kotlinx", "okio", "ktor2" ]
runs-on: ${{ matrix.config.os }}
name: Build ${{ matrix.config.target }} with libBuildType=${{ matrix.libBuildType }}
steps:
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ android.nonTransitiveRClass=true
kotlin.native.ignoreIncorrectDependencies=true
kotlin.mpp.enableCInteropCommonization=true
kotlin.mpp.applyDefaultHierarchyTemplate=false
# dev, common, kotlinx, korlibs, okio, ktor2
# dev, common, lite, kotlinx, korlibs, okio, ktor2
# dev will include all modules in settings.gradle.kts but use kotlinx dep for engine
libBuildType=okio
libBuildType=lite


SONATYPE_HOST=CENTRAL_PORTAL
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.fleeksoft.ksoup.io

internal class SourceReaderByteArray(bytes: ByteArray) : SourceReader {
private var source: ByteArray = bytes
private var currentPosition: Int = 0
private var markedPosition: Int? = null
private var isClosed: Boolean = false

override fun mark(readLimit: Long) {
markedPosition = currentPosition
}

override fun reset() {
isClosed = false
markedPosition?.let {
currentPosition = it
markedPosition = null
}
}


override fun readBytes(count: Int): ByteArray {
val byteArray = ByteArray(count)
var i = 0
while (exhausted().not() && i < count) {
byteArray[i] = source[currentPosition++]
i++
}
return if (i == 0) {
byteArrayOf()
} else if (i != count) {
byteArray.sliceArray(0 until i)
} else {
byteArray
}
}

override fun read(bytes: ByteArray, offset: Int, length: Int): Int {
var i = offset
while (exhausted().not() && i < length) {
bytes[i] = source[currentPosition++]
i++
}
return i
}

override fun readAllBytes(): ByteArray {
return readBytes(source.size - currentPosition)
}

override fun exhausted(): Boolean {
return currentPosition >= source.size
}

override fun close() {
// on reset we need bytes again
// source = ByteArray(0)
// currentPosition = 0
// markedPosition = null
isClosed = true
}

override fun readAtMostTo(sink: KByteBuffer, byteCount: Int): Int {
val bytes = readBytes(byteCount)
sink.writeBytes(bytes, bytes.size)
return bytes.size
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.fleeksoft.ksoup.io

fun SourceReader.Companion.from(byteArray: ByteArray): SourceReader = SourceReaderByteArray(byteArray)
37 changes: 37 additions & 0 deletions ksoup-engine-lite/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
plugins {
alias(libs.plugins.mavenPublish)
}

group = "com.fleeksoft.ksoup"
version = libs.versions.libraryVersion.get()

val artifactId = "ksoup-engine-lite"
mavenPublishing {
coordinates("com.fleeksoft.ksoup", artifactId, libs.versions.libraryVersion.get())
pom {
name.set(artifactId)
description.set("Ksoup is a Kotlin Multiplatform library for working with HTML and XML, and offers an easy-to-use API for URL fetching, data parsing, extraction, and manipulation using DOM and CSS selectors.")
licenses {
license {
name.set("Apache-2.0")
url.set("https://opensource.org/licenses/Apache-2.0")
}
}
url.set("https://github.com/fleeksoft/ksoup")
issueManagement {
system.set("Github")
url.set("https://github.com/fleeksoft/ksoup/issues")
}
scm {
connection.set("https://github.com/fleeksoft/ksoup.git")
url.set("https://github.com/fleeksoft/ksoup")
}
developers {
developer {
name.set("Sabeeh Ul Hussnain Anjum")
email.set("[email protected]")
organization.set("Fleek Soft")
}
}
}
}
11 changes: 11 additions & 0 deletions ksoup-engine-lite/module.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
product:
type: lib
platforms: [ jvm, js, wasm, android, linuxX64, linuxArm64, tvosArm64, tvosX64, tvosSimulatorArm64, macosX64, macosArm64, iosArm64, iosSimulatorArm64, iosX64, mingwX64 ]

apply: [ ../common.module-template.yaml ]

aliases:
- jvmAndAndroid: [ jvm, android ]

dependencies:
- ../ksoup-engine-common
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.fleeksoft.ksoup.engine

import com.fleeksoft.ksoup.io.*

object KsoupEngineImpl : KsoupEngine {

override fun getUtf8Charset(): Charset {
return CharsetImpl("UTF-8")
}

override fun charsetForName(name: String): Charset {
return CharsetImpl(name)
}

override fun pathToFileSource(path: String): FileSource {
TODO("File Source not supported in lite")
}
}
57 changes: 57 additions & 0 deletions ksoup-engine-lite/src/com/fleeksoft/ksoup/io/CharsetImpl.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.fleeksoft.ksoup.io

import kotlin.math.max


class CharsetImpl(override val name: String) : Charset {
init {
require(name.lowercase() == "utf8" || name.lowercase() == "utf-8" || name.lowercase() == "iso-8859-1" || name.lowercase() == "ascii" || name.lowercase() == "us-ascii") {
"Charset $name not supported"
}
}

override fun onlyUtf8(): Boolean = true

override fun decode(stringBuilder: StringBuilder, byteArray: ByteArray, start: Int, end: Int): Int {
if (end <= 0) return 0
var incompleteByteIndex = -1

val isUtf8 = name.lowercase() == "utf-8" || name.lowercase() == "utf8"
if (isUtf8) {
// TODO:// may be we can use this for other charsets
val startIndex = if (end > 4) end - 4 else 0
var i = startIndex
while (i < end) {
val byteLength = guessByteSequenceLength(byteArray[i])
if (byteLength > 1 && (i + byteLength) > end) {
incompleteByteIndex = i
break
} else {
i += max(byteLength, 1)
}
}
}
val toDecodeSize = if (incompleteByteIndex > 0) {
incompleteByteIndex
} else {
end
}

stringBuilder.append(byteArray.sliceArray(start until toDecodeSize).decodeToString())
return toDecodeSize - start
}

private fun guessByteSequenceLength(byte: Byte): Int {
return when ((byte.toInt() and 0xFF) shr 4) {
in 0b0000..0b0111 -> 1
in 0b1100..0b1101 -> 2
0b1110 -> 3
0b1111 -> 4
else -> 0
}
}

override fun toByteArray(value: String): ByteArray {
return value.encodeToByteArray()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.fleeksoft.ksoup.io

import java.io.File
import java.io.InputStream

// todo for jvm we can use streaming api in lite module
fun SourceReader.Companion.from(inputStream: InputStream): SourceReader = SourceReader.from(inputStream.readAllBytes())
fun FileSource.Companion.from(file: File): FileSource = TODO("File Source not supported in lite")
fun FileSource.Companion.from(file: String): FileSource = TODO("File Source not supported in lite")
1 change: 1 addition & 0 deletions ksoup-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ val generateBuildConfigFile: Task by tasks.creating {
const val isKorlibs: Boolean = ${libBuildType == "korlibs"}
const val isOkio: Boolean = ${libBuildType == "okio"}
const val isKtor2: Boolean = ${libBuildType == "ktor2"}
const val isLite: Boolean = ${libBuildType == "lite"}
}
""".trimIndent()
file.get().asFile.writeText(content)
Expand Down
11 changes: 8 additions & 3 deletions ksoup-test/test/com/fleeksoft/ksoup/TestHelper.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.fleeksoft.ksoup

import com.fleeksoft.ksoup.io.SourceReader
import com.fleeksoft.ksoup.ported.io.Charsets
import com.fleeksoft.ksoup.ported.openSourceReader
import com.fleeksoft.ksoup.ported.toByteArray
import korlibs.io.compression.deflate.GZIP
import korlibs.io.compression.uncompress
import korlibs.io.file.std.uniVfs
Expand Down Expand Up @@ -80,9 +82,12 @@ object TestHelper {
}

fun isGzipSupported(): Boolean = BuildConfig.isKorlibs
fun isUtf16Supported(): Boolean = !((BuildConfig.isKotlinx || BuildConfig.isOkio || BuildConfig.isKtor2) && Platform.isJsOrWasm())
fun isUtf16Supported(): Boolean = !(((BuildConfig.isKotlinx || BuildConfig.isOkio || BuildConfig.isKtor2) && Platform.isJsOrWasm()) || BuildConfig.isLite)
fun isUtf32Supported(): Boolean = !(Platform.isJsOrWasm() || Platform.isWindows() || Platform.isLinux())
fun isEUCKRSupported(): Boolean = !(Platform.isJsOrWasm() || Platform.isApple() || Platform.isWindows() || (BuildConfig.isKorlibs && Platform.isLinux()))
fun isGB2312Supported(): Boolean = !(Platform.isApple() || Platform.isWindows() || ((BuildConfig.isKotlinx || BuildConfig.isOkio || BuildConfig.isKtor2) && Platform.isJsOrWasm()) || (BuildConfig.isKorlibs && Platform.isLinux()))
fun canReadResourceFile(): Boolean = !Platform.isWasmJs() || BuildConfig.isKorlibs
fun isGB2312Supported(): Boolean = !(BuildConfig.isLite || Platform.isApple() || Platform.isWindows() || ((BuildConfig.isKotlinx || BuildConfig.isOkio || BuildConfig.isKtor2) && Platform.isJsOrWasm()) || (BuildConfig.isKorlibs && Platform.isLinux()))

fun canReadResourceFile(): Boolean = (!Platform.isWasmJs() || BuildConfig.isKorlibs) && !BuildConfig.isLite

fun isFileSourceSupported(): Boolean = !BuildConfig.isLite
}
5 changes: 5 additions & 0 deletions ksoup/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ kotlin {
commonMain {
dependencies {
when (libBuildType) {
"lite" -> {
api(project(":ksoup-engine-lite"))
}

"korlibs" -> {
api(project(":ksoup-engine-korlibs"))
}
Expand All @@ -39,6 +43,7 @@ val artifactId = when (libBuildType) {
"korlibs" -> "ksoup-korlibs"
"okio" -> "ksoup-okio"
"ktor2" -> "ksoup-ktor2"
"lite" -> "ksoup-lite"
else -> "ksoup"
}

Expand Down
3 changes: 3 additions & 0 deletions publishToMaven.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ add_projects_based_on_key() {
"common")
projects=("ksoup-engine-common")
;;
"lite")
projects=("ksoup-engine-lite" "ksoup-lite")
;;
"kotlinx")
projects=("ksoup-engine-kotlinx" "ksoup" "ksoup-network")
;;
Expand Down
4 changes: 2 additions & 2 deletions runTests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ run_tests() {
echo "Running tests with libBuildType=$libBuildType and tasks=${tasks[*]}..."

# Only add/remove wasm for kotlinx and korlibs
if [[ "$libBuildType" == "kotlinx" || "$libBuildType" == "korlibs" ]]; then
if [[ "$libBuildType" == "kotlinx" || "$libBuildType" == "korlibs" || "$libBuildType" == "lite" ]]; then
add_wasm_platform
fi

Expand All @@ -98,7 +98,7 @@ run_tests() {
}

# Supported parameters
SUPPORTED_PARAMS=("korlibs" "okio" "kotlinx" "ktor2")
SUPPORTED_PARAMS=("lite" "korlibs" "okio" "kotlinx" "ktor2")

# Function to check if the provided parameter is supported
is_supported_param() {
Expand Down
5 changes: 5 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ dependencyResolutionManagement {
val libBuildType = settings.providers.gradleProperty("libBuildType").get()

include("ksoup-engine-common")

if (libBuildType == "lite" || libBuildType == "dev") {
include("ksoup-engine-lite")
}

if (libBuildType == "korlibs" || libBuildType == "dev") {
include("ksoup-engine-korlibs", "ksoup-network-korlibs")
}
Expand Down

0 comments on commit 13afa6d

Please sign in to comment.