slate_algolia 0.0.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: e80c2ccf5f876da5001c305e6d272a21287e313d
4
+ data.tar.gz: 8e96a63ef046eef75dc8c32dbccf3e837acf892d
5
+ SHA512:
6
+ metadata.gz: 87591952fd37a850f76772d4a4935210cbcef20f7a38f3015f8fd187b6ebc78ee83170cd80a9bd77d84b13ee41db0a5cf7cbbd6e7a49401652069bd01d6092f6
7
+ data.tar.gz: f5ef2a9aeb23290ab53e4b03cfc9cb8f1fb49b4a9bcc68ecda146c294c4863f342e1115d99043313dcdbb6b0065c78ef93559947b89327136597de2335be653e
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ # Ignore bundler lock file
2
+ /Gemfile.lock
3
+
4
+ # Ignore pkg folder
5
+ /pkg
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,63 @@
1
+ # Slate Algolia
2
+
3
+ First off, thank you for considering contributing to slate algolia :)
4
+
5
+ We put these guidelines together to try and make working with this library as straight forward as possible, and hopefully help you understand how we communicate about potential changes and improvements.
6
+
7
+ Improving documentation, bug triaging, building modules for various frameworks or writing tutorials are all examples of helpful contributions we really appreciate.
8
+
9
+ ## Guidelines
10
+
11
+ * Create issues for any major changes and enhancements that you wish to make. Discuss things transparently and get community feedback.
12
+ * Be welcoming to newcomers and encourage diverse new contributors from all backgrounds.
13
+ * Please take time to read our [Community Code of Conduct](#community-code-of-conduct) which includes Reporting Guidelines.
14
+
15
+ ## Community Code of Conduct
16
+
17
+ The Keen IO Community is dedicated to providing a safe, inclusive, welcoming, and harassment-free space and experience for all community participants, regardless of gender identity and expression, sexual orientation, disability, physical appearance, socioeconomic status, body size, ethnicity, nationality, level of experience, age, religion (or lack thereof), or other identity markers. Our Code of Conduct exists because of that dedication, and we do not tolerate harassment in any form. See our reporting guidelines [here](https://github.com/keen/community-code-of-conduct/blob/master/incident-reporting.md). Our full Code of Conduct can be found at this [link](https://github.com/keen/community-code-of-conduct/blob/master/long-form-code-of-conduct.md).
18
+
19
+ ## Your First Contribution
20
+
21
+ Here are a couple of friendly tutorials with more information about contributing to OSS projects: http://makeapullrequest.com/, http://www.firsttimersonly.com/, and [How to Contribute to an Open Source Project on GitHub](https://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github).
22
+
23
+ At this point, you're ready to make your changes! Feel free to ask for help; everyone is a beginner at first :smile_cat:
24
+
25
+ If a maintainer asks you to "rebase" your PR, they're saying that a lot of code has changed, and that you need to update your branch so it's easier to merge.
26
+
27
+ ### Run the following commands to get this project set up locally.
28
+
29
+ ```ssh
30
+ $ git clone https://github.com/keen/slate_algolia.git && cd slate_algolia
31
+ $ bower install
32
+ ```
33
+
34
+ ### Submitting a Pull Request
35
+
36
+ Use the template below. If certain testing steps are not relevant, specify that in the PR. If additional checks are needed, add 'em! Please run through all testing steps before asking for a review.
37
+
38
+ ```
39
+ ## What does this PR do?
40
+
41
+ ## How should this be tested?
42
+
43
+ Step through the code line by line. Things to keep in mind as you review:
44
+ - Are there any edge cases not covered by this code?
45
+ - Does this code follow conventions (naming, formatting, modularization, etc) where applicable?
46
+
47
+ ## Related tickets?
48
+ ```
49
+
50
+ ## How to report a bug
51
+ If you find a security vulnerability, do NOT open an issue. Email team@keen.io instead.
52
+
53
+ If you find a bug that's not a security vulnerability please head over to the issues tab of this rep and open up an issue.
54
+
55
+ We created these labels to help us organize issues: bugs, docs, enhancements, and feature-request. Please use them when creating an issue where it makes sense!
56
+
57
+ ## Suggesting features
58
+
59
+ We welcome your feedback and requests. If you have a straight forward request please open up an issue that details the request. If you want to talk to someone on the Keen team head over to http://keen.chat or send a note to team@keen.io and we will make sure and get you in touch with the product team.
60
+
61
+ # Code review process
62
+
63
+ The core team looks at Pull Requests and issues on a regular basis and will typically respond within 5 business days.
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ # If you do not have OpenSSL installed, update
2
+ # the following line to use "http://" instead
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in slate_algolia.gemspec
6
+ gemspec
7
+
8
+ group :development do
9
+ gem 'rake'
10
+ gem 'rdoc'
11
+ gem 'yard'
12
+ end
13
+
14
+ group :test do
15
+ gem 'cucumber'
16
+ gem 'aruba'
17
+ gem 'rspec'
18
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Keen IO
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # Slate_Algolia
2
+
3
+ `slate_algolia` is a Middleman extension that allows you to easily and automatically index your [Slate docs](https://github.com/lord/slate) in [Algolia](https://www.algolia.com/)
4
+
5
+ ## Installation
6
+
7
+ The most simple way to activate this is to add this code to your `config.rb`:
8
+
9
+ ```ruby
10
+ activate :slate_algolia do |options|
11
+ options.application_id = 'ABCD'
12
+ options.api_key = '1234'
13
+ end
14
+ ```
15
+
16
+ You also need to add a line to the YAML Frontmatter of your Slate docs index. This is necessary because many companies embed Slate inside of a larger docs site.
17
+
18
+ ```YAML
19
+ algolia_search: true
20
+ ```
21
+
22
+ ## Configuration
23
+
24
+ There are some additional configurations you can enable:
25
+
26
+ ```ruby
27
+ activate :slate_algolia do |options|
28
+ options.application_id = 'ABCD' # Algolia Application ID
29
+ options.api_key = '1234' # Algolia API Key
30
+ options.dry_run = true # Don't send data to Algolia, but output some log information instead
31
+ options.parsers = {} # Custom tag parsers (discussed later in the docs)
32
+ end
33
+ ```
34
+
35
+ ## Custom Tag Parser
36
+
37
+ TODO
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'cucumber/rake/task'
5
+
6
+ Cucumber::Rake::Task.new(:cucumber, 'Run features that should pass') do |t|
7
+ t.cucumber_opts = "--color --tags ~@wip --strict"
8
+ end
9
+
10
+ require 'rake/clean'
11
+
12
+ task test: ['cucumber']
13
+
14
+ task default: :test
@@ -0,0 +1,4 @@
1
+ PROJECT_ROOT_PATH = File.dirname(File.dirname(File.dirname(__FILE__)))
2
+ require 'middleman-core'
3
+ require 'middleman-core/step_definitions'
4
+ require File.join(PROJECT_ROOT_PATH, 'lib', 'slate_algolia')
@@ -0,0 +1 @@
1
+ require 'slate_algolia'
@@ -0,0 +1,79 @@
1
+ require 'oga'
2
+ require 'slate_algolia/parser'
3
+ require 'slate_algolia/index'
4
+
5
+ module Middleman
6
+ module SlateAlgolia
7
+ class Extension < Middleman::Extension
8
+ option :parsers, {}, 'Custom tag parsers'
9
+ option :dry_run, false, 'Send data to Algolia or not?'
10
+ option :application_id, '', 'Algolia Application ID'
11
+ option :api_key, '', 'Algolia API Key'
12
+
13
+ def initialize(app, options_hash={}, &block)
14
+ super
15
+
16
+ tag_parsers = set_parser_defaults(options.parsers)
17
+ dry_run = options.dry_run
18
+ application_id = options.application_id
19
+ api_key = options.api_key
20
+
21
+ app.after_build do |builder|
22
+ sitemap.where(:algolia_search.equal => true).all.each do |slate_page|
23
+ content_parser = Parser.new(slate_page, tag_parsers)
24
+
25
+ if content_parser.sections.length > 0
26
+ index = Index.new(application_id, api_key, dry_run)
27
+
28
+ content_parser.sections.each do |section|
29
+ index.queue_object(section)
30
+ end
31
+
32
+ index.flush_queue
33
+ index.clean_index
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def parser_defaults
42
+ {
43
+ pre: -> (node, section, page) do
44
+ languages = node.get('class').split
45
+ languages.delete('highlight')
46
+
47
+ if languages.length
48
+
49
+ # if the current language is in the list of language tabs
50
+ if page.metadata[:page]['language_tabs'].include?(languages.first)
51
+ code_type = :tabbed_code
52
+ else
53
+ code_type = :permanent_code
54
+ end
55
+
56
+ unless section[code_type]
57
+ section[code_type] = {}
58
+ end
59
+
60
+ section[code_type][languages.first.to_sym] = node.text
61
+ end
62
+ end,
63
+
64
+ blockquote: -> (node, section, page) do
65
+ unless section[:annotations]
66
+ section[:annotations] = []
67
+ end
68
+
69
+ section[:annotations].push(node.text)
70
+ end
71
+ }
72
+ end
73
+
74
+ def set_parser_defaults(custom_parsers)
75
+ parser_defaults.merge(custom_parsers)
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,51 @@
1
+ require 'algoliasearch'
2
+
3
+ module Middleman
4
+ module SlateAlgolia
5
+ class Index
6
+
7
+ def initialize(app_id, api_key, dry_run = false)
8
+ @publish = !dry_run
9
+ @published = []
10
+
11
+ Algolia.init :application_id => app_id,
12
+ :api_key => api_key
13
+
14
+ @index = Algolia::Index.new("API Docs")
15
+ @queue = []
16
+ end
17
+
18
+ def queue_object(data)
19
+ @queue.push(data)
20
+
21
+ if @queue.length >= 1000
22
+ flush_queue()
23
+ end
24
+ end
25
+
26
+ def clean_index
27
+ old_content = @index.browse['hits'].reject { |hit|
28
+ @published.any? { |entry| entry[:id] == hit['id'] }
29
+ }
30
+
31
+ if @publish
32
+ @index.delete_objects(old_content.map { |hit| hit['objectID'] })
33
+ else
34
+ puts "would have deleted #{old_content.size} items if not in dry mode"
35
+ end
36
+ end
37
+
38
+ def flush_queue
39
+ to_publish = @queue.reject { |obj| obj[:id].nil? }
40
+ if @publish
41
+ @index.add_objects(to_publish)
42
+ else
43
+ puts "would have published #{to_publish.size} items if not in dry mode"
44
+ end
45
+
46
+ @published.concat(to_publish)
47
+ @queue = []
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,58 @@
1
+ require 'oga'
2
+
3
+ module Middleman
4
+ module SlateAlgolia
5
+ class Parser
6
+
7
+ def initialize(middleman_page, parsers)
8
+ @middleman_page = middleman_page
9
+ @page = Oga.parse_html(middleman_page.render({}, {current_page: middleman_page }))
10
+ @parsers = parsers
11
+ get_content
12
+ generate_sections
13
+ end
14
+
15
+ def sections
16
+ @sections ||= []
17
+ end
18
+
19
+ private
20
+
21
+ def get_content
22
+ @content = @page.css('.content').first
23
+ end
24
+
25
+ def generate_sections
26
+ @sections = []
27
+ current_section = {title: 'introduction-header'}
28
+
29
+ @content.children.each do |node|
30
+ next unless node.class == Oga::XML::Element
31
+
32
+ if node.name == 'h1' or node.name == 'h2'
33
+ @sections.push(current_section) unless current_section[:title].nil? and current_section[:body].empty?
34
+ current_section = {
35
+ id: node.get('id'),
36
+ title: node.text
37
+ }
38
+ else
39
+ parser = @parsers[node.name.to_sym] || self.method(:default_tag_parser)
40
+ parser.call(node, current_section, @middleman_page)
41
+ end
42
+ end
43
+
44
+ @sections.push(current_section)
45
+ end
46
+
47
+ def default_tag_parser(node, section, page)
48
+ if section[:body]
49
+ section[:body] += "\n"
50
+ else
51
+ section[:body] = ""
52
+ end
53
+
54
+ section[:body] += node.text
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,6 @@
1
+ require 'middleman-core'
2
+
3
+ ::Middleman::Extensions.register(:slate_algolia) do
4
+ require 'slate_algolia/extension'
5
+ ::Middleman::SlateAlgolia::Extension
6
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "slate_algolia"
6
+ s.version = "0.0.1"
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Joe Wegner"]
9
+ s.email = ["joe@wegnerdesign.com"]
10
+ s.summary = "Quickly and easily index Slate Docs in Algolia"
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
15
+ s.require_paths = ["lib"]
16
+
17
+ # The version of middleman-core your extension depends on
18
+ s.add_runtime_dependency("middleman-core", ["~> 3.3", ">= 3.3.12"])
19
+ s.add_runtime_dependency("oga", ["~> 1.3", ">= 1.3.1"])
20
+ s.add_runtime_dependency("algoliasearch", ["~> 1.6", ">= 1.6.1"])
21
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slate_algolia
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Joe Wegner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: middleman-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.3'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 3.3.12
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '3.3'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 3.3.12
33
+ - !ruby/object:Gem::Dependency
34
+ name: oga
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.3'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.3.1
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.3'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.3.1
53
+ - !ruby/object:Gem::Dependency
54
+ name: algoliasearch
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1.6'
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 1.6.1
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '1.6'
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 1.6.1
73
+ description:
74
+ email:
75
+ - joe@wegnerdesign.com
76
+ executables: []
77
+ extensions: []
78
+ extra_rdoc_files: []
79
+ files:
80
+ - ".gitignore"
81
+ - CONTRIBUTING.md
82
+ - Gemfile
83
+ - LICENSE
84
+ - README.md
85
+ - Rakefile
86
+ - features/support/env.rb
87
+ - lib/middleman_extension.rb
88
+ - lib/slate_algolia.rb
89
+ - lib/slate_algolia/extension.rb
90
+ - lib/slate_algolia/index.rb
91
+ - lib/slate_algolia/parser.rb
92
+ - slate_algolia.gemspec
93
+ homepage:
94
+ licenses: []
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.5.1
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Quickly and easily index Slate Docs in Algolia
116
+ test_files:
117
+ - features/support/env.rb