User:Jeblad/Cite

Cite is an extension to add citations and references to wikitext. This variant also makes it possible to list the citations inside the references-tag, not only the ref-tag. This moves large templates out of the wikitext, making the whole text more readable while editing.

The citations or references can be identified inside the references-tag by starting at the leftmost posision, then running up to a pipe (|), equal sign (=), colon, newline or cariage return. Extra text that is part of this citation can continue on the next line if it is properly indented. Next line starting in the leftmost position has a new identifier.

Traditional style for a reference

Alternate style for a reference

Note that the following code is not properly tested, and is a replacement for Cite.php from Extension:Cite.

*		 * This works because: * * PHP's datastructures are guarenteed to be returned in the *  order that things are inserted into them (unless you mess		 *   with that) * * User supplied keys can't be integers, therefore avoiding *  conflict with anonymous keys *		 * @var array **/		var $mRefs = array; /**		 * Count for user displayed output (ref[1], ref[2], ...) *		 * @var int */		var $mOutCnt = 0;

/**		 * Internal counter for anonymous references, seperate from * $mOutCnt because anonymous references won't increment it, * but will incremement $mOutCnt *		 * @var int */		var $mInCnt = 0;

/**		 * The backlinks, in order, to pass as $3 to		 * 'cite_references_link_many_format', defined in		 * 'cite_references_link_many_format_backlink_labels *		 * @var array */		var $mBacklinkLabels; /**		 * @var object */		var $mParser; /**		 * True when a or " );				}			} else {				$this->mInCite = true;				$ret = $this->guardedReferences( $str, $argv, $parser );				$this->mInCite = false;				return $ret;			}		}		function guardedReferences( $str, $argv, $parser ) {			$this->mParser = $parser;			/*if ( $str !== null )				return $this->error( CITE_ERROR_REFERENCES_INVALID_INPUT );			else*/ if ( count( $argv ) )				return $this->error( CITE_ERROR_REFERENCES_INVALID_PARAMETERS );			else				return $this->referencesFormat( $str );		}

/**		 * Make output to be returned from the references function *		 * @param string $str The content of the reference * @return string XHTML ready for output */		function referencesFormat( $str ) { if ( count( $this->mRefs ) == 0 ) return '';

$str = "\n". $str. "\n"; $addents = Array; $fragments = array; # match lines like these: # somename|This is the reference text for some text # lines may continue on following lines if indented preg_match_all( "/\\n([^|=:\\n\\t ]+?)[\\t ]*[|=:][\\t ]*([^\\n]*(?:\\n[\\t ][^\\n]*)*)/s", $str, $fragments, PREG_SET_ORDER ); foreach ( $fragments as $fragment ) { $count++; # Skip empty lines if ( count( $fragment ) == 0 ) { continue; }				$addents[$fragment[1]] = $fragment[2]; }			$ent = array; foreach ( $this->mRefs as $k => $v ) $ent[] = $this->referencesFormatEntry( $k, (is_array( $v ) && $v['text']=='' ) ? $addents[$k] : $v); $prefix = wfMsgForContentNoTrans( 'cite_references_prefix' ); $suffix = wfMsgForContentNoTrans( 'cite_references_suffix' ); $content = implode( "\n", $ent ); // Live hack: parse adds two newlines on WM, can't reproduce it locally -ævar return rtrim( $this->parse( $prefix. $content. $suffix ), "\n" ); }

/**		 * Format a single entry for the referencesFormat function *		 * @param string $key The key of the reference * @param mixed $val The value of the reference, string for anonymous *                  references, array for user-suppplied * @return string Wikitext */		function referencesFormatEntry( $key, $val ) { // Anonymous reference if ( ! is_array( $val ) ) return wfMsgForContentNoTrans(						'cite_references_link_one',						$this->referencesKey( $key ),						$this->refKey( $key ),						$val					); else if ($val['text']=='') return wfMsgForContentNoTrans(						'cite_references_link_one',						$this->referencesKey( $key ),						$this->refKey( $key, $val['count'] ),						$this->error(CITE_ERROR_REFERENCES_NO_TEXT)					); // Standalone named reference, I want to format this like an // anonymous reference because displaying "1. 1.1 Ref text" is			// overkill and users frequently use named references when they // don't need them for convenience else if ( $val['count'] === 0 ) return wfMsgForContentNoTrans(						'cite_references_link_one',						$this->referencesKey( $key ),						$this->refKey( $key, $val['count'] ),						( $val['text'] != '' ? $val['text'] : $this->error( CITE_ERROR_REFERENCES_NO_TEXT ) )					); // Named references with >1 occurrences else { $links = array;

for ( $i = 0; $i <= $val['count']; ++$i ) { $links[] = wfMsgForContentNoTrans(							'cite_references_link_many_format',							$this->refKey( $key, $i ),							$this->referencesFormatEntryNumericBacklinkLabel( $val['number'], $i, $val['count'] ),							$this->referencesFormatEntryAlternateBacklinkLabel( $i )					); }

$list = $this->listToText( $links );

return wfMsgForContentNoTrans( 'cite_references_link_many',						$this->referencesKey( $key ),						$list,						( $val['text'] != '' ? $val['text'] : $this->error( CITE_ERROR_REFERENCES_NO_TEXT ) )					); }		}

/**		 * Generate a numeric backlink given a base number and an * offset, e.g. $base = 1, $offset = 2; = 1.2 * Since bug #5525, it correctly does 1.9 -> 1.10 as well as 1.099 -> 1.100 *		 * @static *		 * @param int $base The base * @param int $offset The offset * @param int $max Maximum value expected. * @return string */		function referencesFormatEntryNumericBacklinkLabel( $base, $offset, $max ) { global $wgContLang; $scope = strlen( $max ); $ret = $wgContLang->formatNum(				sprintf("%s.%0{$scope}s", $base, $offset)			); return $ret; }

/**		 * Generate a custom format backlink given an offset, e.g.		 * $offset = 2; = c if $this->mBacklinkLabels = array( 'a',		 * 'b', 'c', ...). Return an error if the offset > the # of		 * array items *		 * @param int $offset The offset *		 * @return string */		function referencesFormatEntryAlternateBacklinkLabel( $offset ) { if ( !isset( $this->mBacklinkLabels ) ) { $this->genBacklinkLabels; }			if ( isset( $this->mBacklinkLabels[$offset] ) ) { return $this->mBacklinkLabels[$offset]; } else { // Feed me! return $this->error( CITE_ERROR_REFERENCES_NO_BACKLINK_LABEL ); }		}

/**		 * Return an id for use in wikitext output based on a key and * optionally the # of it, used in