Skip to content

Commit

Permalink
Add automatic tesseract detection
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmeyers committed Jul 3, 2023
1 parent 8d641d1 commit 2708018
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 4 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@types/jest": "29.5.2",
"@types/node": "20.3.3",
"@types/nunjucks": "3.2.3",
"@types/which": "^3.0.0",
"@typescript-eslint/eslint-plugin": "5.61.0",
"@typescript-eslint/parser": "5.61.0",
"babel-jest": "29.5.0",
Expand Down Expand Up @@ -55,6 +56,8 @@
"react": "npm:@preact/compat",
"react-dom": "npm:@preact/compat",
"react-json-tree": "0.18.0",
"react-select": "5.7.3"
"react-select": "5.7.3",
"shell-path": "^3.0.0",
"which": "^3.0.1"
}
}
1 change: 1 addition & 0 deletions src/globals.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
declare module 'escape-path-with-spaces';
declare module 'shell-path';
24 changes: 24 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EditableFileView, Events, Plugin, TFile } from 'obsidian';
import { shellPath } from 'shell-path';

import { DataExplorerView, viewType } from './DataExplorerView';
import { LoadingModal } from './bbt/LoadingModal';
Expand Down Expand Up @@ -32,6 +33,27 @@ const DEFAULT_SETTINGS: ZoteroConnectorSettings = {
whichNotesToOpenAfterImport: 'first-imported-note',
};

async function fixPath() {
if (process.platform === 'win32') {
return;
}

try {
const path = await shellPath();

process.env.PATH =
path ||
[
'./node_modules/.bin',
'/.nodebrew/current/bin',
'/usr/local/bin',
process.env.PATH,
].join(':');
} catch (e) {
console.error(e);
}
}

export default class ZoteroConnector extends Plugin {
settings: ZoteroConnectorSettings;
emitter: Events;
Expand Down Expand Up @@ -99,6 +121,8 @@ export default class ZoteroConnector extends Plugin {
});

this.registerEditorSuggest(new CiteSuggest(this.app, this));

fixPath();
}

