Rouge gem not showing line breaks in formatted code?

别说谁变了你拦得住时间么 提交于 2019-12-13 03:26:55

问题


I have followed minimal examples using the rouge gem here (i.e. the documentation), as well as here and here.

Everything working, except that line breaks in the code aren't appearing.

To be explicit, code like this

def plus_two(x)
  x + 2
end

has the colours highlighted correctly, but is rendered across one line, like so

def plus_two(x) x + 2 end 

How can I make it render with the line breaks (just as in the code file)


回答1:


Change

require 'rouge'
source = "def plus_two(x)\n  x + 2\nend"
formatter = Rouge::Formatters::HTML.new
lexer = Rouge::Lexers::R.new
@code = formatter.format(lexer.lex(source)) 

to

require 'rouge'
source = "def plus_two(x)\n  x + 2\nend"
formatter = Rouge::Formatters::HTML.new
formatter = Rouge::Formatters::HTMLLinewise.new(formatter, class: 'line-%i')
lexer = Rouge::Lexers::R.new
@code = formatter.format(lexer.lex(source))  

More formatting options here



来源:https://stackoverflow.com/questions/57612022/rouge-gem-not-showing-line-breaks-in-formatted-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!