Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Added to_latex and to_groff_mm methods to PEGMarkdown class.
Browse files Browse the repository at this point in the history
Documented in README.markdown.
Bumped version to 1.1 in Rakefile.
  • Loading branch information
jgm committed Aug 8, 2008
1 parent 16858b5 commit 26578d3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.markdown
Expand Up @@ -16,6 +16,13 @@ Synopsis
>> puts Markdown.new('_Hello World!_', :smart, :filter_html).to_html
<p><em>Hello World!</em></p>

>> puts Markdown.new('_Hello World!_').to_latex
\emph{Hello World!}

>> puts Markdown.new('_Hello World!_').to_groff_mm
.P
\fIHello world!\fR

>> PEGMarkdown.new('Hello! World!')

Installation / Hacking
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -5,7 +5,7 @@ require 'rake/gempackagetask'
task :default => :test

DLEXT = Config::CONFIG['DLEXT']
VERS = '1.0'
VERS = '1.1'

spec =
Gem::Specification.new do |s|
Expand Down
48 changes: 48 additions & 0 deletions ext/markdown.c
Expand Up @@ -29,10 +29,58 @@ rb_markdown_to_html(int argc, VALUE *argv, VALUE self)
return result;
}

static VALUE
rb_markdown_to_latex(int argc, VALUE *argv, VALUE self)
{
/* grab char pointer to markdown input text */
VALUE text = rb_funcall(self, rb_intern("text"), 0);
Check_Type(text, T_STRING);
char * ptext = StringValuePtr(text);

/* flip extension bits - note that defaults are different than
* for HTML */
int extensions = EXT_SMART | EXT_NOTES | EXT_FILTER_HTML | EXT_FILTER_STYLES;
if ( rb_funcall(self, rb_intern("smart"), 0) == Qfalse )
extensions = extensions & ~ EXT_SMART ;
if ( rb_funcall(self, rb_intern("notes"), 0) == Qfalse )
extensions = extensions & ~ EXT_NOTES ;

char *latex = markdown_to_string(ptext, extensions, LATEX_FORMAT);
VALUE result = rb_str_new2(latex);
free(latex);

return result;
}

static VALUE
rb_markdown_to_groff_mm(int argc, VALUE *argv, VALUE self)
{
/* grab char pointer to markdown input text */
VALUE text = rb_funcall(self, rb_intern("text"), 0);
Check_Type(text, T_STRING);
char * ptext = StringValuePtr(text);

/* flip extension bits - note that defaults are different than
* for HTML */
int extensions = EXT_SMART | EXT_NOTES | EXT_FILTER_HTML | EXT_FILTER_STYLES;
if ( rb_funcall(self, rb_intern("smart"), 0) == Qfalse )
extensions = extensions & ~ EXT_SMART ;
if ( rb_funcall(self, rb_intern("notes"), 0) == Qfalse )
extensions = extensions & ~ EXT_NOTES ;

char *groff = markdown_to_string(ptext, extensions, GROFF_MM_FORMAT);
VALUE result = rb_str_new2(groff);
free(groff);

return result;
}

void Init_peg_markdown()
{
rb_cMarkdown = rb_define_class("PEGMarkdown", rb_cObject);
rb_define_method(rb_cMarkdown, "to_html", rb_markdown_to_html, -1);
rb_define_method(rb_cMarkdown, "to_latex", rb_markdown_to_latex, -1);
rb_define_method(rb_cMarkdown, "to_groff_mm", rb_markdown_to_groff_mm, -1);
}

// vim: ts=4 sw=4

0 comments on commit 26578d3

Please sign in to comment.