{{/* 
  APA-Style Citation


  Book Template : 

    Author Surname, First Initial. Second Initial. (Year). <i>Book title: Subtitle</i>. Place of Publication: Publisher. [DOI]

  Journal Template:

    Author, I. N. (Year). Title of the article. <i>Title of the Journal or Periodical, volume number(issue number)</i>, page numbers. [DOI]

  https://apastyle.apa.org/style-grammar-guidelines/references/examples

*/}}

{{/* BEGIN author */}}
{{- define "authorPart" }}
  {{- $authors := .author }}
  {{- if $authors -}}
  {{- $totalAuthors := len $authors -}}
  {{- range $authorIndex, $author := $authors -}}
  <span itemprop="author" itemscope itemtype="https://schema.org/Person">
    {{- with $author.family -}}
    <span itemprop="familyName">{{ . }}</span>
    {{- end -}} 
    {{- with $author.given -}},&#32;
    <meta itemprop="givenName" content="{{ . }}" />
    {{ substr . 0 1 }}.
    {{- end -}}{{/* First letter (initial). */}}
    {{- with $author.secondInitial -}}&#32;
    <meta itemprop="additionalName" content="{{ . }}" />
    {{- substr . 0 1 -}}.{{- end -}}
  </span>
  {{- if and (gt $totalAuthors 1) (lt (add $authorIndex 2) $totalAuthors) -}},&#32;
  {{ end -}}
  {{ if eq $totalAuthors (add $authorIndex 2) -}}&#32;&amp;&#32;{{ end -}}{{/* Last name has ampersand */}}
  {{- end -}}
  {{- else -}}{{/* Fallback if no authors are specified */}}
  ({{- i18n "apa_no_author_abbr" | default "n.a." | upper -}}).
  {{- end -}}
{{- end -}}{{/* END author */}}

{{/* BEGIN editor */}}
{{- define "editorPart" }}
  {{- $editors := .editor }}
  {{- if $editors -}}
  {{- $totalEditors := len $editors -}}
  {{- range $editorIndex, $editor := $editors -}}
  <span itemprop="editor" itemscope itemtype="https://schema.org/Person">
    {{- with $editor.family -}}
    <span itemprop="familyName">{{ . }}</span>
    {{- end -}} 
    {{- with $editor.given -}},&#32;
    <meta itemprop="givenName" content="{{ . }}" />
    {{ substr . 0 1 }}.
    {{- end -}}{{/* First letter (initial). */}}
    {{- with $editor.secondInitial -}}&#32;
    <meta itemprop="additionalName" content="{{ . }}" />
    {{- substr . 0 1 -}}.{{- end -}}
  </span>
  {{- if and (gt $totalEditors 1) (lt (add $editorIndex 2) $totalEditors) -}},&#32;
  {{ end -}}
  {{ if eq $totalEditors (add $editorIndex 2) -}}&#32;&amp;&#32;{{ end -}}{{/* Last name has ampersand */}}
  {{- end -}}
  {{- end -}}
{{- end -}}{{/* END editor */}}

{{/* BEGIN issued (year of publication) */}}
{{- define "issuedPart" }}
  {{- if and (isset . "issued") (isset .issued "date-parts") -}}
    {{/* range of dates */}}
    (<span itemprop="datePublished">
    {{- range $index, $dateParts := (index .issued "date-parts") -}}
    {{- if gt $index 0 -}}, {{ end -}}{{/* Separate other years by comma */}}
    {{- range first 1 $dateParts -}}{{/* First element in date-part is the year */}}
    {{- . -}}
    {{- end -}}
    {{- end -}}
    </span>)
  {{- else }}
  ({{ i18n "apa_no_date_abbr" | default "n.d." }}){{/* no date */}}
  {{- end }}
{{- end }}{{/* END issued */}}

{{/* BEGIN full issued (year, month day) */}}
{{- define "issuedFullPart" }}
  {{- if and (isset . "issued") (isset .issued "date-parts") -}}
    (<span itemprop="datePublished">
    {{- range (index .issued "date-parts") -}}
    {{- index . 0 -}}{{/* year */}}
    {{- if (index . 1) -}}
    ,&#32;{{- index . 1 -}}{{/* month */}}
    {{- end }}
    {{- with index . 2 -}}
    /{{- . }}{{/* day */}}
    {{- end -}}
    {{- end -}}
    </span>)
  {{- else }}
  (s.d.){{/* no date */}}
  {{- end }}
{{- end }}{{/* END issued */}}

{{/* BEGIN DOI */}}
{{- define "DOIPart" }}
  {{- if .DOI }}
  <a href="https://doi.org/{{ .DOI }}"
     itemprop="identifier"
     itemtype="https://schema.org/URL">https://doi.org/{{- .DOI -}}</a>
  {{- else if .URL }}&#32;
  {{- i18n "apa_retrieved_from" | default "Retrieved from" -}}&#32;
  <a href="{{ .URL }}"
     itemprop="identifier"
     itemtype="https://schema.org/URL">{{ .URL }}</a>
  {{- end -}}
{{- end -}}
{{- /* END DOI*/ -}}

{{/* BEGIN ArchivalID */}}
{{- define "ArchivalID" }}
  {{ if isset . "archive" -}}&#32;{{ i18n "archive_available_from" | default "Available from" }}
    <meta itemprop="Organization" itemtype="http://schema.org/Organization" itemscope>
    <span itemprop="name">{{- echoParam . "archive" -}}</span>.
    {{- if isset . "archive_location" -}}&#32; ({{- echoParam . "archive_location" -}}){{- end }}.
  {{- end }}
{{- end -}}
{{- /* END ArchivalID */ -}}

{{/* -------------------- BEGIN TYPES -------------------- */}}
{{/* -------------------- BEGIN BOOK TYPE -------------------- */}}
{{- if eq .type "book" -}}
<span itemscope 
      itemtype="https://schema.org/Book"
      data-type="book">
  {{- if .author -}}
  {{- template "authorPart" . -}}
  &#32;
  {{- else if .editor -}}{{/* Display editors if no authors*/}}
  {{- template "editorPart" . -}}
  &#32;
  {{- end -}}
  {{- template "issuedPart" . }}.
  &#32;
  {{- if .title -}}
  <span itemprop="name">
    <i>{{/* italicize title */}}
      {{- .title | markdownify -}}
      {{- if .subtitle }}: {{ .subtitle | markdownify }}{{- end -}}
    </i></span>{{/* close to avoid space */}}
  {{- end -}}
  {{- if or (isset . "edition") (isset . "page") }} (
  {{- with .edition -}}
  <span>{{ . }}</span>
  {{- end -}}
  {{- if and (isset . "edition") (isset . "page") -}}, {{ end -}}
  {{- if isset . "page" -}}
  <meta itemprop="numberOfPages" content="{{ .page }}">
  {{- end -}}
  ){{- end -}}.
  {{ if isset . "publisher-place" -}}
  <meta itemprop="contentLocation"
        value="{{- echoParam . "publisher-place" -}}">
  {{- end -}}
  {{- with .publisher -}}&#32;
  <span itemprop="publisher"
             itemtype="http://schema.org/Organization"
             itemscope="">
    <span itemprop="name">
      {{- . -}}
    </span></span>.
  {{- end }}

  {{- template "DOIPart" . -}}
</span>
{{/* -------------------- END BOOK TYPE -------------------- */}}

{{/* -------------------- BEGIN CHAPTER TYPE -------------------- */}}
{{- else if eq .type "chapter" -}}
<span itemscope 
      itemtype="https://schema.org/Chapter"
      data-type="chapter">
  {{ template "authorPart" . }}
  &#32;
  {{- template "issuedPart" . }}.
  &#32;
  {{- if .title -}}
  <span itemprop="name">
    {{- .title | markdownify -}}
    {{- if .subtitle }}: {{ .subtitle | markdownify }}{{- end -}}
  </span>.
  {{- end -}}

  {{- if isset . "container-title" -}}
  &#32;{{- i18n "apa_in" | default "In" -}}
  {{- if .editor -}}
  {{- template "editorPart" . }} (Eds.),&#32;
  {{- end -}}

  <span itemprop="about">
    <i>{{/* italicize title */}}
      {{- echoParam . "container-title" -}}
    </i></span>.{{/* close to avoid space */}}
  {{- end -}}
  {{- if or (isset . "edition") (isset . "page") -}}&#32;(
  {{- if isset . "edition" -}}
  <span itemprop="workExample">{{ .edition }}</span>
  {{- if isset . "page" -}}, {{ end -}}
  {{- end -}}
  {{- if isset . "page" -}}
  <span>pp.&#32;{{ replace .page "-" "–" }}</span>
  {{- end -}}
  ){{- end -}}.
  {{ if isset . "publisher-place" -}}
  <meta itemprop="contentLocation"
        value="{{- echoParam . "publisher-place" -}}">
  {{- end -}}
  {{- with .publisher -}}
    &#32;<span itemprop="publisher"
               itemtype="http://schema.org/Organization"
               itemscope="">
      <span itemprop="name">
      {{- . -}}
      </span></span>.
  {{- end }}

  {{- template "DOIPart" . -}}
</span>
{{/* -------------------- END CHAPTER TYPE -------------------- */}}

{{/* -------------------- BEGIN ARTICLE TYPE -------------------- */}}
{{- else if or (eq .type "article-journal") (eq .type "article") -}}
<span itemscope
      itemtype="https://schema.org/Article"
      data-type="article">
  {{- template "authorPart" . }}
  &#32;
  {{- template "issuedPart" . }}.
  &#32;
  {{- if .title -}}
  <span itemprop="name">
    {{- .title | markdownify -}}
  </span>.
  {{- end -}}

  {{- if isset . "container-title" -}}
  {{- if .editor -}}{{/* Add `In` before name of Eds. and title*/}}
  &#32;{{- i18n "apa_in" | default "In" -}}&#32;
  {{- template "editorPart" . }} (Eds.),&#32;
  {{- end -}}

  <i>{{/* italicize journal title */}}
    <span itemprop="about">
    {{- echoParam . "container-title" -}}</span>
    {{- if isset . "volume" -}}
    ,&#32;{{- .volume -}}
    {{- end -}}
    {{- if isset . "issue" }}({{ .issue }})
    {{- end -}}
    </i>.{{- end -}}
  {{- if isset . "page" -}}
  &#32;<span itemprop="pagination">{{ replace .page "-" "–" }}</span>.
  {{- end -}}

  {{- template "DOIPart" . -}}
</span>
{{/* -------------------- END ARTICLE TYPE -------------------- */}}

{{/* -------------------- BEGIN WEBPAGE TYPE -------------------- */}}
{{- else if eq .type "webpage" -}}
<span itemscope
      itemtype="https://schema.org/WebPage"
      data-type="webpage">
  {{- template "authorPart" . -}}

  {{- template "issuedFullPart" . -}}.

  {{- template "DOIPart" . -}}
</span>
{{/* -------------------- END WEBPAGE TYPE -------------------- */}}

{{/* -------------------- BEGIN BLOGPOST TYPE -------------------- */}}
{{- else if eq .type "post-weblog" -}}
<span itemscope
      itemtype="https://schema.org/BlogPosting"
      data-type="blog">
  {{- template "authorPart" . }}
  &#32;
  {{- template "issuedPart" . }}.
  &#32;
  {{- if .title -}}
  <span itemprop="name">
    {{- .title | markdownify -}}
  </span>
  {{- end -}}
{{- if isset . "genre" -}}<span itemprop="genre">&nbsp;[{{- echoParam . "genre" -}}]</span>
    {{- end -}}.&nbsp;

  {{- if isset . "container-title" -}}
  {{- if .editor -}}{{/* Add `In` before name of Eds. and title*/}}
  &#32;{{- i18n "apa_in" | default "In" -}}&#32;
  {{- template "editorPart" . }} (Eds.),&#32;
  {{- end -}}

<span itemprop="isPartOf">
    {{- echoParam . "container-title" -}}</span>
    {{- if isset . "volume" -}}
    ,&#32;{{- .volume -}}
    {{- end -}}
    {{- if isset . "issue" }}({{ .issue }})
    {{- end -}}
.{{- end -}}
  {{- if isset . "page" -}}
  &#32;<span itemprop="pagination">{{ replace .page "-" "–" }}</span>.
  {{- end -}}
  {{- template "DOIPart" . -}}
</span>
{{/* -------------------- END BLOGPOST TYPE -------------------- */}}

{{/* -------------------- BEGIN DEGREE WORK TYPE -------------------- */}}
{{- else if eq .type "thesis" -}}
<span itemscope
      itemtype="https://schema.org/Thesis"
      data-type="thesis">
  {{- template "authorPart" . }}
  &#32;
  {{- template "issuedPart" . }}.
  &#32;
  {{- if .title -}}
  <span itemprop="name">
    <i>{{- .title | markdownify -}}</i>
  </span>
  {{- end -}}
{{- if isset . "genre" -}}<span itemprop="inSupportOf">&nbsp;({{- echoParam . "genre" -}})</span>
    {{- end -}}.&#32;
{{- if isset . "publisher-place" -}}
  <meta itemprop="locationCreated"
        value="{{- echoParam . "publisher-place" -}}">
  {{- end -}}
  {{- with .publisher -}}
  <span itemprop="publisher"
        itemtype="http://schema.org/EducationalOrganization"
        itemscope>
    <span itemprop="name">{{- . -}}{{- end -}}
    </span>{{ if isset . "publisher-place" -}},&#32; <span itemprop="location">{{- echoParam . "publisher-place" -}}</span></span>.
  {{- end }}
  {{- template "ArchivalID" . -}}
  {{- template "DOIPart" . -}}
</span>
{{/* -------------------- END DEGREE WORK TYPE -------------------- */}}

{{/* -------------------- BEGIN REPORT TYPE -------------------- */}}
{{- else if eq .type "report" -}}
<span itemscope
      itemtype="https://schema.org/Report"
      data-type="report">
  {{- template "authorPart" . }}
  &#32;
  {{- template "issuedPart" . }}.
  &#32;
  {{- if .title -}}
  <span itemprop="name">
    <i>{{- .title | markdownify -}}</i>
  </span>
  {{- end -}}
  {{- if isset . "number" -}}<span itemprop="reportNumber">&nbsp;({{- echoParam . "number" -}})</span>
    {{- end -}}.&nbsp;
  {{- if isset . "publisher-place" -}}
  <meta itemprop="contentLocation"
        value="{{- echoParam . "publisher-place" -}}">
  {{- end -}}
  {{- with .publisher -}}
  <span itemprop="Organization"
        itemtype="http://schema.org/Organization"
        itemscope>
    <span itemprop="name">
      {{- . -}}{{- end -}}
    </span> {{ if isset . "publisher-place" -}}:&#32; <span itemprop="location">{{- echoParam . "publisher-place" -}}</span></span>.
  {{- end }}
  {{- template "ArchivalID" . -}}
  {{- template "DOIPart" . -}}
</span>
{{/* -------------------- END REPORT TYPE -------------------- */}}

{{/* -------------------- BEGIN DEFAULT APA -------------------- */}}
{{- else -}}
<span itemscope
      itemtype="https://schema.org/CreativeWork"
      data-type="{{ .type | default "default" }}">
  {{- template "authorPart" . }}
  &#32;
  {{- template "issuedPart" . }}.
  &#32;
  {{- if .title -}}
  <span itemprop="name">
    <i>{{/* italicize title */}}
      {{- .title | markdownify -}}
      {{- if .subtitle }}: {{ .subtitle | markdownify }}{{- end -}}
    </i></span>{{/* close to avoid space */}}
  {{- end -}}
  {{- if isset . "edition" }} (
  <span span itemprop="workExample">{{ .edition }}</span>
  ){{- end -}}.
  {{ if isset . "publisher-place" -}}
  <meta itemprop="contentLocation"
        content="{{- echoParam . "publisher-place" -}}">
  {{- end -}}
  {{- with .publisher -}}&#32;
  <span itemprop="publisher" itemtype="http://schema.org/Organization" itemscope="">
    <span itemprop="name">
      {{- . -}}
    </span></span>.
  {{- end }}

  {{- template "DOIPart" . -}}
</span>
{{- end -}}
{{/* -------------------- END DEFAULT APA -------------------- */}}
{{/* -------------------- END TYPES -------------------- */}}
