Created
January 13, 2010 09:20
-
-
Save tomohiro/276064 to your computer and use it in GitHub Desktop.
カレントディレクトリ以下の Markdown ファイルを HTML に変換する(要 Markdown.pl)
This file contains hidden or 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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'nokogiri' | |
CONVERTER = 'Markdown.pl' | |
SUFFIX = '.mkdn' | |
class Markdown2HTML | |
def initialize | |
@mkdn_path = nil || Dir.pwd | |
@html_path = nil || Dir.pwd | |
#@css_path = nil || 'http://github.com/Tomohiro/tomohiro.github.com/raw/master/' || 'http://yui.yahooapis.com/3.0.0/build/cssreset/' | |
@css_path = '/' | |
end | |
def run | |
@html_template = create_html_template | |
output_html(convert_html_body) | |
end | |
def create_html_template | |
<<-HTML | |
<?xml version="1.0" encoding="utf-8"?> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja"> | |
<head> | |
<meta name="viewport" content="width=320" /> | |
<link rel="stylesheet" type="text/css" media="screen, print" href="#{@css_path}styles/reset-min.css" /> | |
<link rel="stylesheet" type="text/css" media="screen, print" href="#{@css_path}styles/fonts-min.css" /> | |
<link rel="stylesheet" type="text/css" media="screen and (min-device-width: 481px), print" href="#{@css_path}styles/m2h.css" /> | |
<link rel="stylesheet" type="text/css" media="only screen and (max-device-width: 480px)" href="#{@css_path}styles/iphone.css" /> | |
<!--[if IE]><link rel="stylesheet" type="text/css" media="screen, projection" href="#{@css_path}styles/m2h.css" /><![endif]--> | |
<script type="text/javascript" src="scripts/google-analytics.js"></script> | |
<title>%TITLE%</title> | |
</head> | |
<body> | |
<div id="container"> | |
<!-- Auto generated start --> | |
%CONTENTS% | |
<!-- Auto generated end --> | |
<div id="menu"> | |
<ul> | |
<li><a href="./index.html">トップ</a></li> | |
<li><a href="http://tomohiro.github.com">visit Tomohiro.GitHub</a></li> | |
</ul> | |
</div> | |
<div id="footer"> | |
<p>Copyright © #{Date.today.year} <a href="http://tomohiro.github.com">Tomohiro.GitHub</a> All rights reserved.</p> | |
<p>Generated by <a href="http://gist.github.com/276064" title="m2 - Markdown2HTML">m2 - Markdown2HTML</a></p> | |
</div> | |
</div> | |
</body> | |
</html> | |
HTML | |
end | |
def convert_html_body | |
body_list = {} | |
Dir.glob("#{@mkdn_path}/*#{SUFFIX}").each do |file_name| | |
body_list[file_name] = `#{CONVERTER} #{file_name}` | |
end | |
body_list | |
end | |
def output_html(body_list) | |
body_list.each do |file_name, body| | |
html_name = file_name.gsub(SUFFIX, '.html') | |
title = [(Nokogiri::HTML(body)/'h2').text, (Nokogiri::HTML(body)/'h1').text].join(' - ') | |
content = @html_template.gsub('%TITLE%', title) | |
content.gsub!('%CONTENTS%', body) | |
path = "../#{File.basename(html_name)}" | |
File.open(path, 'w') do |f| | |
f.write content | |
end | |
end | |
end | |
end | |
Markdown2HTML.new.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment