Skip to content

Commit 8a770b5

Browse files
committed
Shorten titles close to MediaWiki's max (255 bytes)
1 parent adc6969 commit 8a770b5

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

main.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,19 @@ func (p *TripleAggregateToWikiPageConverter) convertUriToWikiTitle(uri string, u
536536
// Clean up strange characters
537537
factTitle = str.Replace(factTitle, "[", "(", -1)
538538
factTitle = str.Replace(factTitle, "]", ")", -1)
539-
540539
factTitle = html.EscapeString(factTitle)
541540

541+
// Limit to max 255 chars (due to MediaWiki limitaiton)
542+
titleIsShortened := false
543+
for len(factTitle) >= 250 {
544+
factTitle = removeLastWord(factTitle)
545+
titleIsShortened = true
546+
}
547+
548+
if titleIsShortened {
549+
factTitle += " ..."
550+
}
551+
542552
if uriType == URITypePredicate {
543553
pageTitle = "Property:" + factTitle
544554
} else if uriType == URITypeClass {
@@ -921,3 +931,9 @@ func stringInSlice(a string, list []string) bool {
921931
}
922932
return false
923933
}
934+
935+
func removeLastWord(inStr string) string {
936+
bits := str.Split(inStr, " ")
937+
outStr := str.Join(append(bits[:len(bits)-1]), " ")
938+
return outStr
939+
}

0 commit comments

Comments
 (0)