Extract footnote mark rendering

Change-Id: I79de89e46da36dc1f0ee2b2fdb9a139e6434fde2
This commit is contained in:
Adam Wight 2019-11-28 15:39:11 +01:00
parent 4b58a25459
commit c236524138
2 changed files with 137 additions and 88 deletions

View file

@ -34,7 +34,7 @@ use StatusValue;
class Cite {
private const DEFAULT_GROUP = '';
public const DEFAULT_GROUP = '';
/**
* Wikitext attribute name for Book Referencing.
@ -55,13 +55,6 @@ class Cite {
*/
private $mBacklinkLabels;
/**
* The links to use per group, in order.
*
* @var (string[]|false)[]
*/
private $mLinkLabels = [];
/**
* @var Parser
*/
@ -77,6 +70,11 @@ class Cite {
*/
private $isSectionPreview;
/**
* @var FootnoteMarkFormatter
*/
private $footnoteMarkFormatter;
/**
* @var CiteErrorReporter
*/
@ -135,6 +133,8 @@ class Cite {
);
$this->referenceStack = new ReferenceStack( $this->errorReporter );
$this->citeKeyFormatter = new CiteKeyFormatter();
$this->footnoteMarkFormatter = new FootnoteMarkFormatter(
$this->mParser, $this->errorReporter, $this->citeKeyFormatter );
}
}
@ -362,7 +362,7 @@ class Cite {
return '';
} else {
[ $key, $count, $label, $subkey ] = $result;
return $this->linkRef( $group, $key, $count, $label, $subkey );
return $this->footnoteMarkFormatter->linkRef( $group, $key, $count, $label, $subkey );
}
}
@ -667,68 +667,6 @@ class Cite {
?? $this->errorReporter->plain( 'cite_error_references_no_backlink_label', null );
}
/**
* Generate a custom format link for a group given an offset, e.g.
* the second <ref group="foo"> is b if $this->mLinkLabels["foo"] =
* [ 'a', 'b', 'c', ...].
* Return an error if the offset > the # of array items
*
* @param int $offset
* @param string $group The group name
* @param string $label The text to use if there's no message for them.
*
* @return string
*/
private function getLinkLabel( $offset, $group, $label ) {
$message = "cite_link_label_group-$group";
if ( !isset( $this->mLinkLabels[$group] ) ) {
$this->genLinkLabels( $group, $message );
}
if ( $this->mLinkLabels[$group] === false ) {
// Use normal representation, ie. "$group 1", "$group 2"...
return $label;
}
return $this->mLinkLabels[$group][$offset - 1]
?? $this->errorReporter->plain( 'cite_error_no_link_label_group', [ $group, $message ] );
}
/**
* Generate a link (<sup ...) for the <ref> element from a key
* and return XHTML ready for output
*
* @suppress SecurityCheck-DoubleEscaped
* @param string $group
* @param string $key The key for the link
* @param int|null $count The index of the key, used for distinguishing
* multiple occurrences of the same key
* @param int $label The label to use for the link, I want to
* use the same label for all occurrences of
* the same named reference.
* @param string|null $subkey
*
* @return string
*/
private function linkRef( $group, $key, $count, $label, $subkey ) {
$contLang = MediaWikiServices::getInstance()->getContentLanguage();
return $this->mParser->recursiveTagParse(
wfMessage(
'cite_reference_link',
$this->citeKeyFormatter->normalizeKey(
$this->citeKeyFormatter->refKey( $key, $count )
),
$this->citeKeyFormatter->normalizeKey(
$this->citeKeyFormatter->getReferencesKey( $key . $subkey )
),
Sanitizer::safeEncodeAttribute(
$this->getLinkLabel( $label, $group,
( ( $group === self::DEFAULT_GROUP ) ? '' : "$group " ) . $contLang->formatNum( $label ) )
)
)->inContentLanguage()->plain()
);
}
/**
* This does approximately the same thing as
* Language::listToText() but due to this being used for a
@ -762,23 +700,6 @@ class Cite {
$this->mBacklinkLabels = preg_split( '/\s+/', $text );
}
/**
* Generate the labels to pass to the
* 'cite_reference_link' message instead of numbers, the format is an
* arbitrary number of tokens separated by whitespace.
*
* @param string $group
* @param string $message
*/
private function genLinkLabels( $group, $message ) {
$text = false;
$msg = wfMessage( $message )->inContentLanguage();
if ( $msg->exists() ) {
$text = $msg->plain();
}
$this->mLinkLabels[$group] = $text ? preg_split( '/\s+/', $text ) : false;
}
/**
* Gets run when Parser::clearState() gets run, since we don't
* want the counts to transcend pages and other instances

View file

@ -0,0 +1,128 @@
<?php
namespace Cite;
use MediaWiki\MediaWikiServices;
use Parser;
use Sanitizer;
class FootnoteMarkFormatter {
/**
* The links to use per group, in order.
*
* @var (string[]|false)[]
*/
private $linkLabels = [];
/**
* @var Parser
*/
private $parser;
/**
* @var CiteKeyFormatter
*/
private $citeKeyFormatter;
/**
* @var CiteErrorReporter
*/
private $errorReporter;
/**
* @param Parser $parser
* @param CiteErrorReporter $errorReporter
* @param CiteKeyFormatter $citeKeyFormatter
*/
public function __construct(
Parser $parser,
CiteErrorReporter $errorReporter,
CiteKeyFormatter $citeKeyFormatter
) {
$this->parser = $parser;
$this->citeKeyFormatter = $citeKeyFormatter;
$this->errorReporter = $errorReporter;
}
/**
* Generate a link (<sup ...) for the <ref> element from a key
* and return XHTML ready for output
*
* @suppress SecurityCheck-DoubleEscaped
* @param string $group
* @param string $key The key for the link
* @param int|null $count The index of the key, used for distinguishing
* multiple occurrences of the same key
* @param int $label The label to use for the link, I want to
* use the same label for all occurrences of
* the same named reference.
* @param string|null $subkey
*
* @return string
*/
public function linkRef( $group, $key, $count, $label, $subkey ) {
$contLang = MediaWikiServices::getInstance()->getContentLanguage();
return $this->parser->recursiveTagParse(
wfMessage(
'cite_reference_link',
$this->citeKeyFormatter->normalizeKey(
$this->citeKeyFormatter->refKey( $key, $count )
),
$this->citeKeyFormatter->normalizeKey(
$this->citeKeyFormatter->getReferencesKey( $key . $subkey )
),
Sanitizer::safeEncodeAttribute(
$this->getLinkLabel( $label, $group,
( ( $group === Cite::DEFAULT_GROUP ) ? '' : "$group " ) .
$contLang->formatNum( $label ) )
)
)->inContentLanguage()->plain()
);
}
/**
* Generate a custom format link for a group given an offset, e.g.
* the second <ref group="foo"> is b if $this->mLinkLabels["foo"] =
* [ 'a', 'b', 'c', ...].
* Return an error if the offset > the # of array items
*
* @param int $offset
* @param string $group The group name
* @param string $label The text to use if there's no message for them.
*
* @return string
*/
private function getLinkLabel( $offset, $group, $label ) {
$message = "cite_link_label_group-$group";
if ( !isset( $this->linkLabels[$group] ) ) {
$this->genLinkLabels( $group, $message );
}
if ( $this->linkLabels[$group] === false ) {
// Use normal representation, ie. "$group 1", "$group 2"...
return $label;
}
return $this->linkLabels[$group][$offset - 1]
?? $this->errorReporter->plain( 'cite_error_no_link_label_group', [ $group, $message ] );
}
/**
* Generate the labels to pass to the
* 'cite_reference_link' message instead of numbers, the format is an
* arbitrary number of tokens separated by whitespace.
*
* @param string $group
* @param string $message
*/
private function genLinkLabels( $group, $message ) {
$text = false;
$msg = wfMessage( $message )->inContentLanguage();
if ( $msg->exists() ) {
$text = $msg->plain();
}
$this->linkLabels[$group] = $text ? preg_split( '/\s+/', $text ) : false;
}
}