Skip to content

Instantly share code, notes, and snippets.

@syou6162
Created September 8, 2012 04:32
Show Gist options
  • Save syou6162/3671796 to your computer and use it in GitHub Desktop.
Save syou6162/3671796 to your computer and use it in GitHub Desktop.
はてな記法からhtmlへのconvertするためのスクリプト
# -*- coding: utf-8 -*-
require "rubygems"
require "mechanize"
require "optparse"
require "hpricot"
require "text/hatena"
require "pp"
@options = {
:c => false,
:i => nil,
:i => nil,
:m => "syou6162@gmail.com",
:p => true,
:s => "■",
:t => nil
}
opts = OptionParser.new
opts.on('-c', "--hogegheo",
TrueClass){|boolean| @options[:c] = boolean}
opts.on('-i file', '--input file',
'specify the input file name') {|f| @options[:i] = f}
opts.on('-m [mail]', '--mail [mail]',
'specify a mail address') {|c| @options[:m] = c}
opts.on('-o [file]', '--output [file]',
'specify the output file name') {|f| @options[:o] = f}
opts.on("-p", "--presentation",
TrueClass){|boolean| @options[:p] = !boolean}
opts.on('-s chat', '--sectionanchor char',
'specify the sectionanchor') {|c| @options[:s] = c }
opts.on('-t title', '--title title',
'specify the sectionanchor') {|c| @options[:t] = c }
opts.parse!(ARGV)
if @options[:i].nil?
puts "file name is needed."
exit
end
sectionanchor = @options[:s]
parser = Text::Hatena.new({
:parmalink => './resume.html',
:sectionanchor => sectionanchor
})
file = @options[:i]
if @options[:t].nil?
title = File.basename(file,".*")
else
title = @options[:t]
end
f = open(file,"r")
text = f.read
f.close
header =<<EOS
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:#{@options[:m]}" />
<title>#{title}</title>
</head>
<style type="text/css">
<!--
body {
margin-left: 88px;
margin-right: 7%;
background-color: #fff;
background-repeat: repeat-y;
text-align: left;
font-family: sans-serif;
}
h3 {
font-size: 120%;
color : #000000;
text-align : left;
background : #fefeee;
padding : 0.3em 0.3em 0.3em 0.5em;
border-width : 1px 1px 1px 1px;
border-style : solid;
border-color : #aa2;
}
h4 {
font-size: 110%;
margin: 1em 0 1em 0;
padding-left : 0.2em;
color : #000;
text-align : left;
background : transparent;
border-width : 0em 0em 1pt 0pt;
border-style : dashed;
border-color : #aaa;
}
h5 {
font-size: 100%;
color : #000;
text-align : left;
background : transparent;
border-width : 0em 0em 1pt 0pt;
border-style : dotted;
border-color : #274;
margin-left : 0.4em;
}
img {
border: 0pt;
}
.title {
color: #fff;
background-color: #225;
border-color : #9ad;
margin: 1em 0 1em 0;
font-size: 170%;
padding: 0.4em 0.6em;
border-width : 0px 4px 4px 0px;
border-style : solid;
}
a {
text-decoration:none;
}
a:hover {
background-color: #CCCCFF;
}
p, ul, ol, li, pre {
margin-left: 1em;
line-height: 120%;
}
pre.hatena-super-pre {
padding: 0.5em 0.5em 0.5em 0.5em;
background-color: #efd;
font-size: 100%;
}
table {
width: 90%;
margin-left: 1em;
border-spacing: 0.2em 0.5em;
}
th {
border-color : #aaa;
border-width : 0px 2px 2px 0px;
border-style : solid;
}
td {
border-color : #aaa;
border-width : 0px 2px 2px 0px;
border-style : solid;
}
tr {
margin: 1em 0 1em 0;
}
blockquote {
margin: 0 1em 0 2em;
padding: 1em 1em 1em 1em;
background-color: #efd;
font-size: 90%;
}
.footer {
border-top: gray thin solid;
margin: 3em 0 0 0;
padding: 1em 0 0 0;
}
-->
</style>
<body>
<a name="top"></a>
<h1 class="title">#{title}</h1>
EOS
footer =<<EOS
</body>
</html>
EOS
text.sub!(/\[:presentation\]/,"") if @options[:p]
text.gsub!(/(asin:(.*?)):(title|detail|image)/){$1}
parser.parse(text)
source = Hpricot(parser.html)
tableofcontents = ""
if @options[:c]
tableofcontents = tableofcontents + "<ul>\n"
(source/"div>h3").each_with_index{|item, index|
tableofcontents = tableofcontents +
"\t<li><a href=\"#p#{index}\">" +
item.inner_text.sub(/^#{@options[:s]}\s(.*?)/){$1} + "</a></li>\n"
}
tableofcontents = tableofcontents + "</ul>\n"
end
html = header + tableofcontents + parser.html.gsub(/(<\/h3>)/){
"\s<a href=\"#top\"><span class=\"sanchor\">#{@options[:s]}</span></a>#{$1}"
} + footer
if @options[:o].nil?
puts html
else
f = File.open(@options[:o],'w')
f.puts html
f.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment