-
Notifications
You must be signed in to change notification settings - Fork 0
/
xtodoc.cr
152 lines (144 loc) · 3.85 KB
/
xtodoc.cr
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
require "file_utils"
def makeindex(output, dir)
files = Dir["#{dir}/**"]
output.puts
files.each do |file|
if !File.directory?(file)
output.puts("./[#{File.basename(file, File.extname(file))}](./#{File.basename(file, File.extname(file))}.html)")
else
output.puts("./[#{File.basename(file, File.extname(file))}](./#{File.basename(file, File.extname(file))}/index.html)")
end
output.puts
end
end
def image?(file)
file.to_s.includes?(".gif") || file.to_s.includes?(".png") || file.to_s.includes?(".jpg")
end
def begin_file(file, output_file)
output_file.puts("---")
output_file.puts("title: #{File.basename(file)}")
output_file.puts("---")
end
def nix(file, output_file, file_extension)
begin_file(file, output_file)
code = true
input_file = File.open(file)
file_lines = input_file.read_line
if file_lines.chomp != "/*"
output_file.puts("```#{file_extension}")
end
input_file.each_line do |line|
if line.chomp.lstrip.starts_with? "/*"
code = false
output_file.puts("```")
elsif line.chomp.lstrip.starts_with? "*/"
code = true
output_file.puts("```#{file_extension}")
else
if code == true
output_file.puts(line)
else
output_file.puts(line.lstrip)
end
end
end
output_file.puts("```")
output_file.close
end
def md(file, output_file)
begin_file(file, output_file)
File.open(file).each_line do |line|
output_file.puts(line)
end
output_file.close
end
def other(file, output_file)
begin_file(file, output_file)
output_file.puts("```")
File.open(file).each_line do |line|
output_file.puts(line)
end
output_file.puts("```")
output_file.close
end
def mdfile(file, output_dir)
case File.extname(file).delete(".")
when "nix"
nix(file, File.new("#{output_dir}/#{file}.md", "w"), "nix")
when "md"
md(file, File.new("#{output_dir}/#{file}", "w"))
else
puts "file #{output_dir + file}"
other(file, File.new("#{output_dir}/#{file}.md", "w"))
end
end
OUTPUT_DIR = "docs/"
`rm -rf #{OUTPUT_DIR}`
resource_paths = Dir.glob("**/*").reject do |path|
File.directory?(path) || path.ends_with?("index.md") || image?(path) || File.symlink?(path)
end
images = Dir.glob("**/*").select do |path|
image?(path)
end
directories = Dir.glob("**/*").select do |path|
File.directory?(path) & (path != OUTPUT_DIR)
end
`mkdir #{OUTPUT_DIR}`
directories.each do |dir|
`mkdir #{OUTPUT_DIR}/#{dir}`
end
resource_paths.each do |file|
mdfile(file, OUTPUT_DIR)
end
directories.each do |dir|
index = File.new("#{OUTPUT_DIR}/#{dir}/index.md", "w")
begin_file(dir, index)
if File.exists?("#{dir}/index.md")
File.open("#{dir}/index.md").each_line do |output|
index.puts(output)
end
end
makeindex(index, "#{OUTPUT_DIR}#{dir}")
index.close
end
index = File.open("#{OUTPUT_DIR}/index.md", "w")
begin_file(OUTPUT_DIR.to_s, index)
File.open("./index.md").each_line do |output|
index.puts(output)
end
makeindex(index, OUTPUT_DIR.to_s)
images.each do |image|
FileUtils.cp(image, "./docs/#{image}")
end
index.close
resource_paths_html = Dir[Dir.current + "/docs/**/*"].reject do |path|
File.directory?(path) || image?(path)
end
index.close
channel = Channel(Nil).new
num_needed = resource_paths_html.size
resource_paths_html.each do |file|
spawn do
output = IO::Memory.new
error = IO::Memory.new
proc = Process.new(
command: "pandoc",
args: [
"--to=html",
"--highlight-style=./theme/dracula/dracula.theme",
"--css=./theme/wizardwatch.css",
"--self-contained",
"--include-in-header=./theme/header.html"
],
input: IO::Memory.new(File.read(file)),
output: output,
error: error
)
proc.wait
`touch #{file[0..-4] + ".html"}`
File.write(file[0..-4] + ".html", output.to_s)
puts error
channel.send(nil)
end
end
(1..num_needed).each { channel.receive }