Skip to content

Instantly share code, notes, and snippets.

@inao
Forked from yhara/ftplugin-gihyo.vim
Created March 29, 2011 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inao/891965 to your computer and use it in GitHub Desktop.
Save inao/891965 to your computer and use it in GitHub Desktop.
let g:surround_99 = "◆cmd/◆\r◆/cmd◆" " 99 = c
let g:surround_108 = "◆list/◆\r◆/list◆" " 108 = l
let g:surround_109 = "◆column/◆\n■■■■\n\r◆/column◆" " 109 = m
let g:surround_110 = "◆注/◆\r◆/注◆" " 110 = n
inoreabbrev HEADER ■■■■
inoreabbrev CAPTION ●リスト/図/表n ...
inoreabbrev NOTE ◆注/◆◆/注◆
inoreabbrev COMMAND ◆cmd/◆◆/cmd◆
inoreabbrev NAOSI ・p 右左段「」段落目<CR>↓変更
"◆b/◆太字◆/b◆
"◆i/◆イタリック(斜体)◆/i◆
"◆ルビ/◆単語◆よみがな◆/ルビ◆
"==============================================
"【行頭記号(段落スタイル)】
"
"# 見出し
"■大見出し
"■■中見出し
"■■■小見出し
"■■■■コラム
"
"# 箇条書き
"・箇条書き文字列
"(n)番号付き箇条書き(nには数字を入れてください)
"
"# リスト(ソースコードやコマンドライン)
"◆list/◆
"source code
" or
"command
"◆/list◆
"
"※改行なしの◆list/◆source code◆/list◆はNGです。
"※この用途では後述する◆cmd/◆◆/cmd◆を使ってください。
"
"# キャプション
"●リストn リスト(nには数字を入れてください)
"●図n 図タイトル(nには数字を入れてください)
"●表n 表タイトル(nには数字を入れてください)
"
"==============================================
"【文中記号(文字スタイル)】
"
"# 基本
"◆b/◆太字◆/b◆
"◆i/◆イタリック(斜体)◆/i◆
"◆ルビ/◆単語◆よみがな◆/ルビ◆
"本文の参照位置◆注/◆注記の内容◆/注◆
"◆cmd/◆source code or command(本文中で欧文フォントにしたいところ)◆/cmd◆
"
"# 丸数字(黒丸数字)
"(1)
"(2)
"(3)
"(4)
"(5)
"(6)
"(7)
"(8)
"(9)
"(10)
"==============================================
#!/usr/bin/env ruby
# coding: utf-8
require 'sinatra'
#require 'sinatra/reloader' if development?
require 'pp'
GENKOU_PATH = ARGV[0]
IMAGES_DIR = ARGV[1] || File.dirname(ARGV[0])
class Gihyo
def initialize(txt)
@txt = txt
end
def to_html
<<-EOD
<head>
<meta charset="UTF-8" />
<title>#{title_of @txt}</title>
<style>
body{ margin: 0 10%; font-family: serif; font-size: 11pt;}
h1, h4{ border-left: 20px solid #0ce; padding-left: 10px }
#title{ line-height: 300%; border-bottom: 2px solid #0ce; }
h2{ border-bottom: 1px solid #0ce; padding-left: 0px }
.note{ font-size: x-small; color: #a07; padding: 10px }
.list{ background: #333; color: white;
margin-left: 10px; padding: 2px; }
.table{ border: 1px solid black; padding: 3px;}
.caption{ font-weight: bold; margin-top: 1em;}
.column{ background: #eee; padding: 6px; }
img{ border: 1px dashed black; padding: 3px;}
code{ font-family: monospace; }
</style>
</head>
#{miscs commands columns figures notes lists headers tables br Rack::Utils.escape_html @txt}
EOD
end
private
def br(txt)
txt.lines.map{|line|
case line
when /^■/, /^\s*$/, /^◆(?!cmd).*/, /^●/
line
else
" " + line.chomp + "<br>\n"
end
}.join
end
def unbr(txt)
txt.lines.map{|line| line.sub(/^ (.*)<br>$/){ $1 }}.join
end
def title_of(txt)
txt.lines.to_a[3]
end
def title(txt)
txt.sub(/<!--end-toc-->(.*?)$/){
"<h1 id='title'>#{$1}</h1>"
}
end
def headers(txt)
# anchors = []
# last_anchor = 0
# body = txt.gsub(/^(■+)(.*)$/){
# orig_title = $1 + $2
# n = $1.length
# title = $2
# if n == 4
# n = 1
# title = "[コラム] "+title
# end
# last_anchor += 1
# anchors << [last_anchor, orig_title]
# "<a name='header#{last_anchor}' /><h#{n}>#{title}</h#{n}>"
# }
chunks = txt.lines.slice_before{|line|
line =~ /^■/
}.map{|lines|
orig_title = lines.first
title = orig_title.gsub(/^(■+)/, "")
level = (orig_title[/^■+/] || "").length
size = lines.join.length
[level, orig_title, title, size, lines]
}
anchors = []
body = chunks.map.with_index{|(level, orig_title, title, chunk_size, lines), i|
case level
when 0
n = 1
when 4
n = 1
title = "[コラム] #{title}"
else
n = level
end
size = chunks[(i+1)..-1].take_while{|chunk|
chunk.first > level
}.map{|chunk| chunk[3]}.inject(0, :+) + chunk_size
anchors << [i, orig_title, size]
"<a name='header#{i}'/><h#{n}>#{title}</h#{n}>" + lines.drop(1).join
}.join
toc = "<h1>目次</h1><pre>" + anchors.map{|i, orig_title, size|
"#{'%5d' % size}<a href='#header#{i}'>#{orig_title.chomp}</a>\n"
}.join + "</pre><!--end-toc-->"
toc + body
end
def lists(txt)
txt.gsub(%r{(●リスト.*?\n)?◆list/◆(.*?)◆/list◆}m){
"<div class='caption'>#{$1}</div><pre class='list'>#{unbr $2}</pre>"
}
end
def notes(txt)
txt.gsub(%r{◆注/◆(.*?)◆/注◆}m){
content = $1
if content =~ /\Ahttp/
"<span class='note'><a href='#{content}'>#{content}</a></span>"
else
"<span class='note'>#{content}</span>"
end
}
end
def commands(txt)
txt.gsub(%r{◆cmd/◆(.*?)◆/cmd◆}m){
"<code>#{$1}</code>"
}
end
def tables(txt)
txt.gsub(%r{◆table/◆\s*(●表.*?\n)(.*?)◆/table◆}m){
caption = $1
rows = $2.lines.map{|l|
if l =~ /=======+/
""
else
tag = (l =~ /◆table-title◆/) ? "th" : "td"
l.sub!(/◆table-title◆/, "")
"<tr>" +
unbr(l).split(/\t/).map{|s| "<#{tag}>#{s.chomp}</#{tag}>"}.join +
"</tr>\n"
end
}.join
%{
<div class='caption'>#{caption}</div>
<table border=1>
#{rows}
</table>
}
}
end
def figures(txt)
txt.gsub(%r{(●図.*?\n) (?:\(|()(.*?)(?:\)|))}m){
"<div class='caption'>#{$1}</div>" +
"<img src='#{$2}'/>"
}
end
def columns(txt)
txt.gsub(%r{◆column/◆}, "<div class='column'>").
gsub(%r{◆/column◆}, "</div>")
end
def miscs(txt)
txt.gsub(%r{◆1/2行間◆}, "").
gsub(%r{◆b/◆}, "<b>").
gsub(%r{◆/b◆}, "</b>").
gsub(%r{◆上付/◆}, "<sup class=note>").
gsub(%r{◆/上付◆}, "</sup>").
gsub(%r{◆red/◆(.*?)◆/red◆}, "").
gsub(%r{◆comment/◆(.*?)◆/comment◆}){ "# file: #{$1}\n"}
end
end
get %r{(.*).(png|gif|jpg)} do
path = "#{IMAGES_DIR}/#{request.path_info}"
#mime_type "image/png"
if File.exist?(path)
send_file path
else
"image not found"
end
end
get '/' do
Gihyo.new(File.read GENKOU_PATH).to_html
end
helpers do
alias h escape_html
end
get '/test' do
"wow"
end
" Vim syntax file
" Language: Gihyo
" Maintainer: HARA, Yutaka (yhara/at/kmc.gr.jp)
" URL: http://mono.kmc.gr.jp/~yhara/
" Latest Revision: ?
" arch-tag: ?
if version < 600
syntax clear
elseif exists("b:current_syntax")
finish
endif
syn keyword gihyoTodo contained TODO FIXME XXX NOTE
syn region gihyoHeader1 start='^■[^■]' end='$'
syn region gihyoHeader2 start='^■■[^■]' end='$'
syn region gihyoHeader3 start='^■■■[^■]' end='$'
syn region gihyoHeader4 start='^■■■■[^■]' end='$'
syn region gihyoCaption start='^●' end='$'
syn region gihyoList start='◆list/◆' end='◆/list◆'
syn region gihyoColumn start='\v◆/?column/?◆' end='$'
"syn region gihyoPasteLine start='^ ' end='$'
"syn region gihyoItemize start='^*' end='$'
"syn region gihyoEnumerate start='^#' end='$'
"syn region gihyoDescription start='^:' end=':'
"syn region gihyoHref start='\[\[' end='\]\]'
" Define the default highlighting.
" For version 5.7 and earlier: only when not done already
" For version 5.8 and later: only when an item doesn't have highlighting yet
if version >= 508 || !exists("did_gihyo_syn_inits")
if version < 508
let did_gihyo_syn_inits = 1
command -nargs=+ HiLink hi link <args>
else
command -nargs=+ HiLink hi def link <args>
endif
"HiLink hdPasteLine Comment
HiLink gihyoHeader1 Number
HiLink gihyoHeader2 Comment
HiLink gihyoHeader3 Identifier
HiLink gihyoHeader4 Operator
HiLink gihyoCaption Operator
HiLink gihyoList Underlined
HiLink gihyoColumn Operator
" HiLink gihyoItemize String
" HiLink gihyoEnumerate String
" HiLink gihyoHref Underlined
" HiLink gihyoDescription Underlined
HiLink gihyoTodo Todo
" HiLink hdComment Comment
" HiLink hdDocumentEnd PreProc
" HiLink hdDirective Keyword
" HiLink hdNodeProperty Type
" HiLink hdAnchor Type
" HiLink hdAlias Type
" HiLink hdDelimiter Delimiter
" HiLink hdBlock Operator
" HiLink hdOperator Operator
" HiLink hdKey Identifier
" HiLink hdString String
" HiLink hdEscape SpecialChar
" HiLink hdSingleEscape SpecialChar
" HiLink hdNumber Number
" HiLink hdConstant Constant
" HiLink hdTimestamp Number
delcommand HiLink
endif
let b:current_syntax = "gihyo"
" vim: set sts=2 sw=2:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment