ultraviolet 0.10.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (178) hide show
  1. data/.gitmodules +3 -0
  2. data/Gemfile +10 -0
  3. data/Gemfile.lock +28 -0
  4. data/Rakefile +18 -47
  5. data/Readme.md +89 -0
  6. data/bin/theme2latexrender +0 -0
  7. data/bin/theme2xhtmlrender +0 -0
  8. data/bin/uv +0 -0
  9. data/lib/uv.rb +78 -75
  10. data/lib/uv/render_processor.rb +128 -119
  11. data/lib/uv/utility.rb +62 -62
  12. data/lib/uv/version.rb +3 -0
  13. data/spec/spec_helper.rb +2 -0
  14. data/spec/ultraviolet_spec.rb +13 -0
  15. data/ultraviolet.gemspec +15 -0
  16. metadata +123 -271
  17. data/History.txt +0 -25
  18. data/Manifest.txt +0 -233
  19. data/README.txt +0 -52
  20. data/syntax/actionscript.syntax +0 -97
  21. data/syntax/active4d.syntax +0 -276
  22. data/syntax/active4d_html.syntax +0 -311
  23. data/syntax/active4d_ini.syntax +0 -50
  24. data/syntax/active4d_library.syntax +0 -21
  25. data/syntax/ada.syntax +0 -33
  26. data/syntax/antlr.syntax +0 -151
  27. data/syntax/apache.syntax +0 -191
  28. data/syntax/applescript.syntax +0 -384
  29. data/syntax/asp.syntax +0 -70
  30. data/syntax/asp_vb.net.syntax +0 -129
  31. data/syntax/bibtex.syntax +0 -151
  32. data/syntax/blog_html.syntax +0 -41
  33. data/syntax/blog_markdown.syntax +0 -42
  34. data/syntax/blog_text.syntax +0 -27
  35. data/syntax/blog_textile.syntax +0 -27
  36. data/syntax/build.syntax +0 -53
  37. data/syntax/bulletin_board.syntax +0 -287
  38. data/syntax/c++.syntax +0 -109
  39. data/syntax/c.syntax +0 -326
  40. data/syntax/cake.syntax +0 -55
  41. data/syntax/camlp4.syntax +0 -36
  42. data/syntax/cm.syntax +0 -32
  43. data/syntax/coldfusion.syntax +0 -119
  44. data/syntax/context_free.syntax +0 -176
  45. data/syntax/cs.syntax +0 -59
  46. data/syntax/css.syntax +0 -195
  47. data/syntax/css_experimental.syntax +0 -1925
  48. data/syntax/csv.syntax +0 -68
  49. data/syntax/d.syntax +0 -142
  50. data/syntax/diff.syntax +0 -81
  51. data/syntax/dokuwiki.syntax +0 -204
  52. data/syntax/dot.syntax +0 -47
  53. data/syntax/doxygen.syntax +0 -43
  54. data/syntax/dylan.syntax +0 -62
  55. data/syntax/eiffel.syntax +0 -78
  56. data/syntax/erlang.syntax +0 -922
  57. data/syntax/f-script.syntax +0 -80
  58. data/syntax/fortran.syntax +0 -141
  59. data/syntax/fxscript.syntax +0 -142
  60. data/syntax/greasemonkey.syntax +0 -34
  61. data/syntax/gri.syntax +0 -83
  62. data/syntax/groovy.syntax +0 -191
  63. data/syntax/gtd.syntax +0 -22
  64. data/syntax/gtdalt.syntax +0 -143
  65. data/syntax/haml.syntax +0 -88
  66. data/syntax/haskell.syntax +0 -88
  67. data/syntax/html-asp.syntax +0 -27
  68. data/syntax/html.syntax +0 -362
  69. data/syntax/html_django.syntax +0 -36
  70. data/syntax/html_for_asp.net.syntax +0 -424
  71. data/syntax/html_mason.syntax +0 -119
  72. data/syntax/html_rails.syntax +0 -40
  73. data/syntax/html_tcl.syntax +0 -26
  74. data/syntax/icalendar.syntax +0 -32
  75. data/syntax/inform.syntax +0 -48
  76. data/syntax/ini.syntax +0 -55
  77. data/syntax/installer_distribution_script.syntax +0 -77
  78. data/syntax/io.syntax +0 -81
  79. data/syntax/java.syntax +0 -211
  80. data/syntax/javaproperties.syntax +0 -20
  81. data/syntax/javascript.syntax +0 -256
  82. data/syntax/javascript_+_prototype.syntax +0 -72
  83. data/syntax/javascript_+_prototype_bracketed.syntax +0 -140
  84. data/syntax/jquery_javascript.syntax +0 -114
  85. data/syntax/json.syntax +0 -136
  86. data/syntax/languagedefinition.syntax +0 -708
  87. data/syntax/latex.syntax +0 -566
  88. data/syntax/latex_beamer.syntax +0 -41
  89. data/syntax/latex_log.syntax +0 -50
  90. data/syntax/latex_memoir.syntax +0 -64
  91. data/syntax/lexflex.syntax +0 -219
  92. data/syntax/lighttpd.syntax +0 -54
  93. data/syntax/lilypond.syntax +0 -492
  94. data/syntax/lisp.syntax +0 -61
  95. data/syntax/literate_haskell.syntax +0 -24
  96. data/syntax/logo.syntax +0 -29
  97. data/syntax/logtalk.syntax +0 -152
  98. data/syntax/lua.syntax +0 -86
  99. data/syntax/m.syntax +0 -142
  100. data/syntax/macports_portfile.syntax +0 -163
  101. data/syntax/mail.syntax +0 -118
  102. data/syntax/makefile.syntax +0 -36
  103. data/syntax/man.syntax +0 -17
  104. data/syntax/markdown.syntax +0 -543
  105. data/syntax/mediawiki.syntax +0 -567
  106. data/syntax/mel.syntax +0 -92
  107. data/syntax/mips.syntax +0 -66
  108. data/syntax/mod_perl.syntax +0 -50
  109. data/syntax/modula-3.syntax +0 -47
  110. data/syntax/moinmoin.syntax +0 -189
  111. data/syntax/mootools.syntax +0 -572
  112. data/syntax/movable_type.syntax +0 -162
  113. data/syntax/multimarkdown.syntax +0 -39
  114. data/syntax/objective-c++.syntax +0 -18
  115. data/syntax/objective-c.syntax +0 -233
  116. data/syntax/ocaml.syntax +0 -764
  117. data/syntax/ocamllex.syntax +0 -167
  118. data/syntax/ocamlyacc.syntax +0 -184
  119. data/syntax/opengl.syntax +0 -14
  120. data/syntax/pascal.syntax +0 -77
  121. data/syntax/perl.syntax +0 -1115
  122. data/syntax/php.syntax +0 -1253
  123. data/syntax/plain_text.syntax +0 -32
  124. data/syntax/pmwiki.syntax +0 -113
  125. data/syntax/postscript.syntax +0 -114
  126. data/syntax/processing.syntax +0 -106
  127. data/syntax/prolog.syntax +0 -40
  128. data/syntax/property_list.syntax +0 -635
  129. data/syntax/python.syntax +0 -868
  130. data/syntax/python_django.syntax +0 -21
  131. data/syntax/qmake_project.syntax +0 -114
  132. data/syntax/qt_c++.syntax +0 -26
  133. data/syntax/quake3_config.syntax +0 -32
  134. data/syntax/r.syntax +0 -81
  135. data/syntax/r_console.syntax +0 -16
  136. data/syntax/ragel.syntax +0 -201
  137. data/syntax/rd_r_documentation.syntax +0 -91
  138. data/syntax/regexp.syntax +0 -50
  139. data/syntax/regular_expressions_oniguruma.syntax +0 -107
  140. data/syntax/regular_expressions_python.syntax +0 -109
  141. data/syntax/release_notes.syntax +0 -46
  142. data/syntax/remind.syntax +0 -253
  143. data/syntax/restructuredtext.syntax +0 -250
  144. data/syntax/rez.syntax +0 -80
  145. data/syntax/ruby.syntax +0 -1035
  146. data/syntax/ruby_experimental.syntax +0 -145
  147. data/syntax/ruby_on_rails.syntax +0 -88
  148. data/syntax/s5.syntax +0 -69
  149. data/syntax/scheme.syntax +0 -347
  150. data/syntax/scilab.syntax +0 -41
  151. data/syntax/setext.syntax +0 -147
  152. data/syntax/shell-unix-generic.syntax +0 -384
  153. data/syntax/slate.syntax +0 -149
  154. data/syntax/smarty.syntax +0 -63
  155. data/syntax/sql.syntax +0 -237
  156. data/syntax/sql_rails.syntax +0 -18
  157. data/syntax/ssh-config.syntax +0 -33
  158. data/syntax/standard_ml.syntax +0 -121
  159. data/syntax/strings_file.syntax +0 -39
  160. data/syntax/subversion_commit_message.syntax +0 -36
  161. data/syntax/sweave.syntax +0 -84
  162. data/syntax/swig.syntax +0 -57
  163. data/syntax/tcl.syntax +0 -152
  164. data/syntax/template_toolkit.syntax +0 -121
  165. data/syntax/tex.syntax +0 -86
  166. data/syntax/tex_math.syntax +0 -49
  167. data/syntax/textile.syntax +0 -215
  168. data/syntax/tsv.syntax +0 -50
  169. data/syntax/twiki.syntax +0 -241
  170. data/syntax/txt2tags.syntax +0 -79
  171. data/syntax/vectorscript.syntax +0 -57
  172. data/syntax/xhtml_1.0.syntax +0 -4027
  173. data/syntax/xml.syntax +0 -180
  174. data/syntax/xml_strict.syntax +0 -92
  175. data/syntax/xsl.syntax +0 -60
  176. data/syntax/yaml.syntax +0 -160
  177. data/syntax/yui_javascript.syntax +0 -176
  178. data/test/test_uv.rb +0 -0
@@ -0,0 +1,3 @@
1
+ [submodule "_pages"]
2
+ path = _pages
3
+ url = git@github.com:grosser/ultraviolet.git
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source :rubygems
2
+
3
+ gem 'oniguruma', :platform => :ruby_18
4
+ gem 'textpow', '>=1.3'
5
+
6
+ group :dev do
7
+ gem 'rake'
8
+ gem 'rspec'
9
+ gem 'rack'
10
+ end
@@ -0,0 +1,28 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ oniguruma (1.1.0)
6
+ plist (3.1.0)
7
+ rack (1.4.1)
8
+ rake (0.9.2.2)
9
+ rspec (2.11.0)
10
+ rspec-core (~> 2.11.0)
11
+ rspec-expectations (~> 2.11.0)
12
+ rspec-mocks (~> 2.11.0)
13
+ rspec-core (2.11.1)
14
+ rspec-expectations (2.11.2)
15
+ diff-lcs (~> 1.1.3)
16
+ rspec-mocks (2.11.2)
17
+ textpow (1.3.0)
18
+ plist (>= 3.0.1)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ oniguruma
25
+ rack
26
+ rake
27
+ rspec
28
+ textpow (>= 1.3)
data/Rakefile CHANGED
@@ -1,51 +1,22 @@
1
- require 'rubygems'
2
- require 'fileutils'
1
+ require "bundler/gem_tasks"
3
2
 
4
- rubyforge_name = "ultraviolet"
3
+ task :default do
4
+ sh "rspec spec/"
5
+ end
6
+
7
+ # extracted from https://github.com/grosser/project_template
8
+ rule /^version:bump:.*/ do |t|
9
+ sh "git status | grep 'nothing to commit'" # ensure we are not dirty
10
+ index = ['major', 'minor','patch'].index(t.name.split(':').last)
11
+ file = 'lib/uv/version.rb'
5
12
 
6
- begin
7
- require 'hoe'
8
-
9
- class Hoe
10
- # Dirty hack to eliminate Hoe from gem dependencies
11
- def extra_deps
12
- @extra_deps.delete_if{ |x| x.first == 'hoe' }
13
- end
14
- end
15
-
16
- version = /^== *(\d+\.\d+\.\d+)/.match( File.read( 'History.txt' ) )[1]
17
-
18
- h = Hoe.new('ultraviolet', version) do |p|
19
- p.rubyforge_name = 'ultraviolet'
20
- p.author = ['Dizan Vasquez']
21
- p.email = ['dichodaemon@gmail.com']
22
- p.email = 'dichodaemon@gmail.com'
23
- p.summary = 'Syntax highlighting engine'
24
- p.description = p.paragraphs_of('README.txt', 1 ).join('\n\n')
25
- p.url = 'http://ultraviolet.rubyforge.org'
26
- p.rdoc_pattern = /^(lib|bin|ext)|txt$/
27
- p.changes = p.paragraphs_of('History.txt', 0).join("\n\n")
28
- p.extra_deps << ['textpow', '>= 0.10.0']
29
- p.clean_globs = ["manual/*"]
30
- end
31
-
32
- desc 'Create MaMa documentation'
33
- task :mama => :clean do
34
- system "mm -c -t refresh -o manual mm/manual.mm"
35
- end
36
-
37
- desc 'Publish MaMa documentation to RubyForge'
38
- task :mama_publish => [:clean, :mama] do
39
- config = YAML.load(File.read(File.expand_path("~/.rubyforge/user-config.yml")))
40
- host = "#{config["username"]}@rubyforge.org"
41
- remote_dir = "/var/www/gforge-projects/#{h.rubyforge_name}"
42
- local_dir = 'manual'
43
- system "rsync -av --delete #{local_dir}/ #{host}:#{remote_dir}"
44
- end
13
+ version_file = File.read(file)
14
+ old_version, *version_parts = version_file.match(/(\d+)\.(\d+)\.(\d+)/).to_a
15
+ version_parts[index] = version_parts[index].to_i + 1
16
+ version_parts[2] = 0 if index < 2 # remove patch for minor
17
+ version_parts[1] = 0 if index < 1 # remove minor for major
18
+ new_version = version_parts * '.'
19
+ File.open(file,'w'){|f| f.write(version_file.sub(old_version, new_version)) }
45
20
 
46
- rescue LoadError => e
47
- desc 'Run the test suite.'
48
- task :test do
49
- system "ruby -Ibin:lib:test test_#{rubyforge_name}.rb"
50
- end
21
+ sh "bundle && git add #{file} Gemfile.lock && git commit -m 'bump version to #{new_version}'"
51
22
  end
@@ -0,0 +1,89 @@
1
+ Ultraviolet is a html syntax highlighting library.<br/>
2
+ It uses [Textpow](https://github.com/grosser/textpow) to supports more than 60 programming languages.
3
+
4
+ # INSTALL
5
+
6
+ gem install ultraviolet
7
+
8
+ Ruby 1.8: [Install onigumura](https://github.com/grosser/textpow#ruby-18)
9
+
10
+ # [Demo](http://grosser.github.com/ultraviolet)
11
+
12
+ # Usage
13
+
14
+ ### Commandline interface
15
+
16
+ uv -l syntax # Listing available syntaxes:
17
+ uv -l themes # Listing available themes:
18
+ uv lib/uv.rb # Letting to guess the parameters
19
+ uv -s yaml -t espresso_libre syntax/ini.syntax Specifying a syntax and a theme
20
+
21
+ # Output results to `index.html` and copy required files (/eg/ css):
22
+ uv -c . syntax/ini.syntax > index.html
23
+
24
+ # Producing output in latex and converting to pdf:
25
+ uv -h -o latex lib/uv.rb > uv.tex
26
+ pdflatex uv.tex
27
+
28
+ ### Ruby interface
29
+
30
+ # List of available syntaxes
31
+ puts Uv.syntaxes.join( ", " )
32
+
33
+ # Listing available themes
34
+ puts Uv.themes.join( ", " )
35
+
36
+ # Parsing a css string. Produce xhtml output, with line numbers using amy theme
37
+ result = Uv.parse( text, "xhtml", "css", true, "amy")
38
+
39
+ # Output copy required files for `xhtml` format to directory `site`
40
+ Uv.copy_files "xhtml", "site"
41
+
42
+ ### Examples
43
+ - [Macaronic markup engine](http://mama.rubyforge.org)
44
+ - [Radiograph rails plugin](http://agilewebdevelopment.com/plugins/radiograph)
45
+ - [SimpleHighlight plugin for SimpleLog](http://www.daikini.com/past/2007/6/14/simplehighlight_syntax_highlighting_for_simplelog)
46
+
47
+ # TODO
48
+ - less crazy Uv.parse interface -> options hash
49
+ - replace them gallery example with some nice example code
50
+
51
+ # Maintainer info
52
+
53
+ ### Updating demo
54
+
55
+ cd _pages
56
+ bundle exec ruby generate.rb
57
+ bundle exec rackup
58
+
59
+ open http://localhost:9292
60
+
61
+ ### Updating theme files
62
+
63
+ Currently only XHTML is "officially" supported by ultraviolet, nevertheless,
64
+ experimental latex output support already exists and other output formats can
65
+ be included in the future. This is important because theme conversion is format
66
+ dependent. For xhmtl, the command is `theme2xhtmlrender` as shown in the
67
+ following example.
68
+
69
+ theme2xhtmlrender Amy.tmTheme
70
+
71
+ For latex, the command `theme2latexrender` is used instead.
72
+
73
+ theme2latexrender Amy.tmTheme
74
+
75
+ Authors
76
+ =======
77
+
78
+ Original author [Dizan Vasquez](https://github.com/dichodaemon).
79
+
80
+ ### [Contributors](https://github.com/grosser/ultraviolet/contributors)
81
+ - [Chris Hoffman](https://github.com/cehoffman)
82
+ - [Spox](https://github.com/spox)
83
+ - [Richard Ramsden](https://github.com/rramsden)
84
+ - [Humza](https://github.com/secondplanet)
85
+ - [Harry Brundage](https://github.com/hornairs)
86
+
87
+ [Michael Grosser](http://grosser.it)<br/>
88
+ michael@grosser.it<br/>
89
+ License: MIT<br/>
File without changes
File without changes
data/bin/uv CHANGED
File without changes
data/lib/uv.rb CHANGED
@@ -1,89 +1,92 @@
1
+ #encoding: ascii-8bit
1
2
  require 'fileutils'
2
3
  require 'textpow'
3
4
  require 'uv/render_processor.rb'
5
+ require "uv/version"
4
6
 
5
7
 
6
8
  module Uv
9
+ class << self
10
+ attr_accessor :render_path, :theme_path, :syntax_path, :default_style, :syntaxes
11
+ end
7
12
 
8
- def Uv.path
9
- result = []
10
- result << File.join(File.dirname(__FILE__), ".." )
11
- end
12
-
13
- def Uv.copy_files output, output_dir
14
- Uv.path.each do |dir|
15
- dir_name = File.join( dir, "render", output, "files" )
16
- FileUtils.cp_r( Dir.glob(File.join( dir_name, "." )), output_dir ) if File.exists?( dir_name )
17
- end
18
- end
13
+ self.syntax_path = Textpow.syntax_path
14
+ self.render_path = File.join(File.dirname(__FILE__), '..', 'render')
15
+ self.theme_path = File.join(render_path, 'xhtml', 'files', 'css')
16
+ self.default_style = 'mac_classic'
17
+ self.syntaxes = {}
18
+
19
+ def self.path
20
+ result = []
21
+ result << File.join(File.dirname(__FILE__), ".." )
22
+ end
19
23
 
20
- def Uv.init_syntaxes
21
- @syntaxes = {}
22
- Dir.glob( File.join(File.dirname(__FILE__), '..', 'syntax', '*.syntax') ).each do |f|
23
- @syntaxes[File.basename(f, '.syntax')] = Textpow::SyntaxNode.load( f )
24
- end
25
- end
24
+ def self.syntax_node_for(syntax)
25
+ if @syntaxes.key?(syntax)
26
+ @syntaxes[syntax]
27
+ else
28
+ @syntaxes[syntax] = Textpow.syntax(syntax) || raise(ArgumentError, "No syntax found for #{syntax}")
29
+ end
30
+ end
26
31
 
27
- def Uv.syntaxes
28
- Dir.glob( File.join(File.dirname(__FILE__), '..', 'syntax', '*.syntax') ).collect do |f|
29
- File.basename(f, '.syntax')
30
- end
31
- end
32
-
33
- def Uv.themes
34
- Dir.glob( File.join(File.dirname(__FILE__), '..', 'render', 'xhtml', 'files', 'css', '*.css') ).collect do |f|
35
- File.basename(f, '.css')
36
- end
37
- end
32
+ def self.copy_files(output, output_dir)
33
+ Uv.path.each do |dir|
34
+ dir_name = File.join( dir, "render", output, "files" )
35
+ FileUtils.cp_r( Dir.glob(File.join( dir_name, "." )), output_dir ) if File.exists?( dir_name )
36
+ end
37
+ end
38
38
 
39
- def Uv.syntax_for_file file_name
40
- init_syntaxes unless @syntaxes
41
- first_line = ""
42
- File.open( file_name, 'r' ) { |f|
43
- while (first_line = f.readline).strip.size == 0; end
44
- }
45
- result = []
46
- @syntaxes.each do |key, value|
47
- assigned = false
48
- if value.fileTypes
49
- value.fileTypes.each do |t|
50
- if t == File.basename( file_name ) || t == File.extname( file_name )[1..-1]
51
- result << [key, value]
52
- assigned = true
53
- break
54
- end
55
- end
56
- end
57
- unless assigned
58
- if value.firstLineMatch && value.firstLineMatch =~ first_line
59
- result << [key, value]
60
- end
61
- end
62
- end
63
- result
64
- end
65
-
66
- def Uv.parse text, output = "xhtml", syntax_name = nil, line_numbers = false, render_style = "classic", headers = false
67
- init_syntaxes unless @syntaxes
68
- renderer = File.join( File.dirname(__FILE__), '..',"render", output,"#{render_style}.render")
69
- raise( ArgumentError, "Output for #{output} is not yet implemented" ) unless File.exists?(renderer)
70
- css_class = render_style
71
- render_options = YAML.load( File.open( renderer ) )
72
- render_processor = RenderProcessor.new( render_options, line_numbers, headers )
73
- @syntaxes[syntax_name].parse( text, render_processor )
74
- render_processor.string
75
- end
39
+ def self.syntaxes
40
+ Dir.glob( File.join(@syntax_path, '*.syntax') ).collect do |f|
41
+ File.basename(f, '.syntax')
42
+ end
43
+ end
76
44
 
77
- def Uv.debug text, syntax_name
78
- unless @syntaxes
79
- @syntaxes = {}
80
- Dir.glob( File.join(File.dirname(__FILE__), '..', 'syntax', '*.syntax') ).each do |f|
81
- @syntaxes[File.basename(f, '.syntax')] = Textpow::SyntaxNode.load( f )
82
- end
45
+ def self.themes
46
+ Dir.glob( File.join(@theme_path, '*.css') ).collect do |f|
47
+ File.basename(f, '.css')
48
+ end
49
+ end
50
+
51
+ def self.syntax_for_file(file_name)
52
+ init_syntaxes unless @syntaxes
53
+
54
+ # get first non-empty line
55
+ first_line = ""
56
+ File.open( file_name, 'r' ) { |f|
57
+ while (first_line = f.readline).strip.size == 0; end
58
+ }
59
+
60
+
61
+ # find syntax by file-extension
62
+ result = []
63
+ @syntaxes.each do |key, syntax|
64
+ assigned = false
65
+ if syntax.fileTypes
66
+ syntax.fileTypes.each do |t|
67
+ if t == File.basename( file_name ) || t == File.extname( file_name )[1..-1]
68
+ result << [key, syntax]
69
+ assigned = true
70
+ break
71
+ end
72
+ end
73
+ end
74
+ unless assigned
75
+ if syntax.firstLineMatch && syntax.firstLineMatch =~ first_line
76
+ result << [key, syntax]
77
+ end
83
78
  end
84
- processor = Textpow::DebugProcessor.new
79
+ end
80
+ result
81
+ end
85
82
 
86
- @syntaxes[syntax_name].parse( text, processor )
87
- end
83
+ def self.parse(text, output = "xhtml", syntax_name = nil, line_numbers = false, render_style = nil, headers = false)
84
+ RenderProcessor.load(output, render_style, line_numbers, headers) do |processor|
85
+ syntax_node_for(syntax_name).parse(text, processor)
86
+ end.string
87
+ end
88
88
 
89
- end
89
+ def self.debug(text, syntax_name)
90
+ syntax_node_for(syntax_name).parse(text, Textpow::DebugProcessor.new)
91
+ end
92
+ end
@@ -1,131 +1,140 @@
1
1
  require 'cgi'
2
2
 
3
- module Uv
4
-
3
+ module Uv
5
4
 
6
- class RenderProcessor
7
- @@score_manager = Textpow::ScoreManager.new
8
-
9
- attr_reader :string
10
- attr_accessor :escapeHTML
11
-
12
- def initialize render_options, line_numbers = false, headers = true, score_manager = nil
13
- @score_manager = score_manager || @@score_manager
14
- @render_options = render_options
15
- @options = {}
16
- @headers = headers
17
- @line_numbers = line_numbers
18
- @escapeHTML = true
19
- end
20
-
21
- def start_parsing name
22
- @stack = [name]
23
- @string = ""
24
- @line = nil
25
- @line_number = 0
26
- print @render_options["document"]["begin"] if @headers
27
- print @render_options["listing"]["begin"]
28
- # opt = options @stack
29
- # print opt["begin"] if opt
30
- end
31
-
32
- def print string
33
- @string << string
34
- end
35
-
36
- def escape string
37
- if @render_options["filter"]
38
- @escaped = string
39
- @escaped = self.instance_eval( @render_options["filter"] )
40
- @escaped
41
- else
42
- string
43
- end
44
- end
45
-
46
- def open_tag name, position
47
- @stack << name
48
- print escape(@line[@position...position].gsub(/\n|\r/, '')) if position > @position
49
- @position = position
50
- opt = options @stack
51
- print opt["begin"] if opt
5
+ class RenderProcessor
6
+ @@score_manager = Textpow::ScoreManager.new
7
+
8
+ attr_reader :string
9
+ attr_accessor :escapeHTML
10
+
11
+ def self.load(output, style = nil, line_numbers = false, headers = false)
12
+ style ||= Uv.default_style
13
+ renderer = File.join( Uv.render_path, output,"#{style}.render")
14
+ raise( ArgumentError, "Output for #{output} in #{style} style is not yet implemented" ) unless File.exists?(renderer)
15
+ options = YAML.load_file(renderer)
16
+ processor = RenderProcessor.new(options, line_numbers, headers)
17
+ yield processor if block_given?
18
+ processor
19
+ end
20
+
21
+ def initialize render_options, line_numbers = false, headers = true, score_manager = nil
22
+ @score_manager = score_manager || @@score_manager
23
+ @render_options = render_options
24
+ @options = {}
25
+ @headers = headers
26
+ @line_numbers = line_numbers
27
+ @escapeHTML = true
28
+ end
29
+
30
+ def start_parsing name
31
+ @stack = [name]
32
+ @string = ""
33
+ @line = nil
34
+ @line_number = 0
35
+ print @render_options["document"]["begin"] if @headers
36
+ print @render_options["listing"]["begin"]
37
+ # opt = options @stack
38
+ # print opt["begin"] if opt
39
+ end
40
+
41
+ def print string
42
+ @string << string
43
+ end
44
+
45
+ def escape string
46
+ if @render_options["filter"]
47
+ @escaped = string
48
+ @escaped = self.instance_eval( @render_options["filter"] )
49
+ @escaped
50
+ else
51
+ string
52
52
  end
53
-
54
- def close_tag name, position
55
- print escape(@line[@position...position].gsub(/\n|\r/, '')) if position > @position
56
- @position = position
57
- opt = options @stack
58
- print opt["end"] if opt
59
- @stack.pop
53
+ end
54
+
55
+ def open_tag name, position
56
+ @stack << name
57
+ print escape(@line[@position...position].gsub(/\n|\r/, '')) if position > @position
58
+ @position = position
59
+ opt = options @stack
60
+ print opt["begin"] if opt
61
+ end
62
+
63
+ def close_tag name, position
64
+ print escape(@line[@position...position].gsub(/\n|\r/, '')) if position > @position
65
+ @position = position
66
+ opt = options @stack
67
+ print opt["end"] if opt
68
+ @stack.pop
69
+ end
70
+
71
+ def close_line
72
+ stack = @stack[0..-1]
73
+ while stack.size > 1
74
+ opt = options stack
75
+ print opt["end"] if opt
76
+ stack.pop
60
77
  end
61
-
62
- def close_line
63
- stack = @stack[0..-1]
64
- while stack.size > 1
65
- opt = options stack
66
- print opt["end"] if opt
67
- stack.pop
68
- end
78
+ end
79
+
80
+ def open_line
81
+ stack = [@stack.first]
82
+ clone = @stack[1..-1]
83
+ while stack.size < @stack.size
84
+ stack << clone.shift
85
+ opt = options stack
86
+ print opt["begin"] if opt
69
87
  end
70
-
71
- def open_line
72
- stack = [@stack.first]
73
- clone = @stack[1..-1]
74
- while stack.size < @stack.size
75
- stack << clone.shift
76
- opt = options stack
77
- print opt["begin"] if opt
78
- end
88
+ end
89
+
90
+ def new_line line
91
+ if @line
92
+ print escape(@line[@position..-1].gsub(/\n|\r/, ''))
93
+ close_line
94
+ print @render_options["line"]["end"]
95
+ print "\n"
79
96
  end
80
-
81
- def new_line line
82
- if @line
83
- print escape(@line[@position..-1].gsub(/\n|\r/, ''))
84
- close_line
85
- print @render_options["line"]["end"]
86
- print "\n"
87
- end
88
- @position = 0
89
- @line_number += 1
90
- @line = line
91
- print @render_options["line"]["begin"]
92
- if @line_numbers
93
- print @render_options["line-numbers"]["begin"]
94
- print @line_number.to_s.rjust(4).ljust(5)
95
- print @render_options["line-numbers"]["end"]
96
- print " "
97
- end
98
- open_line
97
+ @position = 0
98
+ @line_number += 1
99
+ @line = line
100
+ print @render_options["line"]["begin"]
101
+ if @line_numbers
102
+ print @render_options["line-numbers"]["begin"]
103
+ print @line_number.to_s.rjust(4).ljust(5)
104
+ print @render_options["line-numbers"]["end"]
105
+ print " "
99
106
  end
100
-
101
- def end_parsing name
102
- if @line
103
- print escape(@line[@position..-1].gsub(/\n|\r/, ''))
104
- while @stack.size > 1
105
- opt = options @stack
106
- print opt["end"] if opt
107
- @stack.pop
108
- end
109
- print @render_options["line"]["end"]
110
- print "\n"
111
- end
112
- # opt = options @stack
113
- # print opt["end"] if opt
114
- @stack.pop
115
- print @render_options["listing"]["end"]
116
- print @render_options["document"]["end"] if @headers
107
+ open_line
108
+ end
109
+
110
+ def end_parsing name
111
+ if @line
112
+ print escape(@line[@position..-1].gsub(/\n|\r/, ''))
113
+ while @stack.size > 1
114
+ opt = options @stack
115
+ print opt["end"] if opt
116
+ @stack.pop
117
+ end
118
+ print @render_options["line"]["end"]
119
+ print "\n"
117
120
  end
118
-
119
- def options stack
120
- ref = stack.join ' '
121
- return @options[ref] if @options.has_key? ref
122
-
123
- result = @render_options['tags'].max do |a, b|
124
- @score_manager.score( a['selector'], ref ) <=> @score_manager.score( b['selector'], ref )
125
- end
126
- result = nil if @score_manager.score( result['selector'], ref ) == 0
127
- @options[ref] = result
121
+ # opt = options @stack
122
+ # print opt["end"] if opt
123
+ @stack.pop
124
+ print @render_options["listing"]["end"]
125
+ print @render_options["document"]["end"] if @headers
126
+ end
127
+
128
+ def options stack
129
+ ref = stack.join ' '
130
+ return @options[ref] if @options.has_key? ref
131
+
132
+ result = @render_options['tags'].max do |a, b|
133
+ @score_manager.score( a['selector'], ref ) <=> @score_manager.score( b['selector'], ref )
128
134
  end
129
- end
135
+ result = nil if @score_manager.score( result['selector'], ref ) == 0
136
+ @options[ref] = result
137
+ end
138
+ end
130
139
  end
131
140