hi-lite 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Pavel Pravosud
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ = hi-lite
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2009 Pavel Pravosud. See LICENSE for details.
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "hi-lite"
8
+ gem.summary = %Q{Textmate-like source code highlighting}
9
+ gem.description = %Q{Textmate-like source code highlighting}
10
+ gem.email = "rwz@duckroll.ru"
11
+ gem.homepage = "http://github.com/rwz/hi-lite"
12
+ gem.authors = ["Pavel Pravosud"]
13
+ gem.add_development_dependency "nokogiri"
14
+ gem.add_development_dependency "ultraviolet"
15
+ gem.add_dependency "nokogiri"
16
+ gem.add_dependency "ultraviolet"
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
22
+ end
23
+
24
+ require 'rake/testtask'
25
+ Rake::TestTask.new(:test) do |test|
26
+ test.libs << 'lib' << 'test'
27
+ test.pattern = 'test/**/test_*.rb'
28
+ test.verbose = true
29
+ end
30
+
31
+ begin
32
+ require 'rcov/rcovtask'
33
+ Rcov::RcovTask.new do |test|
34
+ test.libs << 'test'
35
+ test.pattern = 'test/**/test_*.rb'
36
+ test.verbose = true
37
+ end
38
+ rescue LoadError
39
+ task :rcov do
40
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
41
+ end
42
+ end
43
+
44
+ task :test => :check_dependencies
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/rdoctask'
49
+ Rake::RDocTask.new do |rdoc|
50
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "hi-lite #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,67 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{hi-lite}
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Pavel Pravosud"]
12
+ s.date = %q{2009-12-17}
13
+ s.description = %q{Textmate-like source code highlighting}
14
+ s.email = %q{rwz@duckroll.ru}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "hi-lite.gemspec",
27
+ "lib/hi-lite.rb",
28
+ "lib/hi-lite/css_embedder.rb",
29
+ "lib/hi-lite/css_file.rb",
30
+ "lib/hi-lite/parser.rb",
31
+ "lib/hi-lite/uv_ext.rb",
32
+ "test/helper.rb",
33
+ "test/test_hi-lite.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/rwz/hi-lite}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.5}
39
+ s.summary = %q{Textmate-like source code highlighting}
40
+ s.test_files = [
41
+ "test/helper.rb",
42
+ "test/test_hi-lite.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ s.add_development_dependency(%q<nokogiri>, [">= 0"])
51
+ s.add_development_dependency(%q<ultraviolet>, [">= 0"])
52
+ s.add_runtime_dependency(%q<nokogiri>, [">= 0"])
53
+ s.add_runtime_dependency(%q<ultraviolet>, [">= 0"])
54
+ else
55
+ s.add_dependency(%q<nokogiri>, [">= 0"])
56
+ s.add_dependency(%q<ultraviolet>, [">= 0"])
57
+ s.add_dependency(%q<nokogiri>, [">= 0"])
58
+ s.add_dependency(%q<ultraviolet>, [">= 0"])
59
+ end
60
+ else
61
+ s.add_dependency(%q<nokogiri>, [">= 0"])
62
+ s.add_dependency(%q<ultraviolet>, [">= 0"])
63
+ s.add_dependency(%q<nokogiri>, [">= 0"])
64
+ s.add_dependency(%q<ultraviolet>, [">= 0"])
65
+ end
66
+ end
67
+
@@ -0,0 +1,39 @@
1
+ require "hi-lite/parser"
2
+ require "hi-lite/css_file"
3
+ require "hi-lite/css_embedder"
4
+
5
+ module HiLite
6
+ class << self
7
+
8
+ def hilite(code, opts = {})
9
+ opts = {
10
+ :syntax => "ruby",
11
+ :line_numbers => true,
12
+ :theme => "eiffel",
13
+ :headers => false
14
+ }.merge(opts)
15
+ embed_css(parse(code, opts), opts[:theme])
16
+ end
17
+
18
+ def themes_list
19
+ Uv.themes
20
+ end
21
+
22
+ def lang_list
23
+ Uv.syntaxes
24
+ end
25
+
26
+ private
27
+
28
+ def parse(code, opts)
29
+ Parser.parse(code, opts)
30
+ end
31
+
32
+ def embed_css(code, theme)
33
+ css = Uv.theme_css(theme)
34
+ css = CssFile.new(css)
35
+ CssEmbedder.embed(code, css)
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,26 @@
1
+ require "rubygems"
2
+ require "nokogiri"
3
+
4
+ module HiLite
5
+ class CssEmbedder
6
+ class << self
7
+ def embed(code, css)
8
+ code = Nokogiri::HTML.fragment(code)
9
+ css.each_selector do |selector, style|
10
+ code.css(selector).each do |node|
11
+ node["style"] = ((node["style"] || "").split(";") + style.split(";")).join(";")
12
+ end
13
+ end
14
+ remove_class(code)
15
+ code.to_s
16
+ end
17
+
18
+ private
19
+
20
+ def remove_class(node)
21
+ node.remove_attribute("class") if node["style"]
22
+ node.children.each { |c| remove_class(c) }
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,37 @@
1
+ module HiLite
2
+ class CssFile
3
+
4
+ def initialize(csstext)
5
+ @css = {}
6
+ csstext.scan(/([.#:]?[-_\w]+(?:\s+[.#:]?[-_\w]+)?)\s?\{([^{}]*)\}/im).each do |rule|
7
+ selector = rule.first
8
+ selector.strip!
9
+ style = rule.last
10
+ style.strip!
11
+ style.gsub!(/\s+/, ' ')
12
+ style.gsub!(/([:;])\s+/, '\1')
13
+ style = style.split(';').join(';')
14
+ @css.merge!({ selector => style }) if style != ""
15
+ end
16
+ end
17
+
18
+ def selectors
19
+ @css.keys
20
+ end
21
+
22
+ def styles
23
+ @css.values
24
+ end
25
+
26
+ def each_selector
27
+ @css.each do |k,v|
28
+ yield k, v
29
+ end
30
+ end
31
+
32
+ def to_s
33
+ @css.map { |k,v| "#{k}{#{v}}" }.join("\n")
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,12 @@
1
+ $: << File.dirname(__FILE__)
2
+ require "uv_ext"
3
+
4
+ module HiLite
5
+ class Parser
6
+ class << self
7
+ def parse(code, opts)
8
+ Uv.parse(code, "xhtml", opts[:syntax], opts[:line_numbers], opts[:theme], opts[:headers])
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,26 @@
1
+ require "rubygems"
2
+ require "uv"
3
+
4
+ module Uv
5
+ class << self
6
+
7
+ def theme_css(theme_name)
8
+ raise "Could not find theme" unless themes.include? theme_name
9
+ filename = File.join(find_uv_path, "render", "xhtml", "files", "css", "#{theme_name}.css")
10
+ File.read(filename)
11
+ end
12
+
13
+ private
14
+
15
+ def find_uv_path
16
+ uv_path = []
17
+ Gem.path.each do |gem_path|
18
+ uv_path += Dir.glob(File.join(gem_path, "gems", "*")).grep(/ultraviolet/)
19
+ end
20
+ uv_path.uniq!
21
+ raise "Could not find ultraviolet gem path" if uv_path.empty?
22
+ uv_path.first
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'hi-lite'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestHiLite < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hi-lite
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Pavel Pravosud
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-17 00:00:00 +03:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: nokogiri
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: ultraviolet
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: nokogiri
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: ultraviolet
47
+ type: :runtime
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ description: Textmate-like source code highlighting
56
+ email: rwz@duckroll.ru
57
+ executables: []
58
+
59
+ extensions: []
60
+
61
+ extra_rdoc_files:
62
+ - LICENSE
63
+ - README.rdoc
64
+ files:
65
+ - .document
66
+ - .gitignore
67
+ - LICENSE
68
+ - README.rdoc
69
+ - Rakefile
70
+ - VERSION
71
+ - hi-lite.gemspec
72
+ - lib/hi-lite.rb
73
+ - lib/hi-lite/css_embedder.rb
74
+ - lib/hi-lite/css_file.rb
75
+ - lib/hi-lite/parser.rb
76
+ - lib/hi-lite/uv_ext.rb
77
+ - test/helper.rb
78
+ - test/test_hi-lite.rb
79
+ has_rdoc: true
80
+ homepage: http://github.com/rwz/hi-lite
81
+ licenses: []
82
+
83
+ post_install_message:
84
+ rdoc_options:
85
+ - --charset=UTF-8
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: "0"
93
+ version:
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: "0"
99
+ version:
100
+ requirements: []
101
+
102
+ rubyforge_project:
103
+ rubygems_version: 1.3.5
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Textmate-like source code highlighting
107
+ test_files:
108
+ - test/helper.rb
109
+ - test/test_hi-lite.rb