onunload() {
Expand Down
51 changes: 49 additions & 2 deletions src/settings/settings.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { App, Platform, PluginSettingTab, debounce } from 'obsidian';
import { App, Notice, Platform, PluginSettingTab, debounce } from 'obsidian';
import React from 'react';
import ReactDOM from 'react-dom';
import which from 'which';

import ZoteroConnector from '../main';
import {
Expand All @@ -11,6 +12,7 @@ import {
import { AssetDownloader } from './AssetDownloader';
import { CiteFormatSettings } from './CiteFormatSettings';
import { ExportFormatSettings } from './ExportFormatSettings';
import { Icon } from './Icon';
import { SettingItem } from './SettingItem';

interface SettingsComponentProps {
Expand Down Expand Up @@ -108,6 +110,9 @@ function SettingsComponent({
[removeExportFormat]
);

const tessPathRef = React.useRef<HTMLInputElement>(null);
const tessDataPathRef = React.useRef<HTMLInputElement>(null);

return (
<div>
<SettingItem name="General Settings" isHeading />
Expand Down Expand Up @@ -359,6 +364,7 @@ function SettingsComponent({
}
>
<input
ref={tessPathRef}
onChange={(e) =>
updateSetting(
'pdfExportImageTesseractPath',
Expand All @@ -368,6 +374,30 @@ function SettingsComponent({
type="text"
defaultValue={settings.pdfExportImageTesseractPath}
/>
<div
className="clickable-icon setting-editor-extra-setting-button"
aria-label="Attempt to find tesseract automatically"
onClick={async () => {
try {
const pathToTesseract = await which('tesseract');
if (pathToTesseract) {
tessPathRef.current.value = pathToTesseract;
updateSetting('pdfExportImageTesseractPath', pathToTesseract);
} else {
new Notice(
'Unable to find tesseract on your system. If it is installed, please manually enter a path.'
);
}
} catch (e) {
new Notice(
'Unable to find tesseract on your system. If it is installed, please manually enter a path.'
);
console.error(e);
}
}}
>
<Icon name="magnifying-glass" />
</div>
</SettingItem>
<SettingItem
name="Image OCR Language"
Expand Down Expand Up @@ -408,9 +438,10 @@ function SettingsComponent({
</SettingItem>
<SettingItem
name="Tesseract data directory"
description="Optional: supply an absolute path to the directory where tesseract's language files reside."
description="Optional: supply an absolute path to the directory where tesseract's language files reside. This folder should include *.traineddata files for your selected languages."
>
<input
ref={tessDataPathRef}
onChange={(e) =>
updateSetting(
'pdfExportImageTessDataDir',
Expand All @@ -420,6 +451,22 @@ function SettingsComponent({
type="text"
defaultValue={settings.pdfExportImageTessDataDir}
/>
<div
className="clickable-icon setting-editor-extra-setting-button"
aria-label="Select the tesseract data directory"
onClick={() => {
const path = require('electron').remote.dialog.showOpenDialogSync({
properties: ['openDirectory'],
});

if (path && path.length) {
tessDataPathRef.current.value = path[0];
updateSetting('pdfExportImageTessDataDir', path[0]);
}
}}
>
<Icon name="lucide-folder-open" />
</div>
</SettingItem>
</div>
);
Expand Down
47 changes: 46 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1949,6 +1949,11 @@
resolved "https://registry.yarnpkg.com/@types/tough-cookie/-/tough-cookie-4.0.2.tgz#6286b4c7228d58ab7866d19716f3696e03a09397"
integrity sha512-Q5vtl1W5ue16D+nIaW8JWebSSraJVlK+EthKn7e7UcD4KWsaSJ8BqGPXNaPghgtcn/fhvrN17Tv8ksUsQpiplw==

"@types/which@^3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@types/which/-/which-3.0.0.tgz#849afdd9fdcb0b67339b9cfc80fa6ea4e0253fc5"
integrity sha512-ASCxdbsrwNfSMXALlC3Decif9rwDMu+80KGp5zI2RLRotfMsTv7fHL8W8VDp24wymzDyIFudhUeSCugrgRFfHQ==

"@types/yargs-parser@*":
version "21.0.0"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-21.0.0.tgz#0c60e537fa790f5f9472ed2776c2b71ec117351b"
Expand Down Expand Up @@ -2128,6 +2133,11 @@ ansi-regex@^5.0.1:
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-5.0.1.tgz#082cb2c89c9fe8659a311a53bd6a4dc5301db304"
integrity sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==

ansi-regex@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-6.0.1.tgz#3183e38fae9a65d7cb5e53945cd5897d0260a06a"
integrity sha512-n5M855fKb2SsfMIiFFoVrABHJC8QtHwVx+mHWP3QcEqBHYienj5dHSgjbxtC0WEZXYt4wcD6zrQElDPhFuZgfA==

ansi-styles@^3.2.0, ansi-styles@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d"
Expand Down Expand Up @@ -3041,6 +3051,11 @@ deepmerge@^4.2.2:
resolved "https://registry.yarnpkg.com/deepmerge/-/deepmerge-4.3.1.tgz#44b5f2147cd3b00d4b56137685966f26fd25dd4a"
integrity sha512-3sUqbMEc77XqpdNO7FRyRog+eW3ph+GYCbj+rK+uYyRMuwsVy0rMiVtPn+QJlKFvWP/1PYpapqYn0Me2knFn+A==

default-shell@^2.0.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/default-shell/-/default-shell-2.2.0.tgz#31481c19747bfe59319b486591643eaf115a1864"
integrity sha512-sPpMZcVhRQ0nEMDtuMJ+RtCxt7iHPAMBU+I4tAlo5dU1sjRpNax0crj6nR3qKpvVnckaQ9U38enXcwW9nZJeCw==

defer-to-connect@^1.0.1:
version "1.1.3"
resolved "https://registry.yarnpkg.com/defer-to-connect/-/defer-to-connect-1.1.3.tgz#331ae050c08dcf789f8c83a7b81f0ed94f4ac591"
Expand Down Expand Up @@ -3566,7 +3581,7 @@ execa@^1.0.0:
signal-exit "^3.0.0"
strip-eof "^1.0.0"

execa@^5.0.0:
execa@^5.0.0, execa@^5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/execa/-/execa-5.1.1.tgz#f80ad9cbf4298f7bd1d4c9555c21e93741c411dd"
integrity sha512-8uSpZZocAZRBAPIEINJj3Lo9HyGitllczc27Eh5YYojjMFMn8yHMDMaUHE2Jqfq05D/wucwI4JGURyXt1vchyg==
Expand Down Expand Up @@ -6742,6 +6757,22 @@ shebang-regex@^3.0.0:
resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-3.0.0.tgz#ae16f1644d873ecad843b0307b143362d4c42172"
integrity sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==

shell-env@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/shell-env/-/shell-env-4.0.1.tgz#883302d9426095d398a39b102a851adb306b8cb8"
integrity sha512-w3oeZ9qg/P6Lu6qqwavvMnB/bwfsz67gPB3WXmLd/n6zuh7TWQZtGa3iMEdmua0kj8rivkwl+vUjgLWlqZOMPw==
dependencies:
default-shell "^2.0.0"
execa "^5.1.1"
strip-ansi "^7.0.1"

shell-path@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/shell-path/-/shell-path-3.0.0.tgz#5c95bc68aade43c06082a0655cb5c97586e4feb0"
integrity sha512-HNIZ+W/3P0JuVTV03xjGqYKt3e3h0/Z4AH8TQWeth1LBtCusSjICgkdNdb3VZr6mI7ijE2AiFFpgkVMNKsALeQ==
dependencies:
shell-env "^4.0.0"

side-channel@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/side-channel/-/side-channel-1.0.4.tgz#efce5c8fdc104ee751b25c58d4290011fa5ea2cf"
Expand Down Expand Up @@ -7031,6 +7062,13 @@ strip-ansi@^6.0.0, strip-ansi@^6.0.1:
dependencies:
ansi-regex "^5.0.1"

strip-ansi@^7.0.1:
version "7.1.0"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45"
integrity sha512-iq6eVVI64nQQTRYq2KtEg2d2uU7LElhTJwsH4YzIHZshxlgZms/wIc4VoDQTlG/IvVIrBKG06CrZnp0qv7hkcQ==
dependencies:
ansi-regex "^6.0.1"

strip-bom@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-4.0.0.tgz#9c3505c1db45bcedca3d9cf7a16f5c5aa3901878"
Expand Down Expand Up @@ -7505,6 +7543,13 @@ which@^2.0.1, which@^2.0.2:
dependencies:
isexe "^2.0.0"

which@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/which/-/which-3.0.1.tgz#89f1cd0c23f629a8105ffe69b8172791c87b4be1"
integrity sha512-XA1b62dzQzLfaEOSQFTCOd5KFf/1VSzZo7/7TUjnya6u0vGGKzU96UQBZTAThCb2j4/xjBAyii1OhRLJEivHvg==
dependencies:
isexe "^2.0.0"

widest-line@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/widest-line/-/widest-line-3.1.0.tgz#8292333bbf66cb45ff0de1603b136b7ae1496eca"
Expand Down

0 comments on commit 2708018

Please sign in to comment.