Skip to content

Instantly share code, notes, and snippets.

@jinroh
Last active Oct 1, 2015
Embed
What would you like to do?
Markit!
#!/usr/bin/env ruby
require 'rubygems'
require 'redcarpet'
require 'albino'
class SyntaxRenderer < Redcarpet::Render::HTML
def initialize(options)
super options
@style = options[:style]
@otoc = options[:toc]
@toc = ""
@rlevel = nil
end
def doc_header
<<-HTML.gsub /^\s+/, ""
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
--TITLE
<link rel="stylesheet" href="#{@style}">
</head>
<body>
<header>
--TOC
</header>
HTML
end
def doc_footer
<<-HTML.gsub /^\s+/, ""
</body>
</html>
HTML
end
def header(text, level)
header = ""
if (@rlevel == nil)
@title = text
@rlevel = level
header << "<section id=\"title\">\n"
header << "<h1>#{text}</h1>\n"
else
if (@rlevel - level >= 0)
header << "</section>\n" * (@rlevel - level + 1)
@toc << "</li>\n" + "</ul>\n</li>\n" * (@rlevel - level) unless @toc.empty?
else
@toc << "<ul>\n" + "<li>\n<ul>\n" * (level - @rlevel - 1)
end
id = text.downcase.gsub(/\ /, '-')
header << "<section id=\"#{id}\">\n"
header << "<h#{level}>#{text}</h#{level}>\n"
@toc << "<li>\n"
@toc << "<a href=\"\##{id}\">#{text}</a>"
@rlevel = level
end
header
end
def block_code(code, language)
if language && !language.empty?
Albino.colorize(code, language)
else
"<pre><code>#{code}</code></pre>"
end
end
def preprocess(full_document)
@text = full_document
end
def postprocess(full_document)
full_document.gsub(/--TITLE/, self.title)
.gsub(/--TOC/, self.toc)
end
def toc
@otoc ? "<nav id=\"toc\"><ul>\n#{@toc}\n</ul></nav>" : ""
end
def title
"<title>#{@title}</title>"
end
end
def markdown(text)
renderer = SyntaxRenderer.new(
:style => "style.css",
:toc => true,
:hard_wrap => true,
:xhtml => true
)
markdown = Redcarpet::Markdown.new(renderer,
:fenced_code_blocks => true,
:no_intra_emphasis => true,
:tables => true,
:autolink => true,
:strikethrough => true,
:space_after_headers => true
)
markdown.render(text);
end
puts markdown(ARGF.read)
@jinroh
Copy link
Author

jinroh commented Feb 26, 2012

$ markit foo.md > foo.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment