mbrao 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. data/.gitignore +6 -0
  2. data/.travis-gemfile +13 -0
  3. data/.travis.yml +8 -0
  4. data/.yardopts +1 -0
  5. data/Gemfile +21 -0
  6. data/README.md +60 -0
  7. data/Rakefile +11 -0
  8. data/doc/ActionView/Template/Handlers/MbraoTemplate.html +568 -0
  9. data/doc/ActionView/Template/Handlers.html +125 -0
  10. data/doc/HTML/Pipeline/KramdownFilter.html +354 -0
  11. data/doc/HTML/Pipeline.html +140 -0
  12. data/doc/HTML.html +125 -0
  13. data/doc/Mbrao/Author.html +1402 -0
  14. data/doc/Mbrao/Content.html +4779 -0
  15. data/doc/Mbrao/ContentPublicInterface.html +658 -0
  16. data/doc/Mbrao/Exceptions/InvalidDate.html +133 -0
  17. data/doc/Mbrao/Exceptions/InvalidMetadata.html +133 -0
  18. data/doc/Mbrao/Exceptions/Parsing.html +133 -0
  19. data/doc/Mbrao/Exceptions/Rendering.html +133 -0
  20. data/doc/Mbrao/Exceptions/UnavailableLocalization.html +133 -0
  21. data/doc/Mbrao/Exceptions/Unimplemented.html +133 -0
  22. data/doc/Mbrao/Exceptions/UnknownEngine.html +133 -0
  23. data/doc/Mbrao/Exceptions.html +125 -0
  24. data/doc/Mbrao/Parser.html +418 -0
  25. data/doc/Mbrao/ParsingEngines/Base.html +658 -0
  26. data/doc/Mbrao/ParsingEngines/PlainText.html +571 -0
  27. data/doc/Mbrao/ParsingEngines.html +127 -0
  28. data/doc/Mbrao/PublicInterface/ClassMethods.html +1727 -0
  29. data/doc/Mbrao/PublicInterface.html +134 -0
  30. data/doc/Mbrao/RenderingEngines/Base.html +280 -0
  31. data/doc/Mbrao/RenderingEngines/HtmlPipeline.html +873 -0
  32. data/doc/Mbrao/RenderingEngines.html +127 -0
  33. data/doc/Mbrao/Validations/ClassMethods.html +692 -0
  34. data/doc/Mbrao/Validations.html +134 -0
  35. data/doc/Mbrao/Version.html +189 -0
  36. data/doc/Mbrao.html +130 -0
  37. data/doc/_index.html +395 -0
  38. data/doc/class_list.html +53 -0
  39. data/doc/css/common.css +1 -0
  40. data/doc/css/full_list.css +57 -0
  41. data/doc/css/style.css +338 -0
  42. data/doc/file.README.html +135 -0
  43. data/doc/file_list.html +55 -0
  44. data/doc/frames.html +28 -0
  45. data/doc/index.html +135 -0
  46. data/doc/js/app.js +214 -0
  47. data/doc/js/full_list.js +173 -0
  48. data/doc/js/jquery.js +4 -0
  49. data/doc/method_list.html +516 -0
  50. data/doc/top-level-namespace.html +112 -0
  51. data/lib/mbrao/author.rb +61 -0
  52. data/lib/mbrao/content.rb +300 -0
  53. data/lib/mbrao/exceptions.rb +38 -0
  54. data/lib/mbrao/integrations/rails.rb +51 -0
  55. data/lib/mbrao/parser.rb +276 -0
  56. data/lib/mbrao/parsing_engines/base.rb +51 -0
  57. data/lib/mbrao/parsing_engines/plain_text.rb +187 -0
  58. data/lib/mbrao/rendering_engines/base.rb +22 -0
  59. data/lib/mbrao/rendering_engines/html_pipeline.rb +147 -0
  60. data/lib/mbrao/version.rb +25 -0
  61. data/lib/mbrao.rb +24 -0
  62. data/mbrao.gemspec +31 -0
  63. data/spec/coverage_helper.rb +17 -0
  64. data/spec/mbrao/author_spec.rb +62 -0
  65. data/spec/mbrao/content_spec.rb +280 -0
  66. data/spec/mbrao/integrations/rails_spec.rb +92 -0
  67. data/spec/mbrao/parser_spec.rb +269 -0
  68. data/spec/mbrao/parsing_engines/base_spec.rb +52 -0
  69. data/spec/mbrao/parsing_engines/plain_text_spec.rb +141 -0
  70. data/spec/mbrao/rendering_engines/base_spec.rb +17 -0
  71. data/spec/mbrao/rendering_engines/html_pipeline_spec.rb +128 -0
  72. data/spec/spec_helper.rb +15 -0
  73. metadata +195 -0
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the mbrao gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ module Mbrao
8
+ # Engines used to parse contents with metadata.
9
+ module ParsingEngines
10
+ # A base class for all parsers.
11
+ class Base
12
+ # Parses a whole post content and return its metadata and content parts.
13
+ #
14
+ # @param content [Object] The content to parse.
15
+ # @param options [Hash] Options to customize parsing.
16
+ # @return [Array] An array of metadata and contents parts.
17
+ def separate_components(content, options = {})
18
+ raise Mbrao::Exceptions::Unimplemented.new
19
+ end
20
+
21
+ # Parses metadata part and returns all valid metadata.
22
+ #
23
+ # @param content [Object] The content to parse.
24
+ # @param options [Hash] Options to customize parsing.
25
+ # @return [Hash] All valid metadata for the content.
26
+ def parse_metadata(content, options = {})
27
+ raise Mbrao::Exceptions::Unimplemented.new
28
+ end
29
+
30
+ # Filters content of a post by locale.
31
+ #
32
+ # @param content [Content] The content to filter.
33
+ # @param locales [String|Array] The desired locales. Can include `*` to match all.
34
+ # @param options [Hash] Options to customize parsing.
35
+ # @return [String|HashWithIndifferentAccess] Return the filtered content in the desired locales. If only one locale is required, then a `String` is returned, else a `HashWithIndifferentAccess` with locales as keys.
36
+ def filter_content(content, locales = [], options = {})
37
+ raise Mbrao::Exceptions::Unimplemented.new
38
+ end
39
+
40
+ # Parses a content and return a {Content Content} object.
41
+ #
42
+ # @param content [Object] The content to parse.
43
+ # @param options [Hash] Options to customize parsing.
44
+ def parse(content, options = {})
45
+ metadata, body = separate_components(content, options)
46
+ metadata = parse_metadata(metadata, options)
47
+ Mbrao::Content.create(metadata, body)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,187 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the mbrao gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ module Mbrao
8
+ # Engines used to parse contents with metadata.
9
+ module ParsingEngines
10
+ # A class for parsing plain text files.
11
+ class PlainText < Mbrao::ParsingEngines::Base
12
+ # Parses a whole post content and return its metadata and content parts.
13
+ #
14
+ # @param content [String] The content to parse.
15
+ # @param options [Hash] Options to customize parsing.
16
+ # @return [Array] An array of metadata and contents parts.
17
+ def separate_components(content, options = {})
18
+ metadata = nil
19
+ content = content.ensure_string.strip
20
+ options = sanitize_options(options)
21
+ scanner = StringScanner.new(content)
22
+ end_tag = options[:meta_tags].last
23
+
24
+ if scanner.scan_until(options[:meta_tags].first) && (metadata = scanner.scan_until(end_tag)) then
25
+ metadata = metadata.partition(end_tag).first
26
+ content = scanner.rest.strip
27
+ end
28
+
29
+ [metadata.ensure_string.strip, content]
30
+ end
31
+
32
+ # Parses metadata part and returns all valid metadata.
33
+ #
34
+ # @param content [String] The content to parse.
35
+ # @param options [Hash] Options to customize parsing.
36
+ # @return [Hash] All valid metadata for the content.
37
+ def parse_metadata(content, options = {})
38
+ begin
39
+ YAML.load(content)
40
+ rescue Exception => e
41
+ options[:default] ? options[:default] : (raise ::Mbrao::Exceptions::InvalidMetadata.new(e.to_s))
42
+ end
43
+ end
44
+
45
+ # Filters content of a post by locale.
46
+ #
47
+ # @param content [Content] The content to filter.
48
+ # @param locales [String|Array] The desired locales. Can include `*` to match all. If none are specified, the default Mbrao locale will be requested.
49
+ # @param options [Hash] Options to customize parsing.
50
+ # @return [String|HashWithIndifferentAccess] Return the filtered content in the desired locales. If only one locale is required, then a `String` is returned, else a `HashWithIndifferentAccess` with locales as keys.
51
+ def filter_content(content, locales = [], options = {})
52
+ body = content.body.ensure_string.strip
53
+ options = sanitize_options(options)
54
+ locales = ::Mbrao::Content.validate_locales(locales, content)
55
+
56
+ # Split the content
57
+ result = scan_content(body, options[:content_tags].first, options[:content_tags].last)
58
+
59
+ # Now filter results
60
+ perform_filter_content(result, locales)
61
+ end
62
+
63
+ private
64
+ # Sanitizes options.
65
+ #
66
+ # @param options [Hash] The options to sanitize.
67
+ # @return [HashWithIndifferentAccess] The sanitized options.
68
+ def sanitize_options(options)
69
+ # Tags
70
+ options[:meta_tags] = sanitize_tags(options[:meta_tags], ["{{metadata}}", "{{/metadata}}"])
71
+ options[:content_tags] = sanitize_tags(options[:content_tags], ["{{content: %ARGS%}}", "{{/content}}"])
72
+
73
+ options
74
+ end
75
+
76
+ # Sanitizes tag markers.
77
+ #
78
+ # @param tag [Array|String] The tag to sanitize.
79
+ # @return [Array] Sanitized tags.
80
+ def sanitize_tags(tag, default = ["---"])
81
+ tag = tag.ensure_string.split(/\s*,\s*/).collect(&:strip) if tag && !tag.is_a?(Array)
82
+ (tag.present? ? tag : default).slice(0, 2).collect {|t| /#{Regexp.quote(t).gsub("%ARGS%", "\\s*(?<args>[^\\n\\}]+,?)*")}/ }
83
+ end
84
+
85
+ # Scans a text and content section.
86
+ #
87
+ # @param content [String] The string to scan
88
+ # @param start_tag [Regexp] The tag to match for starting section.
89
+ # @param end_tag [Regexp] The tag to match for ending section.
90
+ def scan_content(content, start_tag, end_tag)
91
+ rv = []
92
+ scanner = StringScanner.new(content)
93
+
94
+ # Begin scanning the string
95
+ while !scanner.eos? do
96
+ if scanner.exist?(start_tag) then # It may start an embedded content
97
+ # Scan until the start tag, remove the tag from the match and then store to results.
98
+ rv << [scanner.scan_until(start_tag).partition(start_tag).first, "*"]
99
+
100
+ # Keep a reference to the start tag
101
+ starting = scanner.matched
102
+
103
+ # Now try to match the rightmost occurring closing tag
104
+ embedded = parse_embedded_content(scanner, start_tag, end_tag)
105
+
106
+ # Append results
107
+ rv << get_embedded_content(starting, embedded, start_tag, end_tag)
108
+ else # Append the rest to the result.
109
+ rv << [scanner.rest, "*"]
110
+ scanner.terminate
111
+ end
112
+ end
113
+
114
+ rv
115
+ end
116
+
117
+ # Gets results for an embedded content.
118
+ #
119
+ # @param [String] starting The match starting expression.
120
+ # @param [String] embedded The embedded contents.
121
+ # @param start_tag [Regexp] The tag to match for starting section.
122
+ # @param end_tag [Regexp] The tag to match for ending section.
123
+ # @return [Array] An array which the first element is the list of valid contents and second is the list of valid locales.
124
+ def get_embedded_content(starting, embedded, start_tag, end_tag)
125
+ # Either we have some content or the content was not closed and therefore we ignore this tag.
126
+ embedded.present? ? [scan_content(embedded, start_tag, end_tag), starting.match(start_tag)["args"]] : [starting, "*"]
127
+ end
128
+
129
+ # Parses embedded content of a tag
130
+ #
131
+ # @param scanner [StringScanner] The scanner to use.
132
+ # @param start_tag [Regexp] The tag to match for starting section.
133
+ # @param end_tag [Regexp] The tag to match for ending section.
134
+ # @return [String] The embedded content or `nil`, if the content was never closed.
135
+ def parse_embedded_content(scanner, start_tag, end_tag)
136
+ rv = ""
137
+ balance = 1
138
+
139
+ while (embedded_part = scanner.scan_until(end_tag)) do
140
+ balance += embedded_part.scan(start_tag).count - 1 # -1 Because there is a closure
141
+ embedded_part = embedded_part.partition(end_tag).first if balance == 0 || !scanner.exist?(end_tag) # This is the last occurrence.
142
+ rv << embedded_part
143
+ break if balance == 0
144
+ end
145
+
146
+ rv
147
+ end
148
+
149
+ # Filters content by locale.
150
+ #
151
+ # @param content [Array] The content to filter. @see #scan_content.
152
+ # @param locales [Array] The desired locales. Can include `*` to match all.
153
+ # @return [String|nil] Return the filtered content or `nil` if the content must be hidden.
154
+ def perform_filter_content(content, locales)
155
+ content.collect { |part|
156
+ part_locales = parse_locales(part[1])
157
+
158
+ if locales_valid?(locales, part_locales) then
159
+ part[0].is_a?(Array) ? perform_filter_content(part[0], locales) : part[0]
160
+ else
161
+ nil
162
+ end
163
+ }.compact.join("")
164
+ end
165
+
166
+ # Parses locales of a content.
167
+ #
168
+ # @param locales [String] The desired locales. Can include `*` to match all. Note that `!*` is ignored.
169
+ # @return [Hash] An hash with valid and invalid locales.
170
+ def parse_locales(locales)
171
+ types = locales.split(/\s*,\s*/).collect(&:strip).group_by {|l| l !~ /^!/ ? "valid" : "invalid" }
172
+ types["valid"] ||= []
173
+ types["invalid"] = types.fetch("invalid", []).reject {|l| l == "!*" }.collect {|l| l.gsub(/^!/, "") }
174
+ types
175
+ end
176
+
177
+ # Checks if all locales in a list are valid for a part.
178
+ #
179
+ # @param locales [Array] The desired locales. Can include `*` to match all.
180
+ # @param part_locales[Hash] An hash with valid and invalid locales.
181
+ # @return [Boolean] `true` if the locales are valid, `false` otherwise.
182
+ def locales_valid?(locales, part_locales)
183
+ locales.include?("*") || part_locales["valid"].include?("*") || ((part_locales["valid"].empty? || (locales & part_locales["valid"]).present?) && (locales & part_locales["invalid"]).blank?)
184
+ end
185
+ end
186
+ end
187
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the mbrao gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ module Mbrao
8
+ # Engines used to render contents with metadata.
9
+ module RenderingEngines
10
+ # A base class for all renderers.
11
+ class Base
12
+ # Renders a content.
13
+ #
14
+ # @param content [Content|String] The content to parse.
15
+ # @param options [Hash] A list of options for renderer.
16
+ # @param context [Hash] A context for rendering.
17
+ def render(content, options = {}, context = {})
18
+ raise Mbrao::Exceptions::Unimplemented.new
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,147 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the mbrao gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ # Main module of the [html-pipeline](https://github.com/jch/html-pipeline) gem.
8
+ module HTML
9
+ # A [html-pipeline](https://github.com/jch/html-pipeline) gem Pipeline.
10
+ class Pipeline
11
+ # A filter to compile Markdown contents.
12
+ class KramdownFilter < TextFilter
13
+ # Creates a new filter.
14
+ #
15
+ # @param text [String] The string to convert.
16
+ # @param context [Hash] The context of the conversion.
17
+ # @param result [Hash] A result hash.
18
+ def initialize(text, context = nil, result = nil)
19
+ super(text, context, result)
20
+ @text = @text.gsub("\r", "")
21
+ end
22
+
23
+ # Converts Markdown to HTML using Kramdown and converts into a DocumentFragment.
24
+ #
25
+ # @return [DocumentFragment] The converted fragment.
26
+ def call
27
+ Kramdown::Document.new(@text, @context).to_html
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ module Mbrao
34
+ # Engines used to render contents with metadata.
35
+ module RenderingEngines
36
+ # A renders which use the [html-pipeline](https://github.com/jch/html-pipeline) gem.
37
+ #
38
+ # @attribute default_pipeline
39
+ # @return [Array] The default pipeline to use. It should be an array of pairs of `Symbol`, which the first element is the filter (in underscored version and without the filter suffix) and the second is a shortcut to disable the pipeline via options. You can also specify a single element to disable shortcuts.
40
+ # @attribute default_options
41
+ # @return [Hash] The default options for the renderer.
42
+ class HtmlPipeline < Mbrao::RenderingEngines::Base
43
+ attr_accessor :default_pipeline
44
+ attr_accessor :default_options
45
+
46
+ # Renders a content.
47
+ #
48
+ # @param content [Content|String] The content to parse.
49
+ # @param options [Hash] A list of options for renderer.
50
+ # @param context [Hash] A context for rendering.
51
+ def render(content, options = {}, context = {})
52
+ options = sanitize_options(options)
53
+ context = context.is_a?(Hash) ? context.symbolize_keys : {}
54
+
55
+ begin
56
+ create_pipeline(options, context).call(get_body(content, options))[:output].to_s
57
+ rescue Mbrao::Exceptions::UnavailableLocalization => le
58
+ raise le
59
+ rescue Exception => e
60
+ raise ::Mbrao::Exceptions::Rendering.new(e.to_s)
61
+ end
62
+ end
63
+
64
+ # Gets the default pipeline.
65
+ #
66
+ # @return [Array] The default pipeline.
67
+ def default_pipeline
68
+ @default_pipeline || [[:kramdown], [:table_of_contents, :toc], [:autolink, :links], [:emoji], [:image_max_width]]
69
+ end
70
+
71
+ # Sets the default pipeline.
72
+ #
73
+ # @return [Array] The default pipeline.
74
+ def default_pipeline=(value)
75
+ @default_pipeline = value.ensure_array.collect {|v| v.ensure_array.flatten.compact.collect { |p| p.ensure_string.to_sym } }
76
+ end
77
+
78
+ # Gets the default options.
79
+ #
80
+ # @return [Hash] The default options.
81
+ def default_options
82
+ @default_options || {gfm: true, asset_root: "/"}
83
+ end
84
+
85
+ # Sets the default options.
86
+ #
87
+ # @param value [Object] The new default options.
88
+ def default_options=(value)
89
+ @default_options = value.is_a?(Hash) ? value : {}
90
+ end
91
+
92
+ private
93
+ # Sanitizes options.
94
+ #
95
+ # @param options [Hash] The options to sanitize.
96
+ # @return [Hash] The sanitized options.
97
+ def sanitize_options(options)
98
+ options = options.is_a?(Hash) ? options.symbolize_keys : {}
99
+ options = filter_filters(options)
100
+ options[:pipeline_options] = self.default_options.merge((options[:pipeline_options].is_a?(Hash) ? options[:pipeline_options] : {}).symbolize_keys)
101
+
102
+ options
103
+ end
104
+
105
+ # Get body of a content.
106
+ #
107
+ # @param content [Content|String] The content to sanitize.
108
+ # @param options [Hash] A list of options for renderer.
109
+ # @return [Array] The body to parse.
110
+ def get_body(content, options)
111
+ content = ::Mbrao::Content.create(nil, content.ensure_string) if !content.is_a?(::Mbrao::Content)
112
+ content.get_body(options.fetch(:locale, ::Mbrao::Parser.locale).ensure_string)
113
+ end
114
+
115
+ # Creates the pipeline for rendering.
116
+ #
117
+ # @param options [Hash] A list of options for renderer.
118
+ # @param context [Hash] A context for rendering.
119
+ # @return [HTML::Pipeline] The pipeline
120
+ def create_pipeline(options, context)
121
+ ::HTML::Pipeline.new(options[:pipeline].collect {|f| ::Mbrao::Parser.find_class(f, "::HTML::Pipeline::%CLASS%Filter", true) }, options[:pipeline_options].merge(context))
122
+ end
123
+
124
+ # Filters pipeline filters basing on the options provided.
125
+ #
126
+ # @param options [Hash] The original options.
127
+ # @return [Hash] The options with the new set of filters.
128
+ def filter_filters(options)
129
+ options[:pipeline] = get_pipeline(options)
130
+
131
+ self.default_pipeline.each do |f|
132
+ options[:pipeline].delete(f.first) if !options.fetch(f.last, true)
133
+ end
134
+
135
+ options
136
+ end
137
+
138
+ # Gets the pipeline for the current options.
139
+ #
140
+ # @param options [Hash] The options to parse.
141
+ # @return [Array] The pipeline to process.
142
+ def get_pipeline(options)
143
+ options.fetch(:pipeline, self.default_pipeline.collect(&:first)).collect(&:to_sym)
144
+ end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the mbrao gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ # A content parser and renderer with embedded metadata support.
8
+ module Mbrao
9
+ # The current version of mbrao, according to semantic versioning.
10
+ #
11
+ # @see http://semver.org
12
+ module Version
13
+ # The major version.
14
+ MAJOR = 1
15
+
16
+ # The minor version.
17
+ MINOR = 0
18
+
19
+ # The patch version.
20
+ PATCH = 0
21
+
22
+ # The current version of mbrao.
23
+ STRING = [MAJOR, MINOR, PATCH].compact.join(".")
24
+ end
25
+ end
data/lib/mbrao.rb ADDED
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the mbrao gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ require "lazier"
8
+ require "html/pipeline"
9
+ require "slim"
10
+ require "kramdown"
11
+ require "yaml"
12
+
13
+ require "mbrao/version" if !defined?(Mbrao::Version)
14
+ require "mbrao/exceptions"
15
+ require "mbrao/content"
16
+ require "mbrao/author"
17
+ require "mbrao/parser"
18
+ require "mbrao/parsing_engines/base"
19
+ require "mbrao/parsing_engines/plain_text"
20
+ require "mbrao/rendering_engines/base"
21
+ require "mbrao/rendering_engines/html_pipeline"
22
+ require "mbrao/integrations/rails" if defined?(ActionView)
23
+
24
+ Lazier.load!(:object)
data/mbrao.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the mbrao gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ require File.expand_path('../lib/mbrao/version', __FILE__)
8
+
9
+ Gem::Specification.new do |gem|
10
+ gem.name = "mbrao"
11
+ gem.version = Mbrao::Version::STRING
12
+ gem.homepage = "http://github.com/ShogunPanda/mbrao"
13
+ gem.summary = "A content parser and renderer with embedded metadata support."
14
+ gem.description = "A content parser and renderer with embedded metadata support."
15
+ gem.rubyforge_project = "mbrao"
16
+
17
+ gem.authors = ["Shogun"]
18
+ gem.email = ["shogun_panda@me.com"]
19
+
20
+ gem.files = `git ls-files`.split($\)
21
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
22
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
23
+ gem.require_paths = ["lib"]
24
+
25
+ gem.required_ruby_version = ">= 1.9.3"
26
+
27
+ gem.add_dependency("lazier", "~> 2.7.0")
28
+ gem.add_dependency("html-pipeline", "~> 0.0.8")
29
+ gem.add_dependency("slim", "~> 1.3.6")
30
+ gem.add_dependency("kramdown", "~> 0.14.2")
31
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the mbrao gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ require "pathname"
8
+ require "simplecov"
9
+
10
+ SimpleCov.start do
11
+ root = Pathname.new(File.dirname(__FILE__)) + ".."
12
+
13
+ add_filter do |src_file|
14
+ path = Pathname.new(src_file.filename).relative_path_from(root).to_s
15
+ path !~ /^(lib)/
16
+ end
17
+ end
@@ -0,0 +1,62 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the mbrao gem. Copyright (C) 2013 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ require "spec_helper"
8
+
9
+ describe Mbrao::Author do
10
+ describe ".create" do
11
+ it "creates a author from a non-hash" do
12
+ Mbrao::Author.should_receive(:new).with("NAME")
13
+ Mbrao::Author.create("NAME")
14
+
15
+ Mbrao::Author.should_receive(:new).with(nil)
16
+ Mbrao::Author.create(nil)
17
+
18
+ Mbrao::Author.should_receive(:new).with([])
19
+ Mbrao::Author.create([])
20
+
21
+ Mbrao::Author.should_receive(:new).with(["NAME"])
22
+ Mbrao::Author.create(["NAME"])
23
+ end
24
+
25
+ it "creates a author from a hash" do
26
+ Mbrao::Author.should_receive(:new).with("NAME", "EMAIL", "WEBSITE", "IMAGE", {"other" => "OTHER"}, "UID")
27
+ Mbrao::Author.create({name: "NAME", email: "EMAIL", website: "WEBSITE", image: "IMAGE", other: "OTHER", uid: "UID"})
28
+ end
29
+ end
30
+
31
+ describe "#initialize" do
32
+ it "create a new object" do
33
+ reference = Mbrao::Author.new("NAME", "name@example.com", "http://example.com", "http://example.com/image.jpg", {a: {b: :c}})
34
+ expect(reference.name).to eq("NAME")
35
+ expect(reference.email).to eq("name@example.com")
36
+ expect(reference.website).to eq("http://example.com")
37
+ expect(reference.image).to eq("http://example.com/image.jpg")
38
+ expect(reference.metadata).to eq({"a" => {"b" => :c}})
39
+ end
40
+
41
+ it "make sure that email is valid" do
42
+ reference = Mbrao::Author.new("NAME", "INVALID", "http://example.com", "http://example.com/image.jpg", {a: {b: :c}})
43
+ expect(reference.email).to be_nil
44
+ end
45
+
46
+ it "make sure that website is a valid URL" do
47
+ reference = Mbrao::Author.new("NAME", "name@example.com", "INVALID", "http://example.com/image.jpg", {a: {b: :c}})
48
+ expect(reference.website).to be_nil
49
+ end
50
+
51
+ it "make sure that image is a valid URL" do
52
+ reference = Mbrao::Author.new("NAME", "name@example.com", "http://example.com", "INVALID", {a: {b: :c}})
53
+ expect(reference.image).to be_nil
54
+ end
55
+
56
+ it "make sure that hash is a recursively a HashWithIndifferentAccess" do
57
+ reference = Mbrao::Author.new("NAME", "name@example.com", "http://example.com", "http://example.com/image.jpg", {a: {b: :c}})
58
+ expect(reference.metadata).to be_a(HashWithIndifferentAccess)
59
+ expect(reference.metadata["a"]).to be_a(HashWithIndifferentAccess)
60
+ end
61
+ end
62
+ end