Last active
January 28, 2016 07:59
-
-
Save nagaki/d1efcf8364c673257235 to your computer and use it in GitHub Desktop.
MarkdownをHTMLにするスクリプト
This file contains 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
# coding: utf-8 | |
require 'rubygems' | |
require 'bundler/setup' | |
require 'redcarpet' | |
require 'pygments' | |
# マークダウンのファイル名をkey | |
# タイトルをvalueとしてセット | |
@names = { | |
index: "ホーム", | |
new: "環境構築", | |
changelog: "変更点", | |
} | |
# シンタックスハイライト | |
class HTMLwithPygments < Redcarpet::Render::HTML | |
def block_code(code, language) | |
Pygments.highlight(code, lexer: language) | |
end | |
end | |
# マークダウンをHTMLにコンバート | |
def convert(file_name) | |
markdown = Redcarpet::Markdown.new(HTMLwithPygments, fenced_code_blocks: true) | |
File.open("./markdown/#{file_name}.md", "r:UTF-8") do |file| | |
markdown.render(file.read) | |
end | |
end | |
# リンクのHTMLを生成 | |
def nav_html | |
links = String.new | |
@names.each do |key, value| | |
links += "<li><a href='./#{key}.html'>#{value}</a>" | |
end | |
"<nav><ul class='navigation'>#{links}</ul></nav>" | |
end | |
# 更新日のHTMLを生成 | |
def update_html | |
now = Time.now | |
"<time datetime='#{now}'>最終更新日 #{now.strftime('%Y.%m.%d')}</time>" | |
end | |
# HTMLを書き出し | |
def publish_html(file_name, title) | |
html = <<-EOS | |
<html> | |
<title>#{title}</title> | |
<meta charset="utf-8"> | |
<link rel="stylesheet" href="./css/style.css"> | |
<style>#{Pygments.css('.highlight', style: 'monokai')}</style> | |
<header> | |
#{nav_html} | |
</header> | |
<div class="main"> | |
#{convert(file_name)} | |
</div> | |
<footer> | |
#{nav_html} | |
#{update_html} | |
</footer> | |
EOS | |
File.open("./site/#{file_name}.html", "w:UTF-8") do |file| | |
file.write(html) | |
end | |
end | |
@names.each do |key, value| | |
publish_html(key, value) | |
end | |
puts 'できた。' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment