forked from Pryz/asciidoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
97 lines (85 loc) · 2.85 KB
/
Rakefile
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
require 'rubygems'
require 'nokogiri'
require 'fileutils'
subheader_link = "https://github.com/pryz[github.com/Pryz]"
html_folder = "./html" # Where you generate your html files
txt_folder = "./" # Where you write your notes
desc "Default task : build html and index"
task :default do
Rake::Task[:genhtml].invoke
sleep 0.2
Rake::Task[:genindex].invoke
end
desc "Generate HTML files from .txt asciidoc files"
task :genhtml, [:theme, :backend] do |t, args|
args.with_defaults(:theme => "pryz", :backend => "lofic_backend")
puts "Building notes with the theme : #{args.theme} and the backend : lofic"
if not File.directory? html_folder
puts "mkdir html"
Dir.mkdir html_folder
end
FileUtils.cp(Dir.glob("#{txt_folder}/*.txt"),html_folder)
txtfilelist=Dir.glob("#{html_folder}/*.txt")
if txtfilelist.empty?
puts "No .txt files... exiting"
exit
end
until txtfilelist.empty?
Thread.new {
file=txtfilelist.pop
%x[asciidoc --backend=#{args.backend} --theme=#{args.theme} #{file}]
} unless Thread.list.length > 32
end
loop do
if Thread.list.length < 2
FileUtils.rm(Dir.glob("#{html_folder}/*.txt")) if Thread.list.length < 2
break
end
sleep 0.1
end
`sed -i '/^Last updated/d' #{html_folder}/*.html`
end
desc "Generate one file"
task :genfile, [:file, :theme, :backend] do |t, args|
args.with_defaults(:theme => "pryz", :backend => "lofic")
if File.exists?(args.file)
puts "Building html for : #{args.file}"
%x[asciidoc -o #{html_folder}/#{args.file.delete(".txt")}.html --theme=#{args.theme} --backend=#{args.backend} #{args.file}]
else
puts "File #{args.file} doesn't exist"
end
end
desc "Generate Index file"
task :genindex, [:theme, :backend] do |t, args|
args.with_defaults(:theme => "", :backend => "index_bootstrap")
puts "Building index with the theme : #{args.theme} and the backend : #{args.backend}"
index_file = "index.txt"
FileUtils.cd(html_folder)
index = File.open(index_file, 'w+')
head_content = <<EOT
Notes
=====
link:#{subheader_link}
List
----
EOT
index.write(head_content)
txtfilelist=Dir.glob("*.html")
until txtfilelist.empty?
note_file = txtfilelist.pop
page = Nokogiri::HTML(open(note_file))
title = page.css('title').text
tags = ""
page.xpath('//div[@class="paragraph"]').each do | div |
tags = div.content if div.content.match(/Additional tag/)
end
if tags.empty?
index.write("* link:#{note_file}[#{title}]\n")
else
index.write("* link:#{note_file}[#{title}] -h-#{tags}-h-\n")
end
end
index.close
%x[asciidoc --theme=#{args.theme} --backend=#{args.backend} #{index_file}]
File.delete(index_file)
end