File size: 4,918 Bytes
5095870
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""
Citation formatters: APA 7th edition and AMA style.
Standard in nursing education programs.
"""


# ---------------------------------------------------------------------------
# APA 7th Edition
# ---------------------------------------------------------------------------

def format_apa7(article: dict) -> str:
    """
    APA 7th edition journal article format:
    Author, F. M., & Author, F. M. (Year). Title of article.
    *Journal Name*, *Volume*(Issue), Pages. https://doi.org/xxxxx
    """
    authors_str = _apa_authors(article.get("authors", []))
    year    = article.get("year") or "n.d."
    title   = _clean_title(article.get("title", ""))
    journal = article.get("journal", "")
    volume  = article.get("volume", "")
    issue   = article.get("issue", "")
    pages   = article.get("pages", "")
    doi     = _clean_doi(article.get("doi", ""))
    url     = article.get("url", "")

    citation = f"{authors_str} ({year}). {title}."

    if journal:
        citation += f" *{journal}*"
        if volume:
            citation += f", *{volume}*"
            if issue:
                citation += f"({issue})"
        if pages:
            citation += f", {pages}"
        citation += "."

    if doi:
        citation += f" https://doi.org/{doi}"
    elif url:
        citation += f" {url}"

    return citation


def _apa_authors(authors: list[str]) -> str:
    if not authors:
        return "Unknown Author"

    formatted = [_apa_single(a) for a in authors[:20]]

    # APA 7: 21+ authors → first 19, ellipsis, last author
    if len(authors) > 20:
        last = _apa_single(authors[-1])
        return ", ".join(formatted[:19]) + ", . . . " + last

    if len(formatted) == 1:
        return formatted[0]
    if len(formatted) == 2:
        return f"{formatted[0]}, & {formatted[1]}"
    return ", ".join(formatted[:-1]) + f", & {formatted[-1]}"


def _apa_single(name: str) -> str:
    name = name.strip()
    if name.lower() in ("et al.", "et al"):
        return "et al."
    if "," in name:
        # "Last, First M." → keep as-is, just ensure initials
        last, *rest = name.split(",", 1)
        initials = _initials(" ".join(rest))
        return f"{last.strip()}, {initials}"
    parts = name.split()
    if len(parts) >= 2:
        last = parts[-1]
        initials = _initials(" ".join(parts[:-1]))
        return f"{last}, {initials}"
    return name


# ---------------------------------------------------------------------------
# AMA Style
# ---------------------------------------------------------------------------

def format_ama(article: dict) -> str:
    """
    AMA citation format (used in some nursing / medical journals):
    Last FM, Last FM. Title. Journal. Year;Vol(Issue):Pages. doi:xxxxx
    """
    authors_str = _ama_authors(article.get("authors", []))
    year    = article.get("year", "")
    title   = _clean_title(article.get("title", ""))
    journal = article.get("journal", "")
    volume  = article.get("volume", "")
    issue   = article.get("issue", "")
    pages   = article.get("pages", "")
    doi     = _clean_doi(article.get("doi", ""))

    citation = f"{authors_str}. {title}. *{journal}*."

    if year:
        citation += f" {year}"
    if volume:
        citation += f";{volume}"
        if issue:
            citation += f"({issue})"
    if pages:
        citation += f":{pages}"
    citation += "."

    if doi:
        citation += f" doi:{doi}"

    return citation


def _ama_authors(authors: list[str]) -> str:
    if not authors:
        return "Unknown Author"

    formatted = [_ama_single(a) for a in authors[:6]]
    if len(authors) > 6:
        formatted.append("et al")

    return ", ".join(formatted)


def _ama_single(name: str) -> str:
    name = name.strip()
    if name.lower() in ("et al.", "et al", "et al."):
        return "et al"
    if "," in name:
        last, *rest = name.split(",", 1)
        initials = "".join(p[0].upper() for p in " ".join(rest).split() if p)
        return f"{last.strip()} {initials}"
    parts = name.split()
    if len(parts) >= 2:
        last = parts[-1]
        initials = "".join(p[0].upper() for p in parts[:-1] if p)
        return f"{last} {initials}"
    return name


# ---------------------------------------------------------------------------
# Shared helpers
# ---------------------------------------------------------------------------

def _initials(first_middle: str) -> str:
    """Convert first/middle name string to initials: "John Paul" → "J. P." """
    parts = first_middle.strip().split()
    return " ".join(p[0].upper() + "." for p in parts if p)


def _clean_title(title: str) -> str:
    return title.strip().rstrip(".")


def _clean_doi(doi: str) -> str:
    doi = doi.strip()
    for prefix in ("https://doi.org/", "http://doi.org/", "doi:", "doi: "):
        if doi.lower().startswith(prefix):
            doi = doi[len(prefix):]
    return doi