Class: MiddlemanMdocs::Extension

Inherits:
Middleman::Extension
  • Object
show all
Defined in:
lib/middleman-mdocs/extension.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, **_kwargs, &block) ⇒ Extension

Returns a new instance of Extension.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/middleman-mdocs/extension.rb', line 38

def initialize(*args, **_kwargs, &block)
  super

  # Only require dependency on activation
  require_relative 'deps'

  # @@cache ||= ActiveSupport::Cache::MemoryStore.new(size: 100.megabytes)
  @@cache ||= ActiveSupport::Cache::FileStore.new('tmp/mdocs/cache')

  register_helpers(::MiddlemanMdocs::Navigation)

  configure_application

  @mdocsmeta = {}

  @mdocs = Controller.new(self)

  ::ActiveSupport::Notifications.subscribe('sitemap.update.middleman') do
    lookup = @mdocs.sitemap.instance_variable_get('@_lookup_by_destination_path')
    @mdocs.sitemap.resources.each do |resource|
      lookup[::Middleman::Util.normalize_path(resource.destination_path)] = resource
    end
  end
end

Instance Attribute Details

#mdocsObject (readonly)

Returns the value of attribute mdocs.



28
29
30
# File 'lib/middleman-mdocs/extension.rb', line 28

def mdocs
  @mdocs
end

#mdocsmetaObject (readonly)

Returns the value of attribute mdocsmeta.



28
29
30
# File 'lib/middleman-mdocs/extension.rb', line 28

def mdocsmeta
  @mdocsmeta
end

#mdocsnavObject (readonly)

Returns the value of attribute mdocsnav.



28
29
30
# File 'lib/middleman-mdocs/extension.rb', line 28

def mdocsnav
  @mdocsnav
end

Instance Method Details

#after_buildObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/middleman-mdocs/extension.rb', line 90

def after_build
  $app = app
  if @sitemap_opts && @sitemap_block
    interpreter = SitemapGenerator::Interpreter.new(@sitemap_opts.merge(compress: true))
    interpreter.instance_eval(&@sitemap_block)

    interpreter = SitemapGenerator::Interpreter.new(@sitemap_opts.merge(compress: false))
    interpreter.instance_eval(&@sitemap_block)
  end
end

#after_configurationObject



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/middleman-mdocs/extension.rb', line 101

def after_configuration
  markdown_exts = %w[markdown mdown md mkd mkdn]
  ::Tilt.register ::MiddlemanMdocs::TiltTemplate, *markdown_exts

  app.files.on_change :source do |changed, _deleted|
    resources = changed.map do |_ch|
      full = changed.first.full_path.to_s
      app.sitemap.resources.find { |r| r.source_file == full }
    end.flatten

    resources.compact.each(&:refresh)
  end
end

#configure_applicationObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/middleman-mdocs/extension.rb', line 63

def configure_application
  Middleman::Sitemap::Store.include(::MiddlemanMdocs::Sitemap)

  app.config[:markdown_engine] = :kramdown
  app.config[:markdown] = {
    tables: true,
    linkable_headers: true,
    auto_ids: true,
    auto_id_prefix: true,
    auto_id_stripping: true,
    transliterated_header_ids: true,
    syntax_highlighter: :rouge,
    syntax_highlighter_opts: {
      default_lang: 'html'
    },
    parse_block_html: true,
    input: 'MdocsKramdown'
  }
end

#enrich_metadata(rx, &block) ⇒ Object



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

def (rx, &block)
  @enrichers ||= []
  @enrichers.push([rx, block])
end

#get_metadata(path) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/middleman-mdocs/extension.rb', line 173

def (path)
  return {} unless path[app.source_dir.to_s]

  path = Pathname.new(path)
  current_dir = path.dirname.relative_path_from(app.source_dir).to_s

  @mdocsmeta[current_dir] || begin
    tokens = current_dir.split('/')

    ([''] + tokens).reduce(Pathname('')) do |parent, token|
      return {} if ['.', ','].include?(token)

      current = parent.join(token)
      token_path = app.source_dir.join(current)
      meta = (token_path)
      @mdocsmeta[current.to_s] ||= (@mdocsmeta[parent.to_s] || {}).merge(meta)
      current
    end

    @mdocsmeta[current_dir] || {}
  end
end

#load_metadata(path) ⇒ Object



196
197
198
199
200
# File 'lib/middleman-mdocs/extension.rb', line 196

def (path)
  ::YAML.load_file(path.join('metadata.yml'))
rescue StandardError
  {}
end

#log(text) ⇒ Object



128
129
130
# File 'lib/middleman-mdocs/extension.rb', line 128

def log(text)
  logger.debug "[MDOCS] #{Time.now}: #{text}"
end

#manipulate_resource_list(resources) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/middleman-mdocs/extension.rb', line 132

def manipulate_resource_list(resources)
  logger.debug '== [MDOCS] enrich resources...'
  resources.each do |resource|
    mdocs.init_resource(resource)

    next if resource.[:ignore]

    if resource.source_file.end_with?('.md')
      resource.add_meta(markdown: true)
      resource.add_options(layout: (resource.options[:layout] || app.config.layout))
      resource.destination_path << '.html' unless resource.destination_path.end_with?('.html')
    end

    mdocs.register(resource)

    nonce = SecureRandom.hex

    state = cache.fetch([resource.cache_key, :manipulate_resource_list]) do
      nonce
    end

    next if mdocs.ready? && nonce != state

    # enrich recursive metadata from metadata.yml in each folder
     = (resource.source_file)
    resource.add_meta(::Middleman::Util.recursively_enhance())

    # run apropriate enrichers
    @enrichers.each do |(rx, block)|
      if (md = rx.match(resource.source_file))
        block.call(resource, md)
      end
    end

    resource.refresh
  end

  mdocs.refresh
  resources
end

#register_helpers(modul) ⇒ Object



83
84
85
86
87
88
# File 'lib/middleman-mdocs/extension.rb', line 83

def register_helpers(modul)
  unless self.class.defined_helpers.include?(modul)
    self.class.include modul
    self.class.defined_helpers << modul
  end
end

#static_sitemap(opts = {}, &block) ⇒ Object



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

def static_sitemap(opts = {}, &block)
  @sitemap_opts = opts = opts.dup
  @sitemap_block = block
end