Created
October 28, 2012 14:24
-
-
Save zonuexe/3968734 to your computer and use it in GitHub Desktop.
もっと強いDSL
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
# -*- Coding: utf-8 -*- | |
module ScenarioDSL | |
Do = ->(mod, scenario){ | |
Class.new{ | |
@mod = mod | |
def self.put sce | |
puts sce.call(@mod) | |
end | |
}.put scenario | |
} | |
module Text | |
Name = "ScenarioDSL::Text" | |
Scripter = ->(*args){ args.join("\n") } | |
Title = ->(text){"『#{text}』\n"} | |
Paragraph = ->(*args){args.join("\n")+"\n"} | |
Chara = ->(name){ | |
->(text){"#{name}「#{text}」"} | |
} | |
end | |
module HTML | |
Name = "ScenarioDSL::HTML" | |
HTML_Template = <<'ENDHTML' | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title><%= title %></title> | |
<style> | |
h1 { | |
quotes: "『" "』"; | |
} | |
</style> | |
</head> | |
<body> | |
<%= body %> | |
</body> | |
</html> | |
ENDHTML | |
Scripter = ->(*args){ | |
Apply[ HTML_Template, args.join("\n") ] | |
} | |
Title = ->(text){"<h1>#{text}</h1>\n"} | |
Paragraph = ->(*args){ "<p>#{args.join("<br>\n")}</p>\n"} | |
Chara = ->(name){ | |
->(text){"#{name}「#{text}」"} | |
} | |
Tag = ->(str){ /<%= #{str} %>/ } | |
Apply = ->(template, text){ | |
title = text.scan(/<h1>(.*)<\/h1>/)[0][0] | |
body = text.dup | |
template.sub!(Tag['title'], title) unless title.nil? | |
template.sub!(Tag['body'], body) | |
} | |
end | |
end | |
scenario = ->(mod){ | |
include mod | |
ryu = Chara[:リュウ] | |
tak = Chara[:たかし] | |
Scripter[ | |
Title[:俺より強い奴に、会いに行く], | |
Paragraph[ | |
:ピンポーン, | |
ryu[:こんにちは], | |
tak[:はいどなた] | |
],Paragraph[ | |
ryu[:いまちょっといいですか], | |
tak[:これから出かけます] | |
],Paragraph[ | |
ryu[:午後出勤ですか?], | |
tak[:はい] | |
],Paragraph[ | |
ryu[:強そうですね], | |
tak[:なにがですか] | |
],Paragraph[ | |
ryu[:態度が] | |
],Paragraph[ | |
:リュウは、自分より強そうな奴に、会いにいったのだった。 完 | |
] | |
] | |
} | |
ScenarioDSL::Do[ScenarioDSL::HTML, scenario] | |
puts '----------' | |
ScenarioDSL::Do[ScenarioDSL::Text, scenario] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment