bjeanes-harsh 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ === 0.1.0 / 2009-04-11
2
+
3
+ * Gemified
@@ -0,0 +1,8 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/harsh.rb
6
+ tasks/harsh_tasks.rake
7
+ test/harsh_test.rb
8
+ test/test_helper.rb
@@ -0,0 +1,149 @@
1
+ == Harsh
2
+
3
+ * http://www.carboni.ca/
4
+
5
+ == DESCRIPTION:
6
+
7
+ "Harsh: Another Rails Syntax Highlighter," is just that - it highlights code
8
+ in Rails, much like Radiograph or tm_syntax_highlighting. However, it does it well, _better_.
9
+ Oh, and it also supports Haml, as well as ERb. And it comes with rake tasks.
10
+
11
+ Firstly, it allows block form:
12
+ <% harsh :theme => :dawn do %>
13
+ class Testing
14
+ def initialize(str)
15
+ puts str
16
+ end
17
+ end
18
+ <% end %>
19
+ as well as the form the other plugins offer, which is text as a parameter:
20
+ <% harsh %Q{
21
+ class Testing
22
+ def initialize(str)
23
+ puts str
24
+ end
25
+ end
26
+ }, :theme => :dawn
27
+
28
+ For haml, harsh is implemented as a filter. First, add this to the bottom of your environment.rb:
29
+ Harsh.enable_haml
30
+
31
+ Then, to use harsh in Haml:
32
+ :harsh
33
+ class Foo < Bar
34
+ end
35
+
36
+ However, haml's filters can't take options. So how on earth are we going to customize it to our
37
+ heart's delight? Easily, my friend, fret not! Enter the BCL (Bootleg Configuration Line):
38
+
39
+ :harsh
40
+ #!harsh theme = all_hallows_eve lines=true syntax=css
41
+ h1 {
42
+ float:left;
43
+ clear:left;
44
+ position:relative;
45
+ }
46
+
47
+ It has to be the first line in the filter. You don't need the config line, though. Also, notice
48
+ that you can have spaces between the arguments and the little = sign.
49
+
50
+ Harsh also offers rake tasks for what tm_syntax_highlighting provides in generators,
51
+ and a :harsh as a stylesheet-includer to load all syntax-highlighting files, as such:
52
+ <%= stylesheet_include_tag :harsh %>
53
+
54
+ The rake tasks for setting up your stylesheets are these:
55
+ rake harsh:theme:list # lists available themes
56
+ rake harsh:theme:install[twilight] # installs the twilight theme into /public/stylesheets/harsh/
57
+ rake harsh:theme:install THEME=twilight # also installs the twilight theme (for *csh shells)
58
+ rake harsh:theme:uninstall[twilight] # removes the twilight theme
59
+ rake harsh:theme:uninstall THEME=twilight # also uninstalls the twilight theme (for *csh shells)
60
+
61
+ While purely informative, you can find out the available syntaxes as follows:
62
+ rake harsh:syntax:list
63
+
64
+ == FEATURES/PROBLEMS
65
+
66
+ * Syntax highlighting with text as a parameter AND block form!
67
+ * Rake tasks for setting up your stylesheets!
68
+ * Free hat!
69
+
70
+ == SYNOPSIS
71
+
72
+ You can pass any of these parameters to tweak things:
73
+ <% harsh :theme => :dawn, :lines => true, :format => :css do %>
74
+ h1 {
75
+ float:left;
76
+ clear:left;
77
+ position:relative;
78
+ }
79
+ <% end %>
80
+
81
+ In its default form though, harsh will use the twilight theme, no lines, and
82
+ will expect Ruby code.
83
+
84
+ <% harsh %Q{
85
+ class Testing
86
+ def initialize(str)
87
+ puts str
88
+ end
89
+ end
90
+ }, :theme => :dawn
91
+
92
+ For Haml:
93
+
94
+ :harsh
95
+ #!harsh theme = all_hallows_eve lines=true syntax=css
96
+ h1 {
97
+ float:left;
98
+ clear:left;
99
+ position:relative;
100
+ }
101
+
102
+ The #!harsh line is a configuration line that is optional. Notice that the parameter is "syntax=css",
103
+ not "format" as is the case with the ERb helper.
104
+
105
+ == REQUIREMENTS
106
+
107
+ * oniguruma - a regex library. See installation.
108
+ * ultraviolet gem - this does the highlighting. See installation.
109
+ * haml - if you want to use the haml filter. duh.
110
+
111
+ == INSTALL
112
+
113
+ First, you'll need oniguruma - a regex library. If you don't think you have this, you probably don't.
114
+ To install it, do the following:
115
+ wget http://www.geocities.jp/kosako3/oniguruma/archive/onig-5.8.0.tar.gz
116
+ tar zxvf onig-5.8.0.tar.gz
117
+ cd onig-5.8.0/
118
+ ./configure && make && sudo make install
119
+
120
+ ultraviolet gem - do this AFTER oniguruma!
121
+ sudo gem install ultraviolet
122
+
123
+ Then, install this plugin:
124
+ script/plugin install git://github.com/michaeledgar/harsh.git
125
+
126
+ == LICENSE
127
+
128
+ (The MIT License)
129
+
130
+ Copyright (c) 2009 Michael J. Edgar
131
+
132
+ Permission is hereby granted, free of charge, to any person obtaining
133
+ a copy of this software and associated documentation files (the
134
+ 'Software'), to deal in the Software without restriction, including
135
+ without limitation the rights to use, copy, modify, merge, publish,
136
+ distribute, sublicense, and/or sell copies of the Software, and to
137
+ permit persons to whom the Software is furnished to do so, subject to
138
+ the following conditions:
139
+
140
+ The above copyright notice and this permission notice shall be
141
+ included in all copies or substantial portions of the Software.
142
+
143
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
144
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
145
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
146
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
147
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
148
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
149
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'hoe'
5
+ require File.join(File.dirname(__FILE__), "lib", "harsh")
6
+
7
+
8
+ class Hoe
9
+ # Dirty hack to eliminate Hoe from gem dependencies
10
+ def extra_deps
11
+ @extra_deps.delete_if{ |x| x.first == 'hoe' }
12
+ end
13
+ end
14
+
15
+ Hoe.new('harsh', Harsh::Version * ".") do |p|
16
+ p.author = "Michael J Edgar"
17
+ p.email = "adgar@carboni.ca"
18
+ p.summary = "Harsh: Another Rails Syntax Highlighter"
19
+ p.extra_deps << ['bjeanes-ultraviolet', '>= 0.10.0']
20
+ p.extra_deps << ['textpow', '>= 0.10.0']
21
+ end
22
+
23
+
24
+ desc 'Default: run unit tests.'
25
+ task :default => :test
26
+
27
+ desc 'Test the harsh plugin.'
28
+ Rake::TestTask.new(:test) do |t|
29
+ t.libs << 'lib'
30
+ t.libs << 'test'
31
+ t.pattern = 'test/**/*_test.rb'
32
+ t.verbose = true
33
+ end
34
+
35
+ desc 'Generate documentation for the harsh plugin.'
36
+ Rake::RDocTask.new(:rdoc) do |rdoc|
37
+ rdoc.rdoc_dir = 'rdoc'
38
+ rdoc.title = 'Harsh'
39
+ rdoc.options << '--line-numbers' << '--inline-source'
40
+ rdoc.rdoc_files.include('README')
41
+ rdoc.rdoc_files.include('lib/**/*.rb')
42
+ end
@@ -0,0 +1,74 @@
1
+ # Harsh
2
+ module Harsh
3
+ Version = [0, 1, 0]
4
+
5
+ module ErbMethods
6
+ def syntax_highlight(*args, &block)
7
+ require 'uv'
8
+ default_options = {:format => "ruby", :theme => "twilight", :lines => false}
9
+ case args.size
10
+ when 0
11
+ opts = default_options
12
+ when 1
13
+ if args[0].is_a? String
14
+ text = args[0]
15
+ opts = default_options
16
+ elsif args[0].is_a? Hash
17
+ opts = default_options.merge(args[0])
18
+ else
19
+ raise ArgumentError.new("Must pass either a string or a hash to syntax_highlight()/harsh()")
20
+ end
21
+ when 2
22
+ text = args[0]
23
+ text = text.sub(/\n/,"") if text =~ /^(\s*)\n/
24
+ opts = default_options.merge(args[1])
25
+ end
26
+ if block_given?
27
+ text = capture(&block)
28
+ text = text.sub(/\n/,"") if text =~ /^(\s*)\n/
29
+ concat(Uv.parse( text, "xhtml", opts[:format].to_s, opts[:lines], opts[:theme].to_s))
30
+ ""
31
+ else
32
+ concat(Uv.parse( text, "xhtml", opts[:format].to_s, opts[:lines], opts[:theme].to_s))
33
+ ""
34
+ end
35
+ end
36
+ alias_method :harsh, :syntax_highlight
37
+ end
38
+
39
+ def enable_haml
40
+ eval <<-EOF
41
+ module ::Haml
42
+ module Filters
43
+ module Harsh
44
+ include Haml::Filters::Base
45
+ lazy_require 'uv'
46
+ def initialize(text)
47
+ @text = highlight_text(text)
48
+ end
49
+ def highlight_text(text, opts = {:format => "ruby", :theme => "twilight", :lines => false})
50
+ Uv.parse( text, "xhtml", opts[:format], opts[:lines], opts[:theme])
51
+ end
52
+ def render(text)
53
+ all_lines = text.split(/\\n/)
54
+ if all_lines.first =~ /#!harsh/
55
+ line = all_lines.first
56
+ syntax = (line =~ /syntax\\s*=\\s*([\\w-]+)/) ? $1 : "ruby"
57
+ theme = (line =~ /theme\\s*=\\s*(\\w+)/) ? $1 : "twilight"
58
+ lines = (line =~ /lines\\s*=\\s*(\\w+)/) ? ($1 == 'true') : false
59
+ text = all_lines[1..-1].join("\\n")
60
+ puts({:format => syntax, :theme => theme, :lines => lines}.inspect)
61
+ Haml::Helpers.preserve(highlight_text(text.rstrip, :format => syntax, :theme => theme, :lines => lines))
62
+ else
63
+ Haml::Helpers.preserve(highlight_text(text.rstrip))
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ EOF
70
+ end
71
+ module_function :enable_haml
72
+ end
73
+
74
+
@@ -0,0 +1,39 @@
1
+ namespace :harsh do
2
+ namespace :theme do
3
+ desc 'List available themes'
4
+ task :list do
5
+ puts `uv -l themes`
6
+ end
7
+ desc 'Install a theme - either use install[themename] or install THEME=themename'
8
+ task :install, :theme do |task, args|
9
+ require 'uv'
10
+ args.with_defaults(:theme => ENV['THEME'])
11
+
12
+ unless Uv.themes.include?(args[:theme])
13
+ puts "Error: #{args[:theme]} is not a valid theme."
14
+ next
15
+ end
16
+
17
+ tmpfolder = File.join(File.dirname(__FILE__),["..","tmp"])
18
+ FileUtils.mkdir_p tmpfolder
19
+ FileUtils.mkdir File.join(RAILS_ROOT,"/public/stylesheets/harsh/") unless File.exists?(File.join(RAILS_ROOT,"/public/stylesheets/harsh/"))
20
+ puts "Generating themes..."
21
+ `uv -s ruby --copy-files #{tmpfolder} #{File.dirname(__FILE__)+"/../README.markdown"}`
22
+ puts "Copying #{args[:theme]}.css..."
23
+ FileUtils.cp File.join(tmpfolder, ["css","#{args[:theme]}.css"]), File.join(RAILS_ROOT,["public","stylesheets","harsh"])
24
+ puts "Cleaning up..."
25
+ FileUtils.rm_rf Dir.glob("#{tmpfolder}/*")
26
+ end
27
+ desc 'Uninstall a theme - either use uninstall[themename] or uninstall THEME=themename'
28
+ task :uninstall, :theme do |task, args|
29
+ args.with_defaults(:theme => ENV['THEME'])
30
+ FileUtils.rm_rf File.join(RAILS_ROOT,"/public/stylesheets/harsh/#{args[:theme]}.css")
31
+ end
32
+ end
33
+ namespace :syntax do
34
+ desc 'List available syntaxes'
35
+ task :list do
36
+ puts `uv -l syntaxes`
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,9 @@
1
+
2
+ require 'test/unit'
3
+ require 'test_helper'
4
+ require File.join(File.dirname(__FILE__), ["..","lib","harsh.rb"])
5
+ class HarshTest < ActiveSupport::TestCase
6
+ # Testing this is very tough due to the way that harsh works.
7
+
8
+
9
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bjeanes-harsh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael J Edgar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-11 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bjeanes-ultraviolet
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.10.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: textpow
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.10.0
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: hoe
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.12.1
44
+ version:
45
+ description: "\"Harsh: Another Rails Syntax Highlighter,\" is just that - it highlights code in Rails, much like Radiograph or tm_syntax_highlighting. However, it does it well, _better_. Oh, and it also supports Haml, as well as ERb. And it comes with rake tasks. Firstly, it allows block form: <% harsh :theme => :dawn do %> class Testing def initialize(str) puts str end end <% end %> as well as the form the other plugins offer, which is text as a parameter: <% harsh %Q{ class Testing def initialize(str) puts str end end }, :theme => :dawn For haml, harsh is implemented as a filter. First, add this to the bottom of your environment.rb: Harsh.enable_haml Then, to use harsh in Haml: :harsh class Foo < Bar end However, haml's filters can't take options. So how on earth are we going to customize it to our heart's delight? Easily, my friend, fret not! Enter the BCL (Bootleg Configuration Line): :harsh #!harsh theme = all_hallows_eve lines=true syntax=css h1 { float:left; clear:left; position:relative; } It has to be the first line in the filter. You don't need the config line, though. Also, notice that you can have spaces between the arguments and the little = sign. Harsh also offers rake tasks for what tm_syntax_highlighting provides in generators, and a :harsh as a stylesheet-includer to load all syntax-highlighting files, as such: <%= stylesheet_include_tag :harsh %> The rake tasks for setting up your stylesheets are these: rake harsh:theme:list # lists available themes rake harsh:theme:install[twilight] # installs the twilight theme into /public/stylesheets/harsh/ rake harsh:theme:install THEME=twilight # also installs the twilight theme (for *csh shells) rake harsh:theme:uninstall[twilight] # removes the twilight theme rake harsh:theme:uninstall THEME=twilight # also uninstalls the twilight theme (for *csh shells) While purely informative, you can find out the available syntaxes as follows: rake harsh:syntax:list"
46
+ email: adgar@carboni.ca
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - History.txt
53
+ - Manifest.txt
54
+ - README.txt
55
+ files:
56
+ - History.txt
57
+ - Manifest.txt
58
+ - README.txt
59
+ - Rakefile
60
+ - lib/harsh.rb
61
+ - tasks/harsh_tasks.rake
62
+ - test/harsh_test.rb
63
+ - test/test_helper.rb
64
+ has_rdoc: true
65
+ homepage: http://www.carboni.ca/
66
+ post_install_message:
67
+ rdoc_options:
68
+ - --main
69
+ - README.txt
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ version:
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ version:
84
+ requirements: []
85
+
86
+ rubyforge_project: harsh
87
+ rubygems_version: 1.2.0
88
+ signing_key:
89
+ specification_version: 2
90
+ summary: "Harsh: Another Rails Syntax Highlighter"
91
+ test_files:
92
+ - test/test_helper.rb