bunto-mentions 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/bunto-mentions.rb +74 -0
  3. metadata +114 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6efd40b178df1e50d0ee6928344766cb96a6b403
4
+ data.tar.gz: a276e58657c9f2cd7404c502844c5c022bf6ed67
5
+ SHA512:
6
+ metadata.gz: 629046fce4c66a9bd613ac2176c7622bb968a5aa9897728d7ab690aa73e53b384112e401e1f41b25fbe6f4e6c1b015e9630a71b3763fe33b65be414ea063e3b2
7
+ data.tar.gz: 6dc93b62ee64886cac0b729903b28993e953bca24d3992d4512843e4a3ae44610443706503c69fb77743ebcea574826b2dc4f5237a33a09793a89c852c0fdffa
@@ -0,0 +1,74 @@
1
+ require 'bunto'
2
+ require 'html/pipeline'
3
+
4
+ module Bunto
5
+ class Mentions
6
+ GITHUB_DOT_COM = "https://github.com".freeze
7
+
8
+ InvalidBuntoMentionConfig = Class.new(Bunto::Errors::FatalException)
9
+
10
+ class << self
11
+ def mentionify(doc)
12
+ src = mention_base(doc.site.config)
13
+ doc.output = filter_with_mention(src).call(doc.output)[:output].to_s
14
+ end
15
+
16
+ # Public: Create or fetch the filter for the given {{src}} base URL.
17
+ #
18
+ # src - the base URL (e.g. https://github.com)
19
+ #
20
+ # Returns an HTML::Pipeline instance for the given base URL.
21
+ def filter_with_mention(src)
22
+ filters[src] ||= HTML::Pipeline.new([
23
+ HTML::Pipeline::MentionFilter
24
+ ], { :base_url => src })
25
+ end
26
+
27
+ # Public: Filters hash where the key is the mention base URL.
28
+ # Effectively a cache.
29
+ def filters
30
+ @filters ||= {}
31
+ end
32
+
33
+ # Public: Calculate the base URL to use for mentioning.
34
+ # The custom base URL can be defined in the config as
35
+ # bunto-mentions.base_url or bunto-mentions, and must
36
+ # be a valid URL (i.e. it must include a protocol and valid domain)
37
+ # It should _not_ have a trailing slash.
38
+ #
39
+ # config - the hash-like configuration of the document's site
40
+ #
41
+ # Returns a URL to use as the base URL for mentions.
42
+ # Defaults to the https://github.com.
43
+ def mention_base(config = {})
44
+ mention_config = config['bunto-mentions']
45
+ case mention_config
46
+ when nil, NilClass
47
+ GITHUB_DOT_COM
48
+ when String
49
+ mention_config.to_s
50
+ when Hash
51
+ mention_config.fetch('base_url', GITHUB_DOT_COM)
52
+ else
53
+ raise InvalidBuntoMentionConfig,
54
+ "Your bunto-mentions config has to either be a" \
55
+ " string or a hash. It's a #{mention_config.class} right now."
56
+ end
57
+ end
58
+
59
+ # Public: Defines the conditions for a document to be emojiable.
60
+ #
61
+ # doc - the Bunto::Document or Bunto::Page
62
+ #
63
+ # Returns true if the doc is written & is HTML.
64
+ def mentionable?(doc)
65
+ (doc.is_a?(Bunto::Page) || doc.write?) &&
66
+ doc.output_ext == ".html" || (doc.permalink && doc.permalink.end_with?("/"))
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ Bunto::Hooks.register [:pages, :documents], :post_render do |doc|
73
+ Bunto::Mentions.mentionify(doc) if Bunto::Mentions.mentionable?(doc)
74
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bunto-mentions
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - GitHub, Inc.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bunto
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: html-pipeline
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
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: rdoc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description:
84
+ email: support@github.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - lib/bunto-mentions.rb
90
+ homepage: https://github.com/bunto/bunto-mentions
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.2.2
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: "@mention support for your Bunto site"
114
+ test_files: []