import re
p = 'backend/app/services/citations.py'
s = open(p).read()

# ── the docstring ────────────────────────────────────────────────────────────
s = s.replace('''Page numbers do not survive into the citation. They are offsets into one
particular ebook build, they differ between the EPUB and the print edition,
and `p. 3722, 4328` in front of a reader is a number they cannot use. They
stay in the stored entry for anybody tracing a passage back.
"""''',
'''An entry is one book at one page, headed by what that page was read for:

    Periventricular leukomalacia (imaging) — Merrow AC, Carlson A. Diagnostic
    Imaging: Pediatrics, 3rd ed., 2017, p. 1099.

Two pages of the same book are two entries, because they are two different
places to look. Collapsing them into `Nelson, pp. 1069, 1071, 3727` saves a
line and costs the reader the thing the list is for.
"""''')

# ── books as parts, so the line can be assembled in the house style ──────────
old = s[s.index('BOOKS: dict[str, str] = {'):s.index('#: The corpus\'s name for a book')]
new = '''#: authors, title, edition, year. Assembled rather than stored whole so every
#: line reads the same and the page can be added at the end.
BOOKS: dict[str, tuple[str, str, str, str]] = {
    "Nelson": ("Kliegman R, ed.", "Nelson Textbook of Pediatrics", "22nd ed.", "2024"),
    "Nelson review": ("Marcdante KJ, Kliegman R, eds.",
                      "Nelson Pediatrics Board Review", "", "2018"),
    "Nelson antimicrobial": ("Bradley JS, Nelson JD, et al.",
                             "Nelson's Pediatric Antimicrobial Therapy", "27th ed.", "2021"),
    "Fleisher": ("Shaw KN, Bachur RG, eds.",
                 "Fleisher & Ludwig's Textbook of Pediatric Emergency Medicine",
                 "8th ed.", "2021"),
    "CURRENT": ("Hay WW, Levin MJ, et al., eds.",
                "CURRENT Diagnosis & Treatment: Pediatrics", "24th ed.", "2018"),
    "MedStudy": ("", "MedStudy Pediatrics Core Curriculum", "11th ed.", "2024"),
    "Netter": ("Florin TA, Ludwig S, eds.", "Netter's Pediatrics", "", "2011"),
    "Zitelli": ("Zitelli BJ, McIntire SC, Nowalk AJ, eds.",
                "Zitelli and Davis' Atlas of Pediatric Physical Diagnosis", "7th ed.", "2018"),
    "Berkowitz": ("Berkowitz CD, ed.", "Berkowitz's Pediatrics", "6th ed.", "2020"),
    "Board guide": ("", "Pediatric Board Study Guide", "", "2017"),
    "Lissauer": ("Lissauer T, Carroll W, eds.",
                 "Illustrated Textbook of Paediatrics", "6th ed.", "2021"),
    "Signs and symptoms": ("Adam HM, Foy JM, eds.",
                           "Signs and Symptoms in Pediatrics", "", "2015"),
    "Ghai": ("Paul VK, Bagga A, eds.", "Ghai Essential Pediatrics", "9th ed.", "2019"),
    "Algorithms": ("", "Algorithms in Pediatrics", "", "2019"),
    "Gomella": ("Gomella TL, Eyal FG, et al.", "Gomella's Neonatology", "8th ed.", "2020"),
    "AAP policies": ("American Academy of Pediatrics",
                     "Pediatric Clinical Practice Guidelines & Policies", "18th ed.", "2018"),
    "Hospitalized child": ("Gershel JC, Rauch DA, eds.",
                           "Caring for the Hospitalized Child", "2nd ed.", "2017"),
    "Imaging": ("Merrow AC, ed.", "Diagnostic Imaging: Pediatrics", "3rd ed.", "2017"),
    "GI textbook": ("Guandalini S, Dhawan A, eds.",
                    "Textbook of Pediatric Gastroenterology, Hepatology and Nutrition",
                    "2nd ed.", "2022"),
    "Update": ("", "Update in Pediatrics", "", "2018"),
    "Decision making": ("Pomeranz AJ, et al.",
                        "Pediatric Decision-Making Strategies", "2nd ed.", "2016"),
    "Harriet Lane": ("The Johns Hopkins Hospital", "The Harriet Lane Handbook",
                     "22nd ed.", "2020"),
    "ICD": ("American Academy of Pediatrics", "Pediatric ICD-10-CM", "", "2023"),
    "Red Book": ("Kimberlin DW, Barnett ED, et al., eds.",
                 "Red Book: Report of the Committee on Infectious Diseases",
                 "32nd ed.", "2021"),
    "Red Book atlas": ("Baker CJ, Tan TQ, eds.",
                       "Red Book Atlas of Pediatric Infectious Diseases", "4th ed.", "2020"),
    "AAP nutrition": ("Kleinman RE, Greer FR, eds.", "Pediatric Nutrition", "8th ed.", "2019"),
    "Practitioner": ("Thora S, ed.", "Pediatrics for Practitioner", "", "2016"),
    "Developmental": ("Voigt RG, Macias MM, et al., eds.",
                      "AAP Developmental and Behavioral Pediatrics", "2nd ed.", "2018"),
    "Pulmonology": ("Light MJ, Van Hook K, eds.", "Pediatric Pulmonology", "", "2018"),
    "Environmental": ("Etzel RA, Balk SJ, eds.", "Pediatric Environmental Health",
                      "4th ed.", "2019"),
    "Dentistry": ("Nowak AJ, Christensen JR, et al., eds.",
                  "Pediatric Dentistry: Infancy through Adolescence", "6th ed.", "2018"),
    "Cardiac": ("Johnson JN, Kamat DM, eds.", "Common Cardiac Issues in Pediatrics", "", "2019"),
    "EM": ("Fox SM, Woolridge DP, eds.", "Pediatric Emergency Medicine", "", "2018"),
    "Challenging cases": ("Kamat DM, ed.", "Challenging Cases in Pediatric Diagnosis", "", "2018"),
    "Clinician nutrition": ("Muth ND, Tanaka M, eds.",
                            "The Clinician's Guide to Pediatric Nutrition", "", "2022"),
    "PPE": ("American Academy of Pediatrics, et al.",
            "Preparticipation Physical Evaluation", "5th ed.", "2019"),
    "GI algorithms": ("Shaoul R, ed.",
                      "Practical Algorithms in Pediatric Gastroenterology", "", "2020"),
    "ENT": ("American Academy of Pediatrics", "Pediatric Otolaryngology", "", "2018"),
    "NRP": ("American Academy of Pediatrics, American Heart Association",
            "Textbook of Neonatal Resuscitation", "8th ed.", "2021"),
    "Plastics": ("Taub PJ, King TW, eds.",
                 "Pediatric Plastic and Reconstructive Surgery for Primary Care",
                 "2nd ed.", "2016"),
    "Depression": ("American Academy of Pediatrics",
                   "Depression and Suicide Prevention", "", "2021"),
    "Asthma guide": ("American Academy of Pediatrics",
                     "Your Child's Allergies and Asthma", "", "2000"),
    "Breastfeeding": ("American Academy of Pediatrics",
                      "New Mother's Guide to Breastfeeding", "3rd ed.", "2017"),
    "Asthma chart": ("American Academy of Pediatrics",
                     "Pediatric Asthma: A Clinical Support Chart", "", "2019"),
    "Coding": ("American Academy of Pediatrics", "Coding for Pediatrics", "", "2021"),
    "Telehealth": ("American Academy of Pediatrics",
                   "Pediatric Telehealth Best Practices", "", "2021"),
}


def book_line(key: str) -> str:
    """`Kliegman R, ed. Nelson Textbook of Pediatrics, 22nd ed., 2024`"""
    authors, title, edition, year = BOOKS[key]
    tail = ", ".join(part for part in (edition, year) if part)
    head = f"{authors} {title}" if authors else title
    return f"{head}, {tail}" if tail else head


'''
s = s.replace(old, new)
open(p, 'w').write(s)
print('books rewritten')
