Skip to content

Commit

Permalink
News title formatting improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mmathieum committed Dec 15, 2024
1 parent 5c8ad71 commit 12d874c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.mtransit.android.commons.UriUtils;
import org.mtransit.android.commons.data.News;
import org.mtransit.android.commons.helpers.MTDefaultHandler;
import org.mtransit.android.commons.provider.news.NewsTextFormatter;
import org.mtransit.commons.SourceUtils;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
Expand Down Expand Up @@ -702,16 +703,14 @@ private void processNouvelle() throws ParseException {
StringBuilder textHTMLSb = new StringBuilder();
if (!TextUtils.isEmpty(title)) {
textSb.append(title);
textHTMLSb.append(HtmlUtils.applyBold(title));
textHTMLSb.append(NewsTextFormatter.formatHTMLTitle(title));
}
if (!TextUtils.isEmpty(resume)) {
if (textSb.length() > 0) {
textSb.append(COLON);
}
textSb.append(HtmlUtils.fromHtml(resume));
if (textHTMLSb.length() > 0) {
textHTMLSb.append(HtmlUtils.BR);
}
textHTMLSb.append(NewsTextFormatter.getHTMLAfterTitleSpace(textHTMLSb.length()));
textHTMLSb.append(resume);
}
if (!TextUtils.isEmpty(contenu)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.mtransit.android.commons.UriUtils;
import org.mtransit.android.commons.data.News;
import org.mtransit.android.commons.helpers.MTDefaultHandler;
import org.mtransit.android.commons.provider.news.NewsTextFormatter;
import org.mtransit.commons.CollectionUtils;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
Expand Down Expand Up @@ -995,7 +996,7 @@ private void processItem() {
StringBuilder textHTMLSb = new StringBuilder();
if (!TextUtils.isEmpty(title)) {
textSb.append(title);
textHTMLSb.append(HtmlUtils.applyBold(title));
textHTMLSb.append(NewsTextFormatter.formatHTMLTitle(title));
}
if (!TextUtils.isEmpty(description)) {
if (textSb.length() > 0) {
Expand All @@ -1013,9 +1014,7 @@ private void processItem() {
textHTML = HtmlUtils.removeComments(textHTML);
textHTML = HtmlUtils.fixTextViewBR(textHTML);
textSb.append(HtmlUtils.fromHtmlCompact(textHTML));
if (textHTMLSb.length() > 0) {
textHTMLSb.append(HtmlUtils.BR).append(HtmlUtils.BR);
}
textHTMLSb.append(NewsTextFormatter.getHTMLAfterTitleSpace(textHTMLSb.length()));
textHTMLSb.append(textHTML);
}
if (!TextUtils.isEmpty(link)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.mtransit.android.commons.data.ServiceUpdate;
import org.mtransit.android.commons.data.Trip;
import org.mtransit.android.commons.helpers.MTDefaultHandler;
import org.mtransit.android.commons.provider.news.NewsTextFormatter;
import org.mtransit.commons.Cleaner;
import org.mtransit.commons.CollectionUtils;
import org.mtransit.commons.FeatureFlags;
Expand Down Expand Up @@ -1159,16 +1160,14 @@ public void endElement(String uri, String localName, String qName) throws SAXExc
if (textHTMLSb.length() > 0) {
textHTMLSb.append(HtmlUtils.BR);
}
textHTMLSb.append(HtmlUtils.applyBold(title));
textHTMLSb.append(NewsTextFormatter.formatHTMLTitle(title));
}
if (!TextUtils.isEmpty(desc)) {
if (textSb.length() > 0) {
textSb.append(COLON);
}
textSb.append(desc);
if (textHTMLSb.length() > 0) {
textHTMLSb.append(HtmlUtils.BR);
}
textHTMLSb.append(NewsTextFormatter.getHTMLAfterTitleSpace(textHTMLSb.length()));
textHTMLSb.append(HtmlUtils.applyBold(desc));
}
if (!TextUtils.isEmpty(content)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.mtransit.android.commons.StringUtils
import org.mtransit.android.commons.TimeUtils
import org.mtransit.android.commons.UriUtils
import org.mtransit.android.commons.data.News
import org.mtransit.android.commons.provider.news.NewsTextFormatter
import org.mtransit.android.commons.provider.news.youtube.YouTubeNewsDbHelper
import org.mtransit.android.commons.provider.news.youtube.YouTubeStorage
import java.io.IOException
Expand Down Expand Up @@ -406,12 +407,10 @@ class YouTubeNewsProvider : NewsProvider() {
}
val textHtml = buildString {
snippet.title?.takeIf { it.isNotBlank() }?.let { title ->
append(HtmlUtils.applyBold(title))
append(NewsTextFormatter.formatHTMLTitle(title))
}
snippet.description?.takeIf { it.isNotBlank() }?.let { description ->
if (isNotEmpty()) {
append(HtmlUtils.BR).append(HtmlUtils.BR)
}
append(NewsTextFormatter.getHTMLAfterTitleSpace(length))
append(
HtmlUtils.toHTML(
HtmlUtils.linkifyAllURLs(description)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
package org.mtransit.android.commons.provider.news

import org.mtransit.android.commons.HtmlUtils
import org.mtransit.commons.StringUtils.EMPTY
import java.util.Locale

object NewsTextFormatter {

private const val TITLE_USE_H= true

@JvmStatic
fun formatHTMLTitle(title: String): String {
if (TITLE_USE_H) {
return "<H1>$title</H1>"
}
return HtmlUtils.applyBold(title)
}

@JvmStatic
fun getHTMLAfterTitleSpace(length: Int): String {
if (length <= 0) {
return EMPTY
}
if (TITLE_USE_H) {
return EMPTY
}
return buildString {
append(HtmlUtils.BR) // after bold
append(HtmlUtils.BR) // empty line
}
}

fun appendVideoLinkToHTMLText(
autoUrl: String? = null,
qualityToUrlList: List<Pair<String, String>> = emptyList(),
Expand Down

0 comments on commit 12d874c

Please sign in to comment.