マークダウンはコードを手軽に表現できる書類です。それからコードだけを出力するコマンドを用意するとドキュメントからコードを生成する事が可能です。従来はコードのコメントでドキュメントを生成しますが、その逆を実現してみようというお話です。
コマンドをmd2codeと名付けてRubyで実装してみました。ソースを以下に示します。とても簡単な内容なのでRuby以外の使用環境に適した言語で実装できるかと思います。
#!/usr/bin/env ruby
| <!DOCTYPE html> | |
| <html lang="ja"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| </head> | |
| <body> | |
| <script type="text/javascript" src="https://unpkg.com/hyperapp"></script> | |
| <script> |
| process.stdin.setEncoding('utf8') | |
| process.stdout.setEncoding('utf8') | |
| process.stderr.setEncoding('utf8') | |
| EVENT = { | |
| DEFAULT : Symbol('default'), | |
| ALL : Symbol('all'), | |
| } | |
| EventTable = [] |
| class StdioEmitter { | |
| constructor(encoding='utf8') { | |
| this.EVENT = { | |
| DEFAULT : Symbol(), | |
| ALL : Symbol(), | |
| } | |
| this.encoding = encoding | |
| this.emitter = new (require('events').EventEmitter)() | |
| this._event_all_listener = undefined | |
| this._regexp_listener_dictionary = {} |
| const STDIN = { | |
| DEFAULT : Symbol(), | |
| ALL : Symbol(), | |
| BEFORE_ALL : Symbol(), | |
| AFTER_ALL : Symbol(), | |
| } | |
| class ProcessStdio { | |
| constructor(encoding='utf8') { | |
| this.encoding = encoding |
| <script>if (typeof module === 'object') {window.module = module; module = undefined;}</script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> | |
| <script>if (window.module) module = window.module;</script> | |
| <script src="http://cdn.opalrb.org/opal/current/opal.js"></script> | |
| <script src="http://cdn.opalrb.org/opal/current/opal-parser.js"></script> | |
| <script src="http://cdn.opalrb.org/opal/0.10.1/external/opal-jquery-0.4.2.js"></script> | |
| <script type="text/javascript">Opal.load('opal-parser')</script> | |
| <script type="text/javascript"> | |
| function electron_spawn(cmd,arg) { |
| <script type="text/javascript"> | |
| function electron_spawn(cmd,arg) { | |
| let p = require('child_process').spawn(cmd,arg) | |
| p.stdout.ondata=(action,listener)=>{ | |
| p.stdout.on('data',(data)=>{ | |
| let d = JSON.parse(data.toString()) | |
| if (d.action == action) listener(d) | |
| }) | |
| } | |
| p.on('exit',(code,signal)=>{ |
| <script type="text/javascript"> | |
| function electron_child_process_spawn(cmd = '', arg = []) { | |
| let proc = require('child_process').spawn(cmd, arg, {detached:true}) | |
| proc.unref() | |
| proc.stderr.on('data', (data) => { | |
| proc.kill() | |
| window.alert(`electron_child_process_spawn error:\n\n ${data}`) | |
| window.close() | |
| }) | |
| window.addEventListener('beforeunload', () => { proc.kill() }) |
| <?xml version="1.0" encoding="UTF-8"?> | |
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
| <plist version="1.0"> | |
| <dict> | |
| <key>CFBundleAllowMixedLocalizations</key> | |
| <true/> | |
| <key>CFBundleDevelopmentRegion</key> | |
| <string>English</string> | |
| <key>CFBundleDocumentTypes</key> | |
| <array> |
| app = Application.currentApplication() | |
| app.includeStandardAdditions = true | |
| se = Application('System Events') | |
| function sh(script, opt={}) { | |
| return app.doShellScript(script, { | |
| administratorPrivileges: !!opt.withPrompt, | |
| withPrompt: opt.withPrompt || '', | |
| alteringLineEndings: opt.alteringLineEndings || false | |
| }).replace(/\n$/,'') |