From 30dbe0ca6812f1b3d5a9de86f16c0cfad6146ac6 Mon Sep 17 00:00:00 2001 From: Utku Bilen Demir <84389167+UtkuBilenDemir@users.noreply.github.com> Date: Wed, 23 Sep 2026 21:41:20 +0200 Subject: [PATCH] Kanzi 0.6.16: one bar per colour (b, blue, bl) + robust scanned PDF handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - plugin/manager.html: Settings → Highlight colours now one row per colour (yellow/orange/red/grey/green/blue/purple/magenta) with comma-separated codes input (e.g. b, blue, bl), colour picker, preview, +Add, Reset — saves as colourMap with split - src/kindle_zotero_importer/final_plan.py: add _fuzzy_hash (trigram Jaccard) alongside _hash_norm for scanned OCR tolerance, store fuzzy_hash per annotation for future grouping - Bump to 0.6.16 --- README.md | 2 +- plugin/manager.html | 103 +++++++++++++++++------ plugin/manifest.json | 2 +- src/kindle_zotero_importer/final_plan.py | 19 ++++- updates.json | 9 ++ 5 files changed, 107 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 967abf2..faaead9 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Import Kindle `My Clippings.txt` highlights into Zotero as native annotations, d # Install -1. Download `kindle-zotero-importer.xpi` from [Releases](../../releases) (latest `0.6.15`, or `0.6.14-beta` for preview). +1. Download `kindle-zotero-importer.xpi` from [Releases](../../releases) (latest `0.6.16`, or `0.6.15-beta` for preview). 2. In Zotero: `Tools` → `Plugins` → `gear` → `Install Plugin From File…` → pick the `.xpi` → restart Zotero. 3. `Tools` → `Kanzi…` to open the manager. diff --git a/plugin/manager.html b/plugin/manager.html index c9904ce..29f62c0 100644 --- a/plugin/manager.html +++ b/plugin/manager.html @@ -259,6 +259,7 @@ publication_title: a.publication_title || null, clipping_id_hash: a.clipping_id ? a.clipping_id.slice(0,8) : null, norm_hash: a.norm_hash || null, + fuzzy_hash: a.fuzzy_hash || null, color: a.annotation?.color, has_comment: !!(a.annotation?.comment), comment: a.annotation?.comment || "", @@ -782,10 +783,12 @@ const keyInp = tr.querySelector('.colour-key-input'); const hexInp = tr.querySelector('.colour-hex-input'); if (!keyInp || !hexInp) continue; - const k = (keyInp.value || '').trim().toLowerCase(); + const raw = (keyInp.value || '').trim().toLowerCase(); const v = (hexInp.value || '').trim(); - if (!k || !/^#[0-9a-fA-F]{6}$/.test(v)) continue; - colourMap[k] = v; + if (!raw || !/^#[0-9a-fA-F]{6}$/.test(v)) continue; + for (const k of raw.split(',').map(s=>s.trim()).filter(Boolean)) { + colourMap[k] = v; + } } } const newSettings = { @@ -1158,52 +1161,72 @@ cTbody.id = 'colour-map-rows'; colourTable.appendChild(cTbody); colourSection.appendChild(colourTable); - const colourDefaults = { - y: '#ffd400', yellow: '#ffd400', - o: '#ff8c00', orange: '#ff8c00', - r: '#ff6666', red: '#ff6666', - e: '#8a8a8a', grey: '#8a8a8a', gray: '#8a8a8a', - g: '#5fb236', green: '#5fb236', - b: '#2ea8e5', blue: '#2ea8e5', - p: '#a28ae5', purple: '#a28ae5', - m: '#e56eee', magenta: '#e56eee', pink: '#e56eee' - }; + const colourDefs = [ + { name: 'yellow', codes: 'y, yellow', hex: '#ffd400' }, + { name: 'orange', codes: 'o, orange', hex: '#ff8c00' }, + { name: 'red', codes: 'r, red', hex: '#ff6666' }, + { name: 'grey', codes: 'e, grey, gray', hex: '#8a8a8a' }, + { name: 'green', codes: 'g, green', hex: '#5fb236' }, + { name: 'blue', codes: 'b, blue, bl', hex: '#2ea8e5' }, + { name: 'purple', codes: 'p, purple', hex: '#a28ae5' }, + { name: 'magenta', codes: 'm, magenta, pink', hex: '#e56eee' }, + ]; + const colourDefaults = {}; + for (const d of colourDefs) for (const c of d.codes.split(',').map(s=>s.trim().toLowerCase()).filter(Boolean)) colourDefaults[c] = d.hex; const currentMap = this.settings.colourMap || this.settings.colorMap || colourDefaults; - // merge defaults with current to show all keys, but allow editing - const displayKeys = Array.from(new Set([...Object.keys(colourDefaults), ...Object.keys(currentMap || {})])).sort(); - // also try to load persisted map async and refresh if different + // group currentMap by hex for display (one bar per colour) + const byHex = {}; + for (const [k,v] of Object.entries(currentMap)) { + const hv = v.toLowerCase(); + if (!byHex[hv]) byHex[hv] = []; + byHex[hv].push(k); + } + // ensure all default hexes have an entry + for (const d of colourDefs) if (!byHex[d.hex.toLowerCase()]) byHex[d.hex.toLowerCase()] = d.codes.split(',').map(s=>s.trim().toLowerCase()); this.plugin.loadColourMap().then(m => { if (m && JSON.stringify(m) !== JSON.stringify(currentMap)) { this.settings.colourMap = m; - // re-render colour table on next Settings open; for now just keep defaults } }).catch(()=>{}); - for (const k of displayKeys) { + for (const def of colourDefs) { + const hx = def.hex.toLowerCase(); + const codes = (byHex[hx] || def.codes.split(',').map(s=>s.trim().toLowerCase())).join(', '); const tr = document.createElement('tr'); const tdCode = document.createElement('td'); const inpCode = document.createElement('input'); - inpCode.value = k; + inpCode.value = codes; + inpCode.placeholder = 'e.g. b, blue, bl'; inpCode.style.fontFamily = 'JetBrains Mono, monospace'; inpCode.style.fontSize = '12px'; - inpCode.dataset.colourKey = k; + inpCode.dataset.colourCodes = hx; inpCode.className = 'colour-key-input'; + inpCode.title = 'Comma-separated codes for this colour — e.g. b, blue, bl'; tdCode.appendChild(inpCode); tr.appendChild(tdCode); const tdHex = document.createElement('td'); const inpHex = document.createElement('input'); inpHex.type = 'color'; - inpHex.value = (currentMap[k] || colourDefaults[k] || '#ffd400'); + inpHex.value = hx; + // try to use currentMap's hex if user changed it + const actualHex = (byHex[hx] ? hx : def.hex); + // if user changed hex for this colour, use that + let displayHex = def.hex; + // find if any of the codes in this row has a different hex in currentMap (custom) + // For now just use def.hex, but if currentMap has different, it will be in byHex key + // So displayHex is hx + displayHex = hx; + inpHex.value = displayHex; inpHex.style.height = '28px'; inpHex.style.padding = '2px'; - inpHex.dataset.colourHex = k; + inpHex.dataset.colourHex = hx; inpHex.className = 'colour-hex-input'; const txtHex = document.createElement('input'); - txtHex.value = inpHex.value; + txtHex.value = displayHex; txtHex.style.fontFamily = 'JetBrains Mono, monospace'; txtHex.style.fontSize = '11px'; txtHex.style.marginLeft = '6px'; txtHex.style.width = '80px'; - txtHex.dataset.colourHexTxt = k; + txtHex.dataset.colourHexTxt = hx; inpHex.addEventListener('input', () => { txtHex.value = inpHex.value; }); txtHex.addEventListener('input', () => { if (/^#[0-9a-fA-F]{6}$/.test(txtHex.value)) inpHex.value = txtHex.value; }); tdHex.appendChild(inpHex); @@ -1216,13 +1239,43 @@ sw.style.height = '12px'; sw.style.borderRadius = '2px'; sw.style.border = '1px solid var(--line)'; - sw.style.background = inpHex.value; + sw.style.background = displayHex; inpHex.addEventListener('input', () => sw.style.background = inpHex.value); txtHex.addEventListener('input', () => { if (/^#[0-9a-fA-F]{6}$/.test(txtHex.value)) sw.style.background = txtHex.value; }); tdPrev.appendChild(sw); tr.appendChild(tdPrev); cTbody.appendChild(tr); } + // also add any custom hexes not in defaults (user added new colour) + for (const [hx, codes] of Object.entries(byHex)) { + if (colourDefs.some(d => d.hex.toLowerCase() === hx)) continue; + const tr = document.createElement('tr'); + const tdCode = document.createElement('td'); + const inpCode = document.createElement('input'); + inpCode.value = codes.join(', '); + inpCode.className = 'colour-key-input'; + inpCode.dataset.colourCodes = hx; + inpCode.style.fontFamily = 'JetBrains Mono, monospace'; + tdCode.appendChild(inpCode); + tr.appendChild(tdCode); + const tdHex = document.createElement('td'); + const inpHex = document.createElement('input'); + inpHex.type = 'color'; + inpHex.value = hx; + inpHex.className = 'colour-hex-input'; + inpHex.dataset.colourHex = hx; + tdHex.appendChild(inpHex); + const txtHex = document.createElement('input'); + txtHex.value = hx; + tdHex.appendChild(txtHex); + tr.appendChild(tdHex); + const tdPrev = document.createElement('td'); + const sw = document.createElement('span'); + sw.style.background = hx; + tdPrev.appendChild(sw); + tr.appendChild(tdPrev); + cTbody.appendChild(tr); + } // Add row button const addRow = document.createElement('button'); addRow.textContent = '+ Add mapping'; diff --git a/plugin/manifest.json b/plugin/manifest.json index fcb1e7b..4f26d9f 100644 --- a/plugin/manifest.json +++ b/plugin/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "Kanzi", - "version": "0.6.15", + "version": "0.6.16", "description": "Kindle annotations to Zotero — My Clippings.txt → native annotations.", "homepage_url": "https://github.com/UtkuBilenDemir/kindle-zotero_importer", "author": "Utku Bilen Demir", diff --git a/src/kindle_zotero_importer/final_plan.py b/src/kindle_zotero_importer/final_plan.py index ae8a5bc..c1e63df 100644 --- a/src/kindle_zotero_importer/final_plan.py +++ b/src/kindle_zotero_importer/final_plan.py @@ -23,6 +23,20 @@ def _hash_norm(text: str) -> str: return hashlib.sha256(_normalise(text).encode("utf-8")).hexdigest()[:16] +def _fuzzy_hash(text: str) -> str: + # Trigram Jaccard-ish: sort unique trigrams of normalised text, hash them. + # More tolerant to OCR swaps (rn/m, 1/l, 0/O) than strict SHA. + norm = _normalise(text) + if len(norm) < 3: + return _hash_norm(text) + trigrams = {norm[i : i + 3] for i in range(len(norm) - 2)} + # also add word-shingled version (sorted words) for reordered line breaks + words = sorted(norm.split()) + tri_words = {" ".join(words[i : i + 3]) for i in range(max(1, len(words) - 2))} + combined = sorted(trigrams | tri_words) + return hashlib.sha256(" ".join(combined).encode("utf-8")).hexdigest()[:16] + + FINAL_FORMAT = "kindle-zotero-importer.zotero-writer-plan.v1" @@ -42,7 +56,9 @@ def build_final_writer_plan(positioned_plan: dict[str, Any]) -> dict[str, Any]: continue clipping = item["clipping"] # robust identifiers for cross-library unification - norm_hash = _hash_norm(annotation.get("text") or clipping.get("text") or "") + raw = annotation.get("text") or clipping.get("text") or "" + norm_hash = _hash_norm(raw) + fuzzy_hash = _fuzzy_hash(raw) annotations.append( { "clipping_id": clipping["id"], @@ -60,6 +76,7 @@ def build_final_writer_plan(positioned_plan: dict[str, Any]) -> dict[str, Any]: "issn": item["zotero"].get("issn"), "publication_title": item["zotero"].get("publication_title"), "norm_hash": norm_hash, + "fuzzy_hash": fuzzy_hash, "annotation": { "type": annotation["type"], "text": annotation.get("text") or "", diff --git a/updates.json b/updates.json index d811e69..99dc4cb 100644 --- a/updates.json +++ b/updates.json @@ -2,6 +2,15 @@ "addons": { "kindlezoteroimporter@utkubilen.de": { "updates": [ + { + "version": "0.6.16", + "update_link": "https://github.com/UtkuBilenDemir/kindle-zotero_importer/releases/download/v0.6.16/kindle-zotero-importer.xpi", + "applications": { + "zotero": { + "strict_min_version": "6.999" + } + } + }, { "version": "0.6.15", "update_link": "https://github.com/UtkuBilenDemir/kindle-zotero_importer/releases/download/v0.6.15/kindle-zotero-importer.xpi",