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 ⇒ Object
- #copy_from(other) ⇒ Object
- #created_at ⇒ Object
- #destination_path(*args) ⇒ Object
- #force_render ⇒ Object
- #has_meta?(*keys) ⇒ Boolean
- #has_tags?(*keys) ⇒ Boolean
- #html? ⇒ Boolean
- #keywords ⇒ Object
- #markdown? ⇒ Boolean
- #meta_match?(meta) ⇒ Boolean
- #ready? ⇒ Boolean
- #refresh ⇒ Object
- #render(opts = {}, locs = {}) ⇒ Object
- #tags ⇒ Object
- #text ⇒ Object
- #timestamp ⇒ Object
- #title ⇒ Object
- #toc ⇒ 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
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/middleman-mdocs/resource.rb', line 138 def add_keywords(*args) return unless args.flatten.any? added = ::MiddlemanMdocs::Resource.normalize_keywords(args) return if (added - ([:keywords] || [])).empty? @updated = SecureRandom.hex [:keywords] = ::MiddlemanMdocs::Resource.normalize_keywords([:tags], [:keywords], data[:keywords], [:keywords], added) end |
#add_meta(opts) ⇒ Object
124 125 126 |
# File 'lib/middleman-mdocs/resource.rb', line 124 def (opts) .deep_merge!(opts) end |
#add_options(opts) ⇒ Object
120 121 122 |
# File 'lib/middleman-mdocs/resource.rb', line 120 def (opts) .deep_merge!(opts) end |
#add_tags(*args) ⇒ Object
128 129 130 131 132 133 134 135 136 |
# File 'lib/middleman-mdocs/resource.rb', line 128 def (*args) return unless args.flatten.any? added = ::MiddlemanMdocs::Resource.(args) return if (added - ([:tags] || [])).empty? @updated = SecureRandom.hex [:tags] = ::MiddlemanMdocs::Resource.([:tags], data[:tags], [:tags], added) end |
#cache_key ⇒ Object
73 74 75 |
# File 'lib/middleman-mdocs/resource.rb', line 73 def cache_key "#{page_id}/#{}/" end |
#copy_from(other) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/middleman-mdocs/resource.rb', line 29 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
61 62 63 64 65 66 67 |
# File 'lib/middleman-mdocs/resource.rb', line 61 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 |
#destination_path(*args) ⇒ Object
166 167 168 |
# File 'lib/middleman-mdocs/resource.rb', line 166 def destination_path(*args) super.force_encoding(Encoding.default_external) end |
#force_render ⇒ Object
170 171 172 |
# File 'lib/middleman-mdocs/resource.rb', line 170 def force_render render({ layout: false }, { current_path: path, force_current_page: self }) end |
#has_meta?(*keys) ⇒ Boolean
156 157 158 159 160 |
# File 'lib/middleman-mdocs/resource.rb', line 156 def (*keys) keys.compact! keys.uniq! (.keys.uniq.sort & keys).size == keys.size end |
#has_tags?(*keys) ⇒ Boolean
162 163 164 |
# File 'lib/middleman-mdocs/resource.rb', line 162 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
103 104 105 106 107 108 |
# File 'lib/middleman-mdocs/resource.rb', line 103 def keywords with_cache(:keywords, @updated) 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
150 151 152 153 154 |
# File 'lib/middleman-mdocs/resource.rb', line 150 def () .all? do |k, v| .include?(k) && ([k].is_a?(Array) ? [k].include?(v) : [k] == v) end end |
#ready? ⇒ Boolean
25 26 27 |
# File 'lib/middleman-mdocs/resource.rb', line 25 def ready? mdocs.ready? end |
#refresh ⇒ Object
83 84 85 86 87 |
# File 'lib/middleman-mdocs/resource.rb', line 83 def refresh @guards ||= {} # @updated = SecureRandom.hex end |
#render(opts = {}, locs = {}) ⇒ Object
174 175 176 177 178 179 180 181 |
# File 'lib/middleman-mdocs/resource.rb', line 174 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
93 94 95 96 97 98 99 100 101 |
# File 'lib/middleman-mdocs/resource.rb', line 93 def with_cache(:tags, @updated) do guard_recursive('tags', page_id, []) do |_key| text end [:tags] = ::MiddlemanMdocs::Resource.([:tags], data[:tags], [:tags]) end end |
#text ⇒ Object
89 90 91 |
# File 'lib/middleman-mdocs/resource.rb', line 89 def text force_render if ![:ignore] && html? end |
#timestamp ⇒ Object
69 70 71 |
# File 'lib/middleman-mdocs/resource.rb', line 69 def (mdocs.dependencies(self).map(&:timestamp) + [File.mtime(File.realdirpath(source_file)), File.mtime(source_file)]).max end |
#title ⇒ Object
77 78 79 80 81 |
# File 'lib/middleman-mdocs/resource.rb', line 77 def title with_cache(:title, @updated) do data[:title] || [:title] || toc&.children&.first&.raw_text end end |
#toc ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'lib/middleman-mdocs/resource.rb', line 110 def toc with_cache(:toc, @updated) do if [:ignore] || !html? nil else mdocs.table_of_contents(self) end end end |
#updated_at ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/middleman-mdocs/resource.rb', line 53 def updated_at @updated_at ||= begin ts = data[:updated_at] || [:updated_at] || ts = DateTime.parse(ts) if ts.is_a?(String) ts end end |