beckett 0.1.0

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: d7148156b64407c8fef7638ada09657342bce3ec
4
+ data.tar.gz: 566418a2020fc159e1c1c0625ddd3c696e2a0ee8
5
+ SHA512:
6
+ metadata.gz: 1a06cae48be39a93d807636a8ed4200ae46bdcc72994f3609be66d9cead6635372ccd259dacd2bc7cd3cf28a1577eec3394c2f2d5a409c2d819598a609bad083
7
+ data.tar.gz: c504afffa363d350e7f1a34d2bfa3612961da40f1d734e0dd485f2a517d2c6d5c627b0cd705c8d11de843203b1ac0b6c7a7672cd0be28b795cc136ca48cea3ee
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.6
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in beckett.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Corey Innis
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+ # Beckett
2
+
3
+ A Markdown renderer, using Kramdown, to convert to JSON, HTML5 or a Ruby Hash.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'beckett'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install beckett
20
+
21
+ ## Usage
22
+
23
+ `beckett help`
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. Run `bundle exec beckett` to use the gem in this directory, ignoring other installed copies of this gem.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/coreyti/beckett. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'beckett/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "beckett"
8
+ spec.version = Beckett::VERSION
9
+ spec.authors = ["Corey Innis"]
10
+ spec.email = ["corey@coolerator.net"]
11
+
12
+ spec.summary = "A Markdown-to-JSON/HTML5/Ruby-Hash Renderer."
13
+ spec.description = "A Markdown renderer, using Kramdown, to convert to JSON, HTML5 or a Ruby Hash."
14
+ spec.homepage = "https://github.com/coreyti/beckett"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "kramdown"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.3.0"
27
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "beckett"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+ require 'json'
2
+ require 'kramdown'
3
+
4
+ require 'beckett/renderer'
5
+ require 'beckett/node'
6
+ require 'beckett/node/root'
7
+
8
+ require 'beckett/node/element'
9
+ require 'beckett/node/article'
10
+ require 'beckett/node/section'
11
+ require 'beckett/node/header'
12
+ require 'beckett/node/codeblock'
13
+
14
+ require 'beckett/node/text'
15
+ require 'beckett/node/codespan'
16
+ require 'beckett/node/br'
17
+
18
+
19
+ module Beckett
20
+ class Document
21
+ def initialize(content)
22
+ @content = content
23
+ end
24
+
25
+ def to_hash
26
+ document = Kramdown::Document.new(@content, input: 'GFM')
27
+ renderer = Renderer.send(:new, document.root)
28
+ renderer.convert(document.root)
29
+ end
30
+
31
+ def to_json
32
+ to_hash.to_json
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,47 @@
1
+ module Beckett
2
+ module Node
3
+ def self.find(node)
4
+ type = node.type.capitalize
5
+
6
+ if self.const_defined?(type)
7
+ const_get(type)
8
+ else
9
+ Element
10
+ end
11
+ end
12
+
13
+ class Base
14
+ $position = 0
15
+ attr_reader :node, :children, :position
16
+
17
+ def initialize(node)
18
+ @node = node
19
+ @children = []
20
+ @position = $position
21
+ $position += 1
22
+ end
23
+
24
+ def inspect
25
+ "<#{node_name} position=#{position}>"
26
+ end
27
+
28
+ def to_s
29
+ inspect
30
+ end
31
+
32
+ def to_h
33
+ raise NotImplementedError
34
+ end
35
+
36
+ def <<(nodes)
37
+ @children += ([nodes].flatten)
38
+ end
39
+
40
+ private
41
+
42
+ def node_name
43
+ @node_name ||= node.type.upcase
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,10 @@
1
+ module Beckett
2
+ class Node::Article < Node::Element
3
+
4
+ private
5
+
6
+ def node_name
7
+ 'ARTICLE'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module Beckett
2
+ class Node::Br < Node::Element
3
+ def to_h
4
+ {}.tap do |element|
5
+ element[:node_name] = node_name
6
+ element[:node_type] = node_type
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,17 @@
1
+ module Beckett
2
+ class Node::Codeblock < Node::Element
3
+ def to_h
4
+ {}.tap do |element|
5
+ element[:node_name] = node_name
6
+ element[:node_type] = node_type
7
+ element[:node_text] = node_text
8
+ end
9
+ end
10
+
11
+ private
12
+
13
+ def node_text
14
+ node.value.strip
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ module Beckett
2
+ class Node::Codespan < Node::Text
3
+ private
4
+
5
+ def node_name
6
+ 'CODESPAN'
7
+ end
8
+
9
+ def node_type
10
+ 1
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,31 @@
1
+ module Beckett
2
+ class Node::Element < Node::Base
3
+ def to_h
4
+ {}.tap do |element|
5
+ element[:node_name] = node_name
6
+ element[:node_type] = node_type
7
+ # NOTE: might add attributes as `nil` or empty Hash at a later date...
8
+ element[:attributes] = node_attributes if node_attributes
9
+ element[:children] = children.map(&:to_h)
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def node_attributes
16
+ unless defined?(@node_attributes)
17
+ @node_attributes = node.attr
18
+ @node_attributes = nil if @node_attributes.empty?
19
+ end
20
+ @node_attributes
21
+ end
22
+
23
+ def node_name
24
+ @node_name ||= node.type.upcase
25
+ end
26
+
27
+ def node_type
28
+ 1
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,10 @@
1
+ module Beckett
2
+ class Node::Header < Node::Element
3
+
4
+ private
5
+
6
+ def node_name
7
+ 'HEADER'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ module Beckett
2
+ class Node::Root < Node::Base
3
+ def to_h
4
+ {
5
+ root: {}.tap do |root|
6
+ root[:children] = children.map(&:to_h)
7
+ end
8
+ }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ module Beckett
2
+ class Node::Section < Node::Element
3
+
4
+ private
5
+
6
+ def node_name
7
+ 'SECTION'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,30 @@
1
+ module Beckett
2
+ class Node::Text < Node::Base
3
+ def to_h
4
+ {}.tap do |element|
5
+ element[:node_name] = node_name
6
+ element[:node_type] = node_type
7
+ element[:node_text] = node_text
8
+ end
9
+ end
10
+
11
+ def nil?
12
+ # improves list rendering.
13
+ node_text.strip.empty?
14
+ end
15
+
16
+ private
17
+
18
+ def node_name
19
+ '#text'
20
+ end
21
+
22
+ def node_text
23
+ node.value
24
+ end
25
+
26
+ def node_type
27
+ 3
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,108 @@
1
+ module Beckett
2
+ class Renderer < Kramdown::Converter::Base
3
+ DISPATCHER = Hash.new { |h, k|
4
+ method = :"convert_#{k}"
5
+ h[k] = Renderer.method_defined?(method) ? method : :convert_node
6
+ }
7
+
8
+ def initialize(root, options = {})
9
+ super
10
+ @stack = []
11
+ end
12
+
13
+ def convert(node)
14
+ send(DISPATCHER[node.type], node)
15
+ end
16
+
17
+ protected
18
+
19
+ def convert_root(node)
20
+ build(node).to_h
21
+ end
22
+
23
+ def convert_node(node)
24
+ build(node)
25
+ end
26
+
27
+ # ---
28
+
29
+ def convert_blank(node)
30
+ nil
31
+ end
32
+
33
+ def convert_text(node)
34
+ Node.find(node).new(node)
35
+ end
36
+
37
+ # ---
38
+
39
+ def build(node)
40
+ Node.find(node).new(node).tap do |result|
41
+ push(result)
42
+ inner(node)
43
+ pop
44
+ end
45
+ end
46
+
47
+ def build_section(node)
48
+ level = node.options[:level]
49
+
50
+ while @stack.length > level
51
+ pop
52
+ end
53
+
54
+ if level == 1
55
+ Node::Article.new(node)
56
+ else
57
+ Node::Section.new(node)
58
+ end
59
+ end
60
+
61
+ def context
62
+ @stack.last
63
+ end
64
+
65
+ def inner(node)
66
+ node.children.each do |child|
67
+ if child.type == :header
68
+ build_section(child).tap do |result|
69
+ context << result
70
+ push(result)
71
+ end
72
+ end
73
+
74
+ convert(child).tap do |result|
75
+ context << result unless result.nil?
76
+ end
77
+ end
78
+ end
79
+
80
+ def push(context)
81
+ if debug?
82
+ @indent ||= 0
83
+ puts "#{' ' * @indent}#{context}"
84
+ @indent += 2
85
+ end
86
+
87
+ @stack.push(context)
88
+ end
89
+
90
+ def pop
91
+ context = @stack.pop
92
+
93
+ if debug?
94
+ @indent -= 2
95
+ puts "#{' ' * @indent}#{context}".sub(/^(\s*)<([A-Z]+).*/, '\1</\2>')
96
+ end
97
+ end
98
+
99
+ # ---
100
+
101
+ def debug?
102
+ if @debug == nil
103
+ @debug = (ENV['DEBUG'] == 'true')
104
+ end
105
+ @debug
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,3 @@
1
+ module Beckett
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: beckett
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Corey Innis
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: kramdown
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.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.3.0
69
+ description: A Markdown renderer, using Kramdown, to convert to JSON, HTML5 or a Ruby
70
+ Hash.
71
+ email:
72
+ - corey@coolerator.net
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - beckett.gemspec
86
+ - bin/console
87
+ - bin/setup
88
+ - lib/beckett.rb
89
+ - lib/beckett/node.rb
90
+ - lib/beckett/node/article.rb
91
+ - lib/beckett/node/br.rb
92
+ - lib/beckett/node/codeblock.rb
93
+ - lib/beckett/node/codespan.rb
94
+ - lib/beckett/node/element.rb
95
+ - lib/beckett/node/header.rb
96
+ - lib/beckett/node/root.rb
97
+ - lib/beckett/node/section.rb
98
+ - lib/beckett/node/text.rb
99
+ - lib/beckett/renderer.rb
100
+ - lib/beckett/version.rb
101
+ homepage: https://github.com/coreyti/beckett
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.2.3
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: A Markdown-to-JSON/HTML5/Ruby-Hash Renderer.
125
+ test_files: []