New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Rouge with Redcarpet: uninitialized constant Redcarpet (NameError) #140

Closed
xautjzd opened this Issue May 24, 2014 · 20 comments

Comments

Projects
None yet
4 participants
@xautjzd
Copy link

xautjzd commented May 24, 2014

I want to write a ruby program to parse markdown with syntax highlighting, and write my code according to README.md, below is snippet of my code, more,please refer:https://gist.github.com/xautjzd/18188de959d3c3c27577

#!/usr/bin/env ruby

require 'rouge'
require 'rouge/plugins/redcarpet'

class HTML < Redcarpet::Render::HTML
  include Rouge::Plugins::Redcarpet
  # overriding Redcarpet method
  def block_code(code, language)
    Rouge.highlight(code, language || 'text', 'html') 
  end
end

render_options = {
    filter_html:     true,
    # will insert <br /> tags in paragraphs where are newlines 
    # (ignored by default)
    hard_wrap:       true, 
    # hash for extra link options, for example 'nofollow'
    link_attributes: { rel: 'nofollow' }
}

# renderer = Redcarpet::Render::HTML.new(render_options)
renderer = HTML.new(render_options)

markdown = Redcarpet::Markdown.new(renderer, fenced_code_blocks: true, autolink: true)

But when I run it ,I will get the error:

uninitialized constant Redcarpet (NameError)

this error is at class HTML definition.It cann't find the class Redcarpet, why? I have installed rouge via gem install rouge and redcarpet via gem install redcarpet

@jneen

This comment has been minimized.

Copy link
Owner

jneen commented May 24, 2014

You may have to add require 'redcarpet' to the top of your file.

@jneen

This comment has been minimized.

Copy link
Owner

jneen commented May 24, 2014

Also, you might try include Rouge::Plugins::Redcarpet instead of overriding block_code. That module works around a few redcarpet bugs.

I'm glad you're finding rouge useful!

@xautjzd

This comment has been minimized.

Copy link

xautjzd commented May 25, 2014

@jneen thank you. You mean I need to add require 'redcarpet' and require 'rouge/plugins/redcarpet' at the same time? And look like this:

require 'redcarpet'
require 'rouge'
require 'rouge/plugins/redcarpet'

class HTML < Redcarpet::Render::HTML
  include Rouge::Plugins::Redcarpet
end
renderer = HTML.new
...
@Arcovion

This comment has been minimized.

Copy link
Contributor

Arcovion commented May 25, 2014

@xautjzd That code works, I checked. =]

@xautjzd

This comment has been minimized.

Copy link

xautjzd commented May 25, 2014

@Arcovion when add require 'redcarpet', it really works.But cann't highlight the code blocks in the markdown. and cann't parse it correctly.

Below is the code blocks in my markdown file:

#include <stdio.h>
#include <unistd.h>

int main(char **argv, int argc)
{
    int fd;
    FILE *fp;
    char proclnk[255];
    char filepath[255];

    # test.txt为已存在的文件
    fp = fopen("test.txt", "r");
    if (fp != NULL) {
        fd = fileno(fp);
        sprintf(proclnk, "/proc/self/fd/%d", fd);
        readlink(proclnk, filepath, 255);

        printf("fp->fd->filepath: %p->%d->%s\n", fp, fd, filepath);
    }

    return 0;
}

But it parse #include <stdio.h> to <h1>include</h1>.

@Arcovion

This comment has been minimized.

Copy link
Contributor

Arcovion commented May 25, 2014

@xautjzd Here's the code I had, hope it helps:

require 'redcarpet'
require 'rouge'
require 'rouge/plugins/redcarpet'

class HTML < Redcarpet::Render::HTML
  include Rouge::Plugins::Redcarpet
end


render_options = {
  filter_html: true,
  hard_wrap: true,
  link_attributes: {rel: 'nofollow'}
}
engine_options = {
  fenced_code_blocks: true,
  autolink: true
}

renderer = HTML.new render_options
markdown = Redcarpet::Markdown.new renderer, engine_options

puts markdown.render <<EOS
```c
#include <stdio.h>
#include <unistd.h>

int main(char **argv, int argc)
{
    int fd;
    FILE *fp;
    char proclnk[255];
    char filepath[255];

    # test.txt为已存在的文件
    fp = fopen("test.txt", "r");
    if (fp != NULL) {
        fd = fileno(fp);
        sprintf(proclnk, "/proc/self/fd/%d", fd);
        readlink(proclnk, filepath, 255);

        printf("fp->fd->filepath: %p->%d->%s\n", fp, fd, filepath);
    }

    return 0;
}
EOS

Rouge can auto-detect what language is needed but it isn't working on your code for some reason, I think that's a bug. For now you have to specify the language explicitly.

Looking at the readme I think we should add require 'redcarpet' and maybe some more examples regarding this stuff.

@robin850

This comment has been minimized.

Copy link
Contributor

robin850 commented May 25, 2014

Hello there,

@Arcovion : Your example won't work because you forgot to close the backticks of the code block.

Looking at the readme I think we should add require 'redcarpet'

I agree or may be require Redcarpet by default on the plugin side.

@jneen : Any reason why the plugin doesn't require Redcarpet automatically ?

@Arcovion

This comment has been minimized.

Copy link
Contributor

Arcovion commented May 25, 2014

I didn't close it because it would end the github code block which also used three backticks, I don't know how to avoid that. It still technically works though, try it!

Rouge is a syntax highlighter, it's not just designed for Redcarpet; compatibility is provided but adding it as a dependency would be silly and could cause conflicts in other gems that don't want/need Redcarpet.
Edit: I thought you meant Rouge in general; the plugin bit is another matter, though you'd think Redcarpet would already be required in that case (no harm to add it in Rouge though, I agree).

@xautjzd

This comment has been minimized.

Copy link

xautjzd commented May 26, 2014

@Arcovion Thanks very much. I have learned the here document frome the code above. It can parse correctly now when added require 'redcarpet', I thought require 'rouge/plugins/redcarpet' and require 'redcarpet' is the same at past, and redcarpet was integrated to rouge. so require 'rouge/plugins/redcarpet' will include Redcarpet class. Now I got it. Thanks again.

but there is still a qustion: the code blocks doesn't highlight. I write the parsing result to a html file, and open the html via Chrome,but I doesn't highlight. I don't know why.

@jneen

This comment has been minimized.

Copy link
Owner

jneen commented May 26, 2014

@xautjzd you will have to include a CSS stylesheet. You can use any stylesheet designed for pygments, or run rougify style in your shell to print some CSS that will cause it to be highlighted. See rougify help style too.

@jneen

This comment has been minimized.

Copy link
Owner

jneen commented May 26, 2014

Any one of these css files should work too: https://github.com/richleland/pygments-css

edit: although you'll have to change their weird .codehilite class to .highlight

@xautjzd

This comment has been minimized.

Copy link

xautjzd commented May 26, 2014

@jneen thank you. But I find another question: when there is some Chinese comments in the code block. It will get the error:

xxx.rb:38: invalid multibyte char (US-ASCII)
xxx .rb:26: syntax error, unexpected $end, expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END

It cann't parse correctly, when I remove the Chinese comments, It will be OK.

@jneen

This comment has been minimized.

Copy link
Owner

jneen commented May 26, 2014

Ah, well that's a bug. Would you mind opening a separate issue for that, and posting the following:

  • the full code you're using to highlight it
  • the code you're trying to highlight
  • the value of your $LANG environment variable

Thanks in advance for the bug report!

@jneen

This comment has been minimized.

Copy link
Owner

jneen commented May 26, 2014

Also your ruby --version

@xautjzd

This comment has been minimized.

Copy link

xautjzd commented May 26, 2014

@jneen I have read the README file, but still don't understand how to add css to my html file. Can you give me some tips?

Here is my code:

#!/usr/bin/env ruby

require 'redcarpet'
require 'rouge'
require 'rouge/plugins/redcarpet'

class HTML < Redcarpet::Render::HTML
  include Rouge::Plugins::Redcarpet
end


render_options = {
  filter_html: true,
  hard_wrap: true,
  link_attributes: {rel: 'nofollow'}
}
engine_options = {
  fenced_code_blocks: true,
  autolink: true
}

renderer = HTML.new render_options
markdown = Redcarpet::Markdown.new renderer, engine_options

file = File.open "ttt.html", 'w'
file.write markdown.render <<EOF
```c
#include <stdio.h>
#include <unistd.h>

int main(char **argv, int argc)
{
    int fd;
    FILE *fp;
    char proclnk[255];
    char filepath[255];

        # comments
    fp = fopen("test.txt", "r");
    if (fp != NULL) {
        fd = fileno(fp);
        sprintf(proclnk, "/proc/self/fd/%d", fd);
        readlink(proclnk, filepath, 255);

        printf("fp->fd->filepath: %p->%d->%s\n", fp, fd, filepath);
    }

    return 0;
}
\```
EOF
file.close
@jneen

This comment has been minimized.

Copy link
Owner

jneen commented May 26, 2014

If you want it to be a standalone html file, you can paste it into a <style type="text/css"></style> tag. But it is much better to serve a css file separately - that way browsers can cache it independently from your content. I suggest you find some intro material about html and css - that will probably be far more helpful than I can be on the topic :)

@Arcovion

This comment has been minimized.

Copy link
Contributor

Arcovion commented May 26, 2014

Rendering CSS to file is trivial:

ruby -r rouge -e "puts Rouge::Themes::Monokai.render scope: '.highlight'" > syntax.css

There's also :inline_theme but you need to patch Redcarpet's #block_code method to use it.

Edit: @jneen can you mention Theme#find in the readme? Rouge::Theme.find('monokai.sublime') is nicer than using Rouge::Theme::Monokai.mode(:sublime), also maybe show writing it to file for clarity?

@jneen

This comment has been minimized.

Copy link
Owner

jneen commented May 26, 2014

even simpler, rougify style monokai.sublime > syntax.css

@jneen

This comment has been minimized.

Copy link
Owner

jneen commented May 26, 2014

@Arcovion I'm happy to merge a pull request that adds that

@jneen

This comment has been minimized.

Copy link
Owner

jneen commented May 26, 2014

I'm going to close this since the original issue has been resolved - @xautjzd, I'd appreciate more info (in a separate issue) about the encoding problem.

@jneen jneen closed this May 26, 2014

@Arcovion Arcovion referenced this issue May 29, 2014

Merged

Update README #144

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment