slodown 0.1.0

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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ # - jruby-19mode # JRuby in 1.9 mode
6
+ # - rbx-19mode
7
+ script: bundle exec rake
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in slodown.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Hendrik Mans
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Slodown
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'slodown'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install slodown
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ # integrate rspec
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new('spec')
6
+ task :default => :spec
data/lib/slodown.rb ADDED
@@ -0,0 +1,108 @@
1
+ require 'oembed'
2
+ require 'rinku'
3
+ require 'kramdown'
4
+ require 'coderay'
5
+ require 'sanitize'
6
+
7
+ require "slodown/version"
8
+
9
+ OEmbed::Providers.register_all
10
+
11
+ class Kramdown::Converter::SloblogHtml < Kramdown::Converter::Html
12
+ def convert_img(el, indent)
13
+ oembed = OEmbed::Providers.get(el.attr['src'])
14
+ %q(<div class="embedded %s %s">%s</div>) % [oembed.type, oembed.provider_name.parameterize, oembed.html]
15
+ rescue OEmbed::NotFound
16
+ super
17
+ end
18
+ end
19
+
20
+ module Slodown
21
+ class EmbedTransformer
22
+ ALLOWED_DOMAINS = %w[youtube.com soundcloud.com vimeo.com]
23
+
24
+ def self.call(env)
25
+ node = env[:node]
26
+ node_name = env[:node_name]
27
+
28
+ return if env[:is_whitelisted] || !env[:node].element?
29
+ return unless %w[iframe embed].include? env[:node_name]
30
+
31
+ uri = URI(env[:node]['src'])
32
+ domains = ALLOWED_DOMAINS.map { |d| Regexp.escape(d) }.join("|")
33
+ return unless uri.host =~ /^(.+\.)?(#{domains})/
34
+
35
+ Sanitize.clean_node!(node, {
36
+ elements: %w[iframe embed],
37
+ attributes: {
38
+ all: %w[allowfullscreen frameborder height src width]
39
+ }
40
+ })
41
+
42
+ { node_whitelist: [node] }
43
+ end
44
+ end
45
+
46
+ class Formatter
47
+ def initialize(source)
48
+ @current = @source = source.to_s
49
+ end
50
+
51
+ def complete
52
+ markdown.autolink.sanitize
53
+ end
54
+
55
+ def markdown
56
+ @current = Kramdown::Document.new(@current).to_sloblog_html
57
+ self
58
+ end
59
+
60
+ def autolink
61
+ @current = Rinku.auto_link(@current)
62
+ self
63
+ end
64
+
65
+ def sanitize(mode = :normal)
66
+ @current = case mode
67
+ when :normal
68
+ Sanitize.clean(@current,
69
+ elements: %w(
70
+ p a span sub sup strong em div hr abbr
71
+ ul ol li
72
+ blockquote pre code
73
+ h1 h2 h3 h4 h5 h6
74
+ img object param del
75
+ ),
76
+ attributes: {
77
+ :all => ['class', 'style', 'title'],
78
+ 'a' => ['href', 'rel', 'name'],
79
+ 'li' => ['id'],
80
+ 'sup' => ['id'],
81
+ 'img' => ['src', 'title', 'alt', 'width', 'height'],
82
+ 'object' => ['width', 'height'],
83
+ 'param' => ['name', 'value'],
84
+ 'embed' => ['allowscriptaccess', 'width', 'height', 'src'],
85
+ 'iframe' => ['width', 'height', 'src']
86
+ },
87
+ protocols: {
88
+ 'a' => { 'href' => ['ftp', 'http', 'https', 'mailto', '#fn', '#fnref', :relative] },
89
+ 'img' => {'src' => ['http', 'https', :relative]},
90
+ 'iframe' => {'src' => ['http', 'https']},
91
+ 'embed' => {'src' => ['http', 'https']},
92
+ 'object' => {'src' => ['http', 'https']},
93
+ 'li' => {'id' => ['fn']},
94
+ 'sup' => {'id' => ['fnref']}
95
+ },
96
+ transformers: EmbedTransformer)
97
+ else
98
+ Sanitize.clean(@current)
99
+ end
100
+
101
+ self
102
+ end
103
+
104
+ def to_s
105
+ @current
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,3 @@
1
+ module Slodown
2
+ VERSION = "0.1.0"
3
+ end
data/slodown.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'slodown/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "slodown"
8
+ gem.version = Slodown::VERSION
9
+ gem.authors = ["Hendrik Mans"]
10
+ gem.email = ["hendrik@mans.de"]
11
+ gem.description = %q{Markdown formatting code, extracted from sloblog.io.}
12
+ gem.summary = %q{Markdown formatting code, extracted from sloblog.io.}
13
+ gem.homepage = "http://github.com/hmans/slodown"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'kramdown'
21
+ gem.add_dependency 'coderay'
22
+ gem.add_dependency 'sanitize'
23
+ gem.add_dependency 'rinku'
24
+ gem.add_dependency 'ruby-oembed', '~> 0.8.8'
25
+
26
+ gem.add_development_dependency 'rspec', '>= 2.12.0'
27
+ gem.add_development_dependency 'rspec-html-matchers'
28
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ def render(text)
4
+ Slodown::Formatter.new(text).complete.to_s
5
+ end
6
+
7
+ describe 'basic formatting syntax' do
8
+ # Just a silly little spec to get things started. I don't intend
9
+ # to test the whole of kramdown here. :)~
10
+ #
11
+ it "renders **this** as bold text" do
12
+ expect(render "**foo**").to eq "<p><strong>foo</strong></p>"
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ SPEC_DIR = File.dirname(__FILE__)
2
+ lib_path = File.expand_path("#{SPEC_DIR}/../lib")
3
+ $LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path)
4
+
5
+ require 'slodown'
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slodown
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hendrik Mans
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: kramdown
16
+ requirement: !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: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: coderay
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sanitize
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rinku
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: ruby-oembed
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 0.8.8
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 0.8.8
94
+ - !ruby/object:Gem::Dependency
95
+ name: rspec
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: 2.12.0
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: 2.12.0
110
+ - !ruby/object:Gem::Dependency
111
+ name: rspec-html-matchers
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: Markdown formatting code, extracted from sloblog.io.
127
+ email:
128
+ - hendrik@mans.de
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - .gitignore
134
+ - .rspec
135
+ - .travis.yml
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - lib/slodown.rb
141
+ - lib/slodown/version.rb
142
+ - slodown.gemspec
143
+ - spec/basic_formatting_spec.rb
144
+ - spec/spec_helper.rb
145
+ homepage: http://github.com/hmans/slodown
146
+ licenses: []
147
+ post_install_message:
148
+ rdoc_options: []
149
+ require_paths:
150
+ - lib
151
+ required_ruby_version: !ruby/object:Gem::Requirement
152
+ none: false
153
+ requirements:
154
+ - - ! '>='
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ required_rubygems_version: !ruby/object:Gem::Requirement
158
+ none: false
159
+ requirements:
160
+ - - ! '>='
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubyforge_project:
165
+ rubygems_version: 1.8.23
166
+ signing_key:
167
+ specification_version: 3
168
+ summary: Markdown formatting code, extracted from sloblog.io.
169
+ test_files:
170
+ - spec/basic_formatting_spec.rb
171
+ - spec/spec_helper.rb