html-me 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt ADDED
@@ -0,0 +1,8 @@
1
+ === 0.0.2 / 2008-02-12
2
+
3
+ * Now an entire entry can be processed using a combination of RedCloth
4
+ and Syntax.
5
+
6
+ === 0.0.1 / 2008-08-14
7
+
8
+ * Creates syntax highlighted code blocks for HTML.
data/Manifest.txt ADDED
@@ -0,0 +1,8 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ bin/html_me
5
+ lib/embedded_styles_html.rb
6
+ lib/html_me.rb
7
+ rakefile.rb
8
+ test/embedded_styles_test.rb
data/README.txt ADDED
@@ -0,0 +1,56 @@
1
+ = html-me
2
+
3
+ * http://html-me.rubyforge.org/
4
+
5
+ == DESCRIPTION:
6
+
7
+ html-me converts text to html for posting in the web. It does this two
8
+ ways. First, it processes the text unsing RedCloth (a textile engine),
9
+ then it finds all of the _pre_ tags and adds syntax highlighting.
10
+
11
+ == FEATURES/PROBLEMS:
12
+
13
+ * The syntax highlighting embeds the styles in the HTML tags. I
14
+ currently don't have access to my blog's stylesheet (damned
15
+ Wordpress) so that's how it needed to be. CSS class names should be
16
+ added soon.
17
+
18
+ == SYNOPSIS:
19
+
20
+ cat some_file.txt | html-me > some_file.html
21
+
22
+ == REQUIREMENTS:
23
+
24
+ * Syntax
25
+ * RedCloth
26
+ * Hpricot
27
+ * An army of trained weasles (ok, not really)
28
+
29
+ == INSTALL:
30
+
31
+ * sudo gem install html-me
32
+
33
+ == LICENSE:
34
+
35
+ (The MIT License)
36
+
37
+ Copyright (c) 2008 Ryan L. Bell
38
+
39
+ Permission is hereby granted, free of charge, to any person obtaining
40
+ a copy of this software and associated documentation files (the
41
+ 'Software'), to deal in the Software without restriction, including
42
+ without limitation the rights to use, copy, modify, merge, publish,
43
+ distribute, sublicense, and/or sell copies of the Software, and to
44
+ permit persons to whom the Software is furnished to do so, subject to
45
+ the following conditions:
46
+
47
+ The above copyright notice and this permission notice shall be
48
+ included in all copies or substantial portions of the Software.
49
+
50
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
51
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
52
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
53
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
54
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
55
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
56
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/bin/html_me ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift(File.join( File.dirname( __FILE__ ), '..', 'lib' ) ).uniq!
3
+
4
+ require 'html_me'
5
+
6
+ require 'embedded_styles_html'
7
+ HtmlMe::Converter.new do
8
+ self.in_source = $stdin
9
+ self.out_source = $stdout
10
+ self.code_converter = Syntax::Convertors::EmbeddedStylesHtml.for_syntax 'ruby'
11
+ end.convert
@@ -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( /&/, "&amp;" ).
100
+ gsub( /</, "&lt;" ).
101
+ gsub( />/, "&gt;" ).
102
+ gsub( /"/, "&quot;" )
103
+ end
104
+ end
105
+ end
106
+ end
data/lib/html_me.rb ADDED
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+
3
+ module HtmlMe
4
+ VERSION = '0.0.2'
5
+
6
+ class Converter
7
+ attr_accessor :in_source, :out_source, :code_converter
8
+
9
+ def initialize( &block )
10
+ instance_eval &block if block_given?
11
+ end
12
+
13
+
14
+ def convert
15
+ require 'redcloth'
16
+ require 'hpricot'
17
+
18
+ doc = RedCloth.new( in_source.read )
19
+ html = Hpricot( doc.to_html )
20
+ ( html/"pre" ).each do | code_block |
21
+ code_block.inner_html = code_converter.convert code_block.inner_html, false # no additional pre tags needed
22
+ end
23
+
24
+ out_source.puts html.to_s
25
+ end
26
+ end
27
+ end
data/rakefile.rb ADDED
@@ -0,0 +1,18 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require File.dirname( __FILE__) + '/lib/html_me.rb'
6
+
7
+ Hoe.new('html-me', HtmlMe::VERSION) do |p|
8
+ p.rubyforge_name = 'html-me'
9
+ p.author = 'Ryan Bell'
10
+ p.email = 'ryan.l.bell@gmail.com'
11
+ p.summary = 'Converts text using RedCloth, and then syntax highlights all pre sections using Syntax'
12
+ p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
13
+ p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
14
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
15
+ p.extra_deps = %w( syntax hpricot RedCloth )
16
+ end
17
+
18
+ # vim: syntax=Ruby
@@ -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,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html-me
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Bell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-02-14 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: syntax
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: hpricot
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: "0"
32
+ version:
33
+ - !ruby/object:Gem::Dependency
34
+ name: RedCloth
35
+ version_requirement:
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "0"
41
+ version:
42
+ - !ruby/object:Gem::Dependency
43
+ name: hoe
44
+ version_requirement:
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 1.5.0
50
+ version:
51
+ description: "== DESCRIPTION: html-me converts text to html for posting in the web. It does this two ways. First, it processes the text unsing RedCloth (a textile engine), then it finds all of the _pre_ tags and adds syntax highlighting. == FEATURES/PROBLEMS: * The syntax highlighting embeds the styles in the HTML tags. I currently don't have access to my blog's stylesheet (damned Wordpress) so that's how it needed to be. CSS class names should be added soon."
52
+ email: ryan.l.bell@gmail.com
53
+ executables:
54
+ - html_me
55
+ extensions: []
56
+
57
+ extra_rdoc_files:
58
+ - History.txt
59
+ - Manifest.txt
60
+ - README.txt
61
+ files:
62
+ - History.txt
63
+ - Manifest.txt
64
+ - README.txt
65
+ - bin/html_me
66
+ - lib/embedded_styles_html.rb
67
+ - lib/html_me.rb
68
+ - rakefile.rb
69
+ - test/embedded_styles_test.rb
70
+ has_rdoc: true
71
+ homepage:
72
+ post_install_message:
73
+ rdoc_options:
74
+ - --main
75
+ - README.txt
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ version:
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ version:
90
+ requirements: []
91
+
92
+ rubyforge_project: html-me
93
+ rubygems_version: 1.0.1
94
+ signing_key:
95
+ specification_version: 2
96
+ summary: Converts text using RedCloth, and then syntax highlights all pre sections using Syntax
97
+ test_files: []
98
+