rails 源码语法高亮 和 代码显示格式化

代码语法高亮

rhighlightjs

官网:

[url]http://softwaremaniacs.org/soft/highlight/en/[/url]

使用起来比较简单

enviroment.rb
config.gem 'romanvbabenko-rhighlightjs', :lib => 'rhighlightjs',
:source => 'http://gems.github.com'


sudo rake gems:install



#your.view
= highlighter :style => 'sunburst', :languages => ["css", "html-xml", "django", "rubyscript", "www"]



html的显示和整理

html tidy
也很简单

tidy介绍
[url]http://www.w3.org/People/Raggett/tidy/[/url]

 config.gem "tidy"


    Tidy.path = '/usr/lib/libtidy.A.dylib'
curl = Monitoring::Index.find(params[:id]).url
@source = Nokogiri::HTML(open(curl)).to_html#.gsub(/\s{4,}/,'')
charset = @source[/meta http-equiv=".+content=".+charset=[^"]+/].split("=").last rescue "utf8"
@source = Iconv.new('UTF-8', charset).iconv(@source) unless charset == "utf8"
opts = {
#:output_xhtml => true,
:wrap => 0,
:char_encoding => charset,
:show_warnings => true,
#:show_body_only => true,
#:doctype => 'omit',
:indent => true, # optional
}
Tidy.open(opts) do |tidy|
@source = tidy.clean(@source)
#puts tidy.errors # array
#puts tidy.diagnostics # array
#puts xml
end



Tidy Ruby Gem
Tidy: http://tidy.sourceforge.net/
Tidy-Ruby: http://rubyforge.org/projects/tidy/

If you're experiencing a Segmentation Fault (Rails app could not start)
see http://github.com/FireRabbit/tidy/tree

List of Tidy-Ruby Options
===========================================
See http://tidy.sourceforge.net/docs/quickref.html

libtidy is the backend of this gem.
Just replace hyphens with underscores.

For example,
show-warnings will be show_warnings
show-body-only will be show_body_only
etc


Installation
===========================================
# Do not use gem install tidy. It is outdated and buggy
# if you've done so already, do gem uninstall tidy and
# follow the directions below

$ git clone git://github.com/FireRabbit/tidy.git
$ cd tidy/
$ sudo ruby setup.rb


#Rails app? make Tidy a dependency environment.rb
#===========================================
Rails::Initializer.run do |config|
...
config.gem "tidy"
end

To install the gem on another machine,
run rake gems:install



#Usage & Examples
#===========================================
require 'tidy'

# This might be required if your app is crashing.
# This is only available in the forked tidy.
# see Installation above
Tidy.fresh_tidy_version = true

Tidy.path = '/usr/lib/tidylib.so'
html = '<html><title>title</title>Body</html>'

Tidy.open(:show_warnings=>true) do |tidy|
puts tidy.options.show_warnings # returns True
xml = tidy.clean(html) # See note below

puts tidy.errors # array
puts tidy.diagnostics # array
puts xml
end



Note: The code above will add a DOCTYPE, html, head, body tags where
appropriate, if they do
not exist.

If you would like to check and clean an HTML SNIPPET, use these options:

    opts = {
:show_warnings => true,
:show_body_only => true,
:doctype => 'omit',
:indent => true, # optional
}

Tidy.open(opts) do |tidy|
puts tidy.clean(html)
diagnostics = tidy.diagnostics
errors = tidy.errors
end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值