Module: MiddlemanMdocs::Resource
- Defined in:
- lib/middleman-mdocs/resource.rb
Instance Attribute Summary collapse
-
#extension ⇒ Object
Returns the value of attribute extension.
-
#mdocs ⇒ Object
Returns the value of attribute mdocs.
-
#updated ⇒ Object
Returns the value of attribute updated.
Class Method Summary collapse
Instance Method Summary collapse
- #add_keywords(*args) ⇒ Object
- #add_meta(opts) ⇒ Object
- #add_options(opts) ⇒ Object
- #add_tags(*args) ⇒ Object
- #cache_key(*key) ⇒ Object
- #copy_from(other) ⇒ Object
- #created_at ⇒ Object
- #data ⇒ Object
- #destination_path(*args) ⇒ Object
- #force_render(key = :force) ⇒ Object
- #has_meta?(*keys) ⇒ Boolean
- #has_tags?(*keys) ⇒ Boolean
- #html? ⇒ Boolean
- #keywords ⇒ Object
- #markdown? ⇒ Boolean
- #meta_match?(meta) ⇒ Boolean
- #metadata ⇒ Object
- #ready? ⇒ Boolean
- #refresh ⇒ Object
- #render(opts = {}, locs = {}) ⇒ Object
- #tags ⇒ Object
- #text ⇒ Object
- #timestamp ⇒ Object
- #title ⇒ Object
- #toc ⇒ Object
- #touch ⇒ Object
- #updated_at ⇒ Object
Instance Attribute Details
#extension ⇒ Object
Returns the value of attribute extension.
3 4 5 |
# File 'lib/middleman-mdocs/resource.rb', line 3 def extension @extension end |
#mdocs ⇒ Object
Returns the value of attribute mdocs.
3 4 5 |
# File 'lib/middleman-mdocs/resource.rb', line 3 def mdocs @mdocs end |
#updated ⇒ Object
Returns the value of attribute updated.
3 4 5 |
# File 'lib/middleman-mdocs/resource.rb', line 3 def updated @updated end |
Class Method Details
.normalize_keywords(*args) ⇒ Object
11 12 13 14 15 |
# File 'lib/middleman-mdocs/resource.rb', line 11 def self.normalize_keywords(*args) raw = args.flatten.compact.map(&:to_s) normalized = (raw) (raw + normalized).uniq.sort end |
.normalize_tags(*args) ⇒ Object
5 6 7 8 9 |
# File 'lib/middleman-mdocs/resource.rb', line 5 def self.(*args) args.flatten.compact.map(&:to_s).map do |tag| ::MiddlemanMdocs::Controller.tr(tag) end.map(&:upcase).uniq.sort end |
Instance Method Details
#add_keywords(*args) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/middleman-mdocs/resource.rb', line 148 def add_keywords(*args) return unless args.flatten.any? added = ::MiddlemanMdocs::Resource.normalize_keywords(args) return if (added - ([:keywords] || [])).empty? cache_reset(:keywords) [:keywords] = ::MiddlemanMdocs::Resource.normalize_keywords([:tags], [:keywords], data[:keywords], [:keywords], added) end |
#add_meta(opts) ⇒ Object
134 135 136 |
# File 'lib/middleman-mdocs/resource.rb', line 134 def (opts) .deep_merge!(opts) end |
#add_options(opts) ⇒ Object
130 131 132 |
# File 'lib/middleman-mdocs/resource.rb', line 130 def (opts) .deep_merge!(opts) end |
#add_tags(*args) ⇒ Object
138 139 140 141 142 143 144 145 146 |
# File 'lib/middleman-mdocs/resource.rb', line 138 def (*args) return unless args.flatten.any? added = ::MiddlemanMdocs::Resource.(args) return if (added - ([:tags] || [])).empty? cache_reset(:tags) [:tags] = ::MiddlemanMdocs::Resource.([:tags], data[:tags], [:tags], added) end |
#cache_key(*key) ⇒ Object
193 194 195 |
# File 'lib/middleman-mdocs/resource.rb', line 193 def cache_key(*key) "#{page_id}/#{}/#{key.map(&:to_s).sort.join('_')}" end |
#copy_from(other) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/middleman-mdocs/resource.rb', line 43 def copy_from(other) resource = self unless @patched data.define_singleton_method(:title) do resource.title end data.define_singleton_method(:keywords) do resource.keywords end data.define_singleton_method(:text) do resource.text end @patched = true end custom_deep_merge!(data, other.data) custom_deep_merge!(, other.) self.updated = other.updated end |
#created_at ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/middleman-mdocs/resource.rb', line 75 def created_at @created_at ||= begin ts = data[:created_at] || [:created_at] || File.ctime(source_file) ts = DateTime.parse(ts) if ts.is_a?(String) [ts, updated_at].min end end |
#data ⇒ Object
33 34 35 36 |
# File 'lib/middleman-mdocs/resource.rb', line 33 def data $data_cache ||= {} $data_cache[page_id] ||= super end |
#destination_path(*args) ⇒ Object
176 177 178 |
# File 'lib/middleman-mdocs/resource.rb', line 176 def destination_path(*args) super.force_encoding(Encoding.default_external) end |
#force_render(key = :force) ⇒ Object
180 181 182 |
# File 'lib/middleman-mdocs/resource.rb', line 180 def force_render key = :force render({ layout: false, key: key}, { current_path: path, force_current_page: self }) end |
#has_meta?(*keys) ⇒ Boolean
166 167 168 169 170 |
# File 'lib/middleman-mdocs/resource.rb', line 166 def (*keys) keys.compact! keys.uniq! (.keys.uniq.sort & keys).size == keys.size end |
#has_tags?(*keys) ⇒ Boolean
172 173 174 |
# File 'lib/middleman-mdocs/resource.rb', line 172 def (*keys) ( & keys).size == keys.size end |
#html? ⇒ Boolean
21 22 23 |
# File 'lib/middleman-mdocs/resource.rb', line 21 def html? @html ||= destination_path.end_with?('.html') end |
#keywords ⇒ Object
113 114 115 116 117 118 |
# File 'lib/middleman-mdocs/resource.rb', line 113 def keywords with_cache(:keywords) do [:keywords] = ::MiddlemanMdocs::Resource.normalize_keywords(, [:keywords], data[:keywords], [:keywords]) end end |
#markdown? ⇒ Boolean
17 18 19 |
# File 'lib/middleman-mdocs/resource.rb', line 17 def markdown? [:markdown] end |
#meta_match?(meta) ⇒ Boolean
160 161 162 163 164 |
# File 'lib/middleman-mdocs/resource.rb', line 160 def () .all? do |k, v| .include?(k) && ([k].is_a?(Array) ? [k].include?(v) : [k] == v) end end |
#metadata ⇒ Object
38 39 40 41 |
# File 'lib/middleman-mdocs/resource.rb', line 38 def $metadata_cache ||= {} $metadata_cache[destination_path] ||= super end |
#ready? ⇒ Boolean
25 26 27 |
# File 'lib/middleman-mdocs/resource.rb', line 25 def ready? mdocs.ready? end |
#refresh ⇒ Object
93 94 95 96 97 |
# File 'lib/middleman-mdocs/resource.rb', line 93 def refresh @guards ||= {} # @updated = SecureRandom.hex end |
#render(opts = {}, locs = {}) ⇒ Object
184 185 186 187 188 189 190 191 |
# File 'lib/middleman-mdocs/resource.rb', line 184 def render(opts = {}, locs = {}) return super if nocache? || locs.has_key?('rack') || locs.has_key?(:rack) with_cache(Digest::MD5.hexdigest(opts.to_json + locs.to_json) + 'render') do # puts "REFRESH: #{binary?} #{page_id}: #{opts.to_json + locs.to_json}" super end end |
#tags ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/middleman-mdocs/resource.rb', line 103 def with_cache(:tags) do guard_recursive('tags', page_id, []) do |_key| text end [:tags] = ::MiddlemanMdocs::Resource.([:tags], data[:tags], [:tags]) end end |
#text ⇒ Object
99 100 101 |
# File 'lib/middleman-mdocs/resource.rb', line 99 def text force_render(:text) if ![:ignore] && html? end |
#timestamp ⇒ Object
83 84 85 |
# File 'lib/middleman-mdocs/resource.rb', line 83 def (mdocs.dependencies(self).map(&:timestamp) + [File.mtime(File.realdirpath(source_file)), File.mtime(source_file)]).max end |
#title ⇒ Object
87 88 89 90 91 |
# File 'lib/middleman-mdocs/resource.rb', line 87 def title with_cache(:title) do data[:title] || [:title] || toc&.children&.first&.raw_text end end |
#toc ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'lib/middleman-mdocs/resource.rb', line 120 def toc with_cache(:toc) do if [:ignore] || !html? nil else mdocs.table_of_contents(self) end end end |
#touch ⇒ Object
29 30 31 |
# File 'lib/middleman-mdocs/resource.rb', line 29 def touch @updated = SecureRandom.hex end |
#updated_at ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/middleman-mdocs/resource.rb', line 67 def updated_at @updated_at ||= begin ts = data[:updated_at] || [:updated_at] || ts = DateTime.parse(ts) if ts.is_a?(String) ts end end |