benoit 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. data/.gitignore +4 -0
  2. data/.gitmodules +3 -0
  3. data/.rspec +1 -0
  4. data/.rspec-turnip +1 -0
  5. data/.ruby-version +1 -0
  6. data/Assetfile +44 -0
  7. data/Gemfile +5 -0
  8. data/Gemfile.lock +110 -0
  9. data/Rakefile +9 -0
  10. data/benoit.gemspec +50 -0
  11. data/bin/benoit +121 -0
  12. data/bin/bundle-development +1 -0
  13. data/bin/bundle-sandbox +1 -0
  14. data/lib/benoit.rb +36 -0
  15. data/lib/benoit/cadenza.rb +11 -0
  16. data/lib/benoit/cadenza/output_filters.rb +21 -0
  17. data/lib/benoit/cleaner.rb +10 -0
  18. data/lib/benoit/compiler_error.rb +49 -0
  19. data/lib/benoit/configuration.rb +24 -0
  20. data/lib/benoit/current_site.rb +12 -0
  21. data/lib/benoit/file_wrapper_extensions.rb +13 -0
  22. data/lib/benoit/filters.rb +15 -0
  23. data/lib/benoit/filters/base_filter.rb +44 -0
  24. data/lib/benoit/filters/blacklist_filter.rb +18 -0
  25. data/lib/benoit/filters/cadenza_filter.rb +75 -0
  26. data/lib/benoit/filters/content_page_filter.rb +45 -0
  27. data/lib/benoit/filters/markdown_filter.rb +19 -0
  28. data/lib/benoit/filters/metadata_cleaner.rb +30 -0
  29. data/lib/benoit/filters/move_to_root_filter.rb +22 -0
  30. data/lib/benoit/filters/pagination_filter.rb +23 -0
  31. data/lib/benoit/filters/pass_thru_filter.rb +22 -0
  32. data/lib/benoit/filters/sass_filter.rb +74 -0
  33. data/lib/benoit/filters/set_metadata_filter.rb +15 -0
  34. data/lib/benoit/logger.rb +69 -0
  35. data/lib/benoit/page.rb +88 -0
  36. data/lib/benoit/page_metadata.rb +8 -0
  37. data/lib/benoit/page_metadata/container.rb +74 -0
  38. data/lib/benoit/page_metadata/json_converter.rb +28 -0
  39. data/lib/benoit/page_metadata/parser.rb +38 -0
  40. data/lib/benoit/page_metadata/store.rb +75 -0
  41. data/lib/benoit/pipeline.rb +1 -0
  42. data/lib/benoit/pipeline/dsl_extensions.rb +8 -0
  43. data/lib/benoit/pipeline/pagination_matcher.rb +53 -0
  44. data/lib/benoit/pipeline_project.rb +81 -0
  45. data/lib/benoit/site_context.rb +123 -0
  46. data/lib/benoit/utils/finds_layouts_for_template.rb +77 -0
  47. data/lib/benoit/utils/normalizes_path_to_template.rb +43 -0
  48. data/lib/benoit/utils/paginated_list.rb +102 -0
  49. data/lib/benoit/version.rb +3 -0
  50. data/lib/build_notifiers/file_built_notifier.rb +20 -0
  51. data/lib/build_notifiers/progress_notifier.rb +57 -0
  52. data/lib/cli.rb +39 -0
  53. data/spec/.rbenv-gemsets +1 -0
  54. data/spec/Gemfile +9 -0
  55. data/spec/Gemfile.lock +57 -0
  56. data/spec/bin/autospec +16 -0
  57. data/spec/bin/cucumber +16 -0
  58. data/spec/bin/htmldiff +16 -0
  59. data/spec/bin/ldiff +16 -0
  60. data/spec/bin/rspec +16 -0
  61. data/spec/features/build_command.feature +46 -0
  62. data/spec/features/frontmatter_metadata.feature +99 -0
  63. data/spec/features/javascript_files.feature +35 -0
  64. data/spec/features/output_filters.feature +20 -0
  65. data/spec/features/page_layouts.feature +72 -0
  66. data/spec/features/pagination.feature +58 -0
  67. data/spec/features/sass_files.feature +30 -0
  68. data/spec/features/version.feature +5 -0
  69. data/spec/lib/filters/base_filter_spec.rb +141 -0
  70. data/spec/lib/filters/markdown_filter_spec.rb +65 -0
  71. data/spec/lib/filters/sass_filter_spec.rb +73 -0
  72. data/spec/lib/metadata_json_converter_spec.rb +65 -0
  73. data/spec/lib/metadata_store_spec.rb +148 -0
  74. data/spec/lib/page_spec.rb +19 -0
  75. data/spec/lib/site_context_spec.rb +106 -0
  76. data/spec/spec_helper.rb +16 -0
  77. data/spec/steps/output_file_steps.rb +45 -0
  78. data/spec/steps/run_steps.rb +71 -0
  79. data/spec/steps/staticly_steps.rb +194 -0
  80. data/spec/support/aruba/rspec.rb +66 -0
  81. data/spec/support/files/img.png +0 -0
  82. data/spec/support/files/input.css.scss +7 -0
  83. data/spec/support/files/input.scss +7 -0
  84. data/spec/support/spec_helpers/file_helpers.rb +8 -0
  85. data/spec/support/spec_helpers/memory_file_wrapper.rb +43 -0
  86. data/spec/support/spec_helpers/memory_manifest.rb +19 -0
  87. data/spec/turnip_helper.rb +19 -0
  88. data/vendor/frontmatter/frontmatter.gemspec +24 -0
  89. data/vendor/frontmatter/lib/frontmatter.rb +92 -0
  90. data/vendor/frontmatter/lib/frontmatter/version.rb +3 -0
  91. data/vendor/rake-pipeline/.gitignore +18 -0
  92. data/vendor/rake-pipeline/.rspec +1 -0
  93. data/vendor/rake-pipeline/.travis.yml +12 -0
  94. data/vendor/rake-pipeline/.yardopts +2 -0
  95. data/vendor/rake-pipeline/GETTING_STARTED.md +268 -0
  96. data/vendor/rake-pipeline/Gemfile +14 -0
  97. data/vendor/rake-pipeline/LICENSE +20 -0
  98. data/vendor/rake-pipeline/README.markdown +11 -0
  99. data/vendor/rake-pipeline/README.yard +178 -0
  100. data/vendor/rake-pipeline/Rakefile +21 -0
  101. data/vendor/rake-pipeline/bin/rakep +4 -0
  102. data/vendor/rake-pipeline/examples/copying_files.md +12 -0
  103. data/vendor/rake-pipeline/examples/minifying_files.md +37 -0
  104. data/vendor/rake-pipeline/examples/modifying_pipelines.md +67 -0
  105. data/vendor/rake-pipeline/examples/multiple_pipelines.md +77 -0
  106. data/vendor/rake-pipeline/lib/generators/rake/pipeline/install/install_generator.rb +70 -0
  107. data/vendor/rake-pipeline/lib/rake-pipeline.rb +509 -0
  108. data/vendor/rake-pipeline/lib/rake-pipeline/cli.rb +57 -0
  109. data/vendor/rake-pipeline/lib/rake-pipeline/dsl.rb +9 -0
  110. data/vendor/rake-pipeline/lib/rake-pipeline/dsl/pipeline_dsl.rb +246 -0
  111. data/vendor/rake-pipeline/lib/rake-pipeline/dsl/project_dsl.rb +108 -0
  112. data/vendor/rake-pipeline/lib/rake-pipeline/dynamic_file_task.rb +194 -0
  113. data/vendor/rake-pipeline/lib/rake-pipeline/error.rb +17 -0
  114. data/vendor/rake-pipeline/lib/rake-pipeline/file_wrapper.rb +195 -0
  115. data/vendor/rake-pipeline/lib/rake-pipeline/filter.rb +267 -0
  116. data/vendor/rake-pipeline/lib/rake-pipeline/filters.rb +4 -0
  117. data/vendor/rake-pipeline/lib/rake-pipeline/filters/concat_filter.rb +63 -0
  118. data/vendor/rake-pipeline/lib/rake-pipeline/filters/gsub_filter.rb +56 -0
  119. data/vendor/rake-pipeline/lib/rake-pipeline/filters/ordering_concat_filter.rb +38 -0
  120. data/vendor/rake-pipeline/lib/rake-pipeline/filters/pipeline_finalizing_filter.rb +21 -0
  121. data/vendor/rake-pipeline/lib/rake-pipeline/graph.rb +178 -0
  122. data/vendor/rake-pipeline/lib/rake-pipeline/manifest.rb +82 -0
  123. data/vendor/rake-pipeline/lib/rake-pipeline/manifest_entry.rb +34 -0
  124. data/vendor/rake-pipeline/lib/rake-pipeline/matcher.rb +141 -0
  125. data/vendor/rake-pipeline/lib/rake-pipeline/middleware.rb +73 -0
  126. data/vendor/rake-pipeline/lib/rake-pipeline/precompile.rake +8 -0
  127. data/vendor/rake-pipeline/lib/rake-pipeline/project.rb +338 -0
  128. data/vendor/rake-pipeline/lib/rake-pipeline/rails_plugin.rb +10 -0
  129. data/vendor/rake-pipeline/lib/rake-pipeline/railtie.rb +34 -0
  130. data/vendor/rake-pipeline/lib/rake-pipeline/reject_matcher.rb +29 -0
  131. data/vendor/rake-pipeline/lib/rake-pipeline/server.rb +15 -0
  132. data/vendor/rake-pipeline/lib/rake-pipeline/sorted_pipeline.rb +19 -0
  133. data/vendor/rake-pipeline/lib/rake-pipeline/version.rb +6 -0
  134. data/vendor/rake-pipeline/rails/init.rb +2 -0
  135. data/vendor/rake-pipeline/rake-pipeline.gemspec +24 -0
  136. data/vendor/rake-pipeline/spec/cli_spec.rb +73 -0
  137. data/vendor/rake-pipeline/spec/concat_filter_spec.rb +37 -0
  138. data/vendor/rake-pipeline/spec/dsl/pipeline_dsl_spec.rb +165 -0
  139. data/vendor/rake-pipeline/spec/dsl/project_dsl_spec.rb +41 -0
  140. data/vendor/rake-pipeline/spec/dynamic_file_task_spec.rb +119 -0
  141. data/vendor/rake-pipeline/spec/encoding_spec.rb +106 -0
  142. data/vendor/rake-pipeline/spec/file_wrapper_spec.rb +132 -0
  143. data/vendor/rake-pipeline/spec/filter_spec.rb +367 -0
  144. data/vendor/rake-pipeline/spec/graph_spec.rb +56 -0
  145. data/vendor/rake-pipeline/spec/gsub_filter_spec.rb +87 -0
  146. data/vendor/rake-pipeline/spec/manifest_entry_spec.rb +46 -0
  147. data/vendor/rake-pipeline/spec/manifest_spec.rb +67 -0
  148. data/vendor/rake-pipeline/spec/matcher_spec.rb +141 -0
  149. data/vendor/rake-pipeline/spec/middleware_spec.rb +199 -0
  150. data/vendor/rake-pipeline/spec/ordering_concat_filter_spec.rb +42 -0
  151. data/vendor/rake-pipeline/spec/pipeline_spec.rb +232 -0
  152. data/vendor/rake-pipeline/spec/project_spec.rb +295 -0
  153. data/vendor/rake-pipeline/spec/rake_acceptance_spec.rb +720 -0
  154. data/vendor/rake-pipeline/spec/rake_tasks_spec.rb +21 -0
  155. data/vendor/rake-pipeline/spec/reject_matcher_spec.rb +31 -0
  156. data/vendor/rake-pipeline/spec/sorted_pipeline_spec.rb +27 -0
  157. data/vendor/rake-pipeline/spec/spec_helper.rb +38 -0
  158. data/vendor/rake-pipeline/spec/support/spec_helpers/file_utils.rb +35 -0
  159. data/vendor/rake-pipeline/spec/support/spec_helpers/filters.rb +37 -0
  160. data/vendor/rake-pipeline/spec/support/spec_helpers/input_helpers.rb +23 -0
  161. data/vendor/rake-pipeline/spec/support/spec_helpers/memory_file_wrapper.rb +35 -0
  162. data/vendor/rake-pipeline/spec/support/spec_helpers/memory_manifest.rb +19 -0
  163. data/vendor/rake-pipeline/tools/perfs +101 -0
  164. metadata +497 -0
