sundysilence 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/README.md +43 -7
- data/lib/sundysilence.rb +1 -155
- data/lib/sundysilence/builder.rb +156 -0
- data/lib/sundysilence/version.rb +1 -1
- metadata +2 -1
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -18,12 +18,48 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
Create config.yml.
|
22
22
|
|
23
|
-
|
23
|
+
input_dir: inputs
|
24
|
+
output_dir: published
|
25
|
+
template_dir: templates
|
26
|
+
stylesheet_dir: stylesheets
|
27
|
+
|
28
|
+
pre_content: pre_content.html
|
29
|
+
post_content: post_content.html
|
30
|
+
|
31
|
+
title: "The Static Wiki"
|
32
|
+
expect_title: index
|
33
|
+
listpage_title: "All Pages"
|
34
|
+
|
35
|
+
combination_page_file: all
|
36
|
+
|
37
|
+
All entries are put in "input_dir" that are written in Markdown.
|
38
|
+
"template_dir" includes "pre_content" file and "post_content" file.
|
39
|
+
All entries that are replaced *.md with *.html are published to "ouput_dir".
|
40
|
+
|
41
|
+
Write entries.
|
42
|
+
This example file is named "the_page.md".
|
43
|
+
|
44
|
+
The Page
|
45
|
+
|
46
|
+
# The Page Title
|
47
|
+
|
48
|
+
Body text.
|
49
|
+
|
50
|
+
Some sentences.
|
51
|
+
|
52
|
+
The first line is the page title.
|
53
|
+
If a entry contains the title string,
|
54
|
+
that string is linked to this entry.
|
55
|
+
This title string is join the "title" setting above: "The Page - The Static Wiki".
|
56
|
+
The file is set "expect_title" setting is not joined the "title" setting.
|
57
|
+
{{ title }} is replaced with that is joined string or not joined string
|
58
|
+
in "pre_content", "post_content" and all entries.
|
59
|
+
|
60
|
+
Run SundySilence when these files are ready.
|
61
|
+
|
62
|
+
$ sundysilence
|
63
|
+
|
64
|
+
Just do it.
|
24
65
|
|
25
|
-
1. Fork it
|
26
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
-
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
-
5. Create new Pull Request
|
data/lib/sundysilence.rb
CHANGED
@@ -1,156 +1,2 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'fileutils'
|
4
|
-
require 'cgi'
|
5
|
-
require 'yaml'
|
6
|
-
require 'redcarpet'
|
7
|
-
require 'tx'
|
8
|
-
require 'liquid'
|
9
|
-
|
10
|
-
INDEX_FILE = "tmp.index"
|
11
|
-
|
12
|
-
module SundySilence
|
13
|
-
class Builder
|
14
|
-
def initialize
|
15
|
-
@config = YAML::load(open('./config.yml'))
|
16
|
-
@markdown = Redcarpet::Markdown.new(Redcarpet::Render::XHTML.new(:hard_wrap => true), :fenced_code_blocks => true, :autolink => true, :superscript => true)
|
17
|
-
@entries = Hash.new
|
18
|
-
|
19
|
-
if not Dir.exist?(@config['input_dir'])
|
20
|
-
puts "#{@config['input_dir']} is not found\n"
|
21
|
-
exit -1
|
22
|
-
end
|
23
|
-
|
24
|
-
Dir.foreach(@config['input_dir']) do |filename|
|
25
|
-
if filename =~ /^\.+$/
|
26
|
-
next
|
27
|
-
end
|
28
|
-
|
29
|
-
if filename =~ /^.*\.md$/ or filename =~ /^.*\.markdown$/
|
30
|
-
open(File.join(@config['input_dir'], filename)) do |fd|
|
31
|
-
title = fd.gets.chomp
|
32
|
-
@entries[title] = filename
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
tx = Tx::Builder.new
|
38
|
-
tx.add_all(@entries.keys)
|
39
|
-
tx.build(INDEX_FILE)
|
40
|
-
@index = Tx::Index.open(INDEX_FILE)
|
41
|
-
|
42
|
-
FileUtils.rm_r(@config['output_dir']) if Dir.exist?(@config['output_dir'])
|
43
|
-
Dir.mkdir(@config['output_dir'])
|
44
|
-
|
45
|
-
@pre_content = nil
|
46
|
-
open(File.join(@config['template_dir'], @config['pre_content']), 'rb') do |fd|
|
47
|
-
@pre_content = fd.read
|
48
|
-
end
|
49
|
-
|
50
|
-
@post_content = nil
|
51
|
-
open(File.join(@config['template_dir'], @config['post_content']), 'rb') do |fd|
|
52
|
-
@post_content = fd.read
|
53
|
-
end
|
54
|
-
|
55
|
-
if not @config['combination_page_file'].nil?
|
56
|
-
@combination = ""
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def link_to_entries(text)
|
61
|
-
@index.gsub(text) do |s, i|
|
62
|
-
link_to = @entries[s].gsub(/(.*)\.(md|markdown)$/, '\1.html')
|
63
|
-
"<a href=\"./#{CGI.escape(link_to)}\">#{CGI.escape_html(s)}</a>"
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
def render_template(text, title)
|
68
|
-
liquid = Liquid::Template.parse(text)
|
69
|
-
liquid.render({'title' => title})
|
70
|
-
end
|
71
|
-
|
72
|
-
def build_a_entry(body, title)
|
73
|
-
urls = Array.new
|
74
|
-
body.gsub!(/((http|https):\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?)/) do |matched|
|
75
|
-
url = matched
|
76
|
-
breaks = ""
|
77
|
-
if url =~ /^(.*)([\r\n]+)$/
|
78
|
-
url = $1
|
79
|
-
breaks = $2
|
80
|
-
end
|
81
|
-
urls << url
|
82
|
-
"{{#{urls.size - 1}}}" + breaks
|
83
|
-
end
|
84
|
-
body = link_to_entries(body)
|
85
|
-
(0..urls.size - 1).each do |i|
|
86
|
-
body.sub!(/\{\{#{i.to_s}\}\}/, urls[i].to_s)
|
87
|
-
end if urls.size > 0
|
88
|
-
body = @markdown.render(body)
|
89
|
-
if not @config['combination_page_file'].nil?
|
90
|
-
@combination += body
|
91
|
-
end
|
92
|
-
page = @pre_content + body + @post_content
|
93
|
-
page = render_template(page, title)
|
94
|
-
end
|
95
|
-
|
96
|
-
def build_entries
|
97
|
-
@entries.each do |page_title, filename|
|
98
|
-
output_filename = nil
|
99
|
-
open(File.join(@config['input_dir'], filename), 'rb') do |fd|
|
100
|
-
fd.gets
|
101
|
-
body = fd.read
|
102
|
-
if not filename =~ /#{@config['expect_title']}\.(md|markdown)/
|
103
|
-
title = page_title + " - " + @config['title']
|
104
|
-
else
|
105
|
-
title = page_title
|
106
|
-
end
|
107
|
-
page = build_a_entry(body, title)
|
108
|
-
output_filename = filename.gsub(/(.*)\.(md|markdown)$/, '\1.html')
|
109
|
-
open(File.join(@config['output_dir'], output_filename), 'w') do |output_fd|
|
110
|
-
output_fd.puts page
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
def build_pages
|
117
|
-
build_entries
|
118
|
-
build_list_page
|
119
|
-
if not @config['combination_page_file'].nil?
|
120
|
-
build_combination_page
|
121
|
-
end
|
122
|
-
FileUtils.cp_r(@config['stylesheet_dir'], @config['output_dir'])
|
123
|
-
end
|
124
|
-
|
125
|
-
def build_combination_page
|
126
|
-
open(File.join(@config['output_dir'], @config['combination_page_file'] + '.html'), 'w') do |fd|
|
127
|
-
title = 'all - ' + @config['title']
|
128
|
-
page = @pre_content + @combination + @post_content
|
129
|
-
page = render_template(page, title)
|
130
|
-
fd.puts page
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
def build_list_page
|
135
|
-
open(File.join(@config['output_dir'], 'list.html'), 'w') do |fd|
|
136
|
-
title = @config['listpage_title'] + ' - ' + @config['title']
|
137
|
-
body = "\# #{title}\n\n"
|
138
|
-
list_text = ""
|
139
|
-
@entries.each do |title, filename|
|
140
|
-
list_text += "* #{title}\n"
|
141
|
-
end
|
142
|
-
body += list_text
|
143
|
-
body = link_to_entries(body)
|
144
|
-
body = @markdown.render(body)
|
145
|
-
page = @pre_content + body + @post_content
|
146
|
-
page = render_template(page, title)
|
147
|
-
fd.puts page
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
|
-
def cleanup
|
152
|
-
File.unlink(INDEX_FILE)
|
153
|
-
end
|
154
|
-
end
|
155
|
-
end
|
1
|
+
require 'sundysilence/builder'
|
156
2
|
|
@@ -0,0 +1,156 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'fileutils'
|
4
|
+
require 'cgi'
|
5
|
+
require 'yaml'
|
6
|
+
require 'redcarpet'
|
7
|
+
require 'tx'
|
8
|
+
require 'liquid'
|
9
|
+
|
10
|
+
INDEX_FILE = "tmp.index"
|
11
|
+
|
12
|
+
module SundySilence
|
13
|
+
class Builder
|
14
|
+
def initialize
|
15
|
+
@config = YAML::load(open('./config.yml'))
|
16
|
+
@markdown = Redcarpet::Markdown.new(Redcarpet::Render::XHTML.new(:hard_wrap => true), :fenced_code_blocks => true, :autolink => true, :superscript => true)
|
17
|
+
@entries = Hash.new
|
18
|
+
|
19
|
+
if not Dir.exist?(@config['input_dir'])
|
20
|
+
puts "#{@config['input_dir']} is not found\n"
|
21
|
+
exit -1
|
22
|
+
end
|
23
|
+
|
24
|
+
Dir.foreach(@config['input_dir']) do |filename|
|
25
|
+
if filename =~ /^\.+$/
|
26
|
+
next
|
27
|
+
end
|
28
|
+
|
29
|
+
if filename =~ /^.*\.md$/ or filename =~ /^.*\.markdown$/
|
30
|
+
open(File.join(@config['input_dir'], filename)) do |fd|
|
31
|
+
title = fd.gets.chomp
|
32
|
+
@entries[title] = filename
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
tx = Tx::Builder.new
|
38
|
+
tx.add_all(@entries.keys)
|
39
|
+
tx.build(INDEX_FILE)
|
40
|
+
@index = Tx::Index.open(INDEX_FILE)
|
41
|
+
|
42
|
+
FileUtils.rm_r(@config['output_dir']) if Dir.exist?(@config['output_dir'])
|
43
|
+
Dir.mkdir(@config['output_dir'])
|
44
|
+
|
45
|
+
@pre_content = nil
|
46
|
+
open(File.join(@config['template_dir'], @config['pre_content']), 'rb') do |fd|
|
47
|
+
@pre_content = fd.read
|
48
|
+
end
|
49
|
+
|
50
|
+
@post_content = nil
|
51
|
+
open(File.join(@config['template_dir'], @config['post_content']), 'rb') do |fd|
|
52
|
+
@post_content = fd.read
|
53
|
+
end
|
54
|
+
|
55
|
+
if not @config['combination_page_file'].nil?
|
56
|
+
@combination = ""
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def link_to_entries(text)
|
61
|
+
@index.gsub(text) do |s, i|
|
62
|
+
link_to = @entries[s].gsub(/(.*)\.(md|markdown)$/, '\1.html')
|
63
|
+
"<a href=\"./#{CGI.escape(link_to)}\">#{CGI.escape_html(s)}</a>"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def render_template(text, title)
|
68
|
+
liquid = Liquid::Template.parse(text)
|
69
|
+
liquid.render({'title' => title})
|
70
|
+
end
|
71
|
+
|
72
|
+
def build_a_entry(body, title)
|
73
|
+
urls = Array.new
|
74
|
+
body.gsub!(/((http|https):\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?)/) do |matched|
|
75
|
+
url = matched
|
76
|
+
breaks = ""
|
77
|
+
if url =~ /^(.*)([\r\n]+)$/
|
78
|
+
url = $1
|
79
|
+
breaks = $2
|
80
|
+
end
|
81
|
+
urls << url
|
82
|
+
"{{#{urls.size - 1}}}" + breaks
|
83
|
+
end
|
84
|
+
body = link_to_entries(body)
|
85
|
+
(0..urls.size - 1).each do |i|
|
86
|
+
body.sub!(/\{\{#{i.to_s}\}\}/, urls[i].to_s)
|
87
|
+
end if urls.size > 0
|
88
|
+
body = @markdown.render(body)
|
89
|
+
if not @config['combination_page_file'].nil?
|
90
|
+
@combination += body
|
91
|
+
end
|
92
|
+
page = @pre_content + body + @post_content
|
93
|
+
page = render_template(page, title)
|
94
|
+
end
|
95
|
+
|
96
|
+
def build_entries
|
97
|
+
@entries.each do |page_title, filename|
|
98
|
+
output_filename = nil
|
99
|
+
open(File.join(@config['input_dir'], filename), 'rb') do |fd|
|
100
|
+
fd.gets
|
101
|
+
body = fd.read
|
102
|
+
if not filename =~ /#{@config['expect_title']}\.(md|markdown)/
|
103
|
+
title = page_title + " - " + @config['title']
|
104
|
+
else
|
105
|
+
title = page_title
|
106
|
+
end
|
107
|
+
page = build_a_entry(body, title)
|
108
|
+
output_filename = filename.gsub(/(.*)\.(md|markdown)$/, '\1.html')
|
109
|
+
open(File.join(@config['output_dir'], output_filename), 'w') do |output_fd|
|
110
|
+
output_fd.puts page
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def build_pages
|
117
|
+
build_entries
|
118
|
+
build_list_page
|
119
|
+
if not @config['combination_page_file'].nil?
|
120
|
+
build_combination_page
|
121
|
+
end
|
122
|
+
FileUtils.cp_r(@config['stylesheet_dir'], @config['output_dir'])
|
123
|
+
end
|
124
|
+
|
125
|
+
def build_combination_page
|
126
|
+
open(File.join(@config['output_dir'], @config['combination_page_file'] + '.html'), 'w') do |fd|
|
127
|
+
title = 'all - ' + @config['title']
|
128
|
+
page = @pre_content + @combination + @post_content
|
129
|
+
page = render_template(page, title)
|
130
|
+
fd.puts page
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def build_list_page
|
135
|
+
open(File.join(@config['output_dir'], 'list.html'), 'w') do |fd|
|
136
|
+
title = @config['listpage_title'] + ' - ' + @config['title']
|
137
|
+
body = "\# #{title}\n\n"
|
138
|
+
list_text = ""
|
139
|
+
@entries.each do |title, filename|
|
140
|
+
list_text += "* #{title}\n"
|
141
|
+
end
|
142
|
+
body += list_text
|
143
|
+
body = link_to_entries(body)
|
144
|
+
body = @markdown.render(body)
|
145
|
+
page = @pre_content + body + @post_content
|
146
|
+
page = render_template(page, title)
|
147
|
+
fd.puts page
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def cleanup
|
152
|
+
File.unlink(INDEX_FILE)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
data/lib/sundysilence/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sundysilence
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -27,6 +27,7 @@ files:
|
|
27
27
|
- bin/sundysilence
|
28
28
|
- config.yml.sample
|
29
29
|
- lib/sundysilence.rb
|
30
|
+
- lib/sundysilence/builder.rb
|
30
31
|
- lib/sundysilence/version.rb
|
31
32
|
- sundysilence.gemspec
|
32
33
|
homepage: https://github.com/aycabta/sundysilence
|