代码语法高亮
rhighlightjs
官网:
http://softwaremaniacs.org/soft/highlight/en/
使用起来比较简单
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介绍
http://www.w3.org/People/Raggett/tidy/
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