Mholloway has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/298036

Change subject: Remove in-Wikitext image captions from news stories
......................................................................

Remove in-Wikitext image captions from news stories

The Wikitext we scrape for the In the News stories often includes a
parenthetical referral to a thumbnail for the section, which we don't use.
Instead, we get thumbnails for each card from the metadata for the linked
pages for each story.  This patch removes the inapplicable caption.

Note that this has a soft dependency on
I295c01b48a0d4feea6b21e07e85179deadf1f216 in that merging without it won't
hurt anything, but you'll need that patch applied to the service to see
the effect here.

Among current stories on enwiki, this applies to the one concerning the
Juno spacecraft.

Change-Id: I04284d257858f9d50494c8b0f4a459da099a2091
---
M app/src/main/java/org/wikipedia/feed/news/NewsItemCard.java
M app/src/main/java/org/wikipedia/richtext/RichTextUtil.java
2 files changed, 29 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia 
refs/changes/36/298036/1

diff --git a/app/src/main/java/org/wikipedia/feed/news/NewsItemCard.java 
b/app/src/main/java/org/wikipedia/feed/news/NewsItemCard.java
index 2269e7e..22129ff 100644
--- a/app/src/main/java/org/wikipedia/feed/news/NewsItemCard.java
+++ b/app/src/main/java/org/wikipedia/feed/news/NewsItemCard.java
@@ -1,14 +1,18 @@
 package org.wikipedia.feed.news;
 
+import android.graphics.Typeface;
 import android.net.Uri;
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
 import android.text.Html;
 import android.text.Spanned;
+import android.text.style.StyleSpan;
 
 import org.wikipedia.Site;
 import org.wikipedia.feed.model.Card;
 import org.wikipedia.feed.model.CardPageItem;
+import org.wikipedia.richtext.RichTextUtil;
+import org.wikipedia.util.log.L;
 
 import java.util.List;
 
@@ -39,7 +43,7 @@
 
     @NonNull
     public Spanned text() {
-        return Html.fromHtml(newsItem.story());
+        return removeImageCaption(Html.fromHtml(newsItem.story()));
     }
 
     public List<CardPageItem> links() {
@@ -52,4 +56,20 @@
     public String title() {
         return "";
     }
+
+    /* Remove the in-Wikitext thumbnail caption, which will almost certainly 
not apply here */
+    private Spanned removeImageCaption(Spanned text) {
+        Object[] spans = RichTextUtil.getSpans(text, 0, text.length());
+        for (Object span : spans) {
+            if (span instanceof StyleSpan && ((StyleSpan) span).getStyle() == 
Typeface.ITALIC) {
+                int start = text.getSpanStart(span);
+                int end = text.getSpanEnd(span);
+                if (text.charAt(start) == '(' && text.charAt(end - 1) == ')') {
+                    L.v("Removing spanned text: " + text.subSequence(start, 
end));
+                    return RichTextUtil.splice(text, start, end);
+                }
+            }
+        }
+        return text;
+    }
 }
diff --git a/app/src/main/java/org/wikipedia/richtext/RichTextUtil.java 
b/app/src/main/java/org/wikipedia/richtext/RichTextUtil.java
index 8f726fd..8382252 100644
--- a/app/src/main/java/org/wikipedia/richtext/RichTextUtil.java
+++ b/app/src/main/java/org/wikipedia/richtext/RichTextUtil.java
@@ -5,6 +5,8 @@
 import android.text.Spannable;
 import android.text.SpannableString;
 import android.text.Spanned;
+import android.text.SpannedString;
+import android.text.TextUtils;
 import android.text.style.URLSpan;
 import android.widget.TextView;
 
@@ -127,5 +129,11 @@
         return Html.fromHtml(html).toString();
     }
 
+
+    public static Spanned splice(Spanned text, int start, int end) {
+        return new SpannedString(TextUtils.concat(text.subSequence(0, start - 
1),
+                text.subSequence(end, text.length())));
+    }
+
     private RichTextUtil() { }
 }
\ No newline at end of file

-- 
To view, visit https://gerrit.wikimedia.org/r/298036
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I04284d257858f9d50494c8b0f4a459da099a2091
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Mholloway <mhollo...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to