Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lzilioli improvements #7

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default class TesseractOcrPlugin extends Plugin {
if (this.settings.debug == true) console.log('writing to note!');
this.app.vault.adapter.write(file.path, newContent);
}
if(errorCounter > 0) console.log(errorCounter + ' errors encountered in this file: ' + file.name);
if(errorCounter > 0) console.error(errorCounter + ' errors encountered in this file: ' + file.name);
}
};
statusBarItemEl.remove();
Expand Down Expand Up @@ -151,7 +151,7 @@ export default class TesseractOcrPlugin extends Plugin {
private async getTextFromImage(filePath: string): Promise<string> {
let fullPath = (this.app.vault.adapter as FileSystemAdapter).getFullPath(filePath);
let command = this.settings.tesseractPath + 'tesseract';
let commandArgs = [fullPath, '-', '-l', this.settings.tesseractLanguage];
let commandArgs = [`${fullPath.replace(/ /g, '\ ')}`, "-", "-l", this.settings.tesseractLanguage];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to escape the \ here...
Try maybe something more complete like:
replace(/(\s+)/g, '\\$1')

Copy link
Author

@lzilioli lzilioli Dec 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code snippet only escapes the first space in a sequence of many. We need to escape each individual space.

"path/to my file has a     lot.   of spaces".replace(/(\s+)/g, '\\$1')

returns "path/to\\ my\\ file\\ has\\ a\\ lot.\\ of\\ spaces"

Whereas the regex replace in the PR escapes each individual space

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, right, this should work:

"path/to my file has a     lot.   of spaces".replace(/ /g, '\\ ');

Your solution in the PR will just replace the spaces with spaces :)

Copy link
Author

@lzilioli lzilioli Dec 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So maybe we just need the double quotes and no replace then? The code as modified fixed the issue I was seeing. I do see that my string ends up unmodified with the replace, so it seems like double quotes may be all thats needed here


if (this.settings.debug == true) console.log('command to be run: ' + command + ' ' + commandArgs.join(' '));

Expand All @@ -172,11 +172,15 @@ export default class TesseractOcrPlugin extends Plugin {
});

execution.on('close', () => {
if (this.settings.debug == true) console.log('tesseract output: ' + stdout.join(''));
if (this.settings.debug == true) console.log('tesseract output: ' + error.join(''));
// if we don't get any output, reject
if (stdout.join('').length == 0) reject(error.join(''));
else resolve(stdout.join(''));
if (this.settings.debug == true)
console.log("tesseract stdout: " + stdout.join(""));
if (this.settings.debug == true)
console.log("tesseract stderr: " + error.join(""));
if (stdout.join("").length == 0)
// reject if we dont get any output
reject(error.join(""));
else
resolve(stdout.join(""));
});

});
Expand Down