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

Add Ruby language support #193

Merged
merged 1 commit into from
Dec 26, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ 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
- Support for Ruby (Thanks to @santry)

## [1.5.0]
### Added
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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, Golang, Groovy, Kotlin, Java, JavaScript, TypeScript, Lua, CSharp, Prolog, Rust, Python, R, Wolfram Mathematica, Haskell, Scala, Shell & Powershell.
The following [languages are supported](#supported-programming-languages): C, CPP, Golang, Groovy, Kotlin, Java, JavaScript, TypeScript, Lua, CSharp, Prolog, Rust, Python, R, Ruby, Wolfram Mathematica, Haskell, Scala, Shell & Powershell.


Python and Rust 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 Expand Up @@ -328,6 +328,16 @@ println("Hello, World!")
```
</details>

<details>
<summary>Ruby</summary>

- Requirements: Ruby is installed and the correct path is set in the settings.

```ruby
puts "Hello, World!"
```
</details>

Squiggle: For Squiggle support take a look at the [Obsidian Squiggle plugin](https://github.com/jqhoogland/obsidian-squiggle) by @jqhoogland.

Support for the following is planned:
Expand Down
8 changes: 7 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import runAllCodeBlocks from './runAllCodeBlocks';

export const languageAliases = ["javascript", "typescript", "bash", "csharp", "wolfram", "nb", "wl", "hs", "py"] as const;
export const canonicalLanguages = ["js", "ts", "cs", "lua", "python", "cpp",
"prolog", "shell", "groovy", "r", "go", "rust", "java", "powershell", "kotlin", "mathematica", "haskell", "scala", "c"] as const;
"prolog", "shell", "groovy", "r", "go", "rust", "java", "powershell", "kotlin", "mathematica", "haskell", "scala", "c", "ruby"] as const;
export const supportedLanguages = [...languageAliases, ...canonicalLanguages] as const;


Expand Down Expand Up @@ -336,6 +336,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.clingPath, this.settings.clingArgs, "c", language, file);
})
} else if(language ==="ruby") {
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.rubyPath, this.settings.rubyArgs, "rb", language, file);
})
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/settings/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ export interface ExecutorSettings {
cArgs: string;
cUseMain: boolean;
cInject: string;
rubyPath: string;
rubyArgs: string;
rubyInject: string;

jsInteractive: boolean;
tsInteractive: boolean;
Expand All @@ -103,6 +106,7 @@ export interface ExecutorSettings {
haskellInteractive: boolean;
scalaInteractive: boolean;
cInteractive: boolean;
rubyInteractive: boolean;
}


Expand Down Expand Up @@ -188,6 +192,9 @@ export const DEFAULT_SETTINGS: ExecutorSettings = {
cArgs: "",
cUseMain: true,
cInject: "",
rubyPath: "ruby",
rubyArgs: "",
rubyInject: "",

jsInteractive: true,
tsInteractive: false,
Expand All @@ -208,5 +215,6 @@ export const DEFAULT_SETTINGS: ExecutorSettings = {
mathematicaInteractive: false,
haskellInteractive: false,
scalaInteractive: false,
cInteractive: false
cInteractive: false,
rubyInteractive: false
}
6 changes: 5 additions & 1 deletion src/settings/SettingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import makePowershellSettings from "./per-lang/makePowershellSettings";
import makePrologSettings from "./per-lang/makePrologSettings";
import makePythonSettings from "./per-lang/makePythonSettings";
import makeRSettings from "./per-lang/makeRSettings";
import makeRubySettings from "./per-lang/makeRubySettings";
import makeRustSettings from "./per-lang/makeRustSettings";
import makeScalaSettings from "./per-lang/makeScalaSettings.js";
import makeShellSettings from "./per-lang/makeShellSettings";
Expand Down Expand Up @@ -178,7 +179,10 @@ export class SettingsTab extends PluginSettingTab {

// ========== Scala ===========
makeScalaSettings(this, this.makeContainerFor("scala"));


// ========== Ruby ============
makeRubySettings(this, this.makeContainerFor("ruby"));

this.focusContainer(this.plugin.settings.lastOpenLanguageTab || canonicalLanguages[0]);
}

Expand Down
5 changes: 3 additions & 2 deletions src/settings/languageDisplayName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ export const DISPLAY_NAMES: Record<LanguageId, string> = {
shell: "Shell",
ts: "Typescript",
scala: "Scala",
c: "C"
} as const;
c: "C",
ruby: "Ruby"
} as const;
27 changes: 27 additions & 0 deletions src/settings/per-lang/makeRubySettings.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: 'Ruby Settings' });
new Setting(containerEl)
.setName('ruby path')
.setDesc("Path to your ruby installation")
.addText(text => text
.setValue(tab.plugin.settings.rubyPath)
.onChange(async (value) => {
const sanitized = tab.sanitizePath(value);
tab.plugin.settings.rubyPath = sanitized;
console.log('ruby path set to: ' + sanitized);
await tab.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('ruby arguments')
.addText(text => text
.setValue(tab.plugin.settings.rubyArgs)
.onChange(async (value) => {
tab.plugin.settings.rubyArgs = value;
console.log('ruby args set to: ' + value);
await tab.plugin.saveSettings();
}));
tab.makeInjectSetting(containerEl, "ruby");
}