From 65b6410a6b3f5dfb964347190acfecc2f8bc2d08 Mon Sep 17 00:00:00 2001 From: qiaogaojian Date: Tue, 20 Jun 2023 19:19:41 +0800 Subject: [PATCH 1/9] =?UTF-8?q?fix:=20{pre}=20{post}=20{label}=20etc=20are?= =?UTF-8?q?=20not=20working=20when=20language=20annotation=20begin=20with?= =?UTF-8?q?=20=E2=80=98run-=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/transforms/TransformCode.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/transforms/TransformCode.ts b/src/transforms/TransformCode.ts index c5cf527e..f3ae7ccc 100644 --- a/src/transforms/TransformCode.ts +++ b/src/transforms/TransformCode.ts @@ -56,5 +56,19 @@ export function transformMagicCommands(app: App, srcCode: string) { * @returns The language of the code block. */ export function getCodeBlockLanguage(firstLineOfCode: string) { - return getLanguageAlias(firstLineOfCode.split("```")[1].trim().split(" ")[0].split("{")[0]) + let currentLanguage: string = firstLineOfCode.split("```")[1].trim().split(" ")[0].split("{")[0]; + if (isStringNotEmpty(currentLanguage) && currentLanguage.startsWith("run-")) { + currentLanguage = currentLanguage.replace("run-", ""); + } + return getLanguageAlias(currentLanguage); +} + +/** + * Check if a string is not empty + * + * @param str Input string + * @returns True when string not empty, False when the string is Empty + */ +export function isStringNotEmpty(str: string): boolean { + return !!str && str.trim().length > 0; } From 7bf2b4661eb72d2a5e9e35d344181eba8f835867 Mon Sep 17 00:00:00 2001 From: qiaogaojian Date: Sat, 5 Aug 2023 10:16:58 +0800 Subject: [PATCH 2/9] feat: only show log which in current code block --- src/settings/Settings.ts | 2 ++ src/settings/SettingsTab.ts | 11 +++++++++++ src/transforms/CodeInjector.ts | 15 ++++++++++++--- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/settings/Settings.ts b/src/settings/Settings.ts index d63297cb..323ff458 100644 --- a/src/settings/Settings.ts +++ b/src/settings/Settings.ts @@ -9,6 +9,7 @@ export interface ExecutorSettings { timeout: number; allowInput: boolean; wslMode: boolean; + onlyCurrentBlock: boolean; nodePath: string; nodeArgs: string; jsInject: string; @@ -157,6 +158,7 @@ export const DEFAULT_SETTINGS: ExecutorSettings = { timeout: 10000, allowInput: true, wslMode: false, + onlyCurrentBlock: false, nodePath: "node", nodeArgs: "", jsInject: "", diff --git a/src/settings/SettingsTab.ts b/src/settings/SettingsTab.ts index 78976519..03954c52 100644 --- a/src/settings/SettingsTab.ts +++ b/src/settings/SettingsTab.ts @@ -99,6 +99,17 @@ export class SettingsTab extends PluginSettingTab { })); } + new Setting(containerEl) + .setName('Only Current Log') + .setDesc("Whether or not show print log only in current code block.") + .addToggle(text => text + .setValue(this.plugin.settings.onlyCurrentBlock) + .onChange(async (value) => { + console.log('Only Show Current Block Log set to: ' + value); + this.plugin.settings.onlyCurrentBlock = value + await this.plugin.saveSettings(); + })); + // TODO setting per language that requires main function if main function should be implicitly made or not, if not, non-main blocks will not have a run button containerEl.createEl("hr"); diff --git a/src/transforms/CodeInjector.ts b/src/transforms/CodeInjector.ts index f7e037be..628ba88f 100644 --- a/src/transforms/CodeInjector.ts +++ b/src/transforms/CodeInjector.ts @@ -85,7 +85,7 @@ export class CodeInjector { new Notice(`Named export "${namedImport}" does not exist but was imported`); return true; } - this.namedImportSrcCode += `${this.namedExports[namedImport]}\n`; + this.namedImportSrcCode += `${this.disable_print(this.namedExports[namedImport])}\n`; return false; }; // Single import @@ -145,9 +145,9 @@ export class CodeInjector { if (!Array.isArray(currentArgs.export)) currentArgs.export = [currentArgs.export]; if (currentArgs.export.contains("pre")) - this.prependSrcCode += `${currentCode}\n`; + this.prependSrcCode += `${this.disable_print(currentCode)}\n`; if (currentArgs.export.contains("post")) - this.appendSrcCode += `${currentCode}\n`; + this.appendSrcCode += `${this.disable_print(currentCode)}\n`; currentLanguage = ""; currentCode = ""; insideCodeBlock = false; @@ -170,4 +170,13 @@ export class CodeInjector { } } } + + private disable_print(code: String): String { + if (!this.settings.onlyCurrentBlock) { + return code; + } + const pattern: RegExp = /^print\s*(.*)/gm; + // 使用正则表达式替换函数将符合条件的内容注释掉 + return code.replace(pattern, ' '); + } } From 889c81f80dac46d7429c4c768cf1e0aa9f162afb Mon Sep 17 00:00:00 2001 From: qiaogaojian Date: Sat, 12 Aug 2023 20:48:24 +0800 Subject: [PATCH 3/9] fix: reset current args --- src/transforms/CodeInjector.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transforms/CodeInjector.ts b/src/transforms/CodeInjector.ts index f7e037be..0ded45f3 100644 --- a/src/transforms/CodeInjector.ts +++ b/src/transforms/CodeInjector.ts @@ -151,7 +151,7 @@ export class CodeInjector { currentLanguage = ""; currentCode = ""; insideCodeBlock = false; - + currentArgs = {}; } // reached start of code block From 162f06e0630390b506c3271cd8a2ed28856b5c23 Mon Sep 17 00:00:00 2001 From: qiaogaojian Date: Sun, 13 Aug 2023 18:42:29 +0800 Subject: [PATCH 4/9] config: ignore log files --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index e09a007e..cb6c298a 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ data.json # Exclude macOS Finder (System Explorer) View States .DS_Store +*.log From 9cb62d6dd9e92c4cff1fdbc9a1dd600177ab909b Mon Sep 17 00:00:00 2001 From: Slar <17206261+slar@users.noreply.github.com> Date: Fri, 27 Oct 2023 21:04:06 +1100 Subject: [PATCH 5/9] Added zig support --- README.md | 2 +- src/main.ts | 8 ++++++- src/settings/Settings.ts | 8 +++++++ src/settings/SettingsTab.ts | 4 ++++ src/settings/languageDisplayName.ts | 1 + src/settings/per-lang/makeZigSettings.ts | 27 ++++++++++++++++++++++++ 6 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 src/settings/per-lang/makeZigSettings.ts diff --git a/README.md b/README.md index 5883e081..afcb84f4 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ The result is shown only after the execution is finished. It is not possible to ![Video that shows how the plugin works.](https://github.com/twibiral/obsidian-execute-code/blob/master/images/execute_code_example.gif?raw=true) -The following [languages are supported](#supported-programming-languages-): C, CPP, Dart, Golang, Groovy, Kotlin, Java, JavaScript, TypeScript, Lean, Lua, CSharp, Prolog, Rust, Python, R, Ruby, Wolfram Mathematica, Haskell, Scala, Racket, F#, Batch, Shell & Powershell, Octave, and Maxima. +The following [languages are supported](#supported-programming-languages-): C, CPP, Dart, Golang, Groovy, Kotlin, Java, JavaScript, TypeScript, Lean, Lua, CSharp, Prolog, Rust, Python, R, Ruby, Wolfram Mathematica, Haskell, Scala, Racket, F#, Batch, Shell & Powershell, Octave, Maxima and Zig. Python, Rust, and Octave support embedded plots. All languages support ["magic" commands](#magic-commands-) that help you to access paths in obsidian or show images in your notes. diff --git a/src/main.ts b/src/main.ts index 5276c9f0..99ee7939 100644 --- a/src/main.ts +++ b/src/main.ts @@ -26,7 +26,7 @@ import runAllCodeBlocks from './runAllCodeBlocks'; export const languageAliases = ["javascript", "typescript", "bash", "csharp", "wolfram", "nb", "wl", "hs", "py", "scpt"] as const; export const canonicalLanguages = ["js", "ts", "cs", "lean", "lua", "python", "cpp", "prolog", "shell", "groovy", "r", "go", "rust", "java", "powershell", "kotlin", "mathematica", "haskell", "scala", "racket", "fsharp", "c", "dart", - "ruby", "batch", "sql", "octave", "maxima", "applescript"] as const; + "ruby", "batch", "sql", "octave", "maxima", "applescript", "zig"] as const; export const supportedLanguages = [...languageAliases, ...canonicalLanguages] as const; export type LanguageId = typeof canonicalLanguages[number]; @@ -397,6 +397,12 @@ export default class ExecuteCodePlugin extends Plugin { const transformedCode = await new CodeInjector(this.app, this.settings, language).injectCode(srcCode); this.runCodeInShell(transformedCode, out, button, this.settings.applescriptPath, this.settings.applescriptArgs, this.settings.applescriptFileExtension, language, file); }) + } else if (language === "zig") { + button.addEventListener("click", async () => { + button.className = runButtonDisabledClass; + const transformedCode = await new CodeInjector(this.app, this.settings, language).injectCode(srcCode); + this.runCodeInShell(transformedCode, out, button, this.settings.zigPath, this.settings.zigArgs, "zig", language, file); + }) } } diff --git a/src/settings/Settings.ts b/src/settings/Settings.ts index d49c6442..0489d50c 100644 --- a/src/settings/Settings.ts +++ b/src/settings/Settings.ts @@ -127,6 +127,9 @@ export interface ExecutorSettings { applescriptArgs: string; applescriptFileExtension: string; applescriptInject: string; + zigPath: string; + zigArgs: string; + zigInject: string; jsInteractive: boolean; tsInteractive: boolean; @@ -158,6 +161,7 @@ export interface ExecutorSettings { octaveInteractive: boolean; maximaInteractive: boolean; applescriptInteractive: boolean; + zigInteractive: boolean; } @@ -288,6 +292,9 @@ export const DEFAULT_SETTINGS: ExecutorSettings = { applescriptArgs: "", applescriptFileExtension: "scpt", applescriptInject: "", + zigPath: "zig", + zigArgs: "run", + zigInject: "", jsInteractive: true, tsInteractive: false, csInteractive: false, @@ -318,4 +325,5 @@ export const DEFAULT_SETTINGS: ExecutorSettings = { octaveInteractive: false, maximaInteractive: false, applescriptInteractive: false, + zigInteractive: false, } diff --git a/src/settings/SettingsTab.ts b/src/settings/SettingsTab.ts index 6a9f1870..e5c229fb 100644 --- a/src/settings/SettingsTab.ts +++ b/src/settings/SettingsTab.ts @@ -31,6 +31,7 @@ import makeSQLSettings from "./per-lang/makeSQLSettings"; import makeOctaviaSettings from "./per-lang/makeOctaveSettings"; import makeMaximaSettings from "./per-lang/makeMaximaSettings"; import makeApplescriptSettings from "./per-lang/makeApplescriptSettings"; +import makeZigSettings from "./per-lang/makeZigSettings"; /** @@ -207,6 +208,9 @@ export class SettingsTab extends PluginSettingTab { // ========== Applescript ============ makeApplescriptSettings(this, this.makeContainerFor("applescript")); + // ========== Zig ============ + makeZigSettings(this, this.makeContainerFor("zig")); + this.focusContainer(this.plugin.settings.lastOpenLanguageTab || canonicalLanguages[0]); } diff --git a/src/settings/languageDisplayName.ts b/src/settings/languageDisplayName.ts index 6a7377bf..2a0b6424 100644 --- a/src/settings/languageDisplayName.ts +++ b/src/settings/languageDisplayName.ts @@ -30,4 +30,5 @@ export const DISPLAY_NAMES: Record = { octave: "Octave", maxima: "Maxima", applescript: "Applescript", + zig: "Zig", } as const; diff --git a/src/settings/per-lang/makeZigSettings.ts b/src/settings/per-lang/makeZigSettings.ts new file mode 100644 index 00000000..1eea9351 --- /dev/null +++ b/src/settings/per-lang/makeZigSettings.ts @@ -0,0 +1,27 @@ +import { Setting } from "obsidian"; +import { SettingsTab } from "../SettingsTab"; + +export default (tab: SettingsTab, containerEl: HTMLElement) => { + containerEl.createEl('h3', { text: 'Zig Settings' }); + new Setting(containerEl) + .setName('zig path') + .setDesc("Path to your zig installation") + .addText(text => text + .setValue(tab.plugin.settings.zigPath) + .onChange(async (value) => { + const sanitized = tab.sanitizePath(value); + tab.plugin.settings.zigPath = sanitized; + console.log('zig path set to: ' + sanitized); + await tab.plugin.saveSettings(); + })); + new Setting(containerEl) + .setName('zig arguments') + .addText(text => text + .setValue(tab.plugin.settings.zigArgs) + .onChange(async (value) => { + tab.plugin.settings.zigArgs = value; + console.log('zig args set to: ' + value); + await tab.plugin.saveSettings(); + })); + tab.makeInjectSetting(containerEl, "zig"); +} From b117340dc3550363d4687dabd7fa2d495fd4c1be Mon Sep 17 00:00:00 2001 From: Tim Wibiral Date: Fri, 15 Dec 2023 21:49:41 +0100 Subject: [PATCH 6/9] docs: update CHANGELOG.md --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4effff9d..5ac73f3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +## [Unreleased] +### Added +- Option for better handling of logs (Thanks to @qiaogaojian) + + ## [1.9.0] ### Changed - Fix app://local deprecation (New minimal Obsidian version: v1.2.8) (Thanks to @mayurankv) From adf40f32bd50b50aa7f78b10564869e71209c8f2 Mon Sep 17 00:00:00 2001 From: Tim Wibiral Date: Fri, 15 Dec 2023 22:05:56 +0100 Subject: [PATCH 7/9] docs: update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ac73f3f..312b739f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] +### Changed +- Fix bug produced by duplicate labeled code blocks (Thanks to @qiaogaojian) + ### Added - Option for better handling of logs (Thanks to @qiaogaojian) From f79130ac61f2d06908d13119f7f60c0c91b1653e Mon Sep 17 00:00:00 2001 From: Tim Wibiral Date: Fri, 15 Dec 2023 22:15:38 +0100 Subject: [PATCH 8/9] docs: update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 312b739f..b124ca20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Added - Option for better handling of logs (Thanks to @qiaogaojian) +- Make labels work with code blocks with `run-` prefix (Thanks to @qiaogaojian) ## [1.9.0] From 9c70dbab561d5da2c4a4ac9c3a8eacee85d94c35 Mon Sep 17 00:00:00 2001 From: Tim Wibiral Date: Fri, 15 Dec 2023 22:30:19 +0100 Subject: [PATCH 9/9] New version 1.9.1 - Fix bug produced by duplicate labeled code blocks (Thanks to @qiaogaojian) - Option for better handling of logs (Thanks to @qiaogaojian) - Make labels work with code blocks with `run-` prefix (Thanks to @qiaogaojian) --- CHANGELOG.md | 2 +- manifest.json | 2 +- package-lock.json | 2 +- package.json | 2 +- versions.json | 3 ++- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b124ca20..1850fb3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), -## [Unreleased] +## [1.9.1] ### Changed - Fix bug produced by duplicate labeled code blocks (Thanks to @qiaogaojian) diff --git a/manifest.json b/manifest.json index da2c14f7..c5b73e5e 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "execute-code", "name": "Execute Code", - "version": "1.9.0", + "version": "1.9.1", "minAppVersion": "1.2.8", "description": "Allows to execute code snippets within a note. Supported programming languages: C, CPP, Dart, Golang, Groovy, Kotlin, Java, JavaScript, TypeScript, Lean, Lua, CSharp, Prolog, Rust, Python, R, Ruby, Wolfram Mathematica, Haskell, Scala, Racket, F#, Batch, Shell & Powershell.", "author": "twibiral", diff --git a/package-lock.json b/package-lock.json index bdc1b486..8c9b28ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "execute-code", - "version": "1.9.0", + "version": "1.9.1", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/package.json b/package.json index 2f53314c..c69d5327 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "execute-code", - "version": "1.9.0", + "version": "1.9.1", "description": "This is a sample plugin for Obsidian (https://obsidian.md)", "main": "src/main.js", "scripts": { diff --git a/versions.json b/versions.json index 6d5d9924..35150049 100644 --- a/versions.json +++ b/versions.json @@ -42,5 +42,6 @@ "1.7.1": "0.12.0", "1.8.0": "0.12.0", "1.8.1": "0.12.0", - "1.9.0": "1.2.8" + "1.9.0": "1.2.8", + "1.9.1": "1.2.8" } \ No newline at end of file