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



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

  @updated = SecureRandom.hex
  options[:keywords] =
    ::MiddlemanMdocs::Resource.normalize_keywords(options[:tags], options[:keywords], data[:keywords], [:keywords],
                                                  added)
end

#add_meta(opts) ⇒ Object



124
125
126
# File 'lib/middleman-mdocs/resource.rb', line 124

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

#add_options(opts) ⇒ Object



120
121
122
# File 'lib/middleman-mdocs/resource.rb', line 120

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

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

  @updated = SecureRandom.hex
  options[:tags] = ::MiddlemanMdocs::Resource.normalize_tags(options[:tags], data[:tags], [:tags], added)
end

#cache_keyObject



73
74
75
# File 'lib/middleman-mdocs/resource.rb', line 73

def cache_key
  "#{page_id}/#{timestamp}/"
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_atObject



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_renderObject



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

Returns:

  • (Boolean)


156
157
158
159
160
# File 'lib/middleman-mdocs/resource.rb', line 156

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

#has_tags?(*keys) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

def keywords
  with_cache(:keywords, @updated) 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)


150
151
152
153
154
# File 'lib/middleman-mdocs/resource.rb', line 150

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

#ready?Boolean

Returns:

  • (Boolean)


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

def ready?
  mdocs.ready?
end

#refreshObject



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

#tagsObject



93
94
95
96
97
98
99
100
101
# File 'lib/middleman-mdocs/resource.rb', line 93

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

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

#textObject



89
90
91
# File 'lib/middleman-mdocs/resource.rb', line 89

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

#timestampObject



69
70
71
# File 'lib/middleman-mdocs/resource.rb', line 69

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

#titleObject



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

#tocObject



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_atObject



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