korgi 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b9848cba04694aa95ef726d42866ad3891306361
4
+ data.tar.gz: 1b633acca87fde4f2b64a4f7db9f057ac39ef8f9
5
+ SHA512:
6
+ metadata.gz: 2a27d40f74e16d2f86274f67d6cef0763419649d5aaf713a7cc22d5fd3a34b8b977e02f8b8a8f802185656818fc63b709d465aef9d04be7553d5aad0b872d540
7
+ data.tar.gz: 868a0a9c139f40063b4bcacb863675e9073f3f291cdb6dfbef8ea86a3036e7dd496f41a687178a1d99dd52d73b140cbe2968c575bbddfb2e1938f94fd718852f
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Tsehau Chao
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # Korgi
2
+
3
+ [HTML::Pipeline](https://github.com/jch/html-pipeline) filters to generate urls for Rails resources with Markdown.
4
+
5
+ ## Usage
6
+
7
+ ### Korgi::FileUploadFilter
8
+
9
+ ```
10
+ # input
11
+ $+image.1$
12
+
13
+ # filtered result
14
+ uploads/image/file/1/thumb_pic.jpg
15
+ ```
16
+
17
+ *korgi* is meant to work with [CarrierWave](https://github.com/carrierwaveuploader/carrierwave). You will need to tell *korgi* how to map resources for you:
18
+
19
+ ```
20
+ # config/initializers/korgi.rb
21
+ Korgi.configure do |config|
22
+ config.file_uploads = { image: [Image, :file, :thumb] }
23
+ end
24
+ ```
25
+
26
+ This tells *korgi* that you have a CarrierWave uploader mounted to `Image:file`, with `:thumb` as it's default version. You can then simply write `$+image.1$` or `$+image.1.large$` in your markdown input, and *korgi* will replace the syntax with the associated url (and version).
27
+
28
+ If you need the full url instead, you should change the settings for CarrierWave:
29
+
30
+ ```
31
+ CarrierWave.configure do |config|
32
+ config.asset_host = "http://awesome.host.com"
33
+ end
34
+ ```
35
+
36
+ ### Korgi::NamedRouteFilter
37
+
38
+ ```
39
+ # input
40
+ $#post.1$
41
+
42
+ # filtered result
43
+ /admin/posts/1
44
+ ```
45
+
46
+ You will need to tell *korgi* how to map resouces for you:
47
+
48
+ ```
49
+ # config/initializers/korgi.rb
50
+ Korgi.configure do |config|
51
+ config.named_routes = { post: "admin/posts" }
52
+ end
53
+ ```
54
+
55
+ *korgi* will then be able to replace `$#post.1$` with the result of `admin_post_path(1)`.
56
+
57
+ ### putting it all together
58
+
59
+ I tried to keep *korgi* simple, so you will still need to render the markdown syntax. Please do check out the documentation for [HTML::Pipeline](https://github.com/jch/html-pipeline). Here is an example snippet:
60
+
61
+ ```
62
+ pipeline =
63
+ HTML::Pipeline.new [
64
+ Korgi::FileUploadFilter,
65
+ Korgi::NamedRouteFilter,
66
+ HTML::Pipeline::MarkdownFilter
67
+ ]
68
+ result = pipeline.call <<-EOD
69
+ This is a [link to post1]($#post.1$).
70
+ EOD
71
+ result[:output].to_s
72
+
73
+ =>
74
+ <p>This is a <a href="/admin/posts/1">link to post1</a>.</p>
75
+ ```
76
+
77
+ ## Contributing
78
+ Send me a PR!
79
+
80
+ ## License
81
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ begin
2
+ require "bundler/setup"
3
+ rescue LoadError
4
+ puts "You must `gem install bundler` and `bundle install` to run rake tasks"
5
+ end
6
+
7
+ require "bundler/gem_tasks"
8
+ require "rspec/core/rake_task"
9
+
10
+ RSpec::Core::RakeTask.new(:spec)
11
+ task :default => :spec
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+ require "active_support/configurable"
3
+ module Korgi
4
+ include ActiveSupport::Configurable
5
+ config.instance_eval do
6
+ self.named_routes = {} # { post: "dashboard/posts" }
7
+ self.file_uploads = {} # { image: [Image, :file, :thumb] }
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+ require "rails/engine"
3
+ require "carrierwave"
4
+ require "mini_magick"
5
+ module Korgi
6
+ class Engine < ::Rails::Engine
7
+ end
8
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+ require "html/pipeline"
3
+ module Korgi
4
+ class FileUploadFilter < ::HTML::Pipeline::Filter
5
+ def initialize(doc, context = nil, result = nil)
6
+ super doc, context, result
7
+ @klass, @mount, @version = nil
8
+ end
9
+
10
+ def call
11
+ doc.to_s.gsub(pattern) { replace(Regexp.last_match) }
12
+ end
13
+
14
+ private
15
+
16
+ def replace(matches)
17
+ result, model, id, version = matches.to_a
18
+ @klass, @mount, @version = Korgi.config.file_uploads[model.to_sym]
19
+ version ||= @version
20
+ @klass.find(id).send(@mount).url(file_version(version))
21
+ rescue ActiveRecord::RecordNotFound, NameError
22
+ result
23
+ end
24
+
25
+ def file_version(version)
26
+ if valid_file_version?(version)
27
+ version
28
+ else
29
+ @version
30
+ end
31
+ end
32
+
33
+ def valid_file_version?(version)
34
+ file_versions.include?(version.to_sym)
35
+ end
36
+
37
+ def file_versions
38
+ @klass.uploaders[@mount].versions.keys
39
+ end
40
+
41
+ def pattern
42
+ %r{\$\+([\w]+).([\d]+)(?:.([\w]+))?\$}
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+ require "rails/all"
3
+ require "html/pipeline"
4
+ module Korgi
5
+ class NamedRouteFilter < ::HTML::Pipeline::Filter
6
+ include ActionView::Helpers
7
+ include Rails.application.routes.url_helpers
8
+
9
+ def initialize(doc, context = nil, result = nil)
10
+ super doc, context, result
11
+ end
12
+
13
+ def call
14
+ doc.to_s.gsub(pattern) { replace(Regexp.last_match) }
15
+ end
16
+
17
+ private
18
+
19
+ def replace(matches)
20
+ result, model, id = matches.to_a
21
+ url_for(controller: Korgi.config.named_routes[model.to_sym], action: "show", id: id, only_path: true)
22
+ rescue ActionController::UrlGenerationError
23
+ result
24
+ end
25
+
26
+ def pattern
27
+ %r{\$#([\w]+).([\d]+)\$}
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+ require "rails/railtie"
3
+ module Korgi
4
+ class Railtie < ::Rails::Railtie
5
+ config.after_initialize do
6
+ require "korgi/named_route_filter"
7
+ require "korgi/file_upload_filter"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module Korgi
3
+ VERSION = "0.1.1"
4
+ end
data/lib/korgi.rb ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+ require "korgi/engine"
3
+ require "korgi/railtie"
4
+ require "korgi/config"
5
+ module Korgi
6
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: korgi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Tsehau Chao
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: html-pipeline
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.4'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: carrierwave
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mini_magick
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.6'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.6'
83
+ description: Extends the Markdown syntax to allow quick generation of links to Rails
84
+ resources.
85
+ email:
86
+ - jodeci@5xruby.tw
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - MIT-LICENSE
92
+ - README.md
93
+ - Rakefile
94
+ - lib/korgi.rb
95
+ - lib/korgi/config.rb
96
+ - lib/korgi/engine.rb
97
+ - lib/korgi/file_upload_filter.rb
98
+ - lib/korgi/named_route_filter.rb
99
+ - lib/korgi/railtie.rb
100
+ - lib/korgi/version.rb
101
+ homepage: https://github.com/jodeci/korgi
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.6.6
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Markdown filter for Rails resources
125
+ test_files: []