html_me 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/html_me +10 -0
- data/lib/embedded_styles_html.rb +106 -0
- data/test/embedded_styles_test.rb +12 -0
- metadata +56 -0
data/bin/html_me
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'syntax/convertors/abstract'
|
3
|
+
|
4
|
+
module Syntax
|
5
|
+
module Convertors
|
6
|
+
|
7
|
+
|
8
|
+
# Converts code to HTML, but embeds format information in span
|
9
|
+
# style attributes for those cases where you have no control
|
10
|
+
# over the CSS in the header. Wordpress, I'm looking at you...
|
11
|
+
class EmbeddedStylesHtml < Abstract
|
12
|
+
# Returns a Hash containing styles rules for each token group
|
13
|
+
#
|
14
|
+
# Gonna try and recreate this:
|
15
|
+
# pre {
|
16
|
+
# background-color: #f1f1f3;
|
17
|
+
# color: #112;
|
18
|
+
# padding: 10px;
|
19
|
+
# font-size: 1.1em;
|
20
|
+
# overflow: auto;
|
21
|
+
# margin: 4px 0px;
|
22
|
+
# width: 95%;
|
23
|
+
# }
|
24
|
+
#
|
25
|
+
# /* Syntax highlighting */
|
26
|
+
# pre .normal {}
|
27
|
+
# pre .comment { color: #005; font-style: italic; }
|
28
|
+
# pre .keyword { color: #A00; font-weight: bold; }
|
29
|
+
# pre .method { color: #077; }
|
30
|
+
# pre .class { color: #074; }
|
31
|
+
# pre .module { color: #050; }
|
32
|
+
# pre .punct { color: #447; font-weight: bold; }
|
33
|
+
# pre .symbol { color: #099; }
|
34
|
+
# pre .string { color: #944; background: #FFE; }
|
35
|
+
# pre .char { color: #F07; }
|
36
|
+
# pre .ident { color: #004; }
|
37
|
+
# pre .constant { color: #07F; }
|
38
|
+
# pre .regex { color: #B66; background: #FEF; }
|
39
|
+
# pre .number { color: #F99; }
|
40
|
+
# pre .attribute { color: #5bb; }
|
41
|
+
# pre .global { color: #7FB; }
|
42
|
+
# pre .expr { color: #227; }
|
43
|
+
# pre .escape { color: #277; }
|
44
|
+
def group_colors
|
45
|
+
{ :normal => '',
|
46
|
+
:comment => 'color: #005; font-style: italic;',
|
47
|
+
:keyword => 'color: #A00; font-weight: bold;',
|
48
|
+
:method => 'color: #077',
|
49
|
+
:class => 'color: #074',
|
50
|
+
:module => 'color: #050',
|
51
|
+
:punct => 'color: #447; font-weight: bold;',
|
52
|
+
:symbol => 'color: #099',
|
53
|
+
:string => 'color: #944; background: #FFE;',
|
54
|
+
:char => 'color: #F07;',
|
55
|
+
:ident => 'color: #004;',
|
56
|
+
:constant => 'color: #07F;',
|
57
|
+
:regex => 'color: #B66; background: #FEF;',
|
58
|
+
:number => 'color: #F99;',
|
59
|
+
:attribute => 'color: #5bb;',
|
60
|
+
:global => 'color: #7FB;',
|
61
|
+
:expr => 'color: #227;',
|
62
|
+
:escape => 'color: #277'}
|
63
|
+
end
|
64
|
+
|
65
|
+
# Converts the given text to HTML, using spans to represent token groups
|
66
|
+
# of any type but <tt>:normal</tt> (which is always unhighlighted). If
|
67
|
+
# +pre+ is +true+, the html is automatically wrapped in pre tags.
|
68
|
+
# Style values are looked up in a hash and embedded directly.
|
69
|
+
def convert( text, pre=true )
|
70
|
+
html = ""
|
71
|
+
html << "<pre>" if pre
|
72
|
+
regions = []
|
73
|
+
@tokenizer.tokenize( text ) do |tok|
|
74
|
+
value = html_escape(tok)
|
75
|
+
case tok.instruction
|
76
|
+
when :region_close then
|
77
|
+
regions.pop
|
78
|
+
html << "</span>"
|
79
|
+
when :region_open then
|
80
|
+
regions.push tok.group
|
81
|
+
html << "<span style=\"#{group_colors[tok.group]}\">#{value}"
|
82
|
+
else
|
83
|
+
if tok.group == ( regions.last || :normal )
|
84
|
+
html << value
|
85
|
+
else
|
86
|
+
html << "<span style=\"#{group_colors[tok.group]}\">#{value}</span>"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
html << "</span>" while regions.pop
|
91
|
+
html << "</pre>" if pre
|
92
|
+
html
|
93
|
+
end
|
94
|
+
|
95
|
+
private
|
96
|
+
|
97
|
+
# Replaces some characters with their corresponding HTML entities.
|
98
|
+
def html_escape( string )
|
99
|
+
string.gsub( /&/, "&" ).
|
100
|
+
gsub( /</, "<" ).
|
101
|
+
gsub( />/, ">" ).
|
102
|
+
gsub( /"/, """ )
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'embedded_styles_html'
|
3
|
+
|
4
|
+
class EmbeddedStylesTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@converter = Syntax::Convertors::EmbeddedStylesHtml.for_syntax 'ruby'
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_html_out
|
10
|
+
puts @converter.convert(File.read(__FILE__))
|
11
|
+
end
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.2
|
3
|
+
specification_version: 1
|
4
|
+
name: html_me
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.1
|
7
|
+
date: 2007-08-14 00:00:00 -04:00
|
8
|
+
summary: Ruby HTML converter for Syntax
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email:
|
12
|
+
homepage:
|
13
|
+
rubyforge_project:
|
14
|
+
description: Uses Syntax to convert Ruby code to html, but embeds colors into span tags, for those times when you don't control the embedded stylesheet
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: false
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- bin/html_me
|
33
|
+
- test/embedded_styles_test.rb
|
34
|
+
- lib/embedded_styles_html.rb
|
35
|
+
test_files: []
|
36
|
+
|
37
|
+
rdoc_options: []
|
38
|
+
|
39
|
+
extra_rdoc_files: []
|
40
|
+
|
41
|
+
executables:
|
42
|
+
- html_me
|
43
|
+
extensions: []
|
44
|
+
|
45
|
+
requirements: []
|
46
|
+
|
47
|
+
dependencies:
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: syntax
|
50
|
+
version_requirement:
|
51
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 0.0.0
|
56
|
+
version:
|