ycode 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/README.md +7 -0
- data/Rakefile +1 -0
- data/lib/ycode.rb +10 -0
- data/lib/ycode/base.rb +73 -0
- data/lib/ycode/version.rb +3 -0
- data/spec/lib/ycode/base_spec.rb +39 -0
- data/spec/spec_helper.rb +9 -0
- data/ycode.gemspec +24 -0
- metadata +101 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Y-code parser gem
|
2
|
+
=================
|
3
|
+
Alpha implementation of Y-code: The formatting used on http://y-ch.at/
|
4
|
+
|
5
|
+
This gem was only developed as proof-of-concept and was ported to PHP. This
|
6
|
+
Ruby-based version is outdated, but will be re-implemented for the new RoR
|
7
|
+
version of the site.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/lib/ycode.rb
ADDED
data/lib/ycode/base.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require "will_scan_string"
|
2
|
+
|
3
|
+
module Ycode
|
4
|
+
class Base
|
5
|
+
URL_PATTERN = /((?:www\.|https?:\/\/)[^\/\s]+\.[a-z0-9]+(?:\/[^\s\[\]"'<>]+)?)/i
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
global_string_scanner = WillScanString::StringScanner.new
|
9
|
+
nested_string_scanner = WillScanString::StringScanner.new
|
10
|
+
|
11
|
+
global_string_scanner.register_replacement (/(?:\^(\d)|\A)([\s\S]+?)(?=\Z|\^\d)/), ->(_, color_id, content) {
|
12
|
+
%(<span class="color color#{color_id.to_i}">#{nested_string_scanner.replace(content)}</span>)
|
13
|
+
}
|
14
|
+
|
15
|
+
{
|
16
|
+
/\!\@\#\$\%([\S\s]+?)\^\&\*\(\)/ => ->(_, content) { %(<code style="white-space: pre;">#{CGI.escapeHTML(content)}</code>) },
|
17
|
+
/_(.+?)_/ => ->(_, content) { %(<strong>#{CGI.escapeHTML(content)}</strong>) },
|
18
|
+
/([\s\S]*?)~~~~(?:(?:\r\n|\r|\n)\s*(.+?))?\s*(?:\r\n|\r|\n)([\s\S]+?)(?:\r\n|\r|\n)~~~~([\s\S]*?)/ => ->(_, prefix, quotee, content, postfix) {
|
19
|
+
%(#{global_string_scanner.replace(prefix)}<blockquote>#{quotee.present? ? %(<span class="quoted">#{CGI.escapeHTML(quotee)}</span>) : ""}#{global_string_scanner.replace(content)}</blockquote>#{global_string_scanner.replace(postfix)})
|
20
|
+
},
|
21
|
+
"<" => "<",
|
22
|
+
">" => ">",
|
23
|
+
"&" => "&",
|
24
|
+
/([\s\S]*?)\[rauw\]\s*([\s\S]*?)\s*\[\/rauw\]([\s\S]*?)/ => ->(_, prefix, content, postfix) {
|
25
|
+
%(%s<div class="hide">%s</div>%s) % [global_string_scanner.replace(prefix), global_string_scanner.replace(content), global_string_scanner.replace(postfix)]
|
26
|
+
},
|
27
|
+
/(?:\r\n|\r|\n)/ => "<br>",
|
28
|
+
Regexp.new("!#{URL_PATTERN.source}", Regexp::IGNORECASE) => ->(_, url) {
|
29
|
+
url = "http://#{url}" if url.starts_with? "www"
|
30
|
+
escaped_url = CGI.escapeHTML url
|
31
|
+
%(<a href="%s" target="_blank" rel="nofollow">%s</a>) % [escaped_url, escaped_url]
|
32
|
+
},
|
33
|
+
Regexp.new("M#{URL_PATTERN.source}", Regexp::IGNORECASE) => ->(_, url) {
|
34
|
+
url = "http://#{url}" if url.starts_with? "www"
|
35
|
+
uri_data = URI.parse(url) rescue nil
|
36
|
+
if uri_data.nil?
|
37
|
+
escaped_url = CGI.escapeHTML url
|
38
|
+
%(<a href="%s" target="_blank" rel="nofollow">%s</a>) % [escaped_url, escaped_url]
|
39
|
+
else
|
40
|
+
uri_query_params = uri_data.query.present? ? HashWithIndifferentAccess[CGI.parse(uri_data.query).map{|k,v|[k,v.first]}] : HashWithIndifferentAccess.new
|
41
|
+
%(<embed src="http://www.youtube.com/v/%s&fs=1" type="application/x-shockwave-flash" allowfullscreen="true" width="400" height="25"></embed>) % CGI.escape_html(uri_query_params[:v])
|
42
|
+
end
|
43
|
+
},
|
44
|
+
URL_PATTERN => ->(_, url) {
|
45
|
+
url = "http://#{url}" if url.starts_with? "www"
|
46
|
+
uri_data = URI.parse(url) rescue nil
|
47
|
+
uri_query_params = uri_data.present? && uri_data.query.present? ? HashWithIndifferentAccess[CGI.parse(uri_data.query).map{|k,v|[k,v.first]}] : HashWithIndifferentAccess.new
|
48
|
+
if uri_data.present? && uri_data.host =~ /youtube\.[a-z]{1,3}$/i && uri_query_params[:v] =~ /[a-z0-9_-]{11}/i
|
49
|
+
%(<embed src="http://www.youtube.com/v/%s&fs=1" type="application/x-shockwave-flash" allowfullscreen="true" width="583" height="354"></embed>) % CGI.escape_html($~.to_s)
|
50
|
+
elsif uri_data.present? && uri_data.host =~ /liveleak\.com/i && uri_query_params[:i].present?
|
51
|
+
%(<embed src="http://www.liveleak.com/e/%s" type="application/x-shockwave-flash" allowfullscreen="true" wmode="transparent" width="450" height="370"></embed>) % CGI.escape_html(CGI.escape($~.to_s))
|
52
|
+
elsif uri_data.present? && uri_data.host =~ /dailymotion\.com/i && uri_data.path =~ /^\/video\/([^\/]+)/
|
53
|
+
%(<embed src="http://www.dailymotion.com/swf/%s" type="application/x-shockwave-flash" width="448" height="357" allowfullscreen="true" allowscriptaccess="always"></embed>) % CGI.escape_html($1)
|
54
|
+
elsif uri_data.present? && uri_data.path =~ /\.(?:jpe?g|png|gif)$/i
|
55
|
+
%(<img src="%s">) % CGI.escapeHTML(url)
|
56
|
+
else
|
57
|
+
escaped_url = CGI.escapeHTML url
|
58
|
+
%(<a href="%s" target="_blank" rel="nofollow">%s</a>) % [escaped_url, escaped_url]
|
59
|
+
end
|
60
|
+
}
|
61
|
+
}.each do |k, v|
|
62
|
+
global_string_scanner.register_replacement k, v
|
63
|
+
nested_string_scanner.register_replacement k, v
|
64
|
+
end
|
65
|
+
|
66
|
+
@string_scanner = global_string_scanner
|
67
|
+
end
|
68
|
+
|
69
|
+
def parse_to_html( string )
|
70
|
+
@string_scanner.replace string
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper.rb'
|
2
|
+
|
3
|
+
def get_stripped_parser_result( string, strip = true)
|
4
|
+
base = Ycode::Base.new
|
5
|
+
result = base.parse_to_html string
|
6
|
+
strip ? result[(%(<span class="color color0">).length)...-("</span>".length)] : result
|
7
|
+
end
|
8
|
+
|
9
|
+
describe Ycode::Base do
|
10
|
+
it "should wrap color0 spans around the string" do
|
11
|
+
get_stripped_parser_result("banana", false).should \
|
12
|
+
eql %(<span class="color color0">banana</span>)
|
13
|
+
get_stripped_parser_result("banana").should eql "banana"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should replace new-lines to <br>'s" do
|
17
|
+
get_stripped_parser_result("a\nb").should eql "a<br>b"
|
18
|
+
get_stripped_parser_result("a\rb").should eql "a<br>b"
|
19
|
+
get_stripped_parser_result("a\r\nb").should eql "a<br>b"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should replace HTML entities" do
|
23
|
+
get_stripped_parser_result("&<>").should eql "&<>"
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should enable bold words" do
|
27
|
+
get_stripped_parser_result("_bold!_").should eql "<strong>bold!</strong>"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should enable color switching" do
|
31
|
+
get_stripped_parser_result("black ^1 red ^0 black", false).should \
|
32
|
+
eql %(<span class="color color0">black </span><span class="color color1"> red </span><span class="color color0"> black</span>)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should be able to handle nested patterns" do
|
36
|
+
get_stripped_parser_result("black ^1 _bold & red_").should \
|
37
|
+
eql %(black </span><span class="color color1"> <strong>bold & red</strong>)
|
38
|
+
end
|
39
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/ycode.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "ycode/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "ycode"
|
7
|
+
s.version = Ycode::VERSION
|
8
|
+
s.authors = ["Toby Hinloopen"]
|
9
|
+
s.email = ["toby@kutcomputers.nl"]
|
10
|
+
s.homepage = "http://y-ch.at/"
|
11
|
+
s.summary = %q{Y-Code gem}
|
12
|
+
s.description = %q{Custom-markup parser for http://y-ch.at/}
|
13
|
+
|
14
|
+
s.rubyforge_project = "ycode"
|
15
|
+
s.add_dependency "will_scan_string"
|
16
|
+
s.add_development_dependency "rspec"
|
17
|
+
s.add_dependency "activesupport"
|
18
|
+
s.add_dependency "i18n"
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
22
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
23
|
+
s.require_paths = ["lib"]
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ycode
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Toby Hinloopen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-12 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: will_scan_string
|
16
|
+
requirement: &70342939020500 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70342939020500
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70342939020080 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70342939020080
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: activesupport
|
38
|
+
requirement: &70342939019660 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70342939019660
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: i18n
|
49
|
+
requirement: &70342939050200 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70342939050200
|
58
|
+
description: Custom-markup parser for http://y-ch.at/
|
59
|
+
email:
|
60
|
+
- toby@kutcomputers.nl
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- .gitignore
|
66
|
+
- Gemfile
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/ycode.rb
|
70
|
+
- lib/ycode/base.rb
|
71
|
+
- lib/ycode/version.rb
|
72
|
+
- spec/lib/ycode/base_spec.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
- ycode.gemspec
|
75
|
+
homepage: http://y-ch.at/
|
76
|
+
licenses: []
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project: ycode
|
95
|
+
rubygems_version: 1.8.17
|
96
|
+
signing_key:
|
97
|
+
specification_version: 3
|
98
|
+
summary: Y-Code gem
|
99
|
+
test_files:
|
100
|
+
- spec/lib/ycode/base_spec.rb
|
101
|
+
- spec/spec_helper.rb
|