Compare commits

..

No commits in common. "d3452351e4aeec0f0731e848af778c6a920c03ac" and "7edbd604e7a81df2c185e874bdc2e514de7117b7" have entirely different histories.

7 changed files with 42 additions and 128 deletions

View file

@ -1,7 +0,0 @@
Copyright © 2022 Ruben van de Ven
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View file

@ -1,32 +1,18 @@
# Zoterolens # zoterolens README
Zoterolens scans Markdown files for citation-keys and adds inline links to open their attached pdf's with Zotero. An optional page number is taken into account. Zoterolens scans Markdown files for possible citationkeys and adds links open then with Zotero. Also a convenient option to immediately open the cited pdf-file.
I recommend using it in combination with an extension such as [Citation Picker for Zotero](https://marketplace.visualstudio.com/items?itemName=mblode.zotero), and/or [Pandoc Citer](https://marketplace.visualstudio.com/items?itemName=notZaki.pandocciter) to be able to easily add citations. I recommend using it in combination with an extension such as [Citation Picker for Zotero](https://marketplace.visualstudio.com/items?itemName=mblode.zotero), and/or [Pandoc Citer](https://marketplace.visualstudio.com/items?itemName=notZaki.pandocciter) to be able to easily add citations.
## Requirements
* Zotero (needs to be running)
* Zotero's Better-Bibtex plugin
## Features ## Features
- CodeLens to open PDF's based on citekey - CodeLens to open PDF's based on citekey
- CodeLens to select item in Zotero based on citekey - CodeLens to select item in Zotero based on citekey
It should find the page number following cases: ## Requirements
- @test123
- @test1.23
- @test123 89
- @test123, 89
- [@test123, 89]
- @test123 [89]
- [@test123, p.89]
- [@test123, pp.89-92]
- [@test123, pp.89--92]
* Zotero
* Zotero's Better-Bibtex plugin
## Extension Settings ## Extension Settings
@ -35,13 +21,13 @@ none.
## Known Issues / Wishlist ## Known Issues / Wishlist
* It does not yet check if the citekey actually exists in Zotero, nor if it actually has any attachment. * It does not yet check if the citekey actually exists in Zotero, nor if it actually has any attachment.
* This should be made to happen _before_ showing the ref.
* No error if Zotero isn't running, or the Better-Bibtex plugin is not configured. * No error if Zotero isn't running, or the Better-Bibtex plugin is not configured.
* Does not yet consider the page number of the citation when opening the pdf.
* Zotero port cannot yet be configured. * Zotero port cannot yet be configured.
## Release Notes ## Release Notes
### 0.1.0 ### 1.0.0
Initial release of Zoterolens
Initial pre-release of Zoterolens

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{ {
"name": "zoterolens", "name": "zoterolens",
"version": "0.1.1", "version": "0.0.1",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "zoterolens", "name": "zoterolens",
"version": "0.1.1", "version": "0.0.1",
"devDependencies": { "devDependencies": {
"@types/glob": "^7.2.0", "@types/glob": "^7.2.0",
"@types/mocha": "^9.0.0", "@types/mocha": "^9.0.0",

View file

@ -1,19 +1,11 @@
{ {
"name": "zoterolens", "name": "zoterolens",
"publisher": "rubenvandeven",
"displayName": "ZoteroLens", "displayName": "ZoteroLens",
"description": "When using Markdown citations, Zoterolens provides quick access to Zotero pdf-attachments.", "description": "Lens providing quick access to Zotero information and attachments from citations.",
"version": "0.1.1", "version": "0.0.1",
"engines": { "engines": {
"vscode": "^1.63.0" "vscode": "^1.63.0"
}, },
"author": {
"name": "Ruben van de Ven"
},
"repository": {
"type": "git",
"url": "https://git.rubenvandeven.com/r/vscode-zoterolens.git"
},
"categories": [ "categories": [
"Other" "Other"
], ],
@ -24,11 +16,11 @@
"contributes": { "contributes": {
"commands": [ "commands": [
{ {
"command": "zoterolens.showInZotero", "command": "extension.showInZotero",
"title": "Select item in Zotero by citeref" "title": "Select item in Zotero by citeref"
}, },
{ {
"command": "zoterolens.openZoteroPDF", "command": "extension.openZoteroPDF",
"title": "Open PDF attached in Zotero by citeref." "title": "Open PDF attached in Zotero by citeref."
} }
] ]

View file

@ -1,6 +1,6 @@
import { exec } from "child_process"; import { exec } from "child_process";
import { IncomingMessage, request } from "http"; import { IncomingMessage, request } from "http";
import { Reference } from "./zoteroCodeLensProvider"; import Reference from "./zoteroCodeLensProvider";
async function showInZotero(reference: Reference) { async function showInZotero(reference: Reference) {
@ -8,6 +8,7 @@ async function showInZotero(reference: Reference) {
} }
async function openZoteroPDF(reference: Reference) { async function openZoteroPDF(reference: Reference) {
const req = request("http://127.0.0.1:23119/better-bibtex/json-rpc", { const req = request("http://127.0.0.1:23119/better-bibtex/json-rpc", {
'method': 'POST', 'method': 'POST',
'headers': { 'headers': {
@ -16,7 +17,7 @@ async function openZoteroPDF(reference: Reference) {
} }
}); });
req.on("response", (res: IncomingMessage) => { req.on("response", (res: IncomingMessage) => {
if (res.statusCode !== 200) { if (res.statusCode != 200) {
console.error('Error with request. Is Zotero running and Better-BibTex installed?') console.error('Error with request. Is Zotero running and Better-BibTex installed?')
} else { } else {
// console.log('test2', res.statusCode, res); // console.log('test2', res.statusCode, res);
@ -29,9 +30,7 @@ async function openZoteroPDF(reference: Reference) {
for (const result of data['result']) { for (const result of data['result']) {
if (result['path'].toLowerCase().endsWith('.pdf')) { if (result['path'].toLowerCase().endsWith('.pdf')) {
// TODO add ?page=nr to open given page // TODO add ?page=nr to open given page
openUrl(result['open']);
const pagenr = reference.pagenr !== null ? "?page=" + reference.pagenr : "";
openUrl(result['open'] + pagenr);
break; // only open a single attachement break; // only open a single attachement
} }
} }
@ -43,7 +42,7 @@ async function openZoteroPDF(reference: Reference) {
} }
function openUrl(url: string) { function openUrl(url: string) {
// console.log('open', url);
switch (process.platform) { switch (process.platform) {
case 'darwin': case 'darwin':
exec('open "' + url + '"'); exec('open "' + url + '"');

View file

@ -1,16 +1,16 @@
import { commands, ExtensionContext, languages } from "vscode"; import { commands, ExtensionContext, languages } from "vscode";
import { ZoteroCodeLensProvider } from "./zoteroCodeLensProvider"; import ZoteroCodeLensProvider from "./zoteroCodeLensProvider";
import { openZoteroPDF, showInZotero } from "./commands"; import { openZoteroPDF, showInZotero } from "./commands";
export function activate(context: ExtensionContext) { export function activate(context: ExtensionContext) {
// Register the command // Register the command
let commandDisposable2 = commands.registerCommand( let commandDisposable2 = commands.registerCommand(
"zoterolens.showInZotero", "extension.showInZotero",
showInZotero showInZotero
); );
let commandDisposable3 = commands.registerCommand( let commandDisposable3 = commands.registerCommand(
"zoterolens.openZoteroPDF", "extension.openZoteroPDF",
openZoteroPDF openZoteroPDF
); );
@ -31,7 +31,6 @@ export function activate(context: ExtensionContext) {
context.subscriptions.push(commandDisposable2); context.subscriptions.push(commandDisposable2);
context.subscriptions.push(commandDisposable3); context.subscriptions.push(commandDisposable3);
context.subscriptions.push(codeLensProviderDisposable); context.subscriptions.push(codeLensProviderDisposable);
} }
export function deactivate() {} export function deactivate() {}

View file

@ -7,88 +7,32 @@ import {
Command Command
} from "vscode"; } from "vscode";
export interface Reference { interface Reference {
document: TextDocument; document: TextDocument;
citekey: string; citekey: string;
pagenr: number | null; pagenr: number | null;
range: Range; range: Range;
} }
export class ZoteroCodeLensProvider implements CodeLensProvider { class ZoteroCodeLensProvider implements CodeLensProvider {
// Each provider requires a provideCodeLenses function which will give the various documents // Each provider requires a provideCodeLenses function which will give the various documents
// the code lenses // the code lenses
async provideCodeLenses(document: TextDocument): Promise<CodeLens[]> { async provideCodeLenses(document: TextDocument): Promise<CodeLens[]> {
// const txt = document.getText();
const references = this.findReferences(document); const references = this.findReferences(document);
return references return references.map(reference => [
// sort by line, citekey, pagenr new CodeLens(reference.range, {
.sort((a, b) => { title: '@' + reference.citekey,
if (a.range.start.line !== b.range.start.line) { command: 'extension.showInZotero',
return a.range.start.line - b.range.start.line; arguments: [reference]
} }),
if (a.citekey !== b.citekey) { new CodeLens(reference.range, {
return a.citekey.localeCompare(b.citekey); title: '(pdf)',
} command: 'extension.openZoteroPDF',
// nulls sort before anything else arguments: [reference]
if (a.pagenr === null) { })]).flat();
return -1;
}
if (b.pagenr === null) {
return 1;
}
return a.pagenr - b.pagenr;
})
// remove duplicate items (which are in sequence)
.filter((reference, index) => {
if (index === 0) { return true; }
if (
references[index - 1].range.start.line !== reference.range.start.line ||
references[index - 1].citekey !== reference.citekey ||
references[index - 1].pagenr !== reference.pagenr
) {
return true;
}
return false;
})
.map((reference, index) => {
let title = "";
if (
index === 0 ||
references[index - 1].range.start.line !== reference.range.start.line ||
references[index - 1].citekey !== reference.citekey
) {
title = `@${reference.citekey}`;
if (reference.pagenr) {
title += ` p.${reference.pagenr}`;
}
} else {
// there has to be a pagenr because of filtering
title = `p.${reference.pagenr}`;
}
// ignore char pos so that the order of the array
// is actually used (by default CodeLenses are sorted by position)
const range = new Range(
reference.range.start.line, 0,
reference.range.end.line, 0
);
return [
new CodeLens(range, {
title: title,
// command: 'zoterolens.showInZotero',
command: 'zoterolens.openZoteroPDF',
arguments: [reference]
}),
// new CodeLens(reference.range, {
// title: '(pdf)',
// command: 'zoterolens.openZoteroPDF',
// arguments: [reference]
// })
]
})
.flat();
// let codeLens = new CodeLens(topOfDocument, c); // let codeLens = new CodeLens(topOfDocument, c);
@ -105,16 +49,16 @@ export class ZoteroCodeLensProvider implements CodeLensProvider {
for (let lineNr = 0; lineNr < document.lineCount; lineNr++) { for (let lineNr = 0; lineNr < document.lineCount; lineNr++) {
const line = document.lineAt(lineNr); const line = document.lineAt(lineNr);
let match: RegExpExecArray | null; let match: RegExpExecArray | null;
let regex = /(?<=@)([\w\.]+)[, ]*\[?(?:[p]{0,2}\.)?(\d+)?(?:-+\d+)?\]?/g; let regex = /(?<=@)\w+/g;
regex.lastIndex = 0; regex.lastIndex = 0;
const text = line.text;//.substring(0, 1000); const text = line.text;//.substring(0, 1000);
while ((match = regex.exec(text))) { while ((match = regex.exec(text))) {
const result = { const result = {
document: document, document: document,
citekey: match[1], citekey: match[0],
pagenr: match[2] ? parseInt(match[2]) : null, pagenr: null,
range: new Range(lineNr, match.index, lineNr, match.index + match[0].length) range: new Range(lineNr, match.index, lineNr, match.index + match[0].length)
} as Reference; };
// if (result) { // if (result) {
matches.push(result); matches.push(result);
// } // }
@ -124,3 +68,4 @@ export class ZoteroCodeLensProvider implements CodeLensProvider {
} }
} }
export default ZoteroCodeLensProvider;