Skip to content

Commit

Permalink
fix italics in topic name bug
Browse files Browse the repository at this point in the history
  • Loading branch information
NovemLinguae committed Dec 31, 2021
1 parent 3714544 commit 6d1157c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/Promote.php
Expand Up @@ -55,16 +55,22 @@ function getMainArticleTitle($topicBoxWikicode, $title) {

/** There's 3 sources we can pick the topic name from: 1) main article's title, 2) |title= parameter, 3) /archive page's title. Per a conversation with Aza24, we will get it from #2: the |title= parameter. */
function getTopicTitle($topicBoxWikicode, $mainArticleTitle) {
assert($mainArticleTitle !== ''); // should not be blank. was checked in another step.

// search for |title= parameter
preg_match("/\|\s*title\s*=\s*([^\|\}]+)\s*/is", $topicBoxWikicode, $matches);

// if not found, return $mainArticleTitle as topicTitle
if ( ! $matches ) {
return $mainArticleTitle;
if ( $matches ) {
$matches[1] = trim($matches[1]);
// get rid of apostrophes
$matches[1] = str_replace("'''", "", $matches[1]);
$matches[1] = str_replace("''", "", $matches[1]);
// if getting rid of apostrophes and trimming didn't delete the entire title, return that
if ( $matches[1] ) return $matches[1];
}
// if found, return that as topicTitle
return trim($matches[1]);

// else, return $mainArticleTitle as topicTitle
return $mainArticleTitle;
}

/** It's OK if this one isn't able to find anything. Not a critical error. It can return blank. */
Expand Down
21 changes: 21 additions & 0 deletions tests/PromoteTest.php
Expand Up @@ -849,6 +849,27 @@ function test_getTopicTitle_withTopic() {
$this->assertSame('UEFA European Championship finals', $result);
}

function test_getTopicTitle_withTopic_bold() {
$topicBoxWikicode = "{{Featured topic box |title='''UEFA European Championship finals''' |count=17}}";
$mainArticleTitle = 'List of UEFA European Championship finals';
$result = $this->p->getTopicTitle($topicBoxWikicode, $mainArticleTitle);
$this->assertSame('UEFA European Championship finals', $result);
}

function test_getTopicTitle_withTopic_boldItalic() {
$topicBoxWikicode = "{{Featured topic box |title='''''UEFA European Championship finals''''' |count=17}}";
$mainArticleTitle = 'List of UEFA European Championship finals';
$result = $this->p->getTopicTitle($topicBoxWikicode, $mainArticleTitle);
$this->assertSame('UEFA European Championship finals', $result);
}

function test_getTopicTitle_withTopic_italic() {
$topicBoxWikicode = "{{Featured topic box |title=''UEFA European Championship finals'' |count=17}}";
$mainArticleTitle = 'List of UEFA European Championship finals';
$result = $this->p->getTopicTitle($topicBoxWikicode, $mainArticleTitle);
$this->assertSame('UEFA European Championship finals', $result);
}

function test_getTopicTitle_noTopic() {
$topicBoxWikicode =
'{{Featured topic box |count=17 |image=Coupe Henri Delaunay 2017.jpg |imagesize=
Expand Down

0 comments on commit 6d1157c

Please sign in to comment.