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.
## Requirements
* Zotero (needs to be running)
* Zotero's Better-Bibtex plugin
## Features
- CodeLens to open PDF's based on citekey
- CodeLens to select item in Zotero based on citekey
It should find the page number following cases:
- @test123
- @test1.23
- @test123 89
- @test123, 89
- [@test123, 89]
- @test123 [89]
- [@test123, p.89]
- [@test123, pp.89-92]
- [@test123, pp.89--92]
## Requirements
* Zotero
* Zotero's Better-Bibtex plugin
## Extension Settings
@ -35,13 +21,13 @@ none.
## Known Issues / Wishlist
* 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.
* Does not yet consider the page number of the citation when opening the pdf.
* Zotero port cannot yet be configured.
## 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",
"version": "0.1.1",
"version": "0.0.1",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "zoterolens",
"version": "0.1.1",
"version": "0.0.1",
"devDependencies": {
"@types/glob": "^7.2.0",
"@types/mocha": "^9.0.0",

View file

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

View file

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

View file

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

View file

@ -7,88 +7,32 @@ import {
Command
} from "vscode";
export interface Reference {
interface Reference {
document: TextDocument;
citekey: string;
pagenr: number | null;
range: Range;
}
export class ZoteroCodeLensProvider implements CodeLensProvider {
class ZoteroCodeLensProvider implements CodeLensProvider {
// Each provider requires a provideCodeLenses function which will give the various documents
// the code lenses
async provideCodeLenses(document: TextDocument): Promise<CodeLens[]> {
// const txt = document.getText();
const references = this.findReferences(document);
return references
// sort by line, citekey, pagenr
.sort((a, b) => {
if (a.range.start.line !== b.range.start.line) {
return a.range.start.line - b.range.start.line;
}
if (a.citekey !== b.citekey) {
return a.citekey.localeCompare(b.citekey);
}
// nulls sort before anything else
if (a.pagenr === null) {
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();
return references.map(reference => [
new CodeLens(reference.range, {
title: '@' + reference.citekey,
command: 'extension.showInZotero',
arguments: [reference]
}),
new CodeLens(reference.range, {
title: '(pdf)',
command: 'extension.openZoteroPDF',
arguments: [reference]
})]).flat();
// let codeLens = new CodeLens(topOfDocument, c);
@ -105,16 +49,16 @@ export class ZoteroCodeLensProvider implements CodeLensProvider {
for (let lineNr = 0; lineNr < document.lineCount; lineNr++) {
const line = document.lineAt(lineNr);
let match: RegExpExecArray | null;
let regex = /(?<=@)([\w\.]+)[, ]*\[?(?:[p]{0,2}\.)?(\d+)?(?:-+\d+)?\]?/g;
let regex = /(?<=@)\w+/g;
regex.lastIndex = 0;
const text = line.text;//.substring(0, 1000);
while ((match = regex.exec(text))) {
const result = {
document: document,
citekey: match[1],
pagenr: match[2] ? parseInt(match[2]) : null,
citekey: match[0],
pagenr: null,
range: new Range(lineNr, match.index, lineNr, match.index + match[0].length)
} as Reference;
};
// if (result) {
matches.push(result);
// }
@ -124,3 +68,4 @@ export class ZoteroCodeLensProvider implements CodeLensProvider {
}
}
export default ZoteroCodeLensProvider;