middleman-mdocs 0.1.0.62562 → 0.1.1.64612
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +4 -4
- data/Gemfile.lock +2 -2
- data/bin/console +3 -1
- data/lib/middleman-mdocs/controller.rb +106 -29
- data/lib/middleman-mdocs/deps.rb +12 -0
- data/lib/middleman-mdocs/extension.rb +81 -27
- data/lib/middleman-mdocs/navigation.rb +34 -11
- data/lib/middleman-mdocs/resource.rb +177 -33
- data/lib/middleman-mdocs/tilt_template.rb +1 -1
- data/lib/middleman-mdocs/toc.rb +4 -1
- data/lib/middleman-mdocs/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0017d29f60525f2e65face5f38e5e4aa16f95251a57ae549c6ed85fa6d3b7630
|
4
|
+
data.tar.gz: d2faa46fa54fe14164b4d0b96ded110a19964ebe996a85ef9846f0ac4d4cbe59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 58494151dd0490fd1579ceb58a6e258d88de1ea5b0aefc3f0a03f5c2a76e299b74065e9ab6f19849f31b0ff8df2c71c0728bd1684084bcac1a959254148e6a46
|
7
|
+
data.tar.gz: cb8dec229b7f5c32a9c9c79a6c734954275a6908f7e6354ac85ece03cd9be6096323ddae40bafddec0acd6092f4aa50924e020aed0a718dbcfc9b6dfe918b402
|
data/Gemfile
CHANGED
@@ -2,13 +2,13 @@
|
|
2
2
|
|
3
3
|
source 'https://rubygems.org'
|
4
4
|
|
5
|
-
# Specify your gem's dependencies in mdocs.gemspec
|
6
|
-
gemspec
|
7
|
-
|
8
5
|
gem 'rake', '~> 13.0'
|
9
|
-
gem 'simplecov', '~> 0.21'
|
10
6
|
gem 'rspec', '~> 3.10'
|
11
7
|
gem 'rspec_junit_formatter', '~> 0.4'
|
8
|
+
gem 'simplecov', '~> 0.21'
|
12
9
|
gem 'simplecov-console'
|
13
10
|
|
14
11
|
gem 'rubocop', require: false
|
12
|
+
|
13
|
+
# Specify your gem's dependencies in mdocs.gemspec
|
14
|
+
gemspec
|
data/Gemfile.lock
CHANGED
data/bin/console
CHANGED
@@ -2,7 +2,9 @@
|
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
4
|
require 'bundler/setup'
|
5
|
-
require 'mdocs'
|
5
|
+
require 'middleman-mdocs'
|
6
|
+
require 'middleman-mdocs/extension'
|
7
|
+
require 'middleman-mdocs/deps'
|
6
8
|
|
7
9
|
# You can add fixtures and/or initialization code here to make experimenting
|
8
10
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -1,38 +1,99 @@
|
|
1
1
|
module MiddlemanMdocs
|
2
2
|
class Controller
|
3
|
-
attr_reader :nav, :sitemap, :app
|
3
|
+
attr_reader :nav, :extension, :sitemap, :app, :ready
|
4
|
+
|
5
|
+
include(::MiddlemanMdocs::TOC)
|
6
|
+
|
7
|
+
def initialize(extension)
|
8
|
+
@extension = extension
|
9
|
+
|
10
|
+
@app = extension.app
|
11
|
+
@sitemap = app.sitemap
|
4
12
|
|
5
|
-
def initialize(sitemap)
|
6
|
-
@sitemap = sitemap
|
7
|
-
@app = sitemap.app
|
8
13
|
@guards = {}
|
14
|
+
@dependencies = {}
|
15
|
+
@metadata = {}
|
16
|
+
@updated = SecureRandom.hex
|
17
|
+
@resources = {}
|
18
|
+
end
|
19
|
+
|
20
|
+
def register(resource)
|
21
|
+
if (old = @resources[resource.page_id])
|
22
|
+
resource.copy_from(old)
|
23
|
+
end
|
24
|
+
|
25
|
+
@resources[resource.page_id] = resource
|
26
|
+
end
|
27
|
+
|
28
|
+
def refresh
|
29
|
+
@updated = SecureRandom.hex
|
30
|
+
@ready = true
|
31
|
+
end
|
32
|
+
|
33
|
+
def nav
|
34
|
+
with_cache(:nav) do
|
35
|
+
::MiddlemanMdocs::Navigation.parse(app.data.mdocs['nav'])
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def ready?
|
40
|
+
@ready
|
41
|
+
end
|
42
|
+
|
43
|
+
def add_dependency(resource, child)
|
44
|
+
@dependencies[resource.page_id] ||= {}
|
45
|
+
@dependencies[resource.page_id][child.page_id] = child
|
46
|
+
end
|
47
|
+
|
48
|
+
def dependencies(resource)
|
49
|
+
@dependencies[resource.page_id]&.values || []
|
50
|
+
end
|
51
|
+
|
52
|
+
def metadata(resource, key, default)
|
53
|
+
@metadata[resource.page_id] ||= {}
|
54
|
+
@metadata[resource.page_id][key] ||= default
|
55
|
+
end
|
56
|
+
|
57
|
+
def set_metadata(resource, key, data)
|
58
|
+
@metadata[resource.page_id] ||= {}
|
59
|
+
@metadata[resource.page_id][key] = data
|
60
|
+
end
|
61
|
+
|
62
|
+
def page_id
|
63
|
+
self.class.to_s
|
9
64
|
end
|
10
65
|
|
11
|
-
def
|
12
|
-
|
66
|
+
def init_resource(resource)
|
67
|
+
resource.extend(::MiddlemanMdocs::Resource) unless resource.is_a?(::MiddlemanMdocs::Resource)
|
68
|
+
resource.extension = extension
|
69
|
+
resource.mdocs = self
|
13
70
|
end
|
14
71
|
|
15
72
|
def tags
|
16
|
-
|
17
|
-
|
73
|
+
with_cache(:tags, dependencies(self).map(&:timestamp).max) do
|
74
|
+
sitemap.resources.select do |r|
|
75
|
+
r.is_a?(::MiddlemanMdocs::Resource) && !r.metadata[:ignore] && r.html?
|
76
|
+
end.map do |r|
|
77
|
+
add_dependency(self, r)
|
78
|
+
# r.options[:tags]
|
18
79
|
r.tags
|
19
|
-
end
|
20
|
-
end
|
80
|
+
end.flatten.compact.map(&:to_s).uniq.sort
|
81
|
+
end
|
21
82
|
end
|
22
83
|
|
23
84
|
def has_tag?(tag)
|
24
|
-
tags.include?(tag.
|
85
|
+
tags.include?(tag.to_s)
|
25
86
|
end
|
26
87
|
|
27
88
|
def select_by_meta(meta)
|
28
89
|
with_ensure_resource do
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
90
|
+
sitemap.resources.select do |r|
|
91
|
+
next false unless r.is_a?(::MiddlemanMdocs::Resource) && !r.metadata[:ignore] && r.template?
|
92
|
+
|
93
|
+
if meta.is_a? Hash
|
94
|
+
r.meta_match?(meta)
|
95
|
+
else
|
96
|
+
r.has_meta?(*meta)
|
36
97
|
end
|
37
98
|
end
|
38
99
|
end
|
@@ -40,29 +101,35 @@ module MiddlemanMdocs
|
|
40
101
|
|
41
102
|
def select_by_meta_force(meta)
|
42
103
|
with_ensure_resource do
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
104
|
+
sitemap.resources.select do |r|
|
105
|
+
next false unless r.is_a?(::MiddlemanMdocs::Resource) && !r.metadata[:ignore]
|
106
|
+
|
107
|
+
if meta.is_a? Hash
|
108
|
+
r.meta_match?(meta)
|
109
|
+
else
|
110
|
+
r.has_meta?(*meta)
|
50
111
|
end
|
51
112
|
end
|
52
113
|
end
|
53
114
|
end
|
54
115
|
|
55
116
|
def select_by_tags(*tags)
|
117
|
+
tags = [tags].flatten.compact.uniq
|
118
|
+
|
56
119
|
with_ensure_resource do
|
57
|
-
sitemap.resources.select do |
|
58
|
-
|
120
|
+
sitemap.resources.select do |r|
|
121
|
+
next false unless r.is_a?(::MiddlemanMdocs::Resource) && !r.metadata[:ignore] && r.template?
|
122
|
+
|
123
|
+
r.has_tags?(*tags)
|
59
124
|
end
|
60
125
|
end
|
61
126
|
end
|
62
127
|
|
63
128
|
def select(tags, *meta)
|
64
|
-
|
65
|
-
|
129
|
+
tags = [tags].flatten.compact.uniq
|
130
|
+
|
131
|
+
select_by_meta(*meta).select do |r|
|
132
|
+
r.has_tags?(*tags)
|
66
133
|
end
|
67
134
|
end
|
68
135
|
|
@@ -76,10 +143,20 @@ module MiddlemanMdocs
|
|
76
143
|
end
|
77
144
|
|
78
145
|
def tr(tag, scope: :tags)
|
146
|
+
::MiddlemanMdocs::Controller.tr(tag, scope: scope)
|
147
|
+
end
|
148
|
+
|
149
|
+
def self.tr(tag, scope: :tags)
|
79
150
|
I18n.t(tag.to_s.upcase.presence, scope: [:mdocs, scope],
|
80
151
|
default: I18n.t(tag.to_s.downcase.presence, scope: [:mdocs, scope], default: tag.to_s).presence)
|
81
152
|
end
|
82
153
|
|
154
|
+
private
|
155
|
+
|
156
|
+
def with_cache(*key, &block)
|
157
|
+
extension.cache.fetch([:controller, @updated, *key], &block)
|
158
|
+
end
|
159
|
+
|
83
160
|
def guard_recursive(name, key, default = nil)
|
84
161
|
guard = @guards[name] ||= {}
|
85
162
|
return guard[key] if guard.has_key?(key)
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'kramdown/parser'
|
2
|
+
require 'kramdown/converter'
|
3
|
+
|
4
|
+
require 'mdocs_kramdown'
|
5
|
+
require 'kramdown/parser/mdocs_kramdown'
|
6
|
+
|
7
|
+
require_relative 'resource'
|
8
|
+
require_relative 'toc'
|
9
|
+
require_relative 'tilt_template'
|
10
|
+
require_relative 'navigation'
|
11
|
+
require_relative 'controller'
|
12
|
+
require_relative 'sitemap'
|
@@ -1,37 +1,57 @@
|
|
1
|
+
require 'active_support/cache'
|
2
|
+
require 'active_support/cache/memory_store'
|
3
|
+
|
4
|
+
require 'middleman-core/util'
|
5
|
+
require 'middleman-core/util/files'
|
6
|
+
|
7
|
+
module Middleman
|
8
|
+
module Util
|
9
|
+
include Contracts
|
10
|
+
|
11
|
+
module_function
|
12
|
+
|
13
|
+
# Finds files which should also be considered to be dirty when
|
14
|
+
# the given file(s) are touched.
|
15
|
+
#
|
16
|
+
# @param [Middleman::Application] app The app.
|
17
|
+
# @param [Pathname] files The original touched file paths.
|
18
|
+
# @return [Middleman::SourceFile] All related file paths, not including the source file paths.
|
19
|
+
Contract ::Middleman::Application, ArrayOf[Pathname] => ArrayOf[::Middleman::SourceFile]
|
20
|
+
def find_related_files(_app, _files)
|
21
|
+
[]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
1
26
|
module MiddlemanMdocs
|
2
27
|
class Extension < Middleman::Extension
|
3
28
|
attr_reader :mdocs, :mdocsnav, :mdocsmeta
|
4
29
|
|
30
|
+
cattr_reader :cache
|
31
|
+
|
5
32
|
expose_to_application :mdocs, :mdocsnav
|
6
33
|
expose_to_template :mdocs, :mdocsnav
|
34
|
+
|
7
35
|
expose_to_config :mdocs, :enrich_metadata
|
36
|
+
expose_to_config :mdocs, :static_sitemap
|
8
37
|
|
9
38
|
def initialize(*args, **_kwargs, &block)
|
10
39
|
super
|
11
40
|
|
12
41
|
# Only require dependency on activation
|
13
|
-
|
14
|
-
require 'kramdown/converter'
|
15
|
-
|
16
|
-
require 'mdocs_kramdown'
|
17
|
-
require 'kramdown/parser/mdocs_kramdown'
|
42
|
+
require_relative 'deps'
|
18
43
|
|
19
|
-
|
20
|
-
|
21
|
-
require_relative 'tilt_template'
|
22
|
-
require_relative 'navigation'
|
23
|
-
require_relative 'controller'
|
24
|
-
require_relative 'sitemap'
|
44
|
+
# @@cache ||= ActiveSupport::Cache::MemoryStore.new(size: 100.megabytes)
|
45
|
+
@@cache ||= ActiveSupport::Cache::FileStore.new('tmp/mdocs/cache')
|
25
46
|
|
26
|
-
|
47
|
+
register_helpers(::MiddlemanMdocs::Navigation)
|
27
48
|
|
28
49
|
configure_application
|
29
50
|
|
30
|
-
register_helpers(::MiddlemanMdocs::TOC)
|
31
|
-
register_helpers(::MiddlemanMdocs::Navigation)
|
32
|
-
|
33
51
|
@mdocsmeta = {}
|
34
52
|
|
53
|
+
@mdocs = Controller.new(self)
|
54
|
+
|
35
55
|
::ActiveSupport::Notifications.subscribe('sitemap.update.middleman') do
|
36
56
|
lookup = @mdocs.sitemap.instance_variable_get('@_lookup_by_destination_path')
|
37
57
|
@mdocs.sitemap.resources.each do |resource|
|
@@ -41,7 +61,7 @@ module MiddlemanMdocs
|
|
41
61
|
end
|
42
62
|
|
43
63
|
def configure_application
|
44
|
-
|
64
|
+
Middleman::Sitemap::Store.include(::MiddlemanMdocs::Sitemap)
|
45
65
|
|
46
66
|
app.config[:markdown_engine] = :kramdown
|
47
67
|
app.config[:markdown] = {
|
@@ -67,9 +87,34 @@ module MiddlemanMdocs
|
|
67
87
|
end
|
68
88
|
end
|
69
89
|
|
90
|
+
def after_build
|
91
|
+
$app = app
|
92
|
+
if @sitemap_opts && @sitemap_block
|
93
|
+
interpreter = SitemapGenerator::Interpreter.new(@sitemap_opts.merge(compress: true))
|
94
|
+
interpreter.instance_eval(&@sitemap_block)
|
95
|
+
|
96
|
+
interpreter = SitemapGenerator::Interpreter.new(@sitemap_opts.merge(compress: false))
|
97
|
+
interpreter.instance_eval(&@sitemap_block)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
70
101
|
def after_configuration
|
71
102
|
markdown_exts = %w[markdown mdown md mkd mkdn]
|
72
|
-
::Tilt.register ::MiddlemanMdocs::
|
103
|
+
::Tilt.register ::MiddlemanMdocs::TiltTemplate, *markdown_exts
|
104
|
+
|
105
|
+
app.files.on_change :source do |changed, _deleted|
|
106
|
+
resources = changed.map do |_ch|
|
107
|
+
full = changed.first.full_path.to_s
|
108
|
+
app.sitemap.resources.find { |r| r.source_file == full }
|
109
|
+
end.flatten
|
110
|
+
|
111
|
+
resources.compact.each(&:refresh)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def static_sitemap(opts = {}, &block)
|
116
|
+
@sitemap_opts = opts = opts.dup
|
117
|
+
@sitemap_block = block
|
73
118
|
end
|
74
119
|
|
75
120
|
def enrich_metadata(rx, &block)
|
@@ -77,18 +122,17 @@ module MiddlemanMdocs
|
|
77
122
|
@enrichers.push([rx, block])
|
78
123
|
end
|
79
124
|
|
80
|
-
|
81
|
-
mdocs.load(app.data.mdocs)
|
125
|
+
helpers do
|
82
126
|
end
|
83
127
|
|
84
|
-
|
128
|
+
def log(text)
|
129
|
+
logger.debug "[MDOCS] #{Time.now}: #{text}"
|
85
130
|
end
|
86
131
|
|
87
132
|
def manipulate_resource_list(resources)
|
133
|
+
logger.debug '== [MDOCS] enrich resources...'
|
88
134
|
resources.each do |resource|
|
89
|
-
|
90
|
-
resource.extend(::MiddlemanMdocs::Resource) unless resource.is_a?(::MiddlemanMdocs::Resource)
|
91
|
-
resource.mdocs_extention = self
|
135
|
+
mdocs.init_resource(resource)
|
92
136
|
|
93
137
|
next if resource.metadata[:ignore]
|
94
138
|
|
@@ -98,8 +142,15 @@ module MiddlemanMdocs
|
|
98
142
|
resource.destination_path << '.html' unless resource.destination_path.end_with?('.html')
|
99
143
|
end
|
100
144
|
|
101
|
-
|
102
|
-
|
145
|
+
mdocs.register(resource)
|
146
|
+
|
147
|
+
nonce = SecureRandom.hex
|
148
|
+
|
149
|
+
state = cache.fetch([resource.cache_key, :manipulate_resource_list]) do
|
150
|
+
nonce
|
151
|
+
end
|
152
|
+
|
153
|
+
next if mdocs.ready? && nonce != state
|
103
154
|
|
104
155
|
# enrich recursive metadata from metadata.yml in each folder
|
105
156
|
metadata = get_metadata(resource.source_file)
|
@@ -111,21 +162,24 @@ module MiddlemanMdocs
|
|
111
162
|
block.call(resource, md)
|
112
163
|
end
|
113
164
|
end
|
165
|
+
|
166
|
+
resource.refresh
|
114
167
|
end
|
115
168
|
|
169
|
+
mdocs.refresh
|
116
170
|
resources
|
117
171
|
end
|
118
172
|
|
119
173
|
def get_metadata(path)
|
120
174
|
return {} unless path[app.source_dir.to_s]
|
121
175
|
|
122
|
-
path =
|
176
|
+
path = Pathname.new(path)
|
123
177
|
current_dir = path.dirname.relative_path_from(app.source_dir).to_s
|
124
178
|
|
125
179
|
@mdocsmeta[current_dir] || begin
|
126
180
|
tokens = current_dir.split('/')
|
127
181
|
|
128
|
-
([''] + tokens).reduce(
|
182
|
+
([''] + tokens).reduce(Pathname('')) do |parent, token|
|
129
183
|
return {} if ['.', ','].include?(token)
|
130
184
|
|
131
185
|
current = parent.join(token)
|
@@ -76,7 +76,7 @@ module MiddlemanMdocs
|
|
76
76
|
if block_given?
|
77
77
|
link_to("/tags/#{tag}", *args, &block)
|
78
78
|
else
|
79
|
-
link_to(I18n.t(tag, scope: %i[mdocs tags], default: tag.to_s), "/tags/#{tag}
|
79
|
+
link_to(I18n.t(tag, scope: %i[mdocs tags], default: tag.to_s), "/tags/#{tag}", *args, &block)
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
@@ -85,28 +85,42 @@ module MiddlemanMdocs
|
|
85
85
|
url.try(:source_file)
|
86
86
|
elsif url.start_with?('/')
|
87
87
|
app.source_dir.join(url[1..-1]).to_s
|
88
|
+
elsif current_page
|
89
|
+
Pathname.new(current_page.source_file).dirname.join(url).to_s
|
88
90
|
else
|
89
|
-
|
91
|
+
url
|
90
92
|
end
|
91
93
|
app.sitemap.resources.find { |r| r.source_file == source_file }
|
92
94
|
end
|
93
95
|
|
94
96
|
def merge(url)
|
95
|
-
resource = fuzzy_find_resource(url)
|
97
|
+
resource = fuzzy_find_resource(url) || sitemap.find_resource_by_page_id(url)
|
96
98
|
raise "unable to find resource by #{url}" unless resource
|
97
99
|
|
98
|
-
|
99
|
-
current_page
|
100
|
+
mdocs.init_resource(resource) unless resource.is_a?(::MiddlemanMdocs::Resource)
|
101
|
+
if current_page
|
102
|
+
current_page.add_tags(resource.tags)
|
103
|
+
current_page.add_keywords(resource.keywords)
|
104
|
+
current_page.data.reverse_merge!(resource.data)
|
105
|
+
end
|
100
106
|
end
|
101
107
|
|
102
108
|
def include(page_id_or_path, merge: true, render: true)
|
103
|
-
merge(page_id_or_path) if merge
|
104
109
|
resource = fuzzy_find_resource(page_id_or_path) || sitemap.find_resource_by_page_id(page_id_or_path)
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
+
mdocs.add_dependency(current_page, resource)
|
111
|
+
|
112
|
+
mdocs.init_resource(resource) unless resource.is_a?(::MiddlemanMdocs::Resource)
|
113
|
+
result = if render
|
114
|
+
resource.force_render
|
115
|
+
else
|
116
|
+
::File.read(resource.try(:source_file) || page_id_or_path)
|
117
|
+
end
|
118
|
+
|
119
|
+
merge(page_id_or_path) if merge
|
120
|
+
result
|
121
|
+
rescue StandardError => e
|
122
|
+
puts "Error on including #{page_id_or_path.inspect} to #{current_page.inspect}: #{e.inspect}"
|
123
|
+
raise
|
110
124
|
end
|
111
125
|
|
112
126
|
# some custom magic to find result path to generated resource
|
@@ -121,6 +135,15 @@ module MiddlemanMdocs
|
|
121
135
|
end
|
122
136
|
end
|
123
137
|
|
138
|
+
def current_page
|
139
|
+
r = super
|
140
|
+
if r.nil?
|
141
|
+
@locs&.fetch(:force_current_page, nil)
|
142
|
+
else
|
143
|
+
r
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
124
147
|
# is this is url for current page
|
125
148
|
def active_url?(url)
|
126
149
|
translated = translate_to_destination(url)
|
@@ -1,57 +1,150 @@
|
|
1
1
|
module MiddlemanMdocs
|
2
2
|
module Resource
|
3
|
-
attr_accessor :
|
3
|
+
attr_accessor :extension, :mdocs, :updated
|
4
|
+
|
5
|
+
def self.normalize_tags(*args)
|
6
|
+
args.flatten.compact.map(&:to_s).map do |tag|
|
7
|
+
::MiddlemanMdocs::Controller.tr(tag)
|
8
|
+
end.map(&:upcase).uniq.sort
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.normalize_keywords(*args)
|
12
|
+
raw = args.flatten.compact.map(&:to_s)
|
13
|
+
normalized = normalize_tags(raw)
|
14
|
+
(raw + normalized).uniq.sort
|
15
|
+
end
|
4
16
|
|
5
17
|
def markdown?
|
6
|
-
|
18
|
+
metadata[:markdown]
|
7
19
|
end
|
8
20
|
|
9
|
-
def
|
10
|
-
|
21
|
+
def html?
|
22
|
+
@html ||= destination_path.end_with?('.html')
|
11
23
|
end
|
12
24
|
|
13
|
-
def
|
14
|
-
|
25
|
+
def ready?
|
26
|
+
mdocs.ready?
|
15
27
|
end
|
16
28
|
|
17
|
-
def
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
29
|
+
def copy_from(other)
|
30
|
+
resource = self
|
31
|
+
unless @patched
|
32
|
+
data.define_singleton_method(:title) do
|
33
|
+
resource.title
|
34
|
+
end
|
22
35
|
|
23
|
-
|
24
|
-
|
36
|
+
data.define_singleton_method(:keywords) do
|
37
|
+
resource.keywords
|
38
|
+
end
|
39
|
+
|
40
|
+
data.define_singleton_method(:text) do
|
41
|
+
resource.text
|
42
|
+
end
|
43
|
+
|
44
|
+
@patched = true
|
45
|
+
end
|
46
|
+
|
47
|
+
custom_deep_merge!(data, other.data)
|
48
|
+
custom_deep_merge!(metadata, other.metadata)
|
49
|
+
|
50
|
+
self.updated = other.updated
|
25
51
|
end
|
26
52
|
|
27
|
-
def
|
28
|
-
|
29
|
-
|
30
|
-
|
53
|
+
def updated_at
|
54
|
+
@updated_at ||= begin
|
55
|
+
ts = data[:updated_at] || metadata[:updated_at] || timestamp
|
56
|
+
ts = DateTime.parse(ts) if ts.is_a?(String)
|
57
|
+
ts
|
58
|
+
end
|
31
59
|
end
|
32
60
|
|
33
|
-
def
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
rescue StandardError
|
39
|
-
nil
|
40
|
-
end
|
41
|
-
end
|
42
|
-
@rendered = true
|
61
|
+
def created_at
|
62
|
+
@created_at ||= begin
|
63
|
+
ts = data[:created_at] || metadata[:created_at] || File.ctime(source_file)
|
64
|
+
ts = DateTime.parse(ts) if ts.is_a?(String)
|
65
|
+
[ts, updated_at].min
|
43
66
|
end
|
44
|
-
|
67
|
+
end
|
68
|
+
|
69
|
+
def timestamp
|
70
|
+
(mdocs.dependencies(self).map(&:timestamp) + [File.mtime(source_file)]).max
|
71
|
+
end
|
72
|
+
|
73
|
+
def cache_key
|
74
|
+
"#{page_id}/#{timestamp}/"
|
45
75
|
end
|
46
76
|
|
47
77
|
def title
|
48
|
-
|
78
|
+
with_cache(:title, @updated) do
|
79
|
+
data[:title] || metadata[:title] || toc&.children&.first&.raw_text
|
80
|
+
end
|
81
|
+
end
|
49
82
|
|
50
|
-
|
83
|
+
def refresh
|
84
|
+
@guards ||= {}
|
85
|
+
|
86
|
+
# @updated = SecureRandom.hex
|
87
|
+
end
|
88
|
+
|
89
|
+
def text
|
90
|
+
force_render if !metadata[:ignore] && html?
|
91
|
+
end
|
92
|
+
|
93
|
+
def tags
|
94
|
+
with_cache(:tags, @updated) do
|
95
|
+
guard_recursive('tags', page_id, []) do |_key|
|
96
|
+
text
|
97
|
+
end
|
98
|
+
|
99
|
+
options[:tags] = ::MiddlemanMdocs::Resource.normalize_tags(options[:tags], data[:tags], metadata[:tags])
|
100
|
+
end
|
51
101
|
end
|
52
102
|
|
53
103
|
def keywords
|
54
|
-
(
|
104
|
+
with_cache(:keywords, @updated) do
|
105
|
+
options[:keywords] =
|
106
|
+
::MiddlemanMdocs::Resource.normalize_keywords(tags, options[:keywords], data[:keywords], metadata[:keywords])
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def toc
|
111
|
+
with_cache(:toc, @updated) do
|
112
|
+
if metadata[:ignore] || !html?
|
113
|
+
nil
|
114
|
+
else
|
115
|
+
mdocs.table_of_contents(self)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def add_options(opts)
|
121
|
+
options.deep_merge!(opts)
|
122
|
+
end
|
123
|
+
|
124
|
+
def add_meta(opts)
|
125
|
+
metadata.deep_merge!(opts)
|
126
|
+
end
|
127
|
+
|
128
|
+
def add_tags(*args)
|
129
|
+
return unless args.flatten.any?
|
130
|
+
|
131
|
+
added = ::MiddlemanMdocs::Resource.normalize_tags(args)
|
132
|
+
return if (added - (options[:tags] || [])).empty?
|
133
|
+
|
134
|
+
@updated = SecureRandom.hex
|
135
|
+
options[:tags] = ::MiddlemanMdocs::Resource.normalize_tags(options[:tags], data[:tags], metadata[:tags], added)
|
136
|
+
end
|
137
|
+
|
138
|
+
def add_keywords(*args)
|
139
|
+
return unless args.flatten.any?
|
140
|
+
|
141
|
+
added = ::MiddlemanMdocs::Resource.normalize_keywords(args)
|
142
|
+
return if (added - (options[:keywords] || [])).empty?
|
143
|
+
|
144
|
+
@updated = SecureRandom.hex
|
145
|
+
options[:keywords] =
|
146
|
+
::MiddlemanMdocs::Resource.normalize_keywords(options[:tags], options[:keywords], data[:keywords], metadata[:keywords],
|
147
|
+
added)
|
55
148
|
end
|
56
149
|
|
57
150
|
def meta_match?(meta)
|
@@ -67,9 +160,60 @@ module MiddlemanMdocs
|
|
67
160
|
end
|
68
161
|
|
69
162
|
def has_tags?(*keys)
|
70
|
-
keys.compact!
|
71
|
-
keys.uniq!
|
72
163
|
(tags & keys).size == keys.size
|
73
164
|
end
|
165
|
+
|
166
|
+
def destination_path(*args)
|
167
|
+
super.force_encoding(Encoding.default_external)
|
168
|
+
end
|
169
|
+
|
170
|
+
def force_render
|
171
|
+
render({ layout: false }, { current_path: path, force_current_page: self })
|
172
|
+
end
|
173
|
+
|
174
|
+
def render(opts = {}, locs = {})
|
175
|
+
return super if nocache? || locs.has_key?('rack') || locs.has_key?(:rack)
|
176
|
+
|
177
|
+
with_cache(Digest::MD5.hexdigest(opts.to_json + locs.to_json) + 'render') do
|
178
|
+
# puts "REFRESH: #{binary?} #{page_id}: #{opts.to_json + locs.to_json}"
|
179
|
+
super
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
private
|
184
|
+
|
185
|
+
def nocache?
|
186
|
+
binary? || metadata[:ignore] || metadata[:nocache] || data[:nocache]
|
187
|
+
end
|
188
|
+
|
189
|
+
def with_cache(*key, &block)
|
190
|
+
extension.cache.fetch([cache_key, *key], &block)
|
191
|
+
end
|
192
|
+
|
193
|
+
def custom_deep_merge!(left, right)
|
194
|
+
left.merge!(right) do |_key, this_val, other_val|
|
195
|
+
if this_val.is_a?(Hash) && other_val.is_a?(Hash)
|
196
|
+
custom_deep_merge!(this_val, other_val)
|
197
|
+
elsif this_val.is_a?(Array) && other_val.is_a?(Array)
|
198
|
+
this_val.concat(other_val).uniq!
|
199
|
+
else
|
200
|
+
other_val.nil? ? this_val : other_val
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
def guard_recursive(name, key, default = nil)
|
206
|
+
@guards ||= {}
|
207
|
+
|
208
|
+
guard = @guards[name] ||= {}
|
209
|
+
return guard[key] if guard.has_key?(key)
|
210
|
+
|
211
|
+
begin
|
212
|
+
guard[key] = default
|
213
|
+
guard[key] = yield(key)
|
214
|
+
ensure
|
215
|
+
guard.delete(key)
|
216
|
+
end
|
217
|
+
end
|
74
218
|
end
|
75
219
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'middleman-core/renderers/kramdown'
|
2
2
|
|
3
3
|
module MiddlemanMdocs
|
4
|
-
class
|
4
|
+
class TiltTemplate < ::Middleman::Renderers::KramdownTemplate
|
5
5
|
def render(*args)
|
6
6
|
erb = Middleman::Renderers::ERb::Template.new(file, line, options) { data }
|
7
7
|
@data = erb.render(*args)
|
data/lib/middleman-mdocs/toc.rb
CHANGED
@@ -50,10 +50,13 @@ module MiddlemanMdocs
|
|
50
50
|
## Generate TOC object tree for current page or source file
|
51
51
|
#
|
52
52
|
def table_of_contents(page_or_source, input: 'markdown')
|
53
|
+
# puts "#{Process.pid} =>>>> GENERATE TOC: #{page_or_source.try(:page_id) || page_or_source}"
|
54
|
+
# byebug if page_or_source.page_id.force_encoding('UTF-8') == 'docs/services/gis-gmp/import-refunds'
|
55
|
+
|
53
56
|
begin
|
54
57
|
return nil if page_or_source.metadata[:ignore]
|
55
58
|
|
56
|
-
content = page_or_source.
|
59
|
+
content = page_or_source.text
|
57
60
|
toc = Kramdown::Document.new(content, app.config[:markdown].merge(input: 'html')).to_toc
|
58
61
|
|
59
62
|
return Item.parse_kramdown_toc(toc)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: middleman-mdocs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1.64612
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samoylenko Yuri
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2021-
|
12
|
+
date: 2021-10-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -194,6 +194,7 @@ files:
|
|
194
194
|
- bin/setup
|
195
195
|
- lib/middleman-mdocs.rb
|
196
196
|
- lib/middleman-mdocs/controller.rb
|
197
|
+
- lib/middleman-mdocs/deps.rb
|
197
198
|
- lib/middleman-mdocs/extension.rb
|
198
199
|
- lib/middleman-mdocs/navigation.rb
|
199
200
|
- lib/middleman-mdocs/resource.rb
|