Skip to content

Commit

Permalink
Add sample Manpage renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
vmg committed Aug 4, 2011
1 parent b5693cd commit 97d7a35
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ The following instance methods may be implemented by the renderer:
header(text, header_level)
hrule()
list(contents, list_type)
list_item(text)
list_item(text, list_type)
paragraph(text)
table(header, body)
table_row(content)
Expand Down
1 change: 0 additions & 1 deletion ext/redcarpet/rc_markdown.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ static VALUE rb_redcarpet_md_render(VALUE self, VALUE text)
return rb_redcarpet_md_render_with(self, rb_iv_get(self, "@renderer"), text);
}


void Init_redcarpet()
{
rb_mRedcarpet = rb_define_module("Redcarpet");
Expand Down
3 changes: 2 additions & 1 deletion ext/redcarpet/rc_render.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ rndr_list(struct buf *ob, struct buf *text, int flags, void *opaque)
static void
rndr_listitem(struct buf *ob, struct buf *text, int flags, void *opaque)
{
BLOCK_CALLBACK("list_item", 1, buf2str(text));
BLOCK_CALLBACK("list_item", 2, buf2str(text),
(flags & MKD_LIST_ORDERED) ? CSTR2SYM("ordered") : CSTR2SYM("unordered"));
}

static void
Expand Down
65 changes: 65 additions & 0 deletions lib/redcarpet/render_man.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module Redcarpet
module Render
class ManPage < Base

def normal_text(text)
text.gsub('-', '\\-').strip
end

def block_code(code, language)
"\n.nf\n#{normal_text(code)}\n.fi\n"
end

def codespan(code)
block_code(code, nil)
end

def header(title, level)
case level
when 1
"\n.TH #{title}\n"

when 2:
"\n.SH #{title}\n"

when 3:
"\n.SS #{title}\n"
end
end

def double_emphasis(text)
"\\fB#{text}\\fP"
end

def emphasis(text)
"\\fI#{text}\\fP"
end

def linebreak
"\n.LP\n"
end

def paragraph(text)
"\n.TP\n#{text}\n"
end

def list(content, list_type)
case list_type
when :ordered
"\n\n.nr step 0 1\n#{content}\n"
when :unordered
"\n.\n#{content}\n"
end
end

def list_item(content, list_type)
case list_type
when :ordered
".IP \\n+[step]\n#{content.strip}\n"
when :unordered
".IP \\[bu] 2 \n#{content.strip}\n"
end
end
end
end
end
1 change: 1 addition & 0 deletions test/redcarpet_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

require 'test/unit'
require 'redcarpet'
require 'redcarpet/render_man'
require 'nokogiri'

def html_equal(html_a, html_b)
Expand Down

0 comments on commit 97d7a35

Please sign in to comment.