Newer
Older
2019-madoka / root / textcode3.rb
@mamadoka mamadoka on 7 Nov 2019 1 KB db機能追加
#!/usr/bin/env ruby
# coding: utf-8

require'cgi'
require'open3'
require'sqlite3'

SQLite3::Database.new("ranking.db") do |db|
  db.results_as_hash = true
  
c = CGI.new(:accept_charset => "UTF-8")

print"Content-type: text/html; charset=utf-8\n\n"

coderead = c["example"]
namae = c["name"]

cmd = "ruby -e '#{coderead}'"
o,e,s = Open3.capture3(cmd) #o,e,s = 実行結果、エラー文、プロセス?

print <<EOF
<DOCTYPE html>
  <head> 
    <meta charset=utf-8>
    <title>テキストエディタ読み込み</title>
    <link rel="stylesheet" type="text/css" href="root/editor.css">
    <style>
<!--
-->
    </style>
  </head>
  <body>
EOF


puts("<h1>入力したコード</h1>")
printf("<textarea name=\"example\" readonly=\"readonly\">%s</textarea>", coderead)


puts("<h1>実行結果</h1>")
puts("<div>")
printf("<pre>%s<pre>", o) #evalでRubyコードとして読み込む
puts("</div>")


if e == ""  
  puts("<h2>エラー文はありません!!おめでとう!!</h2>")
  db.execute("insert into ranking3(id, name) values (?, ?)", "suc", namae)
else
    puts("<h2>エラー文</h2>")
    printf("<p>%s</p>\n", e) 
end

puts("<a href=\"./editor3.rb\">エディタ画面へ戻る</a>")
puts("<a href=\"../start.html\">問題選択へ戻る</a>")
puts("</body>\n</html>")
end