Skip to content

Instantly share code, notes, and snippets.

@znz
Last active September 19, 2018 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save znz/185e162ee3ddc950daf014f981fd1fa3 to your computer and use it in GitHub Desktop.
Save znz/185e162ee3ddc950daf014f981fd1fa3 to your computer and use it in GitHub Desktop.
jekyll plugin to eval trusted code
require 'jekyll'
require 'open3'
module Jekyll
class EvalCode < Jekyll::Generator
def generate(site)
@site = site
site.pages.each { |page| eval_code(page) if page.html? }
site.posts.docs.each { |page| eval_code(page) }
end
private
def eval_code(page)
page.content = page.content.gsub(/^(```+)\{(ruby|javascript|js)\}$(.*?)^\1$/m) do
quote, lang, src = $1, $2, $3
case lang
when 'ruby'
output, status = Open3.capture2e('ruby', stdin_data: src)
when 'javascript', 'js'
output, status = Open3.capture2e('node', stdin_data: src)
end
if status.success?
"#{quote}#{lang}#{src}#{quote}\n`````\n#{output.chomp}\n`````\n"
end
end
end
end
end
def print_hi(name)
  puts "Hi, #{name}"
end
print_hi('Tom')
#=> prints 'Hi, Tom' to STDOUT.
puts "hello, world"
console.log('hello')
console.log('hello')

使い方

  • _plugins/eval_code.rbeval_code.rb をおく
  • example.md のような感じで code block をかく
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment