Skip to content

Instantly share code, notes, and snippets.

@epitron
Created January 30, 2012 22:30
Show Gist options
  • Save epitron/1707213 to your computer and use it in GitHub Desktop.
Save epitron/1707213 to your computer and use it in GitHub Desktop.
BlackCarpet (ANSI renderer for RedCarpet)
require 'epitools'
require 'redcarpet'
require 'coderay'
def indented?(text)
indent_sizes = text.lines.map{ |line| if line =~ /^(\s+)/ then $1 else '' end }.map(&:size)
indent_sizes.all? {|dent| dent > 0 }
end
def unwrap(text)
return text unless indented? text
text.lines.to_a.map(&:strip).join ' '
end
def indent(text,amount=2)
text.lines.map{|line| " "*amount + line }.join("\n")
end
class BlackCarpet < Redcarpet::Render::Base
def normal_text(text)
text
end
def raw_html(html)
''
end
def link(link, title, content)
unless content[/^Back /]
content
end
end
def block_code(code, language)
language ||= :ruby
"#{indent CodeRay.scan(code, language).term}\n"
end
def block_quote(text)
indent paragraph(text)
end
def codespan(code)
code.cyan
end
def header(title, level)
color = case level
when 1 then :light_yellow
when 2 then :light_cyan
when 3 then :light_blue
end
bar = ("-"*(title.size+4)).grey
"#{bar}\n #{title.send(color)}\n#{bar}\n\n"
end
def double_emphasis(text)
text.light_green
end
def emphasis(text)
text.green
end
def linebreak
"\n"
end
def paragraph(text)
"#{indented?(text) ? text : unwrap(text)}\n\n"
end
def list(content, list_type)
case list_type
when :ordered
@counter = 0
"#{content}\n"
when :unordered
"#{content}\n"
end
end
def list_item(content, list_type)
case list_type
when :ordered
@counter += 1
" <8>#{@counter}.</8> #{content.strip}\n"
when :unordered
" <8>*</8> #{content.strip}\n".colorize
end
end
end
class DebugCarpet < Redcarpet::Render::Base
%{
# Block-level calls
# If the return value of the method is `nil`, the block
# will be skipped.
# If the method for a document element is not implemented,
# the block will be skipped.
#
# Example:
#
# class RenderWithoutCode < Redcarpet::Render::HTML
# def block_code(code, language)
# nil
# end
# end
#
block_code(code, language)
block_quote(quote)
block_html(raw_html)
header(text, header_level)
hrule()
list(contents, list_type)
list_item(text, list_type)
paragraph(text)
table(header, body)
table_row(content)
table_cell(content, alignment)
# Span-level calls
# A return value of `nil` will not output any data
# If the method for a document element is not implemented,
# the contents of the span will be copied verbatim
autolink(link, link_type)
codespan(code)
double_emphasis(text)
emphasis(text)
image(link, title, alt_text)
linebreak()
link(link, title, content)
raw_html(raw_html)
triple_emphasis(text)
strikethrough(text)
superscript(text)
# Low level rendering
entity(text)
normal_text(text)
# Header of the document
# Rendered before any another elements
doc_header()
# Footer of the document
# Rendered after all the other elements
doc_footer()
# Pre/post-process
# Special callback: preprocess or postprocess the whole
# document before or after the rendering process begins
#preprocess(full_document)
#postprocess(full_document)
}.lines.each do |line|
line.strip!
next if line =~ /^#/ or line.blank?
if line =~ /(\w+)\(([^\)]*)\)/
meth = $1
args = $2.split(/,\s+/).map(&:to_sym)
#p [meth, args]
define_method(meth) do |*theargs|
pp [meth.to_sym, Hash[ *args.zip(theargs).flatten ] ]
"[#{meth}]"
end
else
raise "Error: #{line}"
end
end
end
carpet = Redcarpet::Markdown.new(BlackCarpet, :fenced_code_blocks=>true)
#carpet = Redcarpet::Markdown.new(DebugCarpet, :fenced_code_blocks=>true)
lesspipe(:wrap=>true) do |less|
less.puts carpet.render(File.read "pry.wiki.git/User-Input.md")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment