Skip to content

Instantly share code, notes, and snippets.

@OiNutter OiNutter/ghfm.rb forked from carwin/ghfm.rb
Last active Mar 16, 2016

Embed
What would you like to do?
#!/usr/bin/ruby
require 'rubygems'
require 'redcarpet'
require 'albino'
class String
def make_semantic
self.downcase.strip.gsub(' ','-').gsub(/[^\w-]/,'')
end
end
class SyntaxRenderer < Redcarpet::Render::HTML
def header(title, level)
"<h#{level} id='#{title.make_semantic}'>#{title}</h#{level}>"
end
def block_code(code, language)
if language && !language.empty?
Albino.colorize(code, language)
else
"<pre><code>#{code}</code></pre>"
end
end
end
def markdown(text)
renderer = SyntaxRenderer.new(optionize [
:with_toc_data,
#:hard_wrap,
:xhtml
])
markdown = Redcarpet::Markdown.new(renderer, optionize([
:fenced_code_blocks,
:no_intra_emphasis,
:tables,
:superscript,
:autolink,
:strikethrough,
:space_after_headers,
:with_toc_data,
#:no_styles,
:lax_spacing
]))
markdown.render(text)
end
def optionize(options)
options.inject({}) { |memo, option| memo[option] = true; memo }
end
puts markdown(ARGF.read)
@OiNutter

This comment has been minimized.

Copy link
Owner Author

commented Oct 13, 2013

Override the Redcarpet html header function to use semanticised versions of the header text as the id. Makes for more intuitive table of contents writing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.