-
Notifications
You must be signed in to change notification settings - Fork 46
make readability kotlin #1964
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
make readability kotlin #1964
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| plugins { | ||
| alias(libs.plugins.android.library) | ||
| alias(libs.plugins.kotlin.multiplatform) | ||
| alias(libs.plugins.kotlin.serialization) | ||
| } | ||
|
|
||
| kotlin { | ||
| jvmToolchain(libs.versions.java.get().toInt()) | ||
| explicitApi() | ||
| applyDefaultHierarchyTemplate() | ||
| androidLibrary { | ||
| compileSdk = libs.versions.compileSdk.get().toInt() | ||
| namespace = "dev.dimension.flare.readability" | ||
| minSdk = libs.versions.minSdk.get().toInt() | ||
| } | ||
| jvm() | ||
| macosArm64() | ||
| linuxX64() | ||
| mingwX64() | ||
| iosArm64() | ||
| iosSimulatorArm64() | ||
|
|
||
| sourceSets { | ||
| val commonMain by getting { | ||
| dependencies { | ||
| implementation(libs.ksoup) | ||
| implementation(libs.kotlinx.serialization.json) | ||
| } | ||
| } | ||
| val commonTest by getting { | ||
| dependencies { | ||
| implementation(kotlin("test")) | ||
| } | ||
| } | ||
| } | ||
| } | ||
17 changes: 17 additions & 0 deletions
17
readability/src/commonMain/kotlin/dev/dimension/flare/readability/Article.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package dev.dimension.flare.readability | ||
|
|
||
| /** | ||
| * Result of parsing an article with [Readability]. | ||
| */ | ||
| public data class Article( | ||
| public val title: String, | ||
| public val byline: String?, | ||
| public val dir: String?, | ||
| public val lang: String?, | ||
| public val content: String, | ||
| public val textContent: String, | ||
| public val length: Int, | ||
| public val excerpt: String?, | ||
| public val siteName: String?, | ||
| public val publishedTime: String?, | ||
| ) |
12 changes: 12 additions & 0 deletions
12
readability/src/commonMain/kotlin/dev/dimension/flare/readability/ArticleMetadata.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package dev.dimension.flare.readability | ||
|
|
||
| /** | ||
| * Internal metadata holder used during parsing. | ||
| */ | ||
| internal data class ArticleMetadata( | ||
| var title: String? = null, | ||
| var byline: String? = null, | ||
| var excerpt: String? = null, | ||
| var siteName: String? = null, | ||
| var publishedTime: String? = null, | ||
| ) |
118 changes: 118 additions & 0 deletions
118
readability/src/commonMain/kotlin/dev/dimension/flare/readability/IsProbablyReaderable.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package dev.dimension.flare.readability | ||
|
|
||
| import com.fleeksoft.ksoup.Ksoup | ||
| import com.fleeksoft.ksoup.nodes.DataNode | ||
| import com.fleeksoft.ksoup.nodes.Document | ||
| import com.fleeksoft.ksoup.nodes.Element | ||
| import com.fleeksoft.ksoup.nodes.Node | ||
| import com.fleeksoft.ksoup.nodes.TextNode | ||
|
|
||
| /** | ||
| * Configuration options for [isProbablyReaderable]. | ||
| */ | ||
| public data class ReaderableOptions( | ||
| public val minScore: Double = 20.0, | ||
| public val minContentLength: Int = 140, | ||
| public val visibilityChecker: (Element) -> Boolean = ::isNodeVisible, | ||
| ) | ||
|
|
||
| /** | ||
| * Checks whether a node is visible. | ||
| * | ||
| * In a KMP environment without a browser, we check the `style` attribute | ||
| * for `display:none` and the `hidden` / `aria-hidden` attributes. | ||
| */ | ||
| public fun isNodeVisible(node: Element): Boolean { | ||
| val style = node.attr("style") | ||
| if (style.isNotEmpty() && Regex("display\\s*:\\s*none", RegexOption.IGNORE_CASE).containsMatchIn(style)) { | ||
| return false | ||
| } | ||
|
Comment on lines
+26
to
+29
|
||
| if (node.hasAttr("hidden")) return false | ||
| if (node.hasAttr("aria-hidden") && node.attr("aria-hidden") == "true") { | ||
| val className = node.className() | ||
| return className.contains("fallback-image") | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| /** | ||
| * Mimics JS `textContent` — concatenates all text node values without | ||
| * adding any separators (unlike ksoup's `Element.text()` which | ||
| * normalizes whitespace and adds spaces at element boundaries). | ||
| */ | ||
| private fun textContent(node: Node): String { | ||
| val sb = StringBuilder() | ||
| fun collect(n: Node) { | ||
| if (n is TextNode) { | ||
| sb.append(n.getWholeText()) | ||
| } else if (n is DataNode) { | ||
| sb.append(n.getWholeData()) | ||
| } else { | ||
| for (child in n.childNodes()) { | ||
| collect(child) | ||
| } | ||
| } | ||
| } | ||
| collect(node) | ||
| return sb.toString() | ||
| } | ||
|
|
||
| /** | ||
| * Decides whether or not the document is reader-able without parsing the whole thing. | ||
| * | ||
| * @param doc the parsed [Document] to check | ||
| * @param options configuration for the check | ||
| * @return whether Readability.parse() will likely succeed at returning an article | ||
| */ | ||
| public fun isProbablyReaderable(doc: Document, options: ReaderableOptions = ReaderableOptions()): Boolean { | ||
| val nodes = mutableSetOf<Element>() | ||
|
|
||
| // Get <p>, <pre>, <article> nodes | ||
| nodes.addAll(doc.select("p, pre, article")) | ||
|
|
||
| // Get <div> nodes which have <br> node(s) and add their parents | ||
| val brNodes = doc.select("div > br") | ||
| for (br in brNodes) { | ||
| val parent = br.parent() | ||
| if (parent != null) { | ||
| nodes.add(parent) | ||
| } | ||
| } | ||
|
|
||
| var score = 0.0 | ||
|
|
||
| for (node in nodes) { | ||
| if (!options.visibilityChecker(node)) continue | ||
|
|
||
| val matchString = node.className() + " " + node.id() | ||
| if (RegExps.unlikelyCandidates.containsMatchIn(matchString) && | ||
| !RegExps.okMaybeItsACandidate.containsMatchIn(matchString) | ||
| ) { | ||
| continue | ||
| } | ||
|
|
||
| if (node.`is`("li p")) continue | ||
|
|
||
| val textContentLength = textContent(node).trim().length | ||
| if (textContentLength < options.minContentLength) continue | ||
|
|
||
| score += kotlin.math.sqrt((textContentLength - options.minContentLength).toDouble()) | ||
|
|
||
| if (score > options.minScore) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| /** | ||
| * Convenience overload that parses HTML first. | ||
| * | ||
| * @param html raw HTML string | ||
| * @param options configuration for the check | ||
| * @return whether Readability.parse() will likely succeed | ||
| */ | ||
| public fun isProbablyReaderable(html: String, options: ReaderableOptions = ReaderableOptions()): Boolean { | ||
| val doc = Ksoup.parse(html) | ||
| return isProbablyReaderable(doc, options) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Kotlin Multiplatform DSL here appears to be missing an Android target declaration (
androidTarget()), andcompileSdk/namespace/minSdkare typically configured in the Android Gradle Pluginandroid {}block (not underkotlin {}). As written, this is likely to fail Gradle configuration or produce no Android target; move Android SDK/namespace config toandroid {}and addandroidTarget()(or the correct Android target DSL your Kotlin version supports).