[MediaWiki-commits] [Gerrit] mediawiki...Translate[master]: PageTranslation: do not add line breaks to inline usage of <...
Nikerabbit has submitted this change and it was merged. Change subject: PageTranslation: do not add line breaks to inline usage of tags .. PageTranslation: do not add line breaks to inline usage of tags This causes problems for example with headings. This should also make the source a bit more readable in places when line breaks are not forced. Bug: T150188 Change-Id: Ia54091ba857e8d513ea302a4b80342b5f5b97b22 --- M tag/TPSection.php M tag/TranslatablePage.php A tests/phpunit/TPSectionTest.php A tests/phpunit/TranslatablePageTest.php M tests/phpunit/pagetranslation/Inline.ptsource M tests/phpunit/pagetranslation/Whitespace.ptsource 6 files changed, 166 insertions(+), 20 deletions(-) Approvals: Amire80: Looks good to me, approved jenkins-bot: Verified diff --git a/tag/TPSection.php b/tag/TPSection.php index 105dd98..747812a 100644 --- a/tag/TPSection.php +++ b/tag/TPSection.php @@ -4,7 +4,6 @@ * * @file * @author Niklas Laxström - * @copyright Copyright © 2009-2013 Niklas Laxström * @license GPL-2.0+ */ @@ -40,6 +39,20 @@ public $oldText; /** +* @var bool Whether this section is inline section. +* E.g. "Something foo bar". +*/ + protected $inline = false; + + public function setIsInline( $value ) { + $this->inline = (bool)$value; + } + + public function isInline( $value ) { + return $this->inline; + } + + /** * Returns section text unmodified. * @return string Wikitext. */ @@ -70,6 +83,7 @@ /** * Returns the section text with updated or added section marker. +* * @return string Wikitext. */ public function getMarkedText() { @@ -82,7 +96,11 @@ $text = preg_replace( $re, $rep, $this->text, 1, $count ); if ( $count === 0 ) { - $text = $header . "\n" . $this->text; + if ( $this->inline ) { + $text = $header . ' ' . $this->text; + } else { + $text = $header . "\n" . $this->text; + } } return $text; diff --git a/tag/TranslatablePage.php b/tag/TranslatablePage.php index c0236b9..3b9a5bb 100644 --- a/tag/TranslatablePage.php +++ b/tag/TranslatablePage.php @@ -286,7 +286,7 @@ $tagPlaceHolders = array(); while ( true ) { - $re = '~()\s*(.*?)()~s'; + $re = '~()(.*?)()~s'; $matches = array(); $ok = preg_match_all( $re, $text, $matches, PREG_OFFSET_CAPTURE ); @@ -316,10 +316,11 @@ $sectiontext = self::unArmourNowiki( $nowiki, $sectiontext ); - $ret = $this->sectionise( $sections, $sectiontext ); + $parse = self::sectionise( $sectiontext ); + $sections += $parse['sections']; $tagPlaceHolders[$ph] = - self::index_replace( $contents, $ret, $start, $end ); + self::index_replace( $contents, $parse['template'], $start, $end ); } $prettyTemplate = $text; @@ -400,27 +401,36 @@ /** * Splits the content marked with \ tags into sections, which * are separated with with two or more newlines. Extra whitespace is captured -* in the template and not included in the sections. -* @param array $sections Array of placeholder => TPSection. +* in the template and is not included in the sections. +* * @param string $text Contents of one pair of \ tags. -* @return string Template with placeholders for sections, which itself are added to $sections. +* @return array Contains a template and array of unparsed sections. */ - protected function sectionise( &$sections, $text ) { + public static function sectionise( $text ) { $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE; - $parts = preg_split( '~(\s*\n\n\s*|\s*$)~', $text, -1, $flags ); + $parts = preg_split( '~(^\s*|\s*\n\n\s*|\s*$)~', $text, -1, $flags ); + + $inline = preg_match( '~\n~', $text ) === 0; $template = ''; + $sections = []; + foreach ( $parts as $_ ) { if ( trim( $_ ) === '' ) { $template .= $_; } else { $ph = TranslateUtils::getPlaceholder(); - $sections[$ph] = $this->shakeSection( $_ ); + $tpsection = self::shakeSection( $_ ); + $tpsection
[MediaWiki-commits] [Gerrit] mediawiki...Translate[master]: PageTranslation: do not add line breaks to inline usage of <...
Nikerabbit has uploaded a new change for review. https://gerrit.wikimedia.org/r/320571 Change subject: PageTranslation: do not add line breaks to inline usage of tags .. PageTranslation: do not add line breaks to inline usage of tags This causes problems for example with headings. This should also make the source a bit more readable in places when line breaks are not forced. Bug: T150188 Change-Id: Ia54091ba857e8d513ea302a4b80342b5f5b97b22 --- M tag/TPSection.php M tag/TranslatablePage.php A tests/phpunit/TPSectionTest.php A tests/phpunit/TranslatablePageTest.php M tests/phpunit/pagetranslation/Inline.ptsource M tests/phpunit/pagetranslation/Whitespace.ptsource 6 files changed, 165 insertions(+), 20 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Translate refs/changes/71/320571/1 diff --git a/tag/TPSection.php b/tag/TPSection.php index 105dd98..48e5e2f 100644 --- a/tag/TPSection.php +++ b/tag/TPSection.php @@ -4,7 +4,6 @@ * * @file * @author Niklas Laxström - * @copyright Copyright © 2009-2013 Niklas Laxström * @license GPL-2.0+ */ @@ -40,6 +39,19 @@ public $oldText; /** +* @var bool Whether this section is inline section. E.g. "Something foo bar". +*/ + protected $inline = false; + + public function setIsInline( $value ) { + $this->inline = (bool)$value; + } + + public function isInline( $value ) { + return $this->inline; + } + + /** * Returns section text unmodified. * @return string Wikitext. */ @@ -70,6 +82,7 @@ /** * Returns the section text with updated or added section marker. +* * @return string Wikitext. */ public function getMarkedText() { @@ -82,7 +95,11 @@ $text = preg_replace( $re, $rep, $this->text, 1, $count ); if ( $count === 0 ) { - $text = $header . "\n" . $this->text; + if ( $this->inline ) { + $text = $header . ' ' . $this->text; + } else { + $text = $header . "\n" . $this->text; + } } return $text; diff --git a/tag/TranslatablePage.php b/tag/TranslatablePage.php index c0236b9..3b9a5bb 100644 --- a/tag/TranslatablePage.php +++ b/tag/TranslatablePage.php @@ -286,7 +286,7 @@ $tagPlaceHolders = array(); while ( true ) { - $re = '~()\s*(.*?)()~s'; + $re = '~()(.*?)()~s'; $matches = array(); $ok = preg_match_all( $re, $text, $matches, PREG_OFFSET_CAPTURE ); @@ -316,10 +316,11 @@ $sectiontext = self::unArmourNowiki( $nowiki, $sectiontext ); - $ret = $this->sectionise( $sections, $sectiontext ); + $parse = self::sectionise( $sectiontext ); + $sections += $parse['sections']; $tagPlaceHolders[$ph] = - self::index_replace( $contents, $ret, $start, $end ); + self::index_replace( $contents, $parse['template'], $start, $end ); } $prettyTemplate = $text; @@ -400,27 +401,36 @@ /** * Splits the content marked with \ tags into sections, which * are separated with with two or more newlines. Extra whitespace is captured -* in the template and not included in the sections. -* @param array $sections Array of placeholder => TPSection. +* in the template and is not included in the sections. +* * @param string $text Contents of one pair of \ tags. -* @return string Template with placeholders for sections, which itself are added to $sections. +* @return array Contains a template and array of unparsed sections. */ - protected function sectionise( &$sections, $text ) { + public static function sectionise( $text ) { $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE; - $parts = preg_split( '~(\s*\n\n\s*|\s*$)~', $text, -1, $flags ); + $parts = preg_split( '~(^\s*|\s*\n\n\s*|\s*$)~', $text, -1, $flags ); + + $inline = preg_match( '~\n~', $text ) === 0; $template = ''; + $sections = []; + foreach ( $parts as $_ ) { if ( trim( $_ ) === '' ) { $template .= $_; } else { $ph = TranslateUtils::getPlaceholder(); - $sections[$ph] = $this->shakeSection( $_ ); + $tpsection = self::shakeSection