Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add option to Redcarpet::Render::HTML to emit style classes for google-code-prettify #192

Closed
wants to merge 10 commits into from

1 participant

@jolros

I've added an option to the HTML renderer to add "prettyprint" classes to <code> blocks so that they can be formatted with google-code-prettify (http://code.google.com/p/google-code-prettify/).

I've changed the documentation, and added a unit test.

With a renderer instantiated like this:

rnder = Redcarpet::Render::HTML.new(:prettify => true)
mkdwn = Redcarpet::Markdown.new(rnder, :no_intra_emphasis => true, :fenced_code_blocks => true, :space_after_headers => true)
mkdwn.render(text)

Code blocks will end up looking like this:

<pre><code class="prettyprint">
…
</code></pre>
@robin850 robin850 closed this in 9132288
@robin850 robin850 referenced this pull request from a commit
@robin850 robin850 Move the prettify test to the HTML render tests
Test introduced by #192 wasn't located in the good file.
68d4d1a
@robin850 robin850 referenced this pull request from a commit
@robin850 robin850 Improve prettify feature testing
Actually, the test introduced by #192 for this ferature wasn't
covering anything since the backticks were escaped so the output
was generate by the codespan callback.

This commit should test this feature almost correctly and reflect
changes done in #314.
bf44c83
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
2  README.markdown
@@ -135,6 +135,8 @@ Initializes an HTML renderer. The following flags are available:
:no_links - do not generate any `<a>` tags
:no_styles - do not generate any `<style>` tags
+
+ :prettify - add prettyprint classes to `<code>` tags for google-code-prettify
:safe_links_only - only generate links for protocols which are considered safe
View
20 ext/redcarpet/html.c
@@ -118,11 +118,18 @@ rndr_autolink(struct buf *ob, const struct buf *link, enum mkd_autolink type, vo
static void
rndr_blockcode(struct buf *ob, const struct buf *text, const struct buf *lang, void *opaque)
{
+ struct html_renderopt *options = opaque;
+
if (ob->size) bufputc(ob, '\n');
if (lang && lang->size) {
size_t i, cls;
- BUFPUTSL(ob, "<pre><code class=\"");
+ if (options->flags & HTML_PRETTIFY) {
+ BUFPUTSL(ob, "<pre><code class=\"prettyprint");
+ cls++;
+ } else {
+ BUFPUTSL(ob, "<pre><code class=\"");
+ }
for (i = 0, cls = 0; i < lang->size; ++i, ++cls) {
while (i < lang->size && isspace(lang->data[i]))
@@ -142,8 +149,11 @@ rndr_blockcode(struct buf *ob, const struct buf *text, const struct buf *lang, v
}
BUFPUTSL(ob, "\">");
- } else
+ } else if (options->flags & HTML_PRETTIFY) {
+ BUFPUTSL(ob, "<pre><code class=\"prettyprint\">");
+ } else {
BUFPUTSL(ob, "<pre><code>");
+ }
if (text)
escape_html(ob, text->data, text->size);
@@ -163,7 +173,11 @@ rndr_blockquote(struct buf *ob, const struct buf *text, void *opaque)
static int
rndr_codespan(struct buf *ob, const struct buf *text, void *opaque)
{
- BUFPUTSL(ob, "<code>");
+ struct html_renderopt *options = opaque;
+ if (options->flags & HTML_PRETTIFY)
+ BUFPUTSL(ob, "<code class=\"prettyprint\">");
+ else
+ BUFPUTSL(ob, "<code>");
if (text) escape_html(ob, text->data, text->size);
BUFPUTSL(ob, "</code>");
return 1;
View
1  ext/redcarpet/html.h
@@ -49,6 +49,7 @@ typedef enum {
HTML_HARD_WRAP = (1 << 7),
HTML_USE_XHTML = (1 << 8),
HTML_ESCAPE = (1 << 9),
+ HTML_PRETTIFY = (1 << 10),
} html_render_mode;
typedef enum {
View
4 ext/redcarpet/rc_render.c
@@ -386,6 +386,10 @@ static VALUE rb_redcarpet_html_init(int argc, VALUE *argv, VALUE self)
if (rb_hash_aref(hash, CSTR2SYM("no_links")) == Qtrue)
render_flags |= HTML_SKIP_LINKS;
+ /* prettify */
+ if (rb_hash_aref(hash, CSTR2SYM("prettify")) == Qtrue)
+ render_flags |= HTML_PRETTIFY;
+
/* filter_style */
if (rb_hash_aref(hash, CSTR2SYM("no_styles")) == Qtrue)
render_flags |= HTML_SKIP_STYLE;
View
3  lib/redcarpet.rb
@@ -77,6 +77,7 @@ def to_html(*_dummy)
:fenced_code => :fenced_code_blocks,
:filter_html => :filter_html,
:hard_wrap => :hard_wrap,
+ :prettify => :prettify,
:lax_htmlblock => :lax_spacing,
:no_image => :no_images,
:no_intraemphasis => :no_intra_emphasis,
@@ -96,7 +97,7 @@ def to_html(*_dummy)
}
RENDERER_OPTIONS = [:filter_html, :no_images, :no_links, :no_styles,
- :safe_links_only, :with_toc_data, :hard_wrap, :xhtml]
+ :safe_links_only, :with_toc_data, :hard_wrap, :prettify, :xhtml]
def rename_extensions(exts)
exts.map do |old_name|
View
6 test/redcarpet_test.rb
@@ -326,6 +326,12 @@ def test_that_fenced_flag_works_without_space
assert !out.include?("<pre><code>")
end
+ def test_that_prettify_works
+ text = "foo\nbar\n```\nsome\ncode\n```\nbaz"
+ out = Redcarpet::Markdown.new(Redcarpet::Render::HTML.new(:prettify => true), :fenced_code_blocks => true).render(text)
+ assert !out.include?("<pre><code class=\"prettyprint\">")
+ end
+
def test_that_headers_are_linkable
markdown = @markdown.render('### Hello [GitHub](http://github.com)')
html_equal "<h3>Hello <a href=\"http://github.com\">GitHub</a></h3>", markdown
Something went wrong with that request. Please try again.