Class: SundySilence::Builder
- Inherits:
-
Object
- Object
- SundySilence::Builder
- Defined in:
- lib/sundysilence/builder.rb
Instance Method Summary collapse
- #build_a_entry(body, title) ⇒ Object
- #build_combination_page ⇒ Object
- #build_entries ⇒ Object
- #build_list_page ⇒ Object
- #build_pages ⇒ Object
- #cleanup ⇒ Object
-
#initialize ⇒ Builder
constructor
A new instance of Builder.
- #link_and_render(text) ⇒ Object
- #link_to_entries(text) ⇒ Object
- #render_template(text, title) ⇒ Object
Constructor Details
#initialize ⇒ Builder
Returns a new instance of Builder.
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 |
# File 'lib/sundysilence/builder.rb', line 14 def initialize @config = YAML::load(open('./config.yml')) @markdown = Redcarpet::Markdown.new(Redcarpet::Render::XHTML.new(:hard_wrap => true), :fenced_code_blocks => true, :autolink => true, :superscript => true) @entries = Hash.new if not Dir.exist?(@config['input_dir']) puts "#{@config['input_dir']} is not found\n" exit -1 end Dir.foreach(@config['input_dir']) do |filename| if filename =~ /^\.+$/ next end if filename =~ /^.*\.md$/ or filename =~ /^.*\.markdown$/ open(File.join(@config['input_dir'], filename)) do |fd| title = fd.gets.chomp @entries[title] = filename end end end tx = Tx::Builder.new tx.add_all(@entries.keys) tx.build(INDEX_FILE) @index = Tx::Index.open(INDEX_FILE) FileUtils.rm_r(@config['output_dir']) if Dir.exist?(@config['output_dir']) Dir.mkdir(@config['output_dir']) @pre_content = nil open(File.join(@config['template_dir'], @config['pre_content']), 'rb') do |fd| @pre_content = fd.read end @post_content = nil open(File.join(@config['template_dir'], @config['post_content']), 'rb') do |fd| @post_content = fd.read end if not @config['combination_page_file'].nil? @combination = String.new end end |
Instance Method Details
#build_a_entry(body, title) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/sundysilence/builder.rb', line 89 def build_a_entry(body, title) body = link_and_render(body) if not @config['combination_page_file'].nil? @combination += body end page = @pre_content + body + @post_content render_template(page, title) end |
#build_combination_page ⇒ Object
118 119 120 121 122 123 124 125 |
# File 'lib/sundysilence/builder.rb', line 118 def build_combination_page open(File.join(@config['output_dir'], @config['combination_page_file'] + '.html'), 'w') do |fd| title = "#{@config['combination_page_file']} - #{@config['title']}" page = @pre_content + @combination + @post_content page = render_template(page, title) fd.puts page end end |
#build_entries ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/sundysilence/builder.rb', line 98 def build_entries @entries.each do |page_title, filename| output_filename = nil open(File.join(@config['input_dir'], filename), 'rb') do |fd| fd.gets body = fd.read if filename =~ /#{@config['expect_title']}\.(md|markdown)/ title = page_title else title = page_title + " - " + @config['title'] end page = build_a_entry(body, title) output_filename = filename.gsub(/(.*)\.(md|markdown)$/, '\1.html') open(File.join(@config['output_dir'], output_filename), 'w') do |output_fd| output_fd.puts page end end end end |
#build_list_page ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/sundysilence/builder.rb', line 127 def build_list_page title = @config['listpage_title'] + ' - ' + @config['title'] body = "\# #{title}\n\n" list_text = String.new @entries.each do |title, filename| list_text += "* #{title}\n" end body += list_text body = link_and_render(body) page = @pre_content + body + @post_content page = render_template(page, title) open(File.join(@config['output_dir'], 'list.html'), 'w') do |fd| fd.puts page end end |
#build_pages ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'lib/sundysilence/builder.rb', line 143 def build_pages build_entries build_list_page if not @config['combination_page_file'].nil? build_combination_page end FileUtils.cp_r(@config['stylesheet_dir'], @config['output_dir']) end |
#cleanup ⇒ Object
152 153 154 |
# File 'lib/sundysilence/builder.rb', line 152 def cleanup File.unlink(INDEX_FILE) end |
#link_and_render(text) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/sundysilence/builder.rb', line 73 def link_and_render(text) text.gsub!(/&/, '&') splitted_text = Array.new rest = text while rest =~ /((http|https):\/\/([\w-]+\.)+[\w-]+(\/[\w\- \/.?%&=]*)?)/ pre = $~.pre_match url = $& rest = $~.post_match splitted_text << link_to_entries(pre) splitted_text << url end splitted_text << link_to_entries(rest) text = splitted_text.join @markdown.render(text) end |
#link_to_entries(text) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/sundysilence/builder.rb', line 60 def link_to_entries(text) @index.gsub(text) do |s, i| link_to = @entries[s].gsub(/(.*)\.(md|markdown)$/, '\1.html') "<a href=\"./#{CGI.escape(link_to)}\">#{CGI.escape_html(s)}</a>" end end |
#render_template(text, title) ⇒ Object
67 68 69 70 71 |
# File 'lib/sundysilence/builder.rb', line 67 def render_template(text, title) title = CGI.escape_html(title) liquid = Liquid::Template.parse(text) liquid.render({'title' => title}) end |