/**
	 * Constructor. Overwrites a few style properties of parent class, but
	 * otherwise keeps everything the same as default.
	 */
	public function __construct() {
		parent::__construct();
		$this->punctuationInQuotes   = True;
		$this->replaceAmpersands     = False;
		$this->eraWithoutPunctuation = True;
		$this->eraNoSpace            = False;
		$this->eraBeforeYear         = False;
		$this->styleHTML             = 'Chicago';

	}
	/**
	 * Renders the citation and stores the result in $this->citation.
	 * @param WCStyle $style
	 * @return array = the rendered citation and sorting parts.
	 */
	public function render() {

		# Build the citation
		if ( $this->citationLength->key == WCCitationLengthEnum::short ) {
			switch ( $this->citationType->key ) {
				case WCCitationTypeEnum::note:
					list( $citation, $sortingParts ) = $this->style->renderShortNoteCitation( $this );
					break;
				case WCCitationTypeEnum::inline:
					list( $citation, $sortingParts ) = $this->style->renderShortInlineCitation( $this );
					break;
				case WCCitationTypeEnum::authorDate:
					list( $citation, $sortingParts ) = $this->style->renderShortAuthorDateCitation( $this );
			}
		} else {
			switch ( $this->citationType->key ) {
				case WCCitationTypeEnum::note:
					list( $citation, $sortingParts ) = $this->style->renderLongNoteCitation( $this );
					break;
				case WCCitationTypeEnum::biblio:
					list( $citation, $sortingParts ) = $this->style->renderLongBiblioCitation( $this );
					break;
				case WCCitationTypeEnum::authorDate:
					list( $citation, $sortingParts ) = $this->style->renderLongAuthorDateCitation( $this );
					break;
				default: # case WCCitationTypeEnum::inline:
					list( $citation, $sortingParts ) = $this->style->renderLongInlineCitation( $this );
			}
		}

		# Wrap the entire citation in an HTML span element with classes.
		$classHTML = WCStyle::citationHTML . ' ' .
			$this->style->styleHTML . ' ' .
			$this->reference->getWorkType() . ' ' .
			$this->citationType . ' ' .
			$this->citationLength;

		return array( WCStyle::wrapHTMLSpan( $citation, $classHTML ), $sortingParts );

	}
