Skip to content

Instantly share code, notes, and snippets.

@skoji
Created June 9, 2011 03:36
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 skoji/1015999 to your computer and use it in GitHub Desktop.
Save skoji/1015999 to your computer and use it in GitHub Desktop.
text to HTML convert utility. read from stdin. must define some singleton methods in the initialization block.
require 'uri'
class DenshoConv
attr_accessor :file_prefix, :file_suffix, :title, :stylesheets, :count, :toc, :count_format, :replace_url, :outer_div_class
def init_default_filters
before_filter do
|text|
if @replace_url
URI.extract(text, ['http', 'https']).each { |url|
text.sub!(url, "<a href=\"#{url}\">#{url}</a>")
}
end
text
end
head_inserter do
ret = ""
@stylesheets.each { |s|
ret << "<link rel=\"stylesheet\" type=\"text/css\" href=\"#{s}\" />\n"
}
ret
end
end
def initialize(&block)
@count = 0
@file_prefix=''
@file_suffix=''
@count_format="%03d"
@outer_div_class='contents'
@toc = {}
init_default_filters
yield self
self
end
def current_filename
"#{@file_prefix}#{@count_format%@count}#{@file_suffix}.html"
end
def finish_html
if !@file.nil?
print "</div>\n" if @outer_div_wrote
print <<EOF
</body>
</html>
EOF
@file.close
@file = nil
end
end
def start_html
print <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>#{@title}</title>
EOF
head_inserters.each {
|f|
print f.call
}
print <<EOF
</head>
<body>
EOF
if !@outer_div_class.nil?
print "<div class=\"#{@outer_div_class}\">"
@outer_div_wrote = true
else
@outer_div_wrote = nil
end
end
def before_filters
@before_filters ||= []
end
def after_filters
@after_filters ||= []
end
def head_inserters
@head_inserters ||= []
end
def before_filter (&block)
before_filters << block
end
def after_filter (&block)
after_filters << block
end
def head_inserter(&block)
head_inserters << block
end
def switch_to_next_html(title = nil)
@title = title if !title.nil?
finish_html
@toc[current_filename()] = title unless title.nil?
@file = File.open(current_filename, 'w+')
start_html
@count = @count + 1
end
def print (x)
after_filters.each {
|f|
x = f.call(x)
}
@file.print x
end
def send_if_command(l)
l.strip =~ /^(\w*?):(.*?)$/
if (!$1.nil? && respond_to?($1))
send($1.strip,"#{$2}")
true
else
false
end
end
def parse_and_write(l)
if !send_if_command(l) && l.strip.length > 0
print l.strip
print "<br />" unless l.strip =~ /(<\/[\w]+?>$|<[\w\s'"=]+?\/>$)/
print "\n"
end
end
def run
while l = gets
before_filters.each {
|f|
l = f.call(l)
}
parse_and_write l
end
finish_html
self
end
end
# -*- coding: utf-8 -*-
require 'pp'
load 'densho_conv.rb'
Encoding.default_external='utf-8'
pp DenshoConv.new {
|t|
t.file_suffix = '_file'
t.count_format='%02d'
t.stylesheets = ['style.css']
t.title = 'test'
t.replace_url = true
class << t
def part(x)
switch_to_next_html x.strip
print "<h1>#{x.strip}</h1>\n"
end
def section(x)
print "<h2>#{x.strip}</h2>\n"
end
def speak(x)
x =~ /^(.*?)\s(.*)$/
print "<span class='name'>#{$1.strip}</span><div class='comment'>#{$2.strip}</div>\n"
end
end
}.run.toc
part: はじめに
section: このファイルの概要
<p>これはテスト用のサンプルです
頭にコマンドがなければそのままHTML内にでてきます</p>
part: ここから次のファイル
section: 本題
<p>ここで、次のファイルがはじまります。</p>
speak: 発言者 発言
speak: 発言者2 発言
<p> このサンプルでのspeakは、こういう対談形式を手っ取り早くマークアップするのに使えます</p>
<p> @replace_urlをtrueにすれば、https://gist.github.com/gists/1015999/edit ←こういう文字列を&gt;a&lt;で囲ってhtmlにします。</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment