-
-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(webview): re-enable webview renderer (#568)
* refactor(reading): reenable webview renderer * refactor(reading): reenable webview renderer * refactor(reading): reenable webview renderer * refactor(reading): reenable webview renderer * refactor(reading): reenable webview renderer * refactor(reading): re-enable webview renderer * refactor(webview): support rtl, video, preference styles
- Loading branch information
Showing
32 changed files
with
1,381 additions
and
300 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
app/src/main/java/me/ash/reader/infrastructure/html/RYArticleGrabberExtended.kt
This file contains 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,86 @@ | ||
package me.ash.reader.infrastructure.html | ||
|
||
|
||
import net.dankito.readability4j.extended.processor.ArticleGrabberExtended | ||
import net.dankito.readability4j.extended.util.RegExUtilExtended | ||
import net.dankito.readability4j.model.ArticleGrabberOptions | ||
import net.dankito.readability4j.model.ReadabilityOptions | ||
import org.jsoup.nodes.Document | ||
import org.jsoup.nodes.Element | ||
import org.jsoup.nodes.TextNode | ||
|
||
open class RYArticleGrabberExtended(options: ReadabilityOptions, regExExtended: RegExUtilExtended) : ArticleGrabberExtended(options, regExExtended) { | ||
|
||
override fun prepareNodes(doc: Document, options: ArticleGrabberOptions): List<Element> { | ||
val elementsToScore = ArrayList<Element>() | ||
var node: Element? = doc | ||
|
||
while(node != null) { | ||
val matchString = node.className() + " " + node.id() | ||
|
||
// Check to see if this node is a byline, and remove it if it is. | ||
if(checkByline(node, matchString)) { | ||
node = removeAndGetNext(node, "byline") | ||
continue | ||
} | ||
|
||
// Remove unlikely candidates | ||
if(options.stripUnlikelyCandidates) { | ||
if(regEx.isUnlikelyCandidate(matchString) && | ||
regEx.okMaybeItsACandidate(matchString) == false && | ||
node.tagName() != "body" && | ||
node.tagName() != "a") { | ||
node = this.removeAndGetNext(node, "Removing unlikely candidate") | ||
continue | ||
} | ||
} | ||
|
||
// Remove DIV, SECTION, and HEADER nodes without any content(e.g. text, image, video, or iframe). | ||
if((node.tagName() == "div" || node.tagName() == "section" || node.tagName() == "header" || | ||
node.tagName() == "h1" || node.tagName() == "h2" || node.tagName() == "h3" || | ||
node.tagName() == "h4" || node.tagName() == "h5" || node.tagName() == "h6") && | ||
this.isElementWithoutContent(node)) { | ||
node = this.removeAndGetNext(node, "node without content") | ||
continue | ||
} | ||
|
||
if(DEFAULT_TAGS_TO_SCORE.contains(node.tagName())) { | ||
elementsToScore.add(node) | ||
} | ||
|
||
// Turn all divs that don't have children block level elements into p's | ||
if(node.tagName() == "div") { | ||
// Sites like http://mobile.slate.com encloses each paragraph with a DIV | ||
// element. DIVs with only a P element inside and no text content can be | ||
// safely converted into plain P elements to avoid confusing the scoring | ||
// algorithm with DIVs with are, in practice, paragraphs. | ||
if(this.hasSinglePInsideElement(node)) { | ||
val newNode = node.child(0) | ||
node.replaceWith(newNode) | ||
node = newNode | ||
elementsToScore.add(node) | ||
} | ||
else if(!this.hasChildBlockElement(node)) { | ||
setNodeTag(node, "p") | ||
elementsToScore.add(node) | ||
} | ||
else { | ||
node.childNodes().forEach { childNode -> | ||
if(childNode is TextNode && childNode.text().trim().length > 0) { | ||
val p = doc.createElement("p") | ||
p.text(childNode.text()) | ||
// EXPERIMENTAL | ||
// p.attr("style", "display: inline;") | ||
// p.addClass("readability-styled") | ||
childNode.replaceWith(p) | ||
} | ||
} | ||
} | ||
} | ||
|
||
node = if(node != null) this.getNextNode(node) else null | ||
} | ||
|
||
return elementsToScore | ||
} | ||
} |
This file contains 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
This file contains 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
44 changes: 44 additions & 0 deletions
44
app/src/main/java/me/ash/reader/infrastructure/preference/ReadingBionicReadingPreference.kt
This file contains 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,44 @@ | ||
package me.ash.reader.infrastructure.preference | ||
|
||
import android.content.Context | ||
import androidx.compose.runtime.compositionLocalOf | ||
import androidx.datastore.preferences.core.Preferences | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import me.ash.reader.ui.ext.DataStoreKey | ||
import me.ash.reader.ui.ext.DataStoreKey.Companion.readingBionicReading | ||
import me.ash.reader.ui.ext.dataStore | ||
import me.ash.reader.ui.ext.put | ||
|
||
val LocalReadingBionicReading = | ||
compositionLocalOf<ReadingBionicReadingPreference> { ReadingBionicReadingPreference.default } | ||
|
||
sealed class ReadingBionicReadingPreference(val value: Boolean) : Preference() { | ||
object ON : ReadingBionicReadingPreference(true) | ||
object OFF : ReadingBionicReadingPreference(false) | ||
|
||
override fun put(context: Context, scope: CoroutineScope) { | ||
scope.launch { | ||
context.dataStore.put(readingBionicReading, value) | ||
} | ||
} | ||
|
||
companion object { | ||
|
||
val default = OFF | ||
val values = listOf(ON, OFF) | ||
|
||
fun fromPreferences(preferences: Preferences) = | ||
when (preferences[DataStoreKey.keys[readingBionicReading]?.key as Preferences.Key<Boolean>]) { | ||
true -> ON | ||
false -> OFF | ||
else -> default | ||
} | ||
} | ||
} | ||
|
||
operator fun ReadingBionicReadingPreference.not(): ReadingBionicReadingPreference = | ||
when (value) { | ||
true -> ReadingBionicReadingPreference.OFF | ||
false -> ReadingBionicReadingPreference.ON | ||
} |
This file contains 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
47 changes: 47 additions & 0 deletions
47
app/src/main/java/me/ash/reader/infrastructure/preference/ReadingRendererPreference.kt
This file contains 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,47 @@ | ||
package me.ash.reader.infrastructure.preference | ||
|
||
import android.content.Context | ||
import androidx.compose.runtime.Stable | ||
import androidx.compose.runtime.compositionLocalOf | ||
import androidx.datastore.preferences.core.Preferences | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import me.ash.reader.R | ||
import me.ash.reader.ui.ext.DataStoreKey | ||
import me.ash.reader.ui.ext.DataStoreKey.Companion.readingRenderer | ||
import me.ash.reader.ui.ext.dataStore | ||
import me.ash.reader.ui.ext.put | ||
|
||
val LocalReadingRenderer = | ||
compositionLocalOf<ReadingRendererPreference> { ReadingRendererPreference.default } | ||
|
||
sealed class ReadingRendererPreference(val value: Int) : Preference() { | ||
object WebView : ReadingRendererPreference(0) | ||
object NativeComponent : ReadingRendererPreference(1) | ||
|
||
override fun put(context: Context, scope: CoroutineScope) { | ||
scope.launch { | ||
context.dataStore.put(DataStoreKey.readingRenderer, value) | ||
} | ||
} | ||
|
||
@Stable | ||
fun toDesc(context: Context): String = | ||
when (this) { | ||
WebView -> context.getString(R.string.webview) | ||
NativeComponent -> context.getString(R.string.native_component) | ||
} | ||
|
||
companion object { | ||
|
||
val default = WebView | ||
val values = listOf(WebView, NativeComponent) | ||
|
||
fun fromPreferences(preferences: Preferences) = | ||
when (preferences[DataStoreKey.keys[readingRenderer]?.key as Preferences.Key<Int>]) { | ||
0 -> WebView | ||
1 -> NativeComponent | ||
else -> default | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.