Class: ETBlogCore

Inherits:
Object
  • Object
show all
Defined in:
lib/etblog.rb

Instance Method Summary collapse

Constructor Details

#initialize(folder, static_custom = nil) ⇒ ETBlogCore

Returns a new instance of ETBlogCore.



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
# File 'lib/etblog.rb', line 35

def initialize(folder, static_custom=nil)
	if not File.directory? folder then
		raise 'No such directory - ' + folder
	end
	@base = folder.clone
	@output = @base + '/htdocs'
	@conf = @base + '/blog.plist'
	if not File.exists? @conf then
		File.write(@conf, { 'Title' => 'Blog', 
				    'Author' => ENV['USER'] || 'You', 
				    'Static' => 'static',
				    'Description' => 'Your new ETBlog blog.'}.to_plist )
	end
	plist_parsed = Plist.parse_xml(@conf)
	@title = plist_parsed['Title']
	@author = plist_parsed['Author']
	@static = @base + '/' + plist_parsed['Static'].to_s
	if static_custom != nil then
		@static = static_custom.clone
	end
	@description = plist_parsed['Description'] || 'Your new ETBlog blog.'
	@description += '<br><p>Powered by <a href="http://timkoi.gitlab.io/etblog">ETBlog</a>.</p>'
	if not File.directory? @static then
		FileUtils.mkdir_p(@static)
	end
	@links = []
	plist_parsed.keys.each do |key|
		if not ['Title', 'Author', 'Static', 'Description'].include? key then
			@links.push("<li><a href=\"#{plist_parsed[key]}\">#{key}</a></li>")
		end
	end
	if @links.length < 1 then
		@links.push("<li><a href=\"http://timkoi.gitlab.io/etblog\">ETBlog homepage</a></li>")
		@links.push("<li><a href=\"http://github.com/timkoi\">@timkoi on GitHub</a></li>")
	end
end

Instance Method Details

#buildObject



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
# File 'lib/etblog.rb', line 98

def build
	if not File.exists? @base + '/index.html' then
		FileUtils.cp(File.absolute_path(File.dirname(__FILE__)) + '/index-default-etblog.html', @base + '/index.html')
	end
	newest_post = ''
	all_posts = Dir.glob(@base + '/post-*.md').sort_by{ |f| File.mtime(f) }.reverse
	all_posts_headings = []
	all_posts_outputs = []
	ctnt_homepage = ''
	file_first = true
	all_posts.each do |item|
		output = '/posts/' + File.basename(item) + '/index.html'
		all_posts_outputs.push(output.clone)
		output = @static + output
		if not File.directory? File.dirname(output) then
			FileUtils.mkdir_p(File.dirname(output))
		end
		FileUtils.cp(@base + '/index.html', output)
		ctnt = File.read(output)
		put_ctnt = File.read(item)
		all_images = self.findimages(put_ctnt)
		all_images.each do |item|
			if File.exists? @base + '/' + item then
				FileUtils.cp(@base + '/' + item, File.dirname(output) + '/' + item)
			end
		end
		heading = Kramdown::Document.new(put_ctnt.gsub("\r\n", "\n").split("\n")[0]).to_html.unxml
		all_posts_headings.push(heading)
		put_ctnt = "<p><a href=\"../../index.html\">< Back to the homepage</a></p><br>" + Kramdown::Document.new(put_ctnt).to_html
		ctnt = self.conv(ctnt, put_ctnt)
		File.write(output, ctnt)
		puts "#{item} => #{output}"
		if newest_post == '' || File.mtime(newest_post) < File.mtime(item) then
			newest_post = item
		end
	end
	ctnt_for_main = ''
	maplists(all_posts_headings, all_posts).each do |key, value|
		ctnt_for_main += "<h2><a href=\"posts/#{File.basename(value)}/index.html\">#{key}</a></h2>"
		ctnt_for_main += "<p><i style=\"color: darkgrey;\">Posted on #{File.mtime(value).to_s} by #{@author}</i></p>"
		ctnt_for_main += "<hr>"
	end
	if ctnt_for_main == '' then
		ctnt_for_main = 'Nothing was posted on this blog yet. Stay tuned!'
	end
	File.write(@static + '/index.html', self.conv(File.read(@base + '/index.html'), ctnt_for_main))
end

#conv(string, put_ctnt) ⇒ Object



94
95
96
# File 'lib/etblog.rb', line 94

def conv(string, put_ctnt)
	return string.to_s.gsub('@{post}', put_ctnt).gsub('@{title}', @title).gsub('@{description}', @description).gsub('@{links}', '<ul>' + @links.join("") + '</ul>').gsub('@{author}', @author).gsub("@{year}", Time.now.year.to_s)
end

#findimages(string) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/etblog.rb', line 72

def findimages(string)
	possible_image = false
	inside_image_tag = false
	img = ''
	out = []
	string.split('').each do |item|
		if item == '!' then
			possible_image = true
		elsif item == '(' && possible_image then
			possible_image = false
			inside_image_tag = true
		elsif item == ')' && inside_image_tag then
			inside_image_tag = false
			out.push(img.clone)
			img = ''
		elsif inside_image_tag then
			img += item
		end
	end
	return out
end