magnet-markdown 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3f53ec18f51f0eae26fede22479045722d09bd02
4
+ data.tar.gz: f976dd234f55fb546881aa699b9188a3c3ab177a
5
+ SHA512:
6
+ metadata.gz: 05894ffada70821a3a7eb65454680f4657318697d0a2d14066ae757168c35a6c886d73c8269a82aee7afaa7326502f2ecca0995ad4f62f43f2d3586a6fc5f7b5
7
+ data.tar.gz: 43acabf65ea5373872516a8552068cd16574a3fc9d0b9fc528e7db4baf4a79ee4557fc60b42e75b82ec20b23bce42903161b477dc6f5916da0f5771e1e1da079
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
@@ -0,0 +1,20 @@
1
+ AllCops:
2
+ Include:
3
+ - 'Rakefile'
4
+ - 'config.ru'
5
+ - '*.gemspec'
6
+ - 'Gemfile'
7
+
8
+ Documentation:
9
+ Enabled: false
10
+
11
+ ClassAndModuleChildren:
12
+ Enabled: false
13
+
14
+ FileName:
15
+ Exclude:
16
+ - 'lib/magnet-markdown.rb'
17
+ Enabled: true
18
+
19
+ LineLength:
20
+ Max: 124
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in magnet-markdown.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'guard'
8
+ gem 'guard-rubocop'
9
+ gem 'test-unit-notify'
10
+ gem 'guard-test'
11
+ gem 'pry-byebug'
12
+ gem 'terminal-notifier'
13
+ end
@@ -0,0 +1,13 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rubocop do
5
+ watch(%r{.+\.rb$})
6
+ watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
7
+ end
8
+
9
+ guard :test do
10
+ watch(/^lib\/(.*)\.rb/) { |m| "test/lib/#{m[1]}_test.rb" }
11
+ watch(/^test\/(.*)_test.rb/)
12
+ watch(/^test\/test_helper.rb/) { 'test' }
13
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Sho Kusano
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.
@@ -0,0 +1,58 @@
1
+ # Magnet::Markdown
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/magnet-markdown.svg)](http://badge.fury.io/rb/magnet-markdown) [![Build Status](https://travis-ci.org/magnet-inc/magnet-markdown.svg)](https://travis-ci.org/magnet-inc/magnet-markdown) [![Dependency Status](https://gemnasium.com/magnet-inc/magnet-markdown.svg)](https://gemnasium.com/magnet-inc/magnet-markdown)
4
+
5
+ Magent specified markdown processor. Inspired by [qiita-markdown](https://github.com/increments/qiita-markdown).
6
+
7
+ Default processors:
8
+
9
+ - `Magnet::Markdown::Filter::Markdown`: convert markdown to html
10
+ - `Magnet::Markdown::Filter::Sanitize`: whitelist sanitize user markup
11
+ - `HTML::Pipeline::Filter::ImageMaxWidthFilter`: link to full size image for large images
12
+ - `Magnet::Markdown::Filter::Emoji`: covert :emoji: to img elements
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'magnet-markdown'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ ```shell
25
+ $ bundle
26
+ ```
27
+
28
+ Or install it yourself as:
29
+
30
+ ```shell
31
+ $ gem install magnet-markdown
32
+ ```
33
+
34
+ ## Usage
35
+
36
+ `Magnet::Markdown::Processor` provides markdown rendering:
37
+
38
+ ```ruby
39
+ processor = Magnet::Markdown::Processor.new
40
+ processor.call(markdow)
41
+ # => {
42
+ # output: '<h1>Example</h1> ...'
43
+ # }
44
+ ```
45
+
46
+ ### Options
47
+
48
+ - `emoji_root`: base url to link to emoji sprite
49
+ - `emoji_path`: url path to link to emoji sprite. `:filename` can be used as a placeholder for the sprite file name. If no `emoji_path` is set "emoji/:filename" is used.
50
+ - `allow_script`: Default false. If set true, Do not remove `script` elements.
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it ( https://github.com/magnet-inc/magnet-markdown/fork )
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create a new Pull Request
@@ -0,0 +1,13 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rubocop/rake_task'
3
+ require 'rake/testtask'
4
+
5
+ RuboCop::RakeTask.new
6
+
7
+ Rake::TestTask.new do |t|
8
+ t.libs << 'test'
9
+ t.test_files = FileList['test/**/*test.rb']
10
+ t.verbose = true
11
+ end
12
+
13
+ task default: %i(rubocop test)
@@ -0,0 +1 @@
1
+ require 'magnet/markdown'
@@ -0,0 +1,2 @@
1
+ require 'magnet/markdown/version'
2
+ require 'magnet/markdown/processor'
@@ -0,0 +1,8 @@
1
+ require 'magnet/markdown'
2
+
3
+ module Magnet::Markdown::Filter
4
+ end
5
+
6
+ require 'magnet/markdown/filter/markdown'
7
+ require 'magnet/markdown/filter/sanitize'
8
+ require 'magnet/markdown/filter/emoji'
@@ -0,0 +1,55 @@
1
+ require 'gemoji'
2
+ require 'magnet/markdown/filter'
3
+
4
+ class Magnet::Markdown::Filter::Emoji < HTML::Pipeline::Filter
5
+ def self.emojis
6
+ Emoji.all.map(&:aliases).flatten.sort
7
+ end
8
+
9
+ def self.regexp_pattern
10
+ @regexp_pattern ||= /:(?<name>#{ (emojis.map { |n| Regexp.escape(n) }).join('|') }):/xo
11
+ end
12
+
13
+ def self.img_tag
14
+ '<img class="emoji" alt=":%{name}:" title=":%{name}:" style="width: 1em; height: 1em;" align="absmiddle" src="%{src}">'
15
+ end
16
+
17
+ def call
18
+ doc.search('text()').each do |node|
19
+ content = node.to_html
20
+ next unless content.include?(':')
21
+ next if has_ancestor?(node, %w(pre code tt))
22
+ html = emojify(content)
23
+ next if html == content
24
+ node.replace(html)
25
+ end
26
+ doc
27
+ end
28
+
29
+ def validate
30
+ needs :emoji_root
31
+ end
32
+
33
+ def emojify(html)
34
+ html.gsub(self.class.regexp_pattern) do |_match|
35
+ name = Regexp.last_match['name']
36
+ self.class.img_tag % { name: name, src: src_url(name) }
37
+ end
38
+ end
39
+
40
+ def src_url(emoji_name)
41
+ File.join(context[:emoji_root], src_path(emoji_name))
42
+ end
43
+
44
+ def src_path(emoji_name)
45
+ if context[:emoji_path]
46
+ context[:emoji_path].sub(':filename', emoji_filename(emoji_name))
47
+ else
48
+ File.join('emoji', emoji_filename(emoji_name))
49
+ end
50
+ end
51
+
52
+ def emoji_filename(emoji_name)
53
+ Emoji.find_by_alias(emoji_name).image_filename
54
+ end
55
+ end
@@ -0,0 +1,15 @@
1
+ require 'github/markdown'
2
+ require 'magnet/markdown/filter'
3
+
4
+ class Magnet::Markdown::Filter::Markdown < HTML::Pipeline::TextFilter
5
+ def initialize(text, context = nil, result = nil)
6
+ super
7
+ @text = @text.gsub("\r", '')
8
+ end
9
+
10
+ def call
11
+ html = GitHub::Markdown.to_html(@text, :gfm)
12
+ html.rstrip!
13
+ html
14
+ end
15
+ end
@@ -0,0 +1,87 @@
1
+ require 'sanitize'
2
+ require 'magnet/markdown/filter'
3
+
4
+ class Magnet::Markdown::Filter::Sanitize < HTML::Pipeline::Filter
5
+ RULE = {
6
+ attributes: {
7
+ 'a' => [
8
+ 'href'
9
+ ],
10
+ 'iframe' => %w(allowfullscreen frameborder height marginheight marginwidth scrolling src style width),
11
+ 'img' => [
12
+ 'src'
13
+ ],
14
+ 'div' => %w(itemscope itemtype),
15
+ 'script' => %w(async src),
16
+ all: [
17
+ 'abbr',
18
+ 'align',
19
+ 'alt',
20
+ 'border',
21
+ 'cellpadding',
22
+ 'cellspacing',
23
+ 'cite',
24
+ 'class',
25
+ 'color',
26
+ 'cols',
27
+ 'colspan',
28
+ 'datetime',
29
+ 'height',
30
+ 'hreflang',
31
+ 'id',
32
+ 'itemprop',
33
+ 'lang',
34
+ 'name',
35
+ 'tabindex',
36
+ 'target',
37
+ 'title',
38
+ 'width',
39
+ :data
40
+ ]
41
+ },
42
+ elements: %w(
43
+ a b blockquote br code dd del div dl dt em font h1 h2 h3 h4 h5 h6 h7 h8 hr i img ins kbd li ol p pre q rp rt ruby s
44
+ samp strike strong sub sup table tbody td tfoot th thead tr tt ul var
45
+ ),
46
+ protocols: {
47
+ 'a' => {
48
+ 'href' => [
49
+ :relative,
50
+ 'http',
51
+ 'https'
52
+ ]
53
+ },
54
+ 'img' => {
55
+ 'src' => [
56
+ :relative,
57
+ 'http',
58
+ 'https'
59
+ ]
60
+ }
61
+ },
62
+ remove_contents: [
63
+ 'script'
64
+ ]
65
+ }
66
+
67
+ ALLOW_SCRIPT_RULE = RULE.dup.tap do |rule|
68
+ rule[:elements] = RULE[:elements] + %w(iframe script)
69
+ rule[:remove_contents] = []
70
+ end
71
+
72
+ def call
73
+ ::Sanitize.node!(doc, rule)
74
+ doc
75
+ end
76
+
77
+ def rule
78
+ case
79
+ when context[:sanitize_rule]
80
+ context[:sanitize_rule]
81
+ when context[:allow_script] == true
82
+ ALLOW_SCRIPT_RULE
83
+ else
84
+ RULE
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,29 @@
1
+ require 'html/pipeline'
2
+ require 'magnet/markdown'
3
+ require 'magnet/markdown/filter'
4
+
5
+ class Magnet::Markdown::Processor
6
+ DEFAULT_CONTEXT = {
7
+ emoji_root: '/images'
8
+ }.freeze
9
+
10
+ DEFAULT_FILTERS = [
11
+ Magnet::Markdown::Filter::Markdown,
12
+ Magnet::Markdown::Filter::Sanitize,
13
+ HTML::Pipeline::ImageMaxWidthFilter,
14
+ Magnet::Markdown::Filter::Emoji
15
+ ].freeze
16
+
17
+ def initialize(context = nil)
18
+ context ||= {}
19
+ @context = DEFAULT_CONTEXT.merge(context)
20
+ end
21
+
22
+ def call(input, context = nil)
23
+ HTML::Pipeline.new(filters, @context).call(input, context || {})
24
+ end
25
+
26
+ def filters
27
+ @filters ||= DEFAULT_FILTERS.clone
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ module Magnet
2
+ module Markdown
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'magnet/markdown/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'magnet-markdown'
8
+ spec.version = Magnet::Markdown::VERSION
9
+ spec.authors = ['Sho Kusano']
10
+ spec.email = ['sho-kusano@mgnt-inc.jp']
11
+ spec.summary = 'Magnet specified markdown processor'
12
+ spec.homepage = 'https://github.com/magnet-inc/magnet-markdown'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(/^(test|spec|features)\//)
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.7'
21
+ spec.add_development_dependency 'rake', '~> 10.0'
22
+ spec.add_development_dependency 'rubocop'
23
+ spec.add_development_dependency 'test-unit', '~> 3.0'
24
+
25
+ spec.add_dependency 'github-markdown', '~> 0.6'
26
+ spec.add_dependency 'sanitize', '~> 3.0'
27
+ spec.add_dependency 'gemoji', '~> 2.0'
28
+ spec.add_dependency 'html-pipeline', '~> 1.11'
29
+ end
@@ -0,0 +1,64 @@
1
+ require 'test_helper'
2
+
3
+ class Magnet::Markdown::ProcessorTest < Test::Unit::TestCase
4
+ setup do
5
+ @processor = Magnet::Markdown::Processor.new(@context)
6
+ end
7
+
8
+ test :markdown do
9
+ @input = '# h1'
10
+ assert { output == '<h1>h1</h1>' }
11
+ end
12
+
13
+ test :auto_link do
14
+ @input = 'Google > http://google.com'
15
+ assert { output.include?('Google &gt; <a href="http://google.com">http://google.com</a>') }
16
+ end
17
+
18
+ test :sanitize do
19
+ @input = <<-EOS
20
+ <img src="http://localhost/img.png">
21
+
22
+ <script src="http://localhost/script.js"></script>
23
+ EOS
24
+
25
+ assert do
26
+ output.include?('<img')
27
+ end
28
+
29
+ assert do
30
+ !output.include?('<script')
31
+ end
32
+
33
+ @call_context = { allow_script: true }
34
+
35
+ assert do
36
+ output.include?('<script')
37
+ end
38
+ end
39
+
40
+ test :img_maxwidth do
41
+ @input = '![](http://localhost/img.png)'
42
+ assert { output.include?('max-width') && output.include?('<img') }
43
+ end
44
+
45
+ test :emoji do
46
+ @input = ':thumbsup:'
47
+ assert { output.include?('<img') }
48
+ assert { output.include?('/images/emoji') }
49
+ @call_context = { emoji_root: 'http://static.emoji/' }
50
+ assert { !output.include?('/images/emoji') && output.include?('http://static.emoji/emoji') }
51
+ @call_context = { emoji_root: 'http://static.emoji/', emoji_path: 'images/emoji/:filename' }
52
+ assert { output.include?('http://static.emoji/images/emoji/unicode/') }
53
+ end
54
+
55
+ private
56
+
57
+ def result
58
+ @processor.call(@input, @call_context)
59
+ end
60
+
61
+ def output
62
+ result[:output].to_s
63
+ end
64
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class Magnet::MarkdownTest < Test::Unit::TestCase
4
+ test :version do
5
+ assert { Magnet::Markdown::VERSION == '0.0.1' }
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ require 'magnet-markdown'
2
+ require 'test/unit'
metadata ADDED
@@ -0,0 +1,179 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: magnet-markdown
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sho Kusano
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: test-unit
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: github-markdown
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.6'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sanitize
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: gemoji
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2.0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: html-pipeline
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.11'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.11'
125
+ description:
126
+ email:
127
+ - sho-kusano@mgnt-inc.jp
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rubocop.yml"
134
+ - ".travis.yml"
135
+ - Gemfile
136
+ - Guardfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - lib/magnet-markdown.rb
141
+ - lib/magnet/markdown.rb
142
+ - lib/magnet/markdown/filter.rb
143
+ - lib/magnet/markdown/filter/emoji.rb
144
+ - lib/magnet/markdown/filter/markdown.rb
145
+ - lib/magnet/markdown/filter/sanitize.rb
146
+ - lib/magnet/markdown/processor.rb
147
+ - lib/magnet/markdown/version.rb
148
+ - magnet-markdown.gemspec
149
+ - test/lib/magnet/markdown/processor_test.rb
150
+ - test/lib/magnet/markdown_test.rb
151
+ - test/test_helper.rb
152
+ homepage: https://github.com/magnet-inc/magnet-markdown
153
+ licenses:
154
+ - MIT
155
+ metadata: {}
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubyforge_project:
172
+ rubygems_version: 2.4.2
173
+ signing_key:
174
+ specification_version: 4
175
+ summary: Magnet specified markdown processor
176
+ test_files:
177
+ - test/lib/magnet/markdown/processor_test.rb
178
+ - test/lib/magnet/markdown_test.rb
179
+ - test/test_helper.rb