diff --git a/Cite.i18n.php b/Cite.i18n.php index 3d3e82c..44763d7 100644 --- a/Cite.i18n.php +++ b/Cite.i18n.php @@ -76,7 +76,7 @@ no text was provided for refs named $1', # Although I could just use # instead of
  • above and nothing here that # will break on input that contains linebreaks - 'cite_references_prefix' => '
      ', + 'cite_references_prefix' => '
        ', 'cite_references_suffix' => '
      ', ); diff --git a/Cite.php b/Cite.php index e4d7e92..2637b15 100644 --- a/Cite.php +++ b/Cite.php @@ -34,7 +34,7 @@ $wgExtensionMessagesFiles['Cite'] = dirname( __FILE__ ) . "/Cite.i18n.php"; $wgAutoloadClasses['Cite'] = dirname( __FILE__ ) . "/Cite_body.php"; $wgSpecialPageGroups['Cite'] = 'pagetools'; -define( 'CITE_DEFAULT_GROUP', '' ); +define( 'CITE_DEFAULT_GROUP', 'decimal' ); /** * The emergency shut-off switch. Override in local settings to disable * groups; or remove all references from this file to enable unconditionally diff --git a/Cite_body.php b/Cite_body.php index 62fb086..98c0896 100644 --- a/Cite_body.php +++ b/Cite_body.php @@ -139,6 +139,36 @@ class Cite { var $mRefCallStack = array(); /** + * An array of call-back functions for hardcoded ref groups + * keys are group names + * first column is name of call back function + * second column is secodn parameter of call back func + * + * 'css-value' => array('nameOfCallbackFunc', ), + * + * @var Array + */ + private $mGroupCallbacks = array( + 'lower-alpha' => array('formatAlphabet', 'abcdefghijklmnopqrstuvwxyz'), + 'upper-alpha' => array('formatAlphabet', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), + 'lower-greek' => array('formatAlphabet', 'αβγδεζηθικλμνξοπρστυφχψω'), + 'lower-roman' => array('formatRoman', 0), + 'upper-roman' => array('formatRoman', 1), + ); + + /** + * An array for roman numerals + * @var Array + */ + private $mRomans = array( + 'base' => array('', 'i', 'ii', 'iii', 'iv', 'v', 'vi', 'vii', 'viii', 'ix'), + 'tens' => array('', 'x', 'xx', 'xxx', 'xl', 'l', 'lx', 'lxx', 'lxxx', 'xc'), + 'hundreds' => array('', 'c', 'cc', 'ccc', 'cd', 'd', 'dc', 'dcc', 'dccc', 'cm'), + 'thousand' => 'm', + ); + + /** * Did we install us into $wgHooks yet? * @var Boolean */ @@ -634,7 +664,20 @@ class Cite { $ent[] = $this->referencesFormatEntry( $k, $v ); } - $prefix = wfMsgForContentNoTrans( 'cite_references_prefix' ); + // These variables allow for the insertion extra classes + // and styles into cite_references_prefix + $extraAttributes = ''; + $extraClasses = ''; + $extraStyles = ''; + + // add css class and css style list type for hard coded group types + if ( in_array ( $group, array_keys($this->mGroupCallbacks))) { + $extraClasses .= ' ' . $group; + $extraStyles .= ' list-style-type: ' . $group . ';'; + } + + $extraAttributes .= empty($extraStyles) ? '' : ' style="' . $extraStyles . '"'; + $prefix = wfMsgForContentNoTrans( 'cite_references_prefix', $extraClasses, $extraAttributes); $suffix = wfMsgForContentNoTrans( 'cite_references_suffix' ); $content = implode( "\n", $ent ); @@ -825,11 +868,28 @@ class Cite { */ function getLinkLabel( $offset, $group, $label ) { $message = "cite_link_label_group-$group"; + + // No need to mess around with decimal group + if ( $group === CITE_DEFAULT_GROUP ) { + return $label; + } + + // If this group is hardcoded, call the callback function + if ( in_array ( $group, array_keys($this->mGroupCallbacks))) { + // name of callback unction to call + $method = $this->mGroupCallbacks[$group][0]; + + // call the function and return + return $this->$method($offset, $this->mGroupCallbacks[$group][1]); + } + + // Lets's see if there customs labels if ( !isset( $this->mLinkLabels[$group] ) ) { $this->genLinkLabels( $group, $message ); } - if ( $this->mLinkLabels[$group] === false ) { - // Use normal representation, ie. "$group 1", "$group 2"... + + // Use normal representation, ie. "$group 1", "$group 2"... + if ( $this->mLinkLabels[$group] === false ) { return $label; } @@ -842,6 +902,55 @@ class Cite { } /** + * Convert a arabic numeral into a roman numeral + * + * @private + * + * @param int $offset the number to be converted in roman numerial + * @return string $upper Whether upper case + */ + private function formatRoman($offset, $upper) { + # No major browser supports roman numerials above 3999, so why should we? + if( $offset > 3999 ) return $offset; + + $thousands = intval ( $offset / 1000 ); + $offset %= 1000; + + $hundreds = intval ( $offset / 100 ); + $offset %= 100; + + $tens = intval ( $offset / 10 ); + $offset %= 10; + + $lower = str_repeat ( $this->mRomans['thousand'], $thousands ) . $this->mRomans['hundreds'][$hundreds] . $this->mRomans['tens'][$tens] . $this->mRomans['base'][$offset]; + + return ($upper ? strtoupper ( $lower ) : $lower); + } + + /** + * Convert a decimal numeral into one based on a given alphabet + * ie 27 into aa and 2 into b + * + * @private + * + * @param int $offset the number to be converted in roman numerial + * @return array $upper an array of the alphabet + */ + private function formatAlphabet($offset, $alphabet) { + $result = ''; + + $offset--; + $len = mb_strlen($alphabet); + + while($offset >= 0) { + $result = mb_substr( $alphabet, $offset % $len, 1 ) . $result; + $offset = intval($offset / $len) - 1; + } + + return $result; + } + + /** * Return an id for use in wikitext output based on a key and * optionally the number of it, used in , not * (since otherwise it would link to itself) diff --git a/citeParserTests.txt b/citeParserTests.txt index 738dc50..28d549f 100644 --- a/citeParserTests.txt +++ b/citeParserTests.txt @@ -389,3 +389,31 @@ Bug 31374 regression check: nested strip items
    !! end + +!! test + with normal group +!! input +Wikipedia normal.blah blah + + +!! result +

    Wikipedia normal.[Note 1] +

    +
    1. blah blah +
    + +!! end + +!! test + with hard coded roman numeral group +!! input +Wikipedia classical.Cato the even younger + + +!! result +

    Wikipedia classical.[i] +

    +
    1. Cato the even younger +
    + +!! end