@@ -0,0 +1,194 @@
1
+ require 'yaml'
2
+ class TestSite
3
+ include Aruba::Api
4
+ attr_reader :name
5
+ attr_reader :pages
6
+
7
+ def initialize
8
+ @pages = []
9
+ end
10
+
11
+ def add_page(page)
12
+ @pages << page
13
+ end
14
+
15
+ def delete_page(page)
16
+ @pages = @pages.reject { |p| p.name == page.name }
17
+ end
18
+
19
+ def generate_name
20
+ @name = "site_#{Time.now.to_i}"
21
+ end
22
+ end
23
+
24
+ class Page
25
+ attr_reader :metadata, :name
26
+ attr_accessor :content
27
+
28
+ def initialize(name=nil)
29
+ @name = name
30
+ @metadata = {}
31
+ end
32
+
33
+ def add_metadata(yaml)
34
+ YAML.load(yaml).tap do |m|
35
+ @metadata.merge! m
36
+ end
37
+ end
38
+
39
+ def generate_name(extname)
40
+ num = 1_000_000 + Random.rand(10_000_000 - 1_000_000)
41
+ @name = "page_#{num}#{extname}"
42
+ end
43
+
44
+ def frontmatter(hash)
45
+ unless hash.empty?
46
+ %{#{hash.to_yaml}
47
+ ---}.gsub(/^\s+/, "")
48
+ end.to_s
49
+ end
50
+
51
+ def full_content
52
+ %{#{frontmatter(metadata)}
53
+
54
+ #{@content}}.gsub(/^\s+/, "")
55
+ end
56
+
57
+ end
58
+
59
+ module BenoitSteps
60
+ extend Turnip::DSL
61
+
62
+ def create_file_for_page(page, include_metadata=true)
63
+ content = if include_metadata
64
+ page.full_content
65
+ else
66
+ page.content
67
+ end
68
+ step "a file named \"#{page.name}\" with content:", content
69
+ end
70
+
71
+ def delete_file_for_page(page)
72
+ step 'I remove the file "%s"' % page.name
73
+ end
74
+
75
+ step "I see the current Benoit version" do
76
+ require_relative "../../lib/benoit/version"
77
+ assert_partial_output(Benoit::VERSION, all_output)
78
+ end
79
+
80
+ step "a site" do
81
+ @site = TestSite.new
82
+ @site.generate_name
83
+ step "a site named #{@site.name}"
84
+ end
85
+
86
+ step "a site named :site" do |site|
87
+ create_dir site
88
+ step 'I cd to "%s"' % site
89
+ step 'an empty file named "index.html"'
90
+ end
91
+
92
+ step "a cache directory should exist for that site" do
93
+ path = File.expand_path(File.join("~", ".benoit", "tmpcache", @site.name))
94
+ step 'a directory named "%s" should exist' % path
95
+ end
96
+
97
+ step ":file_name should not exist in the output site" do |file_name|
98
+ file_name = File.join("_build", file_name)
99
+ step 'a file named "%s" should not exist' % file_name
100
+ end
101
+
102
+ step "I build the site" do
103
+ step "I successfully run `benoit build`"
104
+ end
105
+
106
+ step "I build the site with the flag :flag" do |flag|
107
+ step "I successfully run `benoit build #{flag}`"
108
+ end
109
+
110
+ step ":n files containing metadata:" do |n, yaml_snippet|
111
+ n.to_i.times do |i|
112
+ yaml = yaml_snippet + "\npage_num: #{i}"
113
+ step 'a file with an extension of ".html" containing metadata:', yaml
114
+ step 'that file has content:', i.to_s
115
+ end
116
+ end
117
+
118
+ step "a file" do
119
+ step 'a file with an extension of ".html"'
120
+ end
121
+
122
+ step "a file with an extension of :extname" do |extname|
123
+ @page = Page.new
124
+ @page.generate_name(extname)
125
+ @site.add_page @page
126
+ create_file_for_page @page
127
+ end
128
+
129
+ step "a file containing metadata:" do |yaml_snippet|
130
+ step 'a file with an extension of ".html" containing metadata:', yaml_snippet
131
+ end
132
+
133
+ step "a file named :file_name containing metadata:" do |file_name,yaml_snippet|
134
+ @page = Page.new(file_name)
135
+ @site.add_page @page
136
+ @page.add_metadata yaml_snippet
137
+ create_file_for_page @page
138
+ end
139
+
140
+ step "a file with an extension of :extname containing metadata:" do |extname, yaml_snippet|
141
+ @page = Page.new
142
+ @page.generate_name(extname)
143
+ @site.add_page @page
144
+ @page.add_metadata yaml_snippet
145
+ create_file_for_page @page
146
+ end
147
+
148
+ step "a file wih an extension of :extname with content:" do |extname, content|
149
+ @page = Page.new
150
+ @page.generate_name(extname)
151
+ @site.add_page @page
152
+ @page.content = content
153
+ create_file_for_page @page, false
154
+ end
155
+
156
+ step "that file has content:" do |content|
157
+ @page.content = content
158
+ create_file_for_page @page
159
+ end
160
+
161
+ step "a layout named :name with content:" do |name, content|
162
+ name = "_layouts/" + name + ".html" unless File.extname(name) == ".html"
163
+ @layout = Page.new(name)
164
+ @layout.content = content
165
+ @site.add_page @layout
166
+ create_file_for_page @layout
167
+ end
168
+
169
+ step "I delete the file" do
170
+ @site.delete_page @page
171
+ delete_file_for_page @page
172
+ end
173
+
174
+ step "the site has a file that is an image" do
175
+ @image_path = "spec/support/files/img.png"
176
+ img_content = File.read(@image_path)
177
+ write_file("img.png", img_content)
178
+ end
179
+
180
+ step "that file should not exist in the output site" do
181
+ path = File.join("_build", @page.name)
182
+ step 'a file named "%s" should not exist' % path
183
+ end
184
+
185
+ step "that file should exist in the output site" do
186
+ path = File.join("_build", @page.name)
187
+ step 'a file named "%s" should exist' % path
188
+ end
189
+
190
+ step "the image should exist in the output site" do
191
+ step 'a file named "%s" should exist' % ["_build/img.png"]
192
+ end
193
+
194
+ end
@@ -0,0 +1,66 @@
1
+ require 'aruba'
2
+ require 'aruba/api'
3
+
4
+ RSpec.configure do |config|
5
+ config.include Aruba::Api
6
+
7
+ config.treat_symbols_as_metadata_keys_with_true_values = true
8
+
9
+ config.before :each, :"disable-bundler" do
10
+ unset_bundler_env_vars
11
+ end
12
+
13
+ config.before :each do
14
+ @__aruba_original_paths = (ENV['PATH'] || '').split(File::PATH_SEPARATOR)
15
+ ENV['PATH'] = ([File.expand_path('bin')] + @__aruba_original_paths).join(File::PATH_SEPARATOR)
16
+ end
17
+
18
+ config.after :each do
19
+ ENV['PATH'] = @__aruba_original_paths.join(File::PATH_SEPARATOR)
20
+ end
21
+
22
+ config.before :each do
23
+ FileUtils.rm_rf(current_dir) unless example.metadata[:"no-clobber"]
24
+ end
25
+
26
+ config.before :each, :puts do
27
+ @puts = true
28
+ end
29
+
30
+ config.before :each, :"announce-cmd" do
31
+ @announce_cmd = true
32
+ end
33
+
34
+ config.before :each, :"announce-dir" do
35
+ @announce_dir = true
36
+ end
37
+
38
+ config.before :each, :"announce-stdout" do
39
+ @announce_stdout = true
40
+ end
41
+
42
+ config.before :each, :"announce-stderr" do
43
+ @announce_stderr = true
44
+ end
45
+
46
+ config.before :each, :"announce-env" do
47
+ @announce_end = true
48
+ end
49
+
50
+ config.before :each, :announce do
51
+ @announce_stdout = true
52
+ @announce_stderr = true
53
+ @announce_cmd = true
54
+ @announce_dir = true
55
+ @announce_env = true
56
+ end
57
+
58
+ config.before :each, :ansi do
59
+ @aruba_keep_ansi = true
60
+ end
61
+
62
+ config.after :each do
63
+ restore_env
64
+ end
65
+
66
+ end
@@ -0,0 +1,7 @@
1
+ $variable: value;
2
+
3
+ .test {
4
+ .nested {
5
+ attribute: $variable;
6
+ }
7
+ }
@@ -0,0 +1,7 @@
1
+ $variable: value;
2
+
3
+ .test {
4
+ .nested {
5
+ attribute: $variable;
6
+ }
7
+ }
@@ -0,0 +1,8 @@
1
+ module FileHelpers
2
+ def write_file(path, content)
3
+ File.open(path, "w+") do |f|
4
+ f << content
5
+ f.path
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,43 @@
1
+ class Rake::Pipeline
2
+ module SpecHelpers
3
+ class MemoryFileWrapper < Struct.new(:root, :path, :encoding, :original_inputs, :body)
4
+
5
+ @@files = {}
6
+ @@data = {}
7
+
8
+ def self.files
9
+ @@files
10
+ end
11
+
12
+ def self.data
13
+ @@data
14
+ end
15
+
16
+ def with_encoding(new_encoding)
17
+ self.class.new(root, path, new_encoding, original_inputs, body)
18
+ end
19
+
20
+ def original_inputs
21
+ Set.new([])
22
+ end
23
+
24
+ def fullpath
25
+ File.join(root, path)
26
+ end
27
+
28
+ def create
29
+ @@files[fullpath] = self
30
+ self.body = ""
31
+ yield
32
+ end
33
+
34
+ def read
35
+ body || @@data[fullpath] || ""
36
+ end
37
+
38
+ def write(contents)
39
+ self.body << contents
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,19 @@
1
+ class Rake::Pipeline
2
+ module SpecHelpers
3
+ class MemoryManifest
4
+ def initialize
5
+ @entries = {}
6
+ end
7
+
8
+ # Look up an entry by filename.
9
+ def [](key)
10
+ @entries[key]
11
+ end
12
+
13
+ # Set an entry
14
+ def []=(key, value)
15
+ @entries[key] = value
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require 'aruba/rspec' # loaded from spec/support
2
+ require 'turnip/kanban'
3
+
4
+ module Benoit
5
+ end
6
+
7
+ Dir["spec/steps/**/*.rb"].each do |step_file|
8
+ load step_file
9
+ end
10
+
11
+ RSpec.configure do |config|
12
+ config.include RunSteps
13
+ config.include BenoitSteps
14
+
15
+ config.before :each do
16
+ @aruba_timeout_seconds = 10
17
+ FileUtils.rm_rf File.expand_path("~/.benoit/tmpcache")
18
+ end
19
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "frontmatter/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "frontmatter"
7
+ s.version = Frontmatter::VERSION
8
+ s.authors = ["docunext"]
9
+ s.email = ["albert.lash@docunext.com"]
10
+ s.homepage = "http://www.docunext.com/wiki/YAML"
11
+ s.summary = %q{A gem to handle front matter}
12
+ s.description = %q{This gem is all about frontmatter.}
13
+
14
+ s.rubyforge_project = "frontmatter"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "cucumber"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
@@ -0,0 +1,92 @@
1
+ module FrontMatter
2
+
3
+ REGEX = /^(?<metadata>---\s*\n.*?\n?)^(---\s*$\n?)/m
4
+ INSPECT = 13
5
+ PARSER = Psych
6
+ REQUIRED = [:title, :date, :content]
7
+ OPTIONAL = [:author, :categories, :tags, :id, :layout, :template, :permalink, :published, :publish_path]
8
+
9
+ # Quick check to discover if a file might have frontmatter in it.
10
+ # Only checks if the first line starts with "---"
11
+ #
12
+ # path_to_file - the canonical path the the file
13
+ #
14
+ # Returns boolean
15
+ def self.file_might_have_frontmatter?(path_to_file)
16
+ File.open(path_to_file){|f| f.readline}[0..2] == '---'
17
+ end
18
+
19
+ # A more definitive check to find if a file has frontmatter in it.
20
+ # Since we are reading the file, we want to be prudent with with
21
+ # memory and only check LINES_TO_INSPECT
22
+ #
23
+ # path_to_file - the canonical path to the file
24
+ #
25
+ # Returns boolean
26
+ def self.file_has_frontmatter?(path_to_file)
27
+ IO.readlines(path_to_file,INSPECT).join =~ REGEX ? true : false
28
+ end
29
+
30
+
31
+ # Checks a string to find if it has valid frontmatter
32
+ #
33
+ # text - string containing frontmatter
34
+ #
35
+ # Returns boolean
36
+ def self.has_valid_frontmatter?(text)
37
+ front_matter = self.parse(text)
38
+ self.valid_frontmatter?(front_matter)
39
+ end
40
+
41
+ # Checks a hash to find if it has valid frontmatter
42
+ #
43
+ # front_matter - the frontmatter hash
44
+ #
45
+ # Returns boolean
46
+ def self.valid_frontmatter?(front_matter)
47
+ allkeys = REQUIRED
48
+ allkeys.concat(OPTIONAL)
49
+ REQUIRED.each {|key| front_matter.has_key?(key) }.all? &&
50
+ front_matter.keys.each{|key| allkeys.include?(key) }.all?
51
+ end
52
+
53
+ # Check if the text has front matter
54
+ #
55
+ # text - the string to check
56
+ #
57
+ # Returns boolean
58
+ def self.has_frontmatter?(text)
59
+ text =~ REGEX ? true : false
60
+ end
61
+
62
+ # Load a file and parse the frontmatter
63
+ #
64
+ # path_to_file
65
+ #
66
+ # Returns a hash.
67
+ def self.file_parse(path_to_file)
68
+ text = File.open(path_to_file){|f| f.read}
69
+ front_matter = self.parse(text)
70
+ end
71
+
72
+ # Parse the YAML frontmatter.
73
+ #
74
+ # text - The String path to the frontmatter and the content.
75
+ #
76
+ # Returns a hash containing the parsed frontmatter data and the content.
77
+ def self.parse(text)
78
+
79
+ front_matter = Hash.new
80
+
81
+ if md = text.match(REGEX)
82
+ front_matter['content'] = md.post_match
83
+ front_matter.merge!(PARSER.load(md[:metadata]))
84
+ end
85
+
86
+ front_matter
87
+
88
+ rescue => e
89
+ puts "YAML Exception reading #{name}: #{e.message}"
90
+ end
91
+
92
+ end