-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdocumentation.rb
126 lines (95 loc) · 3.09 KB
/
documentation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
require 'asciidoctor'
require 'asciidoctor/pdf_renderer'
require 'asciidoctor/theme_loader'
require 'asciidoctor-pdf/implicit_header_processor'
module Documentation
# decorates each resource with
# documentation specific logic
module Document
def title
metadata[:page]['title']
end
def pdf
metadata[:pdf]
end
def pdf_name
File.basename(pdf.path)
end
def sections
Hash[metadata[:sections]] if metadata[:sections]
end
def render(opts={}, locs={}, &block)
super(opts.merge(:layout => "doc_layout.erb"), locs, &block)
end
end
class Store < Middleman::Sitemap::Store
def initialize(app, extension)
@extension = extension
@docs = []
@pdfs = []
super(app)
end
def all
@docs
end
def manipulate_resource_list(resources)
@docs, @pdfs = [], []
resources.each do |resource|
path = resource.source_file
next unless path.present? && File.extname(path) == '.adoc' && File.dirname(path) =~ /\/docs$/
work_dir, asciidoc_file = File.split path
relative_dir = File.basename(work_dir)
resource.extend Document
options = {
:safe => :safe,
:attributes => @app.config[:asciidoc][:attributes] + %w(idprefix=@ listing-caption=Listing@ buildfor-editor)
}
options[:extensions_registry] = Asciidoctor::Extensions.build_registry :pdf do
include_processor Asciidoctor::Pdf::ImplicitHeaderProcessor.new @document
end
file_rootname = Asciidoctor::Helpers.rootname File.basename(asciidoc_file)
pdf_file = "#{file_rootname}.pdf"
theme = Asciidoctor::ThemeLoader.load_file(File.join(@app.root, 'pdf-theme.yml'))
# render the asciidoc
doc = Asciidoctor.load_file path, options
doc.restore_attributes
# build the .pdf file
Asciidoctor::PdfRenderer.render doc, File.join(work_dir, pdf_file), theme
# add pdf to sitemap
pdf_resource = Middleman::Sitemap::Resource.new(
self, File.join(relative_dir, pdf_file), path.sub(/.adoc$/, '.pdf')
)
@pdfs << pdf_resource
# used for link helpers
resource.add_metadata :pdf => pdf_resource
resource.add_metadata :sections => doc.sections.map { |s| [s.id, s.title] }
@docs << resource
end
# return modified + new pdf resources
@pdfs + resources
end
end
module Helper
attr_accessor :documentation
def docs
documentation.all
end
end
class Extension < Middleman::Extension
self.defined_helpers = [Helper]
option :source, "./source/docs"
helpers do
end
def initialize(app, options_hash={}, &block)
super
app.before do
app.config[:asciidoc][:base_dir] = options_hash[:source] || 'source/docs'
end
end
def after_configuration
app.documentation = Store.new(app, self)
app.sitemap.register_resource_list_manipulator(:documentation, app.documentation, 0)
end
end
end
::Middleman::Extensions.register(:documentation, Documentation::Extension)