-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement basic code highlighting with pygments
- use interface that permits multiple highlighters in the future
- Loading branch information
Showing
6 changed files
with
186 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export type Language = { | ||
relevance: number; | ||
language: string; | ||
}; | ||
|
||
export type CodeHighlighterType = 'CommandLine' | 'Integrated' | 'JSModule'; | ||
|
||
export abstract class CodeHighlighter { | ||
readonly name: string; | ||
readonly type: CodeHighlighterType; | ||
|
||
constructor(name: string, type: CodeHighlighterType) { | ||
this.name = name; | ||
this.type = type; | ||
} | ||
|
||
abstract isInstalled(): boolean; | ||
|
||
abstract detectLanguage(text: string): Language | undefined; | ||
|
||
abstract markupCode(language: string, text: string, characterLength: number): string | undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { CodeHighlighter, type Language } from '@pano/utils/code/highlight'; | ||
import { logger } from '@pano/utils/shell'; | ||
import Gio from 'gi://Gio?version=2.0'; | ||
|
||
const debug = logger('code-highlighter:pygments'); | ||
|
||
export class PygmentsCodeHighlighter extends CodeHighlighter { | ||
private cliName = 'pygmentize'; | ||
|
||
constructor() { | ||
super('pygments', 'CommandLine'); | ||
} | ||
|
||
override isInstalled(): boolean { | ||
try { | ||
const proc = Gio.Subprocess.new( | ||
['which', this.cliName], | ||
Gio.SubprocessFlags.STDOUT_SILENCE | Gio.SubprocessFlags.STDERR_SILENCE, | ||
); | ||
|
||
const success = proc.wait(null); | ||
if (!success) { | ||
throw new Error('Process was cancelled'); | ||
} | ||
|
||
return proc.get_successful(); | ||
} catch (err) { | ||
debug(`An error occurred while testing for the executable: ${err}`); | ||
return false; | ||
} | ||
} | ||
|
||
override detectLanguage(text: string): Language | undefined { | ||
const noLanguageDetected = 'text'; | ||
|
||
try { | ||
const proc = Gio.Subprocess.new( | ||
[this.cliName, '-C'], | ||
Gio.SubprocessFlags.STDERR_PIPE | Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDIN_PIPE, | ||
); | ||
const [success, stdout, stderr] = proc.communicate_utf8(text, null); | ||
|
||
if (!success) { | ||
throw new Error('Process was cancelled'); | ||
} | ||
|
||
if (proc.get_successful()) { | ||
const content = stdout.trim(); | ||
|
||
if (content === noLanguageDetected) { | ||
return undefined; | ||
} | ||
|
||
return { language: content, relevance: 1.0 }; | ||
} else { | ||
throw new Error(`Process exited with exit code: ${proc.get_exit_status()} and output: ${stderr}`); | ||
} | ||
} catch (err) { | ||
debug(`An error occurred while detecting the language: ${err}`); | ||
return undefined; | ||
} | ||
} | ||
|
||
override markupCode(language: string, text: string, characterLength: number): string | undefined { | ||
const finalText = text.substring(0, characterLength); | ||
|
||
try { | ||
const proc = Gio.Subprocess.new( | ||
[this.cliName, '-l', language, '-f', 'pango'], | ||
Gio.SubprocessFlags.STDERR_PIPE | Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDIN_PIPE, | ||
); | ||
const [success, stdout, stderr] = proc.communicate_utf8(finalText, null); | ||
|
||
if (!success) { | ||
throw new Error('Process was cancelled'); | ||
} | ||
|
||
if (proc.get_successful()) { | ||
return stdout; | ||
} else { | ||
throw new Error(`Process exited with exit code: ${proc.get_exit_status()} and output: ${stderr}`); | ||
} | ||
} catch (err) { | ||
debug(`An error occurred while formatting the language: ${err}`); | ||
return undefined; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,64 @@ | ||
import { logger } from '@pano/utils/shell'; | ||
import type { CodeHighlighter, Language } from '@pano/utils/code/highlight'; | ||
import { PygmentsCodeHighlighter } from '@pano/utils/code/pygments'; | ||
|
||
const debug = logger('pango'); | ||
//TODO: | ||
// add highlight.js back, if it is installed and can be found via require() | ||
// add settings, that might change theses things: | ||
// which doe formatter to use, which style to use | ||
// enable, disable formatting, change the threshold (event if its always 1.0 in e.g pygmentize) | ||
// only make these count, if enabled is set, so if at least one formatter is found | ||
// button to recheck tools | ||
// customs settings per highlighter | ||
|
||
export type Language = { | ||
relevance: number; | ||
language: string; | ||
}; | ||
let detectedHighlighter: CodeHighlighter[] | null = null; | ||
|
||
export function detectLanguage(_text: string): Language | undefined { | ||
//TODO: implement language detection | ||
return undefined; | ||
let currentHighlighter: CodeHighlighter | null = null; | ||
|
||
const availableCodeHighlighter: CodeHighlighter[] = [new PygmentsCodeHighlighter()]; | ||
|
||
// this is only implicitly called once, even if nothing is found, it isn't called again later, it has to be initiated by the user later, to scan again | ||
export function detectHighlighter(force = false, preferredHighlighter: string | null = null) { | ||
if (detectedHighlighter !== null && !force) { | ||
return; | ||
} | ||
|
||
detectedHighlighter = []; | ||
|
||
for (const codeHighlighter of availableCodeHighlighter) { | ||
if (codeHighlighter.isInstalled()) { | ||
detectedHighlighter.push(codeHighlighter); | ||
|
||
if (preferredHighlighter === null) { | ||
if (currentHighlighter === null) { | ||
currentHighlighter = codeHighlighter; | ||
} | ||
} else if (codeHighlighter.name == preferredHighlighter) { | ||
currentHighlighter = codeHighlighter; | ||
} | ||
} | ||
} | ||
} | ||
|
||
export function markupCode(_language: string, _text: string, _characterLength: number): string { | ||
//TODO implement code highlighting | ||
debug('TODO'); | ||
return ''; | ||
export function detectLanguage(text: string): Language | undefined { | ||
if (detectedHighlighter === null) { | ||
detectHighlighter(); | ||
} | ||
|
||
if (currentHighlighter === null) { | ||
return undefined; | ||
} | ||
|
||
return currentHighlighter.detectLanguage(text); | ||
} | ||
|
||
export function markupCode(language: string, text: string, characterLength: number): string | undefined { | ||
if (detectedHighlighter === null) { | ||
detectHighlighter(); | ||
} | ||
|
||
if (currentHighlighter === null) { | ||
return undefined; | ||
} | ||
|
||
return currentHighlighter.markupCode(language, text, characterLength); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters