-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRakefile
More file actions
49 lines (40 loc) · 1.29 KB
/
Rakefile
File metadata and controls
49 lines (40 loc) · 1.29 KB
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
# encoding: UTF-8
require 'cgi'
desc 'Convert to Jekyll source'
task :default do
# YAML Front Matter
jekyllHead = <<-EOS
---
title: %title%
repository: %repository%
branch: '%branch%'
layout: default
---
EOS
Dir['./_repos/*/*'].each do |repoDir|
# ./_repos/:repository/:brach -> _posts/:repository/:branch
docsOut = repoDir.gsub /\/_repos/, ''
# _repos/:repository/:brach/docs
docsIn = repoDir + '/docs'
# _posts/:repository/:branch -> [:branch, :repository, '_posts']
branchName, repoName = docsOut.split('/').reverse()
FileUtils.mkdir_p(docsOut)
Dir[docsIn+'/*'].each do |srcPath|
baseName = File.basename(srcPath)
destPath = docsOut + '/' + baseName
if File.extname(srcPath) == '.md'
content = File.read(srcPath)
# http://mattn.kaoriya.net/software/lang/ruby/20121011184445.htm
content.gsub!(/(?:^|\n)```(\w*)\n(.*?\n)```\n/m) do |text|
cls = $1.empty? ? "" : "#{$1}"
"\n<pre class=\"#{cls}\"><code>#{CGI.escapeHTML($2)}</code></pre>\n"
end
fp = File.open(destPath, 'w')
fp.write(jekyllHead.gsub(/%title%/, baseName).gsub(/%repository%/, repoName).gsub(/%branch%/, branchName) + content)
fp.close()
else
FileUtils.cp(srcPath, destPath)
end
end
end
end