Skip to content

Commit

Permalink
Fix misc bugs; Enable auto-updating of pdf utility
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmeyers committed Apr 9, 2022
1 parent 197823d commit 557ef7b
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 34 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-zotero-desktop-connector",
"name": "Zotero Desktop Connector",
"version": "1.2.14",
"version": "1.2.15",
"minAppVersion": "0.12.0",
"description": "Insert citations, bibliographies, and notes directly from Zotero desktop.",
"author": "mgmeyers",
Expand Down
9 changes: 7 additions & 2 deletions src/bbt/basicTemplates/applyBasicTemplates.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { template } from '../template.helpers';
import { moment } from 'obsidian';

const creatorTemplate = `
{%- for creator in creators -%}
Expand Down Expand Up @@ -32,7 +33,7 @@ const annotationsTemplate = `
{% for annotation in annots -%}
{%- if annotation.annotatedText -%}
> “{{annotation.annotatedText}}”{% if annotation.color %} <mark style="background:{{annotation.color}}dd">{{annotation.type | capitalize}}</mark> {% else %} {{annotation.type | capitalize}} {% endif %}[Page {{annotation.page}}](zotero://open-pdf/library/items/{{annotation.attachment.itemKey}}?page={{annotation.page}})
> “{{annotation.annotatedText}}”{% if annotation.color %} {{annotation.colorCategory}} {{annotation.type | capitalize}} {% else %} {{annotation.type | capitalize}} {% endif %}[Page {{annotation.page}}](zotero://open-pdf/library/items/{{annotation.attachment.itemKey}}?page={{annotation.page}})
{%- endif %}
{%- if annotation.imageRelativePath -%}
> ![[{{annotation.imageRelativePath}}]]
Expand Down Expand Up @@ -88,7 +89,11 @@ export function applyBasicTemplates(itemData: Record<any, any>) {
}

if (itemData.annotations?.length) {
itemData.formattedAnnotations = template.renderString(annotationsTemplate, itemData).trim()
itemData.formattedAnnotationsNew = template.renderString(annotationsTemplate, itemData).trim()
itemData.formattedAnnotations = template.renderString(annotationsTemplate, {
...itemData,
lastExportDate: moment(0)
}).trim()
}

return itemData;
Expand Down
96 changes: 70 additions & 26 deletions src/bbt/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ function processAnnotation(
async function processItem(
item: any,
exportDate: moment.Moment,
database: Database
database: Database,
cslStyle?: string
) {
item.exportDate = exportDate;
item.desktopURI = `zotero://select/library/items/${item.itemKey}`;
Expand All @@ -101,7 +102,11 @@ async function processItem(
}

try {
item.bibliography = await getBibFromCiteKey(item.citekey, database);
item.bibliography = await getBibFromCiteKey(
item.citekey,
database,
cslStyle
);
} catch {
item.bibliography = 'Error generating bibliography';
}
Expand Down Expand Up @@ -280,20 +285,50 @@ export async function exportToMarkdown(
}

for (let i = 0, len = itemData.length; i < len; i++) {
await processItem(itemData[i], exportDate, database);
await processItem(itemData[i], exportDate, database, exportFormat.cslStyle);
}

const vaultRoot = getVaultRoot();

for (let i = 0, len = itemData.length; i < len; i++) {
const attachments = itemData[i].attachments;
const attachments = itemData[i].attachments as any[];
const hasPDF = attachments.some((a) => a.path?.endsWith('.pdf'));

// Handle the case of an item with no PDF attachments
if (!hasPDF) {
const templateData = itemData[i];
const markdownPath = exportFormat.outputPathTemplate
? removeStartingSlash(
template.renderString(exportFormat.outputPathTemplate, templateData)
)
: './';

const existingMarkdown = app.vault.getAbstractFileByPath(markdownPath);

applyBasicTemplates(templateData);

const rendered = await renderTemplates(app, params, templateData, '');

if (!rendered) return false;

if (existingMarkdown) {
app.vault.modify(existingMarkdown as TFile, rendered);
} else {
await mkMDDir(markdownPath);
app.vault.create(markdownPath, rendered);
}

continue;
}

// Handle the case of an item WITH PDF attachments
for (let j = 0, jLen = attachments.length; j < jLen; j++) {
const pdfInputPath = attachments[j].path;
if (!pdfInputPath?.endsWith('.pdf')) continue;

const pathTemplateData = {
...itemData[i],
...attachments[j],
...itemData[i],
};

const imageOutputPath = path.resolve(
Expand Down Expand Up @@ -337,7 +372,7 @@ export async function exportToMarkdown(

const existingMarkdown = app.vault.getAbstractFileByPath(markdownPath);

let lastExportDate = moment().set('year', 1970);
let lastExportDate = moment(0);
let existingAnnotations = '';

if (existingMarkdown) {
Expand All @@ -359,24 +394,22 @@ export async function exportToMarkdown(
imageQuality: settings.pdfExportImageQuality,
});
annots = JSON.parse(res);
annots.forEach((a: any) => {
processAnnotation(a, attachments[j], imageRelativePath);
});
attachments[j].annotations = annots;
} catch (e) {
return false;
}

annots.forEach((a: any) => {
processAnnotation(a, attachments[j], imageRelativePath);
});
attachments[j].annotations = annots;
}

const templateData: Record<any, any> = {
...itemData[i],
lastExportDate,
annotations: annots ? annots : []
};

if (annots) templateData.annotations = annots;

applyBasicTemplates(templateData)
applyBasicTemplates(templateData);

const rendered = await renderTemplates(
app,
Expand All @@ -399,6 +432,20 @@ export async function exportToMarkdown(
return true;
}

function getAStyle(settings: ZoteroConnectorSettings) {
const exportStyle = settings.exportFormats.find((f) => !!f.cslStyle);

if (exportStyle) {
return exportStyle.cslStyle;
}

const citeStyle = settings.citeFormats.find((f) => !!f.cslStyle);

if (citeStyle) {
return citeStyle.cslStyle;
}
}

export async function dataExplorerPrompt(settings: ZoteroConnectorSettings) {
const citeKeys: string[] = await getCiteKeys(settings.database);

Expand All @@ -412,9 +459,10 @@ export async function dataExplorerPrompt(settings: ZoteroConnectorSettings) {
}

const exportDate = moment();
const style = getAStyle(settings);

for (let i = 0, len = itemData.length; i < len; i++) {
await processItem(itemData[i], exportDate, settings.database);
await processItem(itemData[i], exportDate, settings.database, style);
}

if (doesEXEExist()) {
Expand All @@ -438,15 +486,14 @@ export async function dataExplorerPrompt(settings: ZoteroConnectorSettings) {
imageQuality: settings.pdfExportImageQuality,
});
annots = JSON.parse(res);
annots.forEach((a: any) => {
processAnnotation(a, attachments[j], 'output_path');
});

attachments[j].annotations = annots;
} catch (e) {
return false;
}

annots.forEach((a: any) => {
processAnnotation(a, attachments[j], 'output_path');
});

attachments[j].annotations = annots;
}
}
}
Expand All @@ -456,11 +503,8 @@ export async function dataExplorerPrompt(settings: ZoteroConnectorSettings) {
a.path?.endsWith('.pdf')
);

if (firstPDF?.annotations) {
data.annotations = firstPDF.annotations;
}

data.lastExportDate = moment().set('year', 1970);
data.annotations = firstPDF?.annotations ? firstPDF.annotations : [];
data.lastExportDate = moment(0);

applyBasicTemplates(data);
});
Expand Down
11 changes: 11 additions & 0 deletions src/bbt/extractAnnotations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { execa } from 'execa';
import { Notice } from 'obsidian';
import path from "path";

import { getExeName, getExeRoot } from 'src/helpers';

Expand Down Expand Up @@ -58,6 +59,11 @@ export async function extractAnnotations(input: string, params: ExtractParams) {

modal.close();

if (result.stderr.toLowerCase().includes('password')) {
new Notice(`Error opening ${path.basename(input)}: PDF is password protected`, 10000);
return '[]'
}

if (result.stderr && !result.stderr.includes('warning')) {
new Notice(`Error processing PDF: ${result.stderr}`, 10000);
throw new Error(result.stderr);
Expand All @@ -66,6 +72,11 @@ export async function extractAnnotations(input: string, params: ExtractParams) {
return result.stdout;
} catch (e) {
modal.close();
if (e.message.toLowerCase().includes('password')) {
new Notice(`Error opening ${path.basename(input)}: PDF is password protected`, 10000);
return '[]'
}

console.error(e);
new Notice(`Error processing PDF: ${e.message}`, 10000);
throw e;
Expand Down
2 changes: 1 addition & 1 deletion src/bbt/template.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export function getLastExport(md: string): moment.Moment {
return moment(new Date(match[1]));
}

return moment().set('year', 1970);
return moment(0);
}

export function appendExportDate(md: string): string {
Expand Down
27 changes: 26 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { DataExplorerView, viewType } from './DataExplorerView';
import { ZoteroConnectorSettingsTab } from './settings/settings';
import { CitationFormat, ExportFormat, ZoteroConnectorSettings } from './types';
import { createEmitter, Emitter } from './emitter';
import { currentVersion, downloadAndExtract } from './settings/AssetDownloader';
import { LoadingModal } from './bbt/LoadingModal';

const citationCommandIDPrefix = 'zdc-';
const exportCommandIDPrefix = 'zdc-exp-';
Expand All @@ -38,9 +40,9 @@ export default class ZoteroConnector extends Plugin {

async onload() {
await this.loadSettings();

this.emitter = createEmitter();

this.updatePDFUtility();
this.addSettingTab(new ZoteroConnectorSettingsTab(this.app, this));
this.registerView(viewType, (leaf) => new DataExplorerView(this, leaf));

Expand Down Expand Up @@ -171,4 +173,27 @@ export default class ZoteroConnector extends Plugin {
type: viewType,
});
}

async updatePDFUtility() {
if (this.settings.exeVersion && this.settings.exeVersion !== currentVersion) {
const modal = new LoadingModal(
(window as any).app,
'Updating Zotero Desktop Connector PDF Utility...'
);
modal.open();

try {
const success = await downloadAndExtract()

if (success) {
this.settings.exeVersion = currentVersion;
this.saveSettings();
}
} catch {
//
}

modal.close();
}
}
}
2 changes: 1 addition & 1 deletion src/settings/AssetDownloader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ZoteroConnectorSettings } from 'src/types';
import { Icon } from './Icon';
import { SettingItem } from './SettingItem';

const currentVersion = '0.0.14';
export const currentVersion = '0.1.0';

const options: Record<string, Record<string, string>> = {
darwin: {
Expand Down
1 change: 0 additions & 1 deletion src/settings/ExportFormatSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ export function ExportFormatSettings({

const onChangeHeaderPath = React.useCallback(
(e: SingleValue<{ value: string; label: string }>) => {
console.log(e?.value);
updateFormat(index, {
...format,
headerTemplatePath: e?.value,
Expand Down
2 changes: 1 addition & 1 deletion src/settings/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function SettingsComponent({
{exportFormatState.map((f, i) => {
return (
<ExportFormatSettings
key={i}
key={f.name}
format={f}
index={i}
updateFormat={updateExport}
Expand Down

0 comments on commit 557ef7b

Please sign in to comment.