Class: Middleman::CoreExtensions::FrontMatter::FrontmatterManager
- Inherits:
-
Object
- Object
- Middleman::CoreExtensions::FrontMatter::FrontmatterManager
- Defined in:
- middleman-core/lib/middleman-core/core_extensions/front_matter.rb
Instance Method Summary (collapse)
- - (Object) clear_data(path)
- - (Object) data(path)
-
- (Array<Thor::CoreExt::HashWithIndifferentAccess, String>) frontmatter_and_content(path)
Get the frontmatter and plain content from a file.
-
- (FrontmatterManager) initialize(app)
constructor
A new instance of FrontmatterManager.
-
- (void) manipulate_resource_list(resources)
Update the main sitemap resource list.
- - (Object) normalize_path(path)
- - (Object) parse_json_front_matter(content)
-
- (Array<Hash, String>) parse_yaml_front_matter(content)
Parse YAML frontmatter out of a string.
Constructor Details
- (FrontmatterManager) initialize(app)
A new instance of FrontmatterManager
47 48 49 50 |
# File 'middleman-core/lib/middleman-core/core_extensions/front_matter.rb', line 47 def initialize(app) @app = app @cache = {} end |
Instance Method Details
- (Object) clear_data(path)
57 58 59 60 |
# File 'middleman-core/lib/middleman-core/core_extensions/front_matter.rb', line 57 def clear_data(path) p = normalize_path(File.(path, @app.root)) @cache.delete(p) end |
- (Object) data(path)
52 53 54 55 |
# File 'middleman-core/lib/middleman-core/core_extensions/front_matter.rb', line 52 def data(path) p = normalize_path(path) @cache[p] ||= frontmatter_and_content(p) end |
- (Array<Thor::CoreExt::HashWithIndifferentAccess, String>) frontmatter_and_content(path)
Get the frontmatter and plain content from a file
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'middleman-core/lib/middleman-core/core_extensions/front_matter.rb', line 112 def frontmatter_and_content(path) full_path = File.(path, @app.source_dir) content = File.read(full_path) if result = parse_yaml_front_matter(content) data, content = result data = ::Middleman::Util.recursively_enhance(data).freeze elsif result = parse_json_front_matter(content) data, content = result data = ::Middleman::Util.recursively_enhance(data).freeze else data = {} end [data, content] end |
- (void) manipulate_resource_list(resources)
This method returns an undefined value.
Update the main sitemap resource list
135 136 137 138 139 140 141 142 143 |
# File 'middleman-core/lib/middleman-core/core_extensions/front_matter.rb', line 135 def manipulate_resource_list(resources) resources.each do |r| if !r.proxy? && r.data["ignored"] == true r.frontmatter_ignored = true end end resources end |
- (Object) normalize_path(path)
129 130 131 |
# File 'middleman-core/lib/middleman-core/core_extensions/front_matter.rb', line 129 def normalize_path(path) path.sub(@app.source_dir, "").sub(/^\//, "") end |
- (Object) parse_json_front_matter(content)
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'middleman-core/lib/middleman-core/core_extensions/front_matter.rb', line 86 def parse_json_front_matter(content) json_regex = /^(\{\{\{\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 puts "JSON Exception: #{e.}" return false end else return false end [data, content] rescue [{}, content] end |
- (Array<Hash, String>) parse_yaml_front_matter(content)
Parse YAML frontmatter out of a string
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'middleman-core/lib/middleman-core/core_extensions/front_matter.rb', line 65 def parse_yaml_front_matter(content) yaml_regex = /^(---\s*\n.*?\n?)^(---\s*$\n?)/m if content =~ yaml_regex content = content.sub(yaml_regex, "") begin data = YAML.load($1) rescue => e puts "YAML Exception: #{e.}" return false end else return false end [data, content] rescue [{}, content] end |