lesstile 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,3 @@
1
+ == 0.1 / 2007-12-24
2
+
3
+ * Initial release. Hooray!
data/Manifest.txt ADDED
@@ -0,0 +1,7 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ bin/lesstile
6
+ lib/lesstile.rb
7
+ spec/spec_lesstile.rb
data/README.txt ADDED
@@ -0,0 +1,66 @@
1
+ = lesstile
2
+ by Xavier Shay (http://rhnh.net)
3
+
4
+ == DESCRIPTION:
5
+
6
+ Converts text formatted with an exceedingly simple markup language into XHTML (iron clad guarantee!) - perfect for comments on your blog. Textile isn't good for this because not only does it do too much (do commenters really need subscript?), but it can also output invalid HTML (try a <b> tag over multiple lines...). Whitelisting HTML is another option, but you still need some sort of parsing if you want syntax highlighting.
7
+
8
+ Integrates with CodeRay for sexy syntax highlighting.
9
+
10
+ == SYNOPSIS:
11
+
12
+ comment = <<-EOS
13
+ Wow, this post is awesome. I'd implement it like this:
14
+
15
+ --- Ruby
16
+ def hello_world
17
+ puts "hello world!"
18
+ end
19
+ ---
20
+
21
+ --- HTML
22
+ <strong>Look, HTML code</strong>
23
+ ---
24
+
25
+ ---
26
+ just some normal code
27
+ ---
28
+ EOS
29
+
30
+ Lesstile.format_as_xhtml(comment)
31
+ Lesstile.format_as_xhtml(comment, :code_formatter => Lesstile::CodeRayFormatter) # Requires code ray
32
+ Lesstile.format_as_xhtml(comment, :code_formatter => lambda {|code, lang| "Code in #{lang}: #{code}" })
33
+
34
+ == REQUIREMENTS:
35
+
36
+ * Facets
37
+ * CodeRay (optional)
38
+
39
+ == INSTALL:
40
+
41
+ sudo gem install lesstile
42
+
43
+ == LICENSE:
44
+
45
+ (The MIT License)
46
+
47
+ Copyright (c) 2007 Xavier Shay
48
+
49
+ Permission is hereby granted, free of charge, to any person obtaining
50
+ a copy of this software and associated documentation files (the
51
+ 'Software'), to deal in the Software without restriction, including
52
+ without limitation the rights to use, copy, modify, merge, publish,
53
+ distribute, sublicense, and/or sell copies of the Software, and to
54
+ permit persons to whom the Software is furnished to do so, subject to
55
+ the following conditions:
56
+
57
+ The above copyright notice and this permission notice shall be
58
+ included in all copies or substantial portions of the Software.
59
+
60
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
61
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
62
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
63
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
64
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
65
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
66
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require 'spec/rake/spectask'
6
+ require './lib/lesstile.rb'
7
+
8
+ Hoe.new('lesstile', Lesstile::VERSION) do |p|
9
+ p.rubyforge_name = 'lesstile'
10
+ p.author = 'Xavier Shay'
11
+ p.email = 'xavier@rhnh.net'
12
+ p.summary = 'Format text using an exceedingly simple markup language - perfect for comments on your blog'
13
+ p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
14
+ p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
15
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
16
+ p.extra_deps << ['facets', '>= 1.0']
17
+ p.remote_rdoc_dir = ""
18
+ end
19
+
20
+ Spec::Rake::SpecTask.new do |t|
21
+ t.warning = false
22
+ t.rcov = false
23
+ t.spec_files = FileList['spec/spec_*.rb']
24
+ end
25
+
26
+ task :test => :spec
27
+ # vim: syntax=Ruby
data/bin/lesstile ADDED
File without changes
data/lib/lesstile.rb ADDED
@@ -0,0 +1,46 @@
1
+ require 'facets/string/nchar'
2
+ require 'cgi'
3
+
4
+ class Lesstile
5
+ VERSION = '0.1'
6
+
7
+ class << self
8
+ # Returns lesstile formatted text as valid XHTML
9
+ #
10
+ # options (all optional):
11
+ # * <tt>text_formatter</tt>: A callback function used to format text.
12
+ # * <tt>code_formatter</tt>: A callback function used to format code. Typically used for syntax highlighting.
13
+ def format_as_xhtml(text, options = {})
14
+ options = default_options.merge(options)
15
+
16
+ text += "\n" unless text.ends_with?("\n")
17
+ text.gsub!(/\r\n/, "\n")
18
+ text = CGI::escapeHTML(text)
19
+
20
+ code_regex = /---\s*?(\w*?)\s*?\n(.*?)---\n/m
21
+ output = ""
22
+
23
+ while match = text.match(code_regex)
24
+ captures = match.captures
25
+ code = captures[1]
26
+ lang = captures[0].blank? ? nil : captures[0].downcase.intern
27
+
28
+ output += options[:text_formatter][match.pre_match] + options[:code_formatter][code, lang]
29
+ text = match.post_match
30
+ end
31
+
32
+ output += options[:text_formatter][text.chomp]
33
+ output
34
+ end
35
+
36
+ def default_options
37
+ {
38
+ :code_formatter => lambda {|code, lang| "<pre><code>#{code}</code></pre>" },
39
+ :text_formatter => lambda {|text| text.gsub(/\n/, "<br />\n") }
40
+ }
41
+ end
42
+ end
43
+
44
+ # A formatter that syntax highlights code using CodeRay
45
+ CodeRayFormatter = lambda {|code, lang| CodeRay.scan(CGI::unescapeHTML(code), lang).html(:line_numbers => :table).div }
46
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec'
2
+ require File.dirname(__FILE__) + '/../lib/lesstile'
3
+
4
+ describe "Lesstile#format_as_xhtml" do
5
+ before(:all) do
6
+ @lesstile = Lesstile
7
+ @format = lambda {|text|
8
+ @lesstile.format_as_xhtml(text,
9
+ :code_formatter => lambda {|code, lang| "|#{code}|" },
10
+ :text_formatter => lambda {|text| text }
11
+ )
12
+ }
13
+ end
14
+
15
+ it "normal text unchanged" do
16
+ @format["hello"].should == "hello"
17
+ end
18
+
19
+ it "does not modify the input parameter" do
20
+ text = 'hello---\nRAR---'
21
+ @format[text]
22
+ text.should == 'hello---\nRAR---'
23
+ end
24
+
25
+ it "surrounds code blocks in appropriate tags" do
26
+ @format["---\nhello\n---\n"].should == "|hello\n|"
27
+ end
28
+
29
+ it "parses code blocks at end of input" do
30
+ @format["---\nhello\n---"].should == "|hello\n|"
31
+ end
32
+ it "parses code blocks inside text" do
33
+ @format["yo\n---\nhello\n---\n"].should == "yo\n|hello\n|"
34
+ end
35
+
36
+ it "parses multiple code blocks" do
37
+ @format["yo\n---\nhello\n---\nnya\n---\nmore code\n---\n"].should == "yo\n|hello\n|nya\n|more code\n|"
38
+ end
39
+
40
+ it "parses unclosed code blocks" do
41
+ @format["yo\n---\nhello"].should == "yo\n---\nhello"
42
+ end
43
+
44
+ it 'escapes html' do
45
+ @format["<a>&---\nyo<---"].should == "&lt;a&gt;&amp;|yo&lt;|"
46
+ end
47
+ end
48
+
49
+ describe "Lesstile#format_with_xhtml with default formatters" do
50
+ before(:all) do
51
+ @lesstile = Lesstile
52
+ @format = @lesstile.method(:format_as_xhtml)
53
+ end
54
+
55
+ it 'adds br and code tags' do
56
+ @format["hello\nyou---\na\nb\n---\nmore text\nyeah"].should ==
57
+ "hello<br />\nyou<pre><code>a\nb\n</code></pre>more text<br />\nyeah"
58
+ end
59
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lesstile
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ""
6
+ authors:
7
+ - Xavier Shay
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2007-12-26 00:00:00 +11:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: facets
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "1.0"
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: hoe
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 1.4.0
32
+ version:
33
+ description: "Integrates with CodeRay for sexy syntax highlighting. == SYNOPSIS: comment = <<-EOS Wow, this post is awesome. I'd implement it like this: --- Ruby def hello_world puts \"hello world!\" end ---"
34
+ email: xavier@rhnh.net
35
+ executables:
36
+ - lesstile
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - History.txt
41
+ - Manifest.txt
42
+ - README.txt
43
+ files:
44
+ - History.txt
45
+ - Manifest.txt
46
+ - README.txt
47
+ - Rakefile
48
+ - bin/lesstile
49
+ - lib/lesstile.rb
50
+ - spec/spec_lesstile.rb
51
+ has_rdoc: true
52
+ homepage: by Xavier Shay (http://rhnh.net)
53
+ post_install_message:
54
+ rdoc_options:
55
+ - --main
56
+ - README.txt
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ requirements: []
72
+
73
+ rubyforge_project: lesstile
74
+ rubygems_version: 0.9.5
75
+ signing_key:
76
+ specification_version: 2
77
+ summary: Format text using an exceedingly simple markup language - perfect for comments on your blog
78
+ test_files: []
79
+