Module: MiddlemanMdocs::Resource

Defined in:
lib/middleman-mdocs/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#extensionObject

Returns the value of attribute extension.



3
4
5
# File 'lib/middleman-mdocs/resource.rb', line 3

def extension
  @extension
end

#mdocsObject

Returns the value of attribute mdocs.



3
4
5
# File 'lib/middleman-mdocs/resource.rb', line 3

def mdocs
  @mdocs
end

#updatedObject

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 = normalize_tags(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.normalize_tags(*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 - (options[:keywords] || [])).empty?

  cache_reset(:keywords)
  options[:keywords] =
    ::MiddlemanMdocs::Resource.normalize_keywords(options[:tags], options[:keywords], data[:keywords], [:keywords],
                                                  added)
end

#add_meta(opts) ⇒ Object



134
135
136
# File 'lib/middleman-mdocs/resource.rb', line 134

def add_meta(opts)
  .deep_merge!(opts)
end

#add_options(opts) ⇒ Object



130
131
132
# File 'lib/middleman-mdocs/resource.rb', line 130

def add_options(opts)
  options.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 add_tags(*args)
  return unless args.flatten.any?

  added = ::MiddlemanMdocs::Resource.normalize_tags(args)
  return if (added - (options[:tags] || [])).empty?

  cache_reset(:tags)
  options[:tags] = ::MiddlemanMdocs::Resource.normalize_tags(options[: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}/#{timestamp}/#{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_atObject



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

#dataObject



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

Returns:

  • (Boolean)


166
167
168
169
170
# File 'lib/middleman-mdocs/resource.rb', line 166

def has_meta?(*keys)
  keys.compact!
  keys.uniq!
  (.keys.uniq.sort & keys).size == keys.size
end

#has_tags?(*keys) ⇒ Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/middleman-mdocs/resource.rb', line 172

def has_tags?(*keys)
  (tags & keys).size == keys.size
end

#html?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/middleman-mdocs/resource.rb', line 21

def html?
  @html ||= destination_path.end_with?('.html')
end

#keywordsObject



113
114
115
116
117
118
# File 'lib/middleman-mdocs/resource.rb', line 113

def keywords
  with_cache(:keywords) do
    options[:keywords] =
      ::MiddlemanMdocs::Resource.normalize_keywords(tags, options[:keywords], data[:keywords], [:keywords])
  end
end

#markdown?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/middleman-mdocs/resource.rb', line 17

def markdown?
  [:markdown]
end

#meta_match?(meta) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
163
164
# File 'lib/middleman-mdocs/resource.rb', line 160

def meta_match?(meta)
  meta.all? do |k, v|
    .include?(k) && ([k].is_a?(Array) ? [k].include?(v) : [k] == v)
  end
end

#metadataObject



38
39
40
41
# File 'lib/middleman-mdocs/resource.rb', line 38

def 
  $metadata_cache ||= {}
  $metadata_cache[destination_path] ||= super
end

#ready?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/middleman-mdocs/resource.rb', line 25

def ready?
  mdocs.ready?
end

#refreshObject



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

#tagsObject



103
104
105
106
107
108
109
110
111
# File 'lib/middleman-mdocs/resource.rb', line 103

def tags
  with_cache(:tags) do
    guard_recursive('tags', page_id, []) do |_key|
      text
    end

    options[:tags] = ::MiddlemanMdocs::Resource.normalize_tags(options[:tags], data[:tags], [:tags])
  end
end

#textObject



99
100
101
# File 'lib/middleman-mdocs/resource.rb', line 99

def text
  force_render(:text) if ![:ignore] && html?
end

#timestampObject



83
84
85
# File 'lib/middleman-mdocs/resource.rb', line 83

def timestamp
  (mdocs.dependencies(self).map(&:timestamp) + [File.mtime(File.realdirpath(source_file)), File.mtime(source_file)]).max
end

#titleObject



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

#tocObject



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

#touchObject



29
30
31
# File 'lib/middleman-mdocs/resource.rb', line 29

def touch
  @updated = SecureRandom.hex
end

#updated_atObject



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] || timestamp
    ts = DateTime.parse(ts) if ts.is_a?(String)
    ts
  end
end