Skip to content

Instantly share code, notes, and snippets.

@kozakana
Last active January 2, 2017 10:34
Show Gist options
  • Save kozakana/efeebe5391569d3ff269bdbfb84a7c16 to your computer and use it in GitHub Desktop.
Save kozakana/efeebe5391569d3ff269bdbfb84a7c16 to your computer and use it in GitHub Desktop.
マークダウンを将棋用に拡張したフォーマットのngx_mruby版
</body>
</article>
</html>
var CANVAS_SIZE = 640;
var CELL_SIZE = 50;
var TOP = 110;
var LEFT = CANVAS_SIZE - CELL_SIZE * 11.5;
var NUM = ["一","二","三","四","五","六","七","八","九"];
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = CANVAS_SIZE;
canvas.height = CANVAS_SIZE;
ctx.fillStyle = "#fff";
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 将棋盤を作る
for (var i = 0; i < 81; ++i) {
ctx.strokeRect(LEFT + CELL_SIZE * (i % 9) , TOP + CELL_SIZE * ((i / 9) | 0), CELL_SIZE, CELL_SIZE);
}
// 配置用関数
function put(param) {
var str = param.str;
var x = 9 - (param.x || 0);
var y = (param.y || 0) - 1;
var isEnemy = !!param.isEnemy;
put.canvas.width = CELL_SIZE;
put.canvas.height = CELL_SIZE;
put.ctx.textAlign = "center";
put.ctx.textBaseline = "middle";
put.ctx.font = "40px 游明朝, YuMincho, ヒラギノ明朝 ProN W3, Hiragino Mincho ProN, HG明朝E, MS P明朝, MS 明朝, serif";
if (isEnemy) {
put.ctx.save();
put.ctx.translate(CELL_SIZE / 2, CELL_SIZE / 2);
put.ctx.rotate(Math.PI);
put.ctx.translate(-CELL_SIZE / 2, -CELL_SIZE / 2);
put.ctx.fillText(param.str, CELL_SIZE / 2, CELL_SIZE / 2);
ctx.restore();
} else {
put.ctx.fillText(param.str, CELL_SIZE / 2, CELL_SIZE / 2);
}
ctx.drawImage(put.canvas, LEFT + x * CELL_SIZE, TOP + y * CELL_SIZE);
}
put.canvas = document.createElement("canvas");
put.ctx = put.canvas.getContext("2d");
// 数字を振る
for (var i = 0; i < 9; ++i) {
put({
str: i + 1,
x: i + 1,
y: 0
});
put({
str: NUM[i],
x: 0,
y: i + 1
});
}
var piece_list =
;
for(var i = 0; i < piece_list.length; ++i){
put(piece_list[i]);
}
// 画像化
img.src = canvas.toDataURL();
</script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">
<style type="text/css">* {
margin: 0;
padding: 0;
}
body {
background: #fff;
//background: #ececec;
}
.smd-body {
padding: 15px;
}
.smd-body h1 {
padding-bottom: 0.3em;
font-size: 2em;
border-bottom: 1px solid #eee;
}
.smd-body h2 {
padding-bottom: 0.3em;
font-size: 1.5em;
border-bottom: 1px solid #eee;
}
.smd-body h3 {
margin-top: 24px;
font-size: 1.25em;
}
.smd-body h1, .smd-body h2, .smd-body h3, .smd-body h4, .smd-body h5, .smd-body h6 {
margin-bottom: 16px;
font-weight: 600;
line-height: 1.25;
}
.smd-board {
display: block;
//position: absolute;
top: 0; bottom: 0;
left: 0; right: 0;
margin: auto;
width: 320px; height: 320px;
}
blockquote {
margin-top: 1em;
margin-bottom: 1em;
margin-left: .8em;
padding: .8em;
border-left: solid 4px #ddd;
color: #555;
font-size: 1em;
}
ul, menu, dir {
display: block;
list-style-type: disc;
-webkit-margin-before: 1em;
-webkit-margin-after: 1em;
-webkit-margin-start: 0px;
-webkit-margin-end: 0px;
-webkit-padding-start: 40px;
}
ol {
display: block;
list-style-type: decimal;
-webkit-margin-before: 1em;
-webkit-margin-after: 1em;
-webkit-margin-start: 0px;
-webkit-margin-end: 0px;
-webkit-padding-start: 40px;
}
</style>
</head>
<body>
<article class="smd-body" itemprop="text">
FILE_ROOT = "/usr/local/nginx/smd/"
class ParseSmd
BODY_IMAGE01 = File.join(FILE_ROOT, "body_image01.tmpl")
BODY_IMAGE02 = File.join(FILE_ROOT, "body_image02.tmpl")
PCE_COLLECTION = {
'FU' => '歩', 'KY' => '香', 'KE' => '桂', 'GI' => '銀',
'KI' => '金', 'KA' => '角', 'HI' => '飛', 'OU' => '王',
'TO' => 'と', 'NY' => '杏', 'NK' => '圭', 'NG' => '全',
'UM' => '馬', 'RY' => '竜'
}
@@num = 0
def initialize
@@num += 1
@title = nil
@body = ""
@board = {first: "☗", second: "☖", pieces: []}
@blockquote = false
@ul = false
@ol = false
end
def parse str
str1 = str.gsub(/\(-\)/, '☖')
line = str1.gsub(/\(\+\)/, '☗')
if line =~ /\A#\s(.*)/
@title ||= "<title>#{$1}</title>"
@body += '<h1 class="smd-body">'+ $1 +'</h1>'
elsif line =~ /\A##\s(.*)/
@body += '<h2 class="smd-body">' + $1 + '</h2>'
elsif line =~ /\A###\s(.*)/
@body += "<h3>#{$1}</h3>"
elsif line =~ /\A--\s(.*)/
@board[:second] += $1
elsif line =~ /\A\+\+\s(.*)/
@board[:first] += $1
@body += "\n"
@body += <<-"EOS"
<img id="img#{@@num}" class="smd-board" src="" />
<script type="text/javascript">
var img = document.getElementById("img#{@@num}");
EOS
@body += file_read(BODY_IMAGE01)
@body += board
@body += file_read(BODY_IMAGE02)
elsif line =~ /\A\P\d(.*)\Z/
@board[:pieces] << line_to_pieces(line)
elsif line =~ /\A>\s(.*)/
@body += @blockquote ? "" : "<blockquote>"
@body += "<p>#{$1}</p>"
@blockquote = true
elsif line =~ /\A-\s(.*)/
@body += @ul ? "" : "<ul>"
@body += "<li>#{$1}</li>"
@ul = true
elsif line =~ /\A\d\.\s(.*)/
@body += @ol ? "" : "<ol>"
@body += "<li>#{$1}</li>"
@ol = true
else
if @blockquote
@body += "</blockquote>"
@blockquote = false
end
if @ul
@body += "</ul>"
@ul = false
end
if @ol
@body += "</ol>"
@ol = false
end
@body += "<p>#{line}</p>" if line.strip != ""
end
end
def file_read filename
html = []
File.open(filename) do |file|
while line = file.gets
html << line.chomp
end
end
html.join("\n")
end
def line_to_pieces line
line.scan(/(-|\+)(..)|(\s\*\s)/)
end
def board
board_data = []
@board[:pieces].each_with_index do |row, index|
row.each_with_index do |pce, idx|
if pce[0] == '+'
board_data << {str: PCE_COLLECTION[pce[1]], x: 9-idx, y: index+1, isEnemy: false}
elsif pce[0] == '-'
board_data << {str: PCE_COLLECTION[pce[1]], x: 9-idx, y: index+1, isEnemy: true}
else
end
end
end
board_data += info_data @board[:first], false
board_data += info_data @board[:second], true
JSON::stringify(board_data)
end
def title
@title || ""
end
def body
@body
end
private
def info_data string, is_enemy
data = []
x = is_enemy ? 10 : -1
str = string.gsub(/\((..)\)/) do
PCE_COLLECTION[$1]
end
str.split(//).each_with_index do |char, idx|
data << {
str: char,
x: x,
y: is_enemy ? 9-idx : idx+1,
isEnemy: is_enemy
}
end
data
end
end
class HtmlFormat
def initialize
@html = []
end
def echo_data
@html.each do |line|
Nginx.echo line
end
end
def add_line line
@html << line
end
def add_file_data file_path
File.open(file_path) do |file|
while line = file.gets
@html << line.chomp
end
end
end
end
files = []
files << File.join(FILE_ROOT, "head01.tmpl")
files << File.join(FILE_ROOT, "head02.tmpl")
files << File.join(FILE_ROOT, "body01.tmpl")
#files << File.join(FILE_ROOT, "body02.tmpl")
r = Nginx::Request.new
s = Nginx::Server.new
smd_file = File.join(s.document_root, r.uri)
smd = ParseSmd.new
html = HtmlFormat.new
File.open(smd_file) do |file|
while line = file.gets
smd.parse line.chomp
end
end
html.add_file_data files[0]
html.add_line smd.title
html.add_file_data files[1]
html.add_line smd.body
html.add_file_data files[2]
html.echo_data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment