Skip to content

Commit

Permalink
Merge pull request #145 from AlexanderBlanchardAC/24.12.00_harvard_ci…
Browse files Browse the repository at this point in the history
…tations

24.12.00 harvard citations
  • Loading branch information
AlexanderBlanchardAC authored Dec 3, 2024
2 parents 7d838b8 + 772ec43 commit 8fb012a
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 3 deletions.
2 changes: 2 additions & 0 deletions code/web/RecordDrivers/EbscoRecordDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,8 @@ public function getCitation($format) {
return $citation->getChicagoHumanities();
case 'MLA':
return $citation->getMLA();
case 'Harvard':
return $citation->getHarvard();
}
return '';
}
Expand Down
2 changes: 2 additions & 0 deletions code/web/RecordDrivers/EbscohostRecordDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,8 @@ public function getCitation($format) {
return $citation->getChicagoHumanities();
case 'MLA':
return $citation->getMLA();
case 'Harvard':
return $citation->getHarvard();
}
return '';
}
Expand Down
4 changes: 4 additions & 0 deletions code/web/RecordDrivers/GroupedWorkDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ public function getCitation($format) {
if (!empty($primary)) {
$authors[] = $primary;
}
$authors = array_unique(array_merge($authors, $this->getContributors()));

// Collect all details for citation builder:
$publishers = $this->getPublishers();
Expand Down Expand Up @@ -700,6 +701,8 @@ public function getCitation($format) {
return $citation->getChicagoHumanities();
case 'MLA':
return $citation->getMLA();
case 'Harvard':
return $citation->getHarvard();
}
return '';
}
Expand All @@ -718,6 +721,7 @@ public function getCitationFormats() {
'ChicagoHumanities',
'ChicagoAuthDate',
'MLA',
'Harvard',
];
}

Expand Down
3 changes: 3 additions & 0 deletions code/web/RecordDrivers/GroupedWorkSubDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ public function getCitation($format) {
return $citation->getChicagoHumanities();
case 'MLA':
return $citation->getMLA();
case 'Harvard':
return $citation->getHarvard();
}
return '';
}
Expand All @@ -147,6 +149,7 @@ public function getCitationFormats() {
'ChicagoHumanities',
'ChicagoAuthDate',
'MLA',
'Harvard',
];
}

Expand Down
2 changes: 2 additions & 0 deletions code/web/RecordDrivers/IndexRecordDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ public function getCitation($format) {
return $citation->getChicagoHumanities();
case 'MLA':
return $citation->getMLA();
case 'Harvard':
return $citation->getHarvard();
}
return '';
}
Expand Down
2 changes: 2 additions & 0 deletions code/web/RecordDrivers/SummonRecordDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ public function getCitation($format) {
return $citation->getChicagoHumanities();
case 'MLA':
return $citation->getMLA();
case 'Harvard':
return $citation->getHarvard();
}
return '';
}
Expand Down
5 changes: 5 additions & 0 deletions code/web/interface/themes/responsive/Citation/harvard.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{if !empty($harvardDetails.authors)}{$harvardDetails.authors|escape} {/if}
{if !empty($harvardDetails.year)}({$harvardDetails.year|escape}). {/if}
<span style="font-style:italic;">{$harvardDetails.title|escape}</span>
{if !empty($harvardDetails.edition)} {$harvardDetails.edition|escape} {/if}
{if !empty($harvardDetails.publisher)}{$harvardDetails.publisher|escape}. {/if}
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,19 @@
</p>
{/if}

{if !empty($harvard)}
<b>{translate text="Harvard Citation" isPublicFacing=true}</b> {if !empty($showCitationStyleGuides)}<span class="styleGuide"><a href="https://library-guides.ucl.ac.uk/harvard/a-z" target="_blank" aria-label="{translate text='style guide' isPublicFacing=true} ({translate text='opens in new window' isPublicFacing=true})">({translate text="style guide" isPublicFacing=true})</a></span>{/if}
<p style="width: 95%; padding-left: 25px; text-indent: -25px;">
{include file=$harvard}
</p>
{/if}

{if !empty($mla)}
<b>{translate text="MLA Citation, 9th Edition" isPublicFacing=true}</b> {if !empty($showCitationStyleGuides)}<span class="styleGuide"><a href="https://owl.purdue.edu/owl/research_and_citation/mla_style/mla_formatting_and_style_guide/mla_general_format.html" target="_blank" aria-label="{translate text='style guide' isPublicFacing=true} ({translate text='opens in new window' isPublicFacing=true})">({translate text="style guide" isPublicFacing=true})</a></span>{/if}
<p style="width: 95%; padding-left: 25px; text-indent: -25px;">
{include file=$mla}
</p>
{/if}

</div>
<div class="alert alert-warning">
<strong>{translate text="Note!" isPublicFacing=true}</strong> {translate text="Citations contain only title, author, edition, publisher, and year published. Citations should be used as a guideline and should be double checked for accuracy. Citation formats are based on standards as of August 2021." isPublicFacing=true}
Expand Down
7 changes: 7 additions & 0 deletions code/web/interface/themes/responsive/Record/cite.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@
</p>
{/if}

{if !empty($harvard)}
<b>{translate text="Harvard Citation" isPublicFacing=true}</b> {if !empty($showCitationStyleGuides)}<span class="styleGuide"><a href="https://library-guides.ucl.ac.uk/harvard/a-z" target="_blank" aria-label="{translate text='style guide' isPublicFacing=true} ({translate text='opens in new window' isPublicFacing=true})">({translate text="style guide" isPublicFacing=true})</a></span>{/if}
<p style="width: 95%; padding-left: 25px; text-indent: -25px;">
{include file=$harvard}
</p>
{/if}

{if !empty($mla)}
<b>{translate text="MLA Citation, 9th Edition" isPublicFacing=true}</b> {if !empty($showCitationStyleGuides)}<span class="styleGuide"><a href="https://owl.purdue.edu/owl/research_and_citation/mla_style/mla_formatting_and_style_guide/mla_general_format.html" target="_blank">({translate text="style guide" isPublicFacing=true})</a></span>{/if}
<p style="width: 95%; padding-left: 25px; text-indent: -25px;">
Expand Down
4 changes: 4 additions & 0 deletions code/web/release_notes/24.12.00.MD
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@
### Web Builder Updates
- Remove edit button in admin view of Grapes JS Pages as breadcrumbs allow navigation back to the editor and are in keeping with the rest of Aspen. (*AB*)

### Citation Updates
- Add Harvard Citation style to the list that appears in the citation section on each full record. (*AB*)
- Add Harvard Citation style to the dropdown list in the 'Generate Citation' section in the lists section. (*AB*)

// Chloe - PTFSE

// Pedro - PTFSE
Expand Down
154 changes: 152 additions & 2 deletions code/web/sys/CitationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static function getCitationFormats() {
'APA' => 'APA',
'ChicagoHumanities' => 'Chicago/Turabian - Humanities',
'ChicagoAuthDate' => 'Chicago/Turabian - Author Date',
'Harvard' => 'Harvard',
'MLA' => 'MLA',
];
}
Expand Down Expand Up @@ -167,6 +168,26 @@ public function getChicagoHumanities() {
return 'Citation/chicago-humanities.tpl';
}

/**
* This function assigns all the necessary variables and then returns a template
* name to display a Harvard citation.
*
* @access public
* @return string
*/
public function getHarvard() {
global $interface;
$harvard = [
'title' => $this->getHarvardTitle(),
'authors' => $this->getHarvardAuthors(),
'publisher' => $this->getPublisher(),
'year' => $this->getHarvardYear(),
'edition' => $this->getHarvardEdition(),
];
$interface->assign('harvardDetails', $harvard);
return 'Citation/harvard.tpl';
}

/**
* Get Chicago Auth Date citation.
*
Expand Down Expand Up @@ -361,6 +382,36 @@ private function capitalizeTitle($str) {
return ucfirst(join(' ', $newwords));
}

/**
* Convert a string to sentence case
*
* @access private
* @param string $text
* @return string
*/
private function convertToSentenceCase($text) {

$abbreviations = [];

//Regex pattern to match common exceptions e.g. "R&B" and "USA"
$pattern = '/\b[A-Z&]+\b/';
//Generate placeholders for abbreviations that match the pattern
$text = preg_replace_callback($pattern, function($matches) use (&$abbreviations) {
$placeholder = '[[abbr_' . count($abbreviations) . ']]';
$abbreviations[$placeholder] = $matches[0];
//Replace abbreviation with placeholder
return $placeholder;
}, $text);

$text = strtolower($text);
$text = ucfirst($text);
//Replace placeholders with their original abbreviations - unchanged by strtolower
foreach ($abbreviations as $placeholder => $abbr) {
$text = str_replace($placeholder, $abbr, $text);
}
return $text;
}

