Module: MiddlemanMdocs::Navigation

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

Defined Under Namespace

Classes: Item

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(list) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/middleman-mdocs/navigation.rb', line 43

def self.parse(list)
  list.map do |p|
    if p.is_a?(Hash)
      k, v = p.first
    elsif p.is_a?(String)
      k = p
      v = nil
    else
      ap "Error: #{p.inspect}"
      next
    end

    v = parse(v) if v.is_a?(Array)
    Item.new(k, v)
  end
end

Instance Method Details

#active_url?(url) ⇒ Boolean

is this is url for current page

Returns:

  • (Boolean)


148
149
150
151
152
153
154
155
# File 'lib/middleman-mdocs/navigation.rb', line 148

def active_url?(url)
  translated = translate_to_destination(url)
  url == current_page.url || current_page.url == translated || begin
    current_page.url == translated.force_encoding('ASCII-8BIT')
  rescue StandardError
    nil
  end
end

#current_pageObject



138
139
140
141
142
143
144
145
# File 'lib/middleman-mdocs/navigation.rb', line 138

def current_page
  r = super
  if r.nil?
    @locs&.fetch(:force_current_page, nil)
  else
    r
  end
end

#fuzzy_find_resource(url) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/middleman-mdocs/navigation.rb', line 83

def fuzzy_find_resource(url)
  source_file = if url.try(:source_file)
                  url.try(:source_file)
                elsif url.start_with?('/')
                  app.source_dir.join(url[1..-1]).to_s
                elsif current_page
                  Pathname.new(current_page.source_file).dirname.join(url).to_s
                else
                  url
                end
  app.sitemap.resources.find { |r| r.source_file == source_file }
end

#include(page_id_or_path, merge: true, render: true) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/middleman-mdocs/navigation.rb', line 108

def include(page_id_or_path, merge: true, render: true)
  resource = fuzzy_find_resource(page_id_or_path) || sitemap.find_resource_by_page_id(page_id_or_path)
  mdocs.add_dependency(current_page, resource)

  mdocs.init_resource(resource) unless resource.is_a?(::MiddlemanMdocs::Resource)
  result = if render
             resource.force_render
           else
             ::File.read(resource.try(:source_file) || page_id_or_path)
           end

  merge(page_id_or_path) if merge
  result
rescue StandardError => e
  puts "Error on including #{page_id_or_path.inspect} to #{current_page.inspect}: #{e.inspect}"
  raise
end

Try to catch relative path and find resource for it



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/middleman-mdocs/navigation.rb', line 61

def link_to(*args, &block)
  url_arg_index = block_given? ? 0 : 1
  options_index = block_given? ? 1 : 2
  url = args[url_arg_index]

  if url
    url = translate_to_destination(url)
    args[url_arg_index] = url
  end
  super(*args, &block)
end


73
74
75
76
77
78
79
80
81
# File 'lib/middleman-mdocs/navigation.rb', line 73

def link_to_tag(tag, *args, &block)
  raise "There no tag #{tag.inspect} defined in mdocs" if tag && !mdocs.has_tag?(tag)

  if block_given?
    link_to("/tags/#{tag}", *args, &block)
  else
    link_to(I18n.t(tag, scope: %i[mdocs tags], default: tag.to_s), "/tags/#{tag}", *args, &block)
  end
end

#merge(url) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/middleman-mdocs/navigation.rb', line 96

def merge(url)
  resource = fuzzy_find_resource(url) || sitemap.find_resource_by_page_id(url)
  raise "unable to find resource by #{url}" unless resource

  mdocs.init_resource(resource) unless resource.is_a?(::MiddlemanMdocs::Resource)
  if current_page
    current_page.add_tags(resource.tags)
    current_page.add_keywords(resource.keywords)
    current_page.data.reverse_merge!(resource.data)
  end
end

#translate_to_destination(url) ⇒ Object

some custom magic to find result path to generated resource Ex: readme.md -> readme.html Absolute Ex: /docs/readme.md -> docs/readme.html Relative Ex: quick.md -> docs/quick.html from docs/page.md



130
131
132
133
134
135
136
# File 'lib/middleman-mdocs/navigation.rb', line 130

def translate_to_destination(url)
  if r = fuzzy_find_resource(url)
    r.url
  else
    url
  end
end