Skip to content

Commit c5b527f

Browse files
1 parent ffbfdab commit c5b527f

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@
9494
import java.util.Locale;
9595
import java.util.Map;
9696
import java.util.Objects;
97+
import java.util.regex.Matcher;
98+
import java.util.regex.Pattern;
9799
import javax.inject.Inject;
98100
import javax.inject.Named;
99101
import org.apache.commons.lang3.StringUtils;
@@ -914,19 +916,15 @@ private void extractCaptionDescription(final String s) {
914916
* @return LinkedHashMap<LanguageCode,Description>
915917
*/
916918
private LinkedHashMap<String,String> getDescriptions(String s) {
917-
// trim spaces next to "=" and "|"
918-
s = s.replace(" =", "=").replace(" |", "|").replace("= ","=").replace("| ","|");
919-
int descriptionIndex = s.indexOf("description=");
920-
if(descriptionIndex == -1){
921-
descriptionIndex = s.indexOf("Description=");
919+
final Pattern pattern = Pattern.compile("[dD]escription *=(.*?)\n *\\|", Pattern.DOTALL);
920+
final Matcher matcher = pattern.matcher(s);
921+
String description = null;
922+
if (matcher.find()) {
923+
description = matcher.group();
922924
}
923-
924-
if( descriptionIndex == -1 ){
925+
if(description == null){
925926
return new LinkedHashMap<>();
926927
}
927-
final String descriptionToEnd = s.substring(descriptionIndex+12);
928-
final int descriptionEndIndex = descriptionToEnd.indexOf("\n|");
929-
final String description = s.substring(descriptionIndex+12, descriptionIndex+12+descriptionEndIndex);
930928

931929
final LinkedHashMap<String,String> descriptionList = new LinkedHashMap<>();
932930

app/src/test/kotlin/fr/free/nrw/commons/media/MediaDetailFragmentUnitTests.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,25 @@ class MediaDetailFragmentUnitTests {
466466
Assert.assertEquals(map, method.invoke(fragment, s))
467467
}
468468

469+
@Test
470+
@Throws(Exception::class)
471+
fun testGetDescriptionsWithLongSpaces() {
472+
`when`(media.filename).thenReturn("")
473+
val method: Method = MediaDetailFragment::class.java.getDeclaredMethod("getDescriptions", String::class.java)
474+
method.isAccessible = true
475+
val s = "=={{int:filedesc}}==\n" +
476+
"{{Information\n" +
477+
"|Description ={{en|1=The interior of Sacred Heart RC Church, Wimbledon, London.}}\n" +
478+
"|Source ={{own}}\n" +
479+
"|Author =[[User:Diliff|Diliff]]\n" +
480+
"|Date =2015-02-17\n" +
481+
"|Permission ={{Diliff/Licensing}}\n" +
482+
"|other_versions=\n" +
483+
"}}"
484+
val map = linkedMapOf("en" to "The interior of Sacred Heart RC Church, Wimbledon, London.")
485+
Assert.assertEquals(map, method.invoke(fragment, s))
486+
}
487+
469488
@Test
470489
@Throws(Exception::class)
471490
fun testGetDescriptionList() {

0 commit comments

Comments
 (0)