/**
* Get the full title for an APA citation.
*
Expand All @@ -378,10 +429,32 @@ private function getAPATitle() {
/*if (!((substr($title, -1) == '?') || (substr($title, -1) == '!'))) {
$title .= '.';
}*/
return $title;
}

/**
* Get the full citation for a Harvard Title
*
* @access private
* @return string
*
*/
private function getHarvardTitle() {
$title = $this->convertToSentenceCase($this->stripPunctuation($this->details['title']));

if (isset($this->details['subtitle']) && strlen($this->details['subtitle']) > 0){
$subtitle = $this->convertToSentenceCase($this->stripPunctuation($this->details['subtitle']));
$subtitle = strtolower(substr($subtitle, 0, 1)) . substr($subtitle, 1);
$title .= ': ' . $subtitle;
}

if (!((substr($title, -1) == '?') || (substr($title, -1) == '!'))) {
$title .= '.';
}
return $title;
}


/**
* Get an array of authors for an APA citation.
*
Expand Down Expand Up @@ -466,6 +539,40 @@ private function getAMAAuthors() {
return (empty($authorStr) ? false : $authorStr);
}

/**
* Get a string of authors for a Harvard citation
*
* @access private
* @return string
*
*/
private function getHarvardAuthors() {
$authorStr = ' ';
if (isset($this->details['authors']) && is_array($this->details['authors'])) {
$i = 0;
$numAuthors = count($this->details['authors']);
foreach($this->details['authors'] as $author) {
$author = $this->abbreviateName($author);
//After listing 8 authors, stop listing and add 'et al.'
if ($i == 7) {
$authorStr .= ' et al';
break;
}

//Add "and" for last author
if (($i +1 == $numAuthors || $i == 6) && ($i > 0)) {
$authorStr .= ' and ' . $this->stripPunctuation($author) . '.';
} elseif ($i > 0) {
$authorStr .= ', ' . $this->stripPunctuation($author) . '.';
} else {
$authorStr .= $this->stripPunctuation($author) . '.';
}
$i++;
}
}
return (empty($authorStr) ? false: $authorStr);
}


/**
* Get edition statement for inclusion in a citation. Shared by APA and
Expand All @@ -489,7 +596,32 @@ private function getEdition() {
}
}
}
// No edition statement found:
return false;
}

/**
* Get edition statement for inclusion in a citation. Used for Harvard functionality.
*
* @access private
* @return string
*/
private function getHarvardEdition() {
if (isset($this->details['edition'])) {
if (is_array($this->details['edition'])) {
foreach ($this->details['edition'] as $edition) {
if ($edition !== '1st ed.'){
$edition =preg_replace('/\bedition\b/i', 'edn', $edition);
return $edition;
}
}
} else {
if ($this->details['edition'] !== '1st ed.') {
$edition = preg_replace('/\bedition\b/i', 'edn', $this->details['edition']);
return $edition;
}
}
}
// No edition statement found:
return false;
}
Expand Down Expand Up @@ -545,8 +677,8 @@ private function getMLAAuthors() {
*/
private function getPublisher() {
$parts = [];
if (isset($this->details['pubPlace']) && !empty($this->details['pubPlace'])) {
$parts[] = $this->stripPunctuation($this->details['pubPlace']);
if (isset($this->details['placeOfPublication']) && !empty($this->details['placeOfPublication'])) {
$parts[] = $this->stripPunctuation($this->details['placeOfPublication']);
}
if (isset($this->details['pubName']) && !empty($this->details['pubName'])) {
$parts[] = $this->details['pubName'];
Expand Down Expand Up @@ -586,6 +718,24 @@ private function getYear() {
}
return false;
}

/**
* Get the uear of publication for inclusion in a Harvard citation
*
* @access private
* @return string
*/

private function getHarvardYear() {
if (isset($this->details['pubDate'])) {
$year = preg_replace('/[^0-9]/', '', $this->details['pubDate']);
if (strlen($year) === 4) {
return $year;
}
}
//Return "n.d." for missing or invalid dates
return 'n.d.';
}
}

?>

0 comments on commit 8fb012a

Please sign in to comment.