Example #3
0
	/**
	 * Composes a single name based on name parts and flags
	 *
	 * @param boolean $namesort = whether the surname appears first
	 * @return string = composed name
	 */
	public function render(
			WCStyle $style,
			WCCitationLengthEnum $citationLength,
			$endSeparator = '',
			$namesort = False ) {


		$surname = isset( $this->parts[ WCNamePartEnum::surname ] ) ?
			$this->parts[ WCNamePartEnum::surname ] : Null;
		$given   = isset( $this->parts[ WCNamePartEnum::given ] ) ?
			$this->transformGivenNames( $this->parts[ WCNamePartEnum::given ] ) : Null;
		$link    = isset( $this->parts[ WCNamePartEnum::nameLink ] ) ?
			$this->parts[ WCNamePartEnum::nameLink ] : Null;
		$dp      = isset( $this->parts[ WCNamePartEnum::droppingParticle ] ) ?
			$this->parts[ WCNamePartEnum::droppingParticle ] : Null;
		$dpSpace = $ndpSpace = $suffixSpace = '';
		if ( $dp ) {
			# if the dropping particle ends in punctuation, there is no space between it and the surname or dropping particle.
			if ( preg_match( '/\p{P}$/u', $dp, $matches ) ) {
				$dpSpace = '';
			} else {
				$dpSpace = $style->nameWhitespace;
			}
		}
		$ndp     = isset( $this->parts[ WCNamePartEnum::nonDroppingParticle ] ) ?
			$this->parts[ WCNamePartEnum::nonDroppingParticle ] : Null;
		if ( $ndp ) {
			# if the non-dropping particle ends in punctuation, there is no space between it and the surname.
			if ( preg_match( '/\p{P}$/u', $ndp, $matches ) ) {
				$ndpSpace = '';
			} else {
				$ndpSpace = $style->nameWhitespace;
			}
		}
		$suffix  = isset( $this->parts[ WCNamePartEnum::suffix ] ) ?
			$this->parts[ WCNamePartEnum::suffix ] : Null;
		if ( $suffix ) {
			$suffixSpace = $style->nameSuffixDelimiter;
		}
		$literal = isset( $this->parts[ WCNamePartEnum::literal ] ) ?
			$this->parts[ WCNamePartEnum::literal ] : Null;

		# literal/organizational name
		if ( $literal ) {
			# Trim redundant separator characters
			$chrL = mb_substr( $literal, -1, 1 );
			$chrR = mb_substr( $endSeparator, 0, 1 );
			if ( $chrL == $chrR ) {
				$endSeparator = ltrim( $endSeparator, $chrR );
			}
			return WCStyle::makeWikiLink( $link, $literal, WCStyle::nameHTML ) . $endSeparator;
		};

		$pre = $post = '';

		# short form
		if ( $citationLength->key == WCCitationLengthEnum::short ) {
			if ( $surname ) {
				if ( $ndp ) {
					$surname = $ndp . $ndpSpace . $surname;
				}
			} else {
				if ( $link ) {
					$post = $link;
				} elseif ( $given ) {
					$post = $given;
				} else {
					$post = $dp . $dpSpace . $ndp . $ndpSpace . $style->missingName . $suffixSpace . $suffix;
				}
			}
		}

		# long form, name sort order
		elseif ( $namesort ) {
			if ( $surname ) {
				if ( !$style->demoteNonDroppingParticle && $ndp ) {
					$pre = $ndp . $ndpSpace;
				}
				if ( $given ) {
					$post = $style->sortOrderSurnameDelimiter . $given;
					if ( $dp ) {
						$post .= $style->nameWhitespace . $dp;
					}
					if ( $style->demoteNonDroppingParticle && $ndp ) {
						if ( $dp ) {
							$post .= $dpSpace;
						} else {
							$post .= $style->nameWhitespace;
						}
						$post .= $ndp;
					}
				} else {
					if ( $dp ) {
						$post .= $style->sortOrderSurnameDelimiter . $dp;
					}
					if ( $style->demoteNonDroppingParticle && $ndp ) {
						if ( $dp ) {
							$post .= $dpSpace;
						} else {
							$post .= $style->nameWhitespace;
						}
						$post .= $ndp;
					}
				}
				if ( $suffix ) {
					$post .= $suffixSpace . $suffix;
				}
			} elseif ( $link ) {
				$post = $link;
			} elseif ( $given ) {
				if ( $dp || $ndp || $suffix ) {
					if ( !$style->demoteNonDroppingParticle && $ndp ) {
						$post = $ndp . $ndpSpace;
					}
					$post = $style->missingName . $style->sortOrderSurnameDelimiter . $given;
					if ( $dp ) {
						$post .= $style->nameWhitespace . $dp;
					}
					if ( $style->demoteNonDroppingParticle && $ndp ) {
						if ( $dp ) {
							$post .= $dpSpace;
						} else {
							$post .= $style->nameWhitespace;
						}
						$post .= $ndp;
					}
					if ( $suffix ) {
						$post .= $suffixSpace . $suffix;
					}
				} else {
					$post = $given;
				}
			} else {
				if ( !$style->demoteNonDroppingParticle && $ndp ) {
					$post = $ndp . $ndpSpace;
				} else {
					$post = '';
				}
				$post = $style->missingName . $style->sortOrderSurnameDelimiter . $style->missingName;
				if ( $dp ) {
					$post .= $style->nameWhitespace . $dp;
				}
				if ( $style->demoteNonDroppingParticle && $ndp ) {
					if ( $dp ) {
						$post .= $dpSpace;
					} else {
						$post .= $style->nameWhitespace;
					}
					$post .= $ndp;
				}
				if ( $suffix ) {
					$post .= $suffixSpace . $suffix;
				}
			}
		}

		# long form, non-name sort order
		else {
			if ( $surname ) {
				if ( $given ) {
					$pre = $given . $style->nameWhitespace;
				}
				$pre .= $dp . $dpSpace . $ndp . $ndpSpace;
				$post = $suffixSpace . $suffix;
			} elseif ( $link ) {
				$post = $link;
			} elseif ( $given ) {
				$post = $given . $style->nameWhitespace;
				if ( $dp || $ndp || $suffix ) {
					$post .= $dp . $dpSpace . $ndp . $ndpSpace . $style->missingName . $suffixSpace . $suffix;
				}
			} else  {
				$post = $style->missingName . $style->nameWhitespace;
				if ( $dp ) {
					$post .= $dp . $dpSpace . $ndp . $ndpSpace . $style->missingName . $suffixSpace . $suffix;
				}
			}
		}

		$htmlClass = WCStyle::nameHTML;

		if ( $namesort ) {
			$surnameClass = WCStyle::surnameSortOrderHTML;
		} else {
			$surnameClass = WCStyle::surnameHTML;
		}

		# Trim redundant separator characters
		if ( $post ) {
			$chrL = mb_substr( $post, -1, 1 );
			$chrR = mb_substr( $endSeparator, 0, 1 );
			if ( $chrL == $chrR ) {
				$endSeparator = ltrim( $endSeparator, $chrR );
			}
			if ( $surname) {
				$nameText = $pre . WCStyle::wrapHTMLSpan( $surname, $surnameClass ) . $post;
			} else {
				$nameText = $pre . $post;
			}
		} elseif ( $surname) {
			$chrL = mb_substr( $surname, -1, 1 );
			$chrR = mb_substr( $endSeparator, 0, 1 );
			if ( $chrL == $chrR ) {
				$endSeparator = ltrim( $endSeparator, $chrR );
			}
			if ( $pre ) {
				$nameText = $pre . WCStyle::wrapHTMLSpan( $surname, $surnameClass );
			} else {
				$nameText = $surname;
				$htmlClass .= ' ' . $surnameClass;
			}
		} else {
			$chrL = mb_substr( $pre, -1, 1 );
			$chrR = mb_substr( $endSeparator, 0, 1 );
			if ( $chrL == $chrR ) {
				$endSeparator = ltrim( $endSeparator, $chrR );
			}
			$nameText = $pre;
		}

		return WCStyle::makeWikiLink( $link, $nameText, $htmlClass ) . $endSeparator;

	}
	/**
	 * Format title with quotes and other optional styling.
	 *
	 * This will wrap the title in an HTML <cite> element. The title will also
	 * be quoted if the reference style is designated as a quoted-style
	 * title in WCTypeEnum::$titleFormat.
	 * @param WCStyle $style
	 * @param string $endSeparator
	 * @return string
	 */
	public function render( WCStyle $style, $endSeparator = '' ) {
		$endSeparator = $this->extendSuffix( $endSeparator );
		if ( $this->titleObject ) {
			$title = $this->titleObject->getTitle();

			# "quoted"-type title
			if ( $this->titleFormat->key == WCTitleFormat::quoted ) {

				if ( $style->punctuationInQuotes ) { # Punctuation is inside quotes:

					# Check for final quotes at the end of the title:
					$p = preg_split( '/(<\/q>+)$/uS', $title, 2, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
					if ( count( $p ) >= 2 ) {
						$title = $p[0] . mb_substr( $endSeparator, 0, 1 ) . $p[1];
						$title = $style->convertSemanticToCharacterQuotes( $title, True );
						$title = $style->transformTitle( $title );
						$title = '<cite class="' . WCStyle::quotedTitleHTML . '">' . $title . '</cite>';
						$title = $style->quote( $title );
					} else {
						$title = $style->convertSemanticToCharacterQuotes( $title, True );
						$title = $style->transformTitle( $title );
						$chrL = mb_substr( $title, -1, 1 );
						$chrR = mb_substr( $endSeparator, 0, 1 );
						if ( $chrR && ( $chrL === $chrR ) ) {
							# Wrap <cite> tag inside the quotes.
							$title = '<cite class="' . WCStyle::quotedTitleHTML . '">' . $title . '</cite>';
							$title = $style->quote( $title );
						} else {
							$title = $style->quote( $title . mb_substr( $endSeparator, 0, 1 ) );
							# Wrap <cite> tag outside the quotes.
							$title = '<cite class="' . WCStyle::quotedTitleHTML . '">' . $title . '</cite>';
						}
					}
					return $this->prefix . $title . mb_substr( $endSeparator, 1 );
				} else {
					# Punctuation follows quotes.
					$title = $style->convertSemanticToCharacterQuotes( $title, True );
					$title = $style->transformTitle( $title );
					$title = '<cite class="' . WCStyle::quotedTitleHTML . '">' . $title . '</cite>';
					return $this->prefix . $style->quote( $title ) . $endSeparator;
				}
			}

			# "italic"-type title
			$title = $style->convertSemanticToCharacterQuotes( $title );
			$title = $style->transformTitle( $title );
			$chrL = mb_substr( $title, -1, 1 );
			$chrR = mb_substr( $endSeparator, 0, 1 );
			if ( $chrR && $chrL == $chrR ) {
				$endSeparator = ltrim( $endSeparator, $chrR );
			}
			if ( $this->titleFormat->key == WCTitleFormat::italic ) {
				# "Italic"-type title
				return $this->prefix . '<cite class="' . WCStyle::italicTitleHTML . '">' . $title . '</cite>' . $endSeparator;
			} else {
				# WCTitleFormat::normal:
				return $this->prefix . '<cite>' . $title . '</cite>' . $endSeparator;
			}
		} else {
			return $this->prefix . $style->segmentMissing . $endSeparator;
		}
	}