-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from geezee/plugins
Basic plugin management tool
- Loading branch information
Showing
18 changed files
with
440 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\File; | ||
|
||
class NoteFormatter | ||
{ | ||
private $state; | ||
private $repo; | ||
|
||
public function __construct() { | ||
$this->state = $this->getState(); | ||
$this->repo = $this->getRepositoryMap(); | ||
} | ||
|
||
const DIR = "./resources/note-format/"; | ||
const STATE = "./resources/note-format/state.json"; | ||
const REPOSITORY = "./resources/note-format/repository.json"; | ||
const OUT = "./resources/note-format/note-format.out.js"; | ||
const COMMON_JS = "./resources/note-format/src/common.js"; | ||
|
||
|
||
public function getRepository() { | ||
return json_decode(File::get(NoteFormatter::REPOSITORY)); | ||
} | ||
|
||
|
||
public function getRepositoryMap() { | ||
return collect(NoteFormatter::getRepository())->reduce(function($map, $plugin) { | ||
$map[$plugin->name] = $plugin; | ||
return $map; | ||
}); | ||
} | ||
|
||
public function getState() { | ||
return json_decode(File::get(NoteFormatter::STATE)); | ||
} | ||
|
||
public function writeToOutput() { | ||
$out = File::get(NoteFormatter::COMMON_JS)."\nconst formatters=["; | ||
foreach ($this->state->installed as $installed) { | ||
$out .= sprintf("%s,", File::get($this::DIR.$this->repo[$installed]->src)); | ||
} | ||
$out .= "]"; | ||
|
||
return File::put($this::OUT, $out); | ||
} | ||
|
||
|
||
public function list($all) { | ||
if ($all) { | ||
printf("%d available formatters\n\n", count(array_keys($this->repo))); | ||
foreach ($this->repo as $plugin) { | ||
printf("%s%-20s - %s\n", | ||
in_array($plugin->name, $this->state->installed) ? "* " : " ", | ||
$plugin->name, $plugin->description); | ||
} | ||
} else { | ||
$index = 1; | ||
foreach ($this->state->installed as $name) { | ||
printf("%3d. %-20s - %s\n", $index, $name, $this->repo[$name]->description); | ||
$index++; | ||
} | ||
} | ||
} | ||
|
||
|
||
public function install($after, $requested) { | ||
if (strlen($after) == 0) { | ||
$index = 0; | ||
} else { | ||
$index = intval($after); | ||
if ($index === false || $index > count($this->state->installed)) { | ||
printf("[ERROR] Index $after does not exist\n"); | ||
$this->list(false); | ||
return -1; | ||
} | ||
} | ||
|
||
foreach ($requested as $plugin) { | ||
if (!isset($this->repo[$plugin])) { | ||
printf("[ERROR] Formatter $plugin does not exist\n"); | ||
return -1; | ||
} | ||
} | ||
|
||
array_splice($this->state->installed, $index, 0, $requested); | ||
|
||
$this->writeToOutput(); | ||
|
||
File::put($this::STATE, json_encode($this->state)); | ||
} | ||
|
||
|
||
public function remove($index) { | ||
$index = intval($index); | ||
if ($index === false || $index <= 0 || $index > count($this->state->installed)) { | ||
printf("[ERROR] Index does not exist\n"); | ||
return; | ||
} | ||
|
||
$this->state->installed = array_values(collect($this->state->installed)->forget($index-1)->toArray()); | ||
|
||
$this->writeToOutput(); | ||
|
||
File::put($this::STATE, json_encode($this->state)); | ||
} | ||
|
||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,66 @@ | ||
function getConfig(key, def) { | ||
if (typeof window.noteFormatConfig === 'undefined') { | ||
window.noteFormatConfig = {}; | ||
return def; | ||
} | ||
const val = window.noteFormatConfig[key]; | ||
return val === undefined ? def : val; | ||
} | ||
|
||
function setConfig(key, val) { | ||
if (typeof window.noteFormatConfig === 'undefined') { | ||
window.noteFormatConfig = {}; | ||
} | ||
window.noteFormatConfig[key] = val; | ||
} | ||
|
||
function loadScript(name, src) { | ||
console.log('NoteFormat requesting', name, src); | ||
|
||
const isLoaded = getConfig(name+'.loaded', false); | ||
|
||
return new Promise((resolve, reject) => { | ||
if (isLoaded) { | ||
console.log('NoteFormat script already loaded', name); | ||
if (typeof resolve === 'function') { | ||
resolve(); | ||
return; | ||
} | ||
} | ||
|
||
var script = document.createElement('script'); | ||
script.type = 'text/javascript'; | ||
script.src = src; | ||
|
||
script.onload = _ => { | ||
console.log('NoteFormat loaded', name); | ||
window.SHOWDOWN_LOADED = true; | ||
setConfig(name+'.loaded', true); | ||
if (typeof resolve === 'function') { | ||
resolve(); | ||
} | ||
} | ||
|
||
document.body.appendChild(script); | ||
}); | ||
} | ||
|
||
|
||
|
||
const formatters=[function (body) { | ||
return loadScript('showdown', 'js/showdown.min.js') | ||
.then(() => { | ||
return new showdown.Converter().makeHtml(body); | ||
}); | ||
} | ||
,function (body) { | ||
var sandbox = document.createElement('div'); | ||
sandbox.innerHTML = body; | ||
|
||
Array.from(sandbox.querySelectorAll('iframe')).forEach(iframe => { | ||
iframe.sandbox = 'allow-scripts allow-same-origin allow-forms'; | ||
}); | ||
|
||
return Promise.resolve(sandbox.innerHTML); | ||
} | ||
,] |
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,27 @@ | ||
{ | ||
"markdown": { | ||
"name": "markdown", | ||
"description": "Renders the note in markdown", | ||
"src": "src\/markdown.js" | ||
}, | ||
"iframe": { | ||
"name": "iframe", | ||
"description": "Play around with iframe attributes or block them", | ||
"src": "src\/iframe.js" | ||
}, | ||
"mathjax": { | ||
"name": "mathjax", | ||
"description": "Render LaTeX equations inside the note", | ||
"src": "src\/mathjax.js" | ||
}, | ||
"asciinema": { | ||
"name": "asciinema", | ||
"description": "Use $asciinema(attachment_url) to include an asciinema cast", | ||
"src": "src\/asciinema.js" | ||
}, | ||
"js-eval": { | ||
"name": "js-eval", | ||
"description": "Evaluate javascript code and display output in an <iframe>", | ||
"src": "src\/js-eval.js" | ||
} | ||
} |
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,10 @@ | ||
function (body, note) { | ||
return loadScript('asciinema', 'js/asciinema-player.js') | ||
.then(() => { | ||
return body.replace(/\$asciinema\([^\)\(]+\)/g, match => { | ||
var filename = match.substring(11).slice(0, -1); | ||
var path = ['./attachments', note.id, filename].join('/'); | ||
return `<asciinema-player src="${path}"></asciinema-player>`; | ||
}) | ||
}); | ||
} |
Oops, something went wrong.