Permalink
Browse files

add table of contents to `github-markup`

  • Loading branch information...
1 parent 08c9776 commit f718addb7d9a868308ffc88cddd2c2af051acee2 @mislav committed May 29, 2012
Showing with 34 additions and 29 deletions.
  1. +2 −2 libexec/coral-doctor
  2. +32 −27 libexec/coral-github-markup
View
@@ -69,8 +69,8 @@ if gem_missing github-markup github/markup; then
gems="${gems} github-markup"
fi
-if gem_missing github-markdown github/markdown; then
- gems="${gems} github-markdown"
+if gem_missing redcarpet; then
+ gems="${gems} redcarpet"
fi
if gem_missing rdoc; then
@@ -1,12 +1,8 @@
#!/usr/bin/env ruby -rubygems
## Render markup from a file and output HTML.
#
-# This script facilitates using the "github-markup" gem, and configures Markdown
-# processing to be the same as on GitHub.com:
-#
-# * fenced code blocks & syntax highlighting using "pygments.rb" gem
-# * sanitizes unsafe HTML
-# * turns off intra-emphasis feature
+# This script facilitates using the "github-markup" library and configures
+# Markdown processing to be the same as on GitHub.com.
file = ARGV[0]
@@ -22,35 +18,44 @@ rescue LoadError
end
renderer = nil
-html_options = {}
+html_options = { }
markdown_options = {
- :autolink => true,
- :no_intra_emphasis => true,
- :fenced_code_blocks => true
+ :autolink => true,
+ :no_intra_emphasis => true,
+ :fenced_code_blocks => true,
}
html_renderer = lambda {
renderer ||= begin
- old_level, $-w = $-w, nil
- require 'pygments'
- rescue LoadError
- warn "error loading pygments.rb; no syntax highlighting available"
- Redcarpet::Render::HTML
- else
- require 'cgi'
- class RenderHtml < Redcarpet::Render::HTML
- def block_code(code, language)
- if language
- Pygments.highlight(code, :lexer => language)
- else
- # can't call super: https://github.com/tanoku/redcarpet/issues/51
- "<pre><code>#{CGI.escapeHTML code}</code></pre>"
+ class TocHTML < Redcarpet::Render::HTML
+ def header text, level
+ href = text.gsub(/<.+?>/, '').strip.gsub(/\s+/, '-').downcase
+ anchor = %(<a name="#{href}" href="\##{href}"></a>)
+ %(<h#{level}>#{anchor}#{text}</h#{level}>\n)
+ end
+ end
+
+ begin
+ old_level, $-w = $-w, nil
+ require 'pygments'
+ rescue LoadError
+ warn "error loading pygments.rb; no syntax highlighting available"
+ TocHTML
+ else
+ require 'cgi'
+ Class.new(TocHTML) do
+ def block_code(code, language)
+ if language
+ Pygments.highlight(code, :lexer => language)
+ else
+ # can't call super: https://github.com/tanoku/redcarpet/issues/51
+ "<pre><code>#{CGI.escapeHTML code}</code></pre>"
+ end
end
end
+ ensure
+ $-w = old_level
end
- RenderHtml
- ensure
- $-w = old_level
end
}

0 comments on commit f718add

Please sign in to comment.