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
#!/usr/bin/env python
import json
from datetime import datetime, timezone
import html


def format_date(date_string):
    date = datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S%z")
    return date.strftime("%b %Y")


def generate_html(json_data):
    # Parse the JSON data
    data = json.loads(json_data)
    items = data["items"]

    # Filter out items where itemType is "webpage"
    filtered_items = [item for item in items if item.get("itemType") != "webpage"]

    # Sort items by dateAdded in descending order
    sorted_items = sorted(filtered_items, key=lambda x: x["dateAdded"], reverse=True)

    # Get current date for "created" and "updated" fields
    current_date = datetime.now(timezone.utc).strftime("%Y-%m-%d")
    current_year = datetime.now(timezone.utc).strftime("%Y")
    current_time = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
    # Generate HTML
    html_content = f"""
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Papers Index</title>
    <link rel="schema.dcterms" href="http://purl.org/dc/terms/">
    <meta name="dcterms.created" content="2024-08-18T19:28:14Z">
    <meta name="dcterms.modified" content="{current_time}">
    <meta name="dcterms.title" content="Papers Index">
    <meta name="dcterms.creator" content="Pradeep Gowda">

    <title>Papers Index</title>
    <link rel="stylesheet" href="../css/site7.css">
    <!--style>
    body {{
        display: flex;
        flex-direction: column;
        min-height: 100vh;
    }}
    .content {{
        flex: 1 0 auto;
    }}
    footer {{
        flex-shrink: 0;
    }}
    a {{
        color: darkslategray;
    }}
    header a {{
        font-size: 1.3em;
    }}
    #search {{
        width: 100%;
        padding: 10px;
        margin-bottom: 20px;
        font-size: 16px;
    }}
    .paper-item {{ border-bottom: 1px solid gold; }}
    .paper-item:last-child  {{ border-bottom: none; }}
    .nourl {{border-bottom: 1px solid #eee;}}
    .date {{ color: #666; }}
    .meta-info {{ font-size: 0.9em; color: #666; }}
    </style-->
</head>
<body class="max-width-4 mx-auto p3">
    <div class="content">
        <header><a href="/">btbytes.com</a></header>
        <h1 class="mb3" id="top">Papers Index</h1>

        <p class="h2">A searchable listing of the papers in my <a href="https://www.zotero.org">Zotero</a>.</p>
        <input type="text" id="search" placeholder="Search...">
        <!-- p class="h6 mb3">Note: Not all titles have a <span class="paper-item">URL attached</span> to them. They
        <span class="nourl">look like this</span></p-->

        <ul id="papers-list" class="list-reset">
    """

    for item in sorted_items:
        title = html.escape(item["title"])
        date_added = format_date(item["dateAdded"])
        url = item.get("url", "#")
        item_key = item.get("key", "")
        if url == "#":
            paper_class = "paper-item nourl"
        else:
            paper_class = "paper-item"
        html_content += f"""
          <li id="{item_key}" class="{paper_class} py2 flex items-center">
            <a href="{url}" target="_blank" class="flex-auto text-decoration-none">{title}</a>
            <span class="date ml2"><a href="#{item_key}">{date_added}</a></span>
          </li>"""

    html_content += f"""
      </ul>
    </div>

    <footer class="mt4 pt2 border-top">
        <div class="meta-info flex justify-between">
            <span>Created: 2024-08-18</span>
            <span>Updated: {current_date}</span>
        </div>
        <p>Inspired by <a href="https://www.rsrch.space">Ishan</a>.</p>
        <p>Code that generates this file <a href="papersindex.html">papersindex.py</a>.
        This program was written with the help of <a href="//claude.ai">Anthropic Claude</a>.</p>
        <p>A traditional "bib2html" output of the above list is available <a href="/bib/nbrefs.html">here</a>.</p>
        <p>&copy; {current_year} <a href="/">Pradeep Gowda</a>.</p>
    </footer>

    <script>
            document.addEventListener('DOMContentLoaded', function() {{
                const searchInput = document.getElementById('search');
                const paperItems = document.querySelectorAll('#papers-list li');

                searchInput.addEventListener('input', function() {{
                    const searchTerm = this.value.toLowerCase();
                    paperItems.forEach(function(item) {{
                        const title = item.textContent.toLowerCase();
                        item.style.display = title.includes(searchTerm) ? '' : 'none';
                    }});
                }});
            }});
        </script>
</body>
</html>
    """

    return html_content


# Read the JSON file
with open("content/bib/mylibrary.json", "r") as file:
    json_data = file.read()

# Generate HTML
html_output = generate_html(json_data)

# Write HTML to file
with open("content/papers/index.html", "w") as file:
    file.write(html_output)

print(
    "HTML file 'content/papers/index.html' has been generated from content/bib/mylibrary.json"
)