#!/usr/bin/env ruby
# coding: utf-8
Encoding.default_external = 'utf-8'

require'cgi'
require'open3'
require'timeout'

c = CGI.new(:accept_charset => "UTF-8")

#questionは問題文
question = "ここが問題文"

#codeは問題の出力結果例
code = "i = 1
        while i <= 10
        puts i
        i += 1
        end
"

#methodはメソッドと想定の説明の変数
method = "ここがメソッドの説明の変数"

cmd = "ruby -Ku -e '#{code}'"
o,e,s = Open3.capture3(cmd)

print <<EOF
Content-type: text/html; charset=utf-8

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

  </head>
  <body>
  
    <form method="POST" action="./textcode.rb">
    <div class="gakuseki">
    <h3>学籍番号</h3>
    <input type="text" name="name"></input>
    </div>
    
EOF

puts("<div class=\"mondai\">")
printf("<h1 class=\"sen1\">%s</h1>\n", question)
puts("</div>")
              
print <<EOF
     <div class="editor">
     <h2><span class="waku1">プログラム入力画面</span></h2>
      <textarea name="example" placeholder="プログラムのコードを入力"></textarea>
      </div>

EOF

puts("<div class=\"ex\">")
puts("<h2><span class=\"waku1\">出力結果例</span></h2>")
printf("<textarea id=\"black\" name=\"ans\">%s</textarea>\n", o)
puts("</div>")

puts("<div class=\"read\">")
puts("<h2 class=\"sen1\">メソッド・想定の説明</h2>")
printf("<p>%s</p>", method)
puts("</div>")

print <<EOF
      <div class="send">
      <input type="submit" value="Let Go">
      <input type="reset" value="reset">
      </div>
EOF

puts("</form>")
puts("</body>\n</html>")
