Created
February 10, 2012 09:41
-
-
Save aoi0308/1788103 to your computer and use it in GitHub Desktop.
エクセルで小テストを作成、CSV形式で保存、GIFTフォーマットに変換 を目的としたスクリプト
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
# | |
# 番号,問題名,問題文,選択肢(選択肢は項目ごとに改行) | |
# とりあえず多岐選択、記述形式、組み合わせに対応 | |
# Cloze形式はログに出力してスキップ(後で手入力する) | |
# 単一選択は ~foo(1個以上) =bar(1個) | |
# 複数選択は ~%10%foo で、正の数の合計100になるように | |
# 記述形式は =foo を1個以上 | |
# 組み合わせは =foo -> bar を1個以上 | |
# | |
require "csv" | |
# HTMLの実体参照と改行のマッピング | |
REPLACE_MAP = {"<" => "<", ">" => ">", "&" => "&", "\n" => "<br />"} | |
# GIFT対応とHTML対応のエスケープ | |
# 組み合わせの -> を避けるためやや複雑 | |
def gift_escape(str) | |
str.gsub(/(?!^)([~=])|([:{}])/, "\\\\\\0").gsub(/(?! -)>(?! )|<|&|\n/) {|w| REPLACE_MAP[w]} | |
end | |
File::open("cloze.log", "w") do |f| | |
CSV.foreach(ARGV[0]) do |line| | |
# Header or Question name is empty | |
next if line[0] == "番号" or line[1] == nil | |
#Cloze | |
if line[3] == nil | |
f.write "#{line[0]}-#{line[1]}\n" | |
next | |
end | |
puts "::#{line[0]}-#{line[1]}::[html]<p>#{gift_escape(line[2])}</p>{" | |
if line[3].index(/^\~/) then | |
# one selection | multi selection | |
line[3].scan(/[~=].+?(?=\n~|\n=|\z)/m) {|a| puts gift_escape(a).split(/^(\~%-?\d+\.?\d+%|\~|=)/).insert(2, "<p>").push("</p>").join} | |
else | |
line[3].scan(/=.+?(?=\n=|\z)/m) do |a| | |
as = gift_escape(a).split(/(^=| -> )/) | |
# combination | |
as = as.insert(2, "<p>").insert(4, "</p>") if as.size == 5 and as[2] != "" | |
puts as.join | |
end | |
end | |
puts "}" | |
puts | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment