-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathIncludeProcessor.rb
63 lines (58 loc) · 1.83 KB
/
IncludeProcessor.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
require 'asciidoctor'
require 'asciidoctor/extensions'
require 'open-uri'
class CommonIncludeProcessor < Asciidoctor::Extensions::IncludeProcessor
def handles? target
(target.start_with? 'common') or (target.start_with? 'opsi-docs-en') or (target.start_with? 'opsi-docs-de')
end
RX = /xref:.*#/
SUB = "xref:"
def process doc, reader, target, attributes
# puts "CommonIncludeProcessor"
# puts reader.class
# puts ENV.keys
# puts Asciidoctor::Document::Title
# puts doc.options().class
# puts doc.options()[:attributes]["lang"]
# puts doc.options()
# puts ENV["LANG"]
if doc.options()[:attributes]["lang"] == "de" then
new_target = "docs/de/modules/"
else
new_target = "docs/en/modules/"
end
split = target.split(/[:$]+/)
for string in split do
if string.strip == "include" then
next
elsif string.start_with?("Unresolved directive") then
next
elsif string.strip == "opsi-docs-en" then
new_target = "docs/en/modules/"
elsif string.strip == "opsi-docs-de" then
new_target = "docs/de/modules/"
elsif string == "partial" then
new_target.concat("partials/")
elsif string.end_with?(".asciidoc") or string.end_with?(".adoc") then
new_target.concat(string)
else
new_target.concat(string)
new_target.concat("/")
end
end
new_target
target = new_target
# puts target
content = (open target).readlines.map do |line|
line = line.force_encoding('utf-8')
line = (line.include? 'xref:') ? (line.gsub RX, SUB) : line
line
end
content = content.join('').force_encoding('utf-8')
reader = reader.push_include content, target, target, 1, attributes
reader
end
end
Asciidoctor::Extensions.register do
include_processor CommonIncludeProcessor
end