Skip to content

Commit

Permalink
Merge branch 'develop' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
twibiral authored Dec 15, 2023
2 parents c10d5f7 + 69249d7 commit c2d1c4c
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ data.json

# Exclude macOS Finder (System Explorer) View States
.DS_Store
*.log
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ 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/),


## [1.9.1]
### Changed
- Fix bug produced by duplicate labeled code blocks (Thanks to @qiaogaojian)

### 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]
### Changed
- Fix app://local deprecation (New minimal Obsidian version: v1.2.8) (Thanks to @mayurankv)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
8 changes: 7 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -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);
})
}

}
Expand Down
10 changes: 10 additions & 0 deletions src/settings/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ExecutorSettings {
allowInput: boolean;
wslMode: boolean;
shellWSLMode: boolean;
onlyCurrentBlock: boolean;
nodePath: string;
nodeArgs: string;
jsFileExtension: string;
Expand Down Expand Up @@ -128,6 +129,9 @@ export interface ExecutorSettings {
applescriptArgs: string;
applescriptFileExtension: string;
applescriptInject: string;
zigPath: string;
zigArgs: string;
zigInject: string;

jsInteractive: boolean;
tsInteractive: boolean;
Expand Down Expand Up @@ -159,6 +163,7 @@ export interface ExecutorSettings {
octaveInteractive: boolean;
maximaInteractive: boolean;
applescriptInteractive: boolean;
zigInteractive: boolean;
}


Expand All @@ -172,6 +177,7 @@ export const DEFAULT_SETTINGS: ExecutorSettings = {
allowInput: true,
wslMode: false,
shellWSLMode: false,
onlyCurrentBlock: false,
nodePath: "node",
nodeArgs: "",
jsFileExtension: "js",
Expand Down Expand Up @@ -290,6 +296,9 @@ export const DEFAULT_SETTINGS: ExecutorSettings = {
applescriptArgs: "",
applescriptFileExtension: "scpt",
applescriptInject: "",
zigPath: "zig",
zigArgs: "run",
zigInject: "",
jsInteractive: true,
tsInteractive: false,
csInteractive: false,
Expand Down Expand Up @@ -320,4 +329,5 @@ export const DEFAULT_SETTINGS: ExecutorSettings = {
octaveInteractive: false,
maximaInteractive: false,
applescriptInteractive: false,
zigInteractive: false,
}
15 changes: 15 additions & 0 deletions src/settings/SettingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";


/**
Expand Down Expand Up @@ -100,6 +101,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");
Expand Down Expand Up @@ -207,6 +219,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]);
}

Expand Down
1 change: 1 addition & 0 deletions src/settings/languageDisplayName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ export const DISPLAY_NAMES: Record<LanguageId, string> = {
octave: "Octave",
maxima: "Maxima",
applescript: "Applescript",
zig: "Zig",
} as const;
27 changes: 27 additions & 0 deletions src/settings/per-lang/makeZigSettings.ts
Original file line number Diff line number Diff line change
@@ -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");
}
17 changes: 13 additions & 4 deletions src/transforms/CodeInjector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -145,13 +145,13 @@ 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;

currentArgs = {};
}

// reached start of code block
Expand All @@ -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, ' ');
}
}
16 changes: 15 additions & 1 deletion src/transforms/TransformCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,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;
}
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit c2d1c4c

Please sign in to comment.