imathis / octopress

Octopress is an obsessively designed framework for Jekyll blogging. It’s easy to configure and easy to deploy. Sweet huh?
http://github.com/imathis/octopress
9.32k stars 2.62k forks source link

Pygments plug-in can't render Swift code #1715

Open Goles opened 9 years ago

Goles commented 9 years ago

Hey there, I have a blogpost with a code block that looks like this gist, (because it's in markdown I couldn't embed it to the Issue text).

I know that Pygments does support Swift, and as far as I know, I'm using the most updated Pygments version... however when I try to rake generate my blog I get:


/Users/a_user/Dropbox/Private/octopress/plugins/pygments_code.rb:27:in `rescue in pygments': Pygments can't parse unknown language: swift. (RuntimeError)
        from /Users/a_user/Dropbox/Private/octopress/plugins/pygments_code.rb:24:in `pygments'
        from /Users/a_user/Dropbox/Private/octopress/plugins/pygments_code.rb:14:in `highlight'
        from /Users/a_user/Dropbox/Private/octopress/plugins/backtick_code_block.rb:37:in `block in render_code_block'
        from /Users/a_user/Dropbox/Private/octopress/plugins/backtick_code_block.rb:13:in `gsub'
        from /Users/a_user/Dropbox/Private/octopress/plugins/backtick_code_block.rb:13:in `render_code_block'
        from /Users/a_user/Dropbox/Private/octopress/plugins/octopress_filters.rb:12:in `pre_filter'
        from /Users/a_user/Dropbox/Private/octopress/plugins/octopress_filters.rb:28:in `pre_render'
        from /Users/a_user/Dropbox/Private/octopress/plugins/post_filters.rb:112:in `block in pre_render'
        from /Users/a_user/Dropbox/Private/octopress/plugins/post_filters.rb:111:in `each'
        from /Users/a_user/Dropbox/Private/octopress/plugins/post_filters.rb:111:in `pre_render'
        from /Users/a_user/Dropbox/Private/octopress/plugins/post_filters.rb:166:in `do_layout'
        from /Users/a_user/.rvm/gems/ruby-head/gems/jekyll-0.12.0/lib/jekyll/post.rb:195:in `render'
        from /Users/a_user/.rvm/gems/ruby-head/gems/jekyll-0.12.0/lib/jekyll/site.rb:200:in `block in render'
        from /Users/a_user/.rvm/gems/ruby-head/gems/jekyll-0.12.0/lib/jekyll/site.rb:199:in `each'
        from /Users/a_user/.rvm/gems/ruby-head/gems/jekyll-0.12.0/lib/jekyll/site.rb:199:in `render'
        from /Users/a_user/.rvm/gems/ruby-head/gems/jekyll-0.12.0/lib/jekyll/site.rb:41:in `process'
        from /Users/a_user/.rvm/gems/ruby-head/gems/jekyll-0.12.0/bin/jekyll:264:in `<top (required)>'
        from /Users/a_user/.rvm/gems/ruby-head/bin/jekyll:23:in `load'
        from /Users/a_user/.rvm/gems/ruby-head/bin/jekyll:23:in `<main>'

I tried fixing pygments_code.rb myself and actually forcing it to colorize Swift, with no luck... could anyone help me out here?

imathis commented 9 years ago

This is an issue that should be tracked on the Pygments issue tracker

Goles commented 9 years ago

@imathis Are you sure about this? I got pygments installed in my machine and made a test project,

from pygments import highlight
from pygments.lexers import SwiftLexer
from pygments.formatters import HtmlFormatter

code = 'var letterCounter = 0'
print highlight(code, :lexer => 'swift', :formatter => 'html', :options => {:encoding => 'utf-8', :startinline => true})

That outputted perfect HTML, I think that it could be related to the way that pygments is imported in pygments_code.rb, maybe it's outdated?

I even tried to run Pygments.get_lexer_by_name() from pygments_code.rb by adding a line like:

    print Pygments.get_lexer_by_name("python")

and got the following error when performing a rake generate

/Users/a_user/Dropbox/Private/octopress/plugins/pygments_code.rb:19:in `pygments': undefined method `get_lexer_by_name' for Pygments:Module (NoMethodError)

Isn't this some kind of namespacing issue in the pygments_code plug-in?

imathis commented 9 years ago

Man, I'm sorry. I somehow misread your comment as "I know Pygments doesn't support Swift". Thanks for your followup. I'll look into this.

Goles commented 9 years ago

@imathis no problem!!

Thanks, and let me know if I can be of further help :+1:

parkr commented 9 years ago

pygments.rb 0.6.0 has Swift support. You'll have to upgrade.

Goles commented 9 years ago

Maybe I have to update octopress... I tried modifying the Gemfile in my blog and changing the pygments line, it previously looked like this:

source "https://rubygems.org"

group :development do
  gem 'rake', '~> 0.9'
  gem 'jekyll', '~> 0.12'
  gem 'rdiscount', '~> 2.0.7'
  gem 'pygments.rb', '~> 0.3.4'
  gem 'RedCloth', '~> 4.2.9'
  gem 'haml', '~> 3.1.7'
  gem 'compass', '~> 0.12.2'
  gem 'sass', '~> 3.2'
  gem 'sass-globbing', '~> 1.0.0'
  gem 'rubypants', '~> 0.2.0'
  gem 'rb-fsevent', '~> 0.9'
  gem 'stringex', '~> 1.4.0'
  gem 'liquid', '~> 2.3.0'
end

gem 'sinatra', '~> 1.4.2'

I changed the pygments line to

  gem 'pygments.rb', '~> 0.6.0'

but got the following error when performing a bundle update pygments.rb

bundle update pygments.rb                                                                                   
Fetching gem metadata from https://rubygems.org/.........
Resolving dependencies...
Bundler could not find compatible versions for gem "pygments.rb":
  In Gemfile:
    jekyll (~> 0.12) ruby depends on
      pygments.rb (~> 0.3.2) ruby

    pygments.rb (0.6.0)
parkr commented 9 years ago

Yeah, your octopress is old :( the latest master has a bunch of changes to the Gemfile and plugin fixes.

Goles commented 9 years ago

@parkr Is there a simple way to update Octopress?

parkr commented 9 years ago

Not for Octopress 2.0 :( i'd recommend copying the gemfile and _plugins directory and see where that gets you. Octopress 3 will be much easier to upgrade!

jeremy-w commented 8 years ago

In case anyone runs into this error message but finds themselves on the latest All The Things: Verify your current Python is CPython. I got the "unknown language" error for all languages, and the resolution was to pyenv local system (rather than my standard default of Pypy).