From 9d11c0322c08268ac8607d4c6806c074e514f7f5 Mon Sep 17 00:00:00 2001 From: altaccosc <97206249+altaccosc@users.noreply.github.com> Date: Thu, 6 Jan 2022 17:00:18 +0100 Subject: [PATCH] Added option for author's notes at end of chapters (#10366) * Added option for author's notes at end of chapters Inspired by the XKCD source: https://github.com/tachiyomiorg/tachiyomi-extensions/blob/05addfb95e833e8f5a98c2b48a2fd554fa8867bb/src/all/xkcd/src/eu/kanade/tachiyomi/extension/all/xkcd/Xkcd.kt#L50 * Bump Tapastic version --- src/en/tapastic/build.gradle | 2 +- .../extension/en/tapastic/Tapastic.kt | 57 ++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/en/tapastic/build.gradle b/src/en/tapastic/build.gradle index 2079db6eb..0ae59a2d6 100644 --- a/src/en/tapastic/build.gradle +++ b/src/en/tapastic/build.gradle @@ -6,7 +6,7 @@ ext { extName = 'Tapas' pkgNameSuffix = 'en.tapastic' extClass = '.Tapastic' - extVersionCode = 13 + extVersionCode = 14 } apply from: "$rootDir/common.gradle" diff --git a/src/en/tapastic/src/eu/kanade/tachiyomi/extension/en/tapastic/Tapastic.kt b/src/en/tapastic/src/eu/kanade/tachiyomi/extension/en/tapastic/Tapastic.kt index 872ca983c..c83175612 100644 --- a/src/en/tapastic/src/eu/kanade/tachiyomi/extension/en/tapastic/Tapastic.kt +++ b/src/en/tapastic/src/eu/kanade/tachiyomi/extension/en/tapastic/Tapastic.kt @@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.extension.en.tapastic import android.app.Application import android.content.SharedPreferences +import android.net.Uri import android.webkit.CookieManager import androidx.preference.PreferenceScreen import androidx.preference.SwitchPreferenceCompat @@ -35,6 +36,7 @@ import uy.kohesive.injekt.api.get import uy.kohesive.injekt.injectLazy import java.text.SimpleDateFormat import java.util.Locale +import kotlin.math.ceil class Tapastic : ConfigurableSource, ParsedHttpSource() { @@ -129,10 +131,24 @@ class Tapastic : ConfigurableSource, ParsedHttpSource() { } } screen.addPreference(lockPref) + + val authorsNotesPref = SwitchPreferenceCompat(screen.context).apply { + key = SHOW_AUTHORS_NOTES_KEY + title = "Show author's notes" + summary = "Enable to see the author's notes at the end of chapters (if they're there)." + setDefaultValue(false) + + setOnPreferenceChangeListener { _, newValue -> + val checkValue = newValue as Boolean + preferences.edit().putBoolean(SHOW_AUTHORS_NOTES_KEY, checkValue).commit() + } + } + screen.addPreference(authorsNotesPref) } private fun showLockedChapterPref() = preferences.getBoolean(CHAPTER_VIS_PREF_KEY, false) private fun showLockPref() = preferences.getBoolean(SHOW_LOCK_PREF_KEY, false) + private fun showAuthorsNotesPref() = preferences.getBoolean(SHOW_AUTHORS_NOTES_KEY, false) // Popular @@ -279,10 +295,48 @@ class Tapastic : ConfigurableSource, ParsedHttpSource() { // Pages + private fun wordwrap(t: String, lineWidth: Int) = buildString { + val text = t.replace("\n", "\n ") + var charCount = 0 + text.split(" ").forEach { w -> + if (w.contains("\n")) { + charCount = 0 + } + if (charCount > lineWidth) { + append("\n") + charCount = 0 + } else { + append("$w ") + } + charCount += w.length + 1 + } + } + + private fun toImage(t: String, fontSize: Int, bold: Boolean = false): String { + val text = wordwrap(t.replace("&", "&").replace("\\s*
\\s*".toRegex(), "\n"), 65) + val imgHeight = (text.lines().size + 2) * fontSize * 1.3 + return "https://placehold.jp/" + fontSize + "/ffffff/000000/1500x" + ceil(imgHeight).toInt() + ".png?" + + "css=%7B%22text-align%22%3A%22%20left%22%2C%22padding-left%22%3A%22%203%25%22" + (if (bold) "%2C%22font-weight%22%3A%22%20600%22" else "") + "%7D&" + + "text=" + Uri.encode(text) + } + override fun pageListParse(document: Document): List { - return document.select("img.content__img").mapIndexed { i, img -> + var pages = document.select("img.content__img").mapIndexed { i, img -> Page(i, "", img.let { if (it.hasAttr("data-src")) it.attr("abs:data-src") else it.attr("abs:src") }) } + + if (showAuthorsNotesPref()) { + val episodeStory = document.select("p.js-episode-story").html() + if (episodeStory.isNotEmpty()) { + val creator = document.select("a.name.js-fb-tracking")[0].text() + val creatorImage = toImage("Author's Notes from $creator", 43, true) + pages = pages + Page(pages.size, "", creatorImage) + + val storyImage = toImage(episodeStory, 42) + pages = pages + Page(pages.size, "", storyImage) + } + } + return pages } override fun imageUrlParse(document: Document) = @@ -431,5 +485,6 @@ class Tapastic : ConfigurableSource, ParsedHttpSource() { companion object { private const val CHAPTER_VIS_PREF_KEY = "lockedChapterVisibility" private const val SHOW_LOCK_PREF_KEY = "showChapterLock" + private const val SHOW_AUTHORS_NOTES_KEY = "showAuthorsNotes" } }