Class: Middleman::CoreExtensions::FrontMatter::FrontmatterManager
- Inherits:
-
Object
- Object
- Middleman::CoreExtensions::FrontMatter::FrontmatterManager
- Defined in:
- lib/middleman-core/core_extensions/front_matter.rb
Constant Summary collapse
- YAML_ERRORS =
[ StandardError ]
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
Instance Method Summary collapse
- #clear_data(file) ⇒ Object
- #data(path) ⇒ Object
-
#frontmatter_and_content(path) ⇒ Array<Thor::CoreExt::HashWithIndifferentAccess, String>
Get the frontmatter and plain content from a file.
-
#initialize(app) ⇒ FrontmatterManager
constructor
A new instance of FrontmatterManager.
-
#manipulate_resource_list(resources) ⇒ void
Update the main sitemap resource list.
- #normalize_path(path) ⇒ Object
- #parse_json_front_matter(content) ⇒ Object
-
#parse_yaml_front_matter(content) ⇒ Array<Hash, String>
Parse YAML frontmatter out of a string.
Constructor Details
#initialize(app) ⇒ FrontmatterManager
Returns a new instance of FrontmatterManager.
54 55 56 57 |
# File 'lib/middleman-core/core_extensions/front_matter.rb', line 54 def initialize(app) @app = app @cache = {} end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
51 52 53 |
# File 'lib/middleman-core/core_extensions/front_matter.rb', line 51 def app @app end |
Instance Method Details
#clear_data(file) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/middleman-core/core_extensions/front_matter.rb', line 64 def clear_data(file) # Copied from Sitemap::Store#file_to_path, but without # removing the file extension file = File.(file, @app.root) prefix = @app.source_dir.sub(/\/$/, "") + "/" return unless file.include?(prefix) path = file.sub(prefix, "") @cache.delete(path) end |
#data(path) ⇒ Object
59 60 61 62 |
# File 'lib/middleman-core/core_extensions/front_matter.rb', line 59 def data(path) p = normalize_path(path) @cache[p] ||= frontmatter_and_content(p) end |
#frontmatter_and_content(path) ⇒ Array<Thor::CoreExt::HashWithIndifferentAccess, String>
Get the frontmatter and plain content from a file
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/middleman-core/core_extensions/front_matter.rb', line 132 def frontmatter_and_content(path) full_path = if Pathname(path).relative? File.join(@app.source_dir, path) else path end data = {} content = nil if !::Middleman::Util.binary?(full_path) content = File.read(full_path) begin if content =~ /\A.*coding:/ lines = content.split(/\n/) lines.shift content = lines.join("\n") end if result = parse_yaml_front_matter(content) data, content = result elsif result = parse_json_front_matter(content) data, content = result end rescue => e # Probably a binary file, move on end end [::Middleman::Util.recursively_enhance(data).freeze, content] end |
#manipulate_resource_list(resources) ⇒ void
This method returns an undefined value.
Update the main sitemap resource list
171 172 173 174 175 176 177 178 179 |
# File 'lib/middleman-core/core_extensions/front_matter.rb', line 171 def manipulate_resource_list(resources) resources.each do |r| if !r.proxy? && !r.data.nil? && r.data["ignored"] == true r.frontmatter_ignored = true end end resources end |
#normalize_path(path) ⇒ Object
165 166 167 |
# File 'lib/middleman-core/core_extensions/front_matter.rb', line 165 def normalize_path(path) path.sub(%r{^#{@app.source_dir}\/}, "") end |
#parse_json_front_matter(content) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/middleman-core/core_extensions/front_matter.rb', line 106 def parse_json_front_matter(content) json_regex = /\A(;;;\s*\n.*?\n?)^(;;;\s*$\n?)/m if content =~ json_regex content = content.sub(json_regex, "") begin json = ($1+$2).sub(";;;", "{").sub(";;;", "}") data = ActiveSupport::JSON.decode(json) rescue => e logger.error "JSON Exception: #{e.}" return false end else return false end [data, content] rescue [{}, content] end |
#parse_yaml_front_matter(content) ⇒ Array<Hash, String>
Parse YAML frontmatter out of a string
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/middleman-core/core_extensions/front_matter.rb', line 85 def parse_yaml_front_matter(content) yaml_regex = /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m if content =~ yaml_regex content = content.sub(yaml_regex, "") begin data = YAML.load($1) rescue *YAML_ERRORS => e logger.error "YAML Exception: #{e.}" return false end else return false end [data, content] rescue [{}, content] end |