Guest User

Untitled

a guest
Mar 1st, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. if not $project_creation
  2. require 'buzz/template'
  3. require 'buzz/html'
  4. require 'buzz/conf'
  5. require 'buzz/db'
  6. require 'net/ftp'
  7. end
  8. require 'date'
  9.  
  10. class Buzz
  11. def self.Log(str)
  12. puts str
  13. log_str = "[" + Date.today.to_s + "] " + str
  14. File.open("buzz.log", "a").puts log_str
  15. end
  16.  
  17. def self.create(arg, args)
  18. ret = 0
  19. if arg == "project"
  20. ret = self.project(args)
  21. elsif arg == "index"
  22. ret = self.index()
  23. elsif arg == "page"
  24. ret = self.page(args)
  25. #elsif arg == "rss"
  26. # ret = self.rss(ARGV)
  27. end
  28. return ret
  29. end
  30.  
  31. def self.upload
  32. ftp == Net::FTP.new($conf['ftpsite'])
  33. ftp.login($conf['ftpuser'], $conf['ftppasswd'])
  34. ftp.chdir($conf['ftpdir'])
  35. files = Hash.new
  36. Page.find.each do |page|
  37. self.Log "Uploading " + page.location + " to " + $conf['ftpsite'] + page.ftplocation + "..."
  38. ftp.puttextfile(page.location, page.ftplocation)
  39. end
  40. ftp.close
  41. return 0
  42. end
  43.  
  44. def self.project(arg)
  45. project_name = arg[0]
  46. Dir.mkdir(project_name)
  47. Dir.chdir(project_name)
  48. Dir.mkdir("themes")
  49. Dir.mkdir("static-html")
  50. Dir.mkdir("templates")
  51. buzzConf = File.open("buzz.yaml", "w")
  52. buzzConf.puts "conf:"
  53. buzzConf.puts " project_name: #{project_name}"
  54. buzzConf.puts " site_title: Example"
  55. buzzConf.puts " index_size: 10"
  56. buzzConf.puts " themename: default"
  57. buzzConf.puts " ftpsite: ftp.example.com"
  58. buzzConf.puts " ftpuser: Jim"
  59. buzzConf.puts " ftppasswd: MySuperSecretAwesomePassword"
  60. buzzConf.puts " ftppagedir: /The/DirectoryWhere/IWant/TheHTML/"
  61. buzzConf.puts " ftppostdir: /The/DirectoryWhere/IWant/TheHTML/"
  62. buzzConf.puts " "
  63. buzzConf.puts "database:"
  64. buzzConf.puts " adapter: mysql"
  65. buzzConf.puts " host: localhost"
  66. buzzConf.puts " username: root"
  67. buzzConf.puts " password: "
  68. buzzConf.puts " database: #{project_name}"
  69. buzzConf.close
  70. buzzLog = File.open("buzz.log", "w")
  71. buzzLog.close
  72. self.Log "Created project: " + project_name + "."
  73. return 0
  74. end
  75.  
  76. def self.index
  77. files = 0
  78. body = Array.new
  79. authors = Array.new
  80. pages = Page.find(:all, :conditions => [ "kind = ?", "post" ])
  81. pages.reverse.each do |page|
  82. if files == $conf['index_size'].to_i
  83. break
  84. end
  85. files += 1
  86. body.concat( [
  87. "<h2><a href=\"#{page.ftplocation}\">#{page.title}</a></h2>\n",
  88. "<small><i>Created On #{page.date} by #{page.author}</i></small>",
  89. "<p>\n#{page.body}\n</p>\n" ] )
  90. authors.concat( [ "#{page.author}" ] )
  91. end
  92. theme = File.open("themes/#{$conf['themename']}/#{$conf['themename']}.html").readlines
  93. html = HTML.create(authors.uniq.join(" "), $conf['site_title'], body, theme)
  94. index = File.open("static-html/index.html", "w")
  95. index.puts html
  96. self.Log "Generated index.html"
  97. return 0
  98. end
  99.  
  100. def self.page(arg)
  101. file = File.basename(arg[0], ".buzz") + ".html"
  102. if File.exists?("static-html/#{file}")
  103. print "#{file} already exists. Would you like to overwrite it? [y/N] "
  104. ans = STDIN.gets
  105. ans.strip!
  106. if ans == "y" or ans == "Y"
  107. File.delete("static-html/#{file}")
  108. page = Page.find(:first, :conditions => [ "location = ?", "static-html/#{file}"] )
  109. page.destroy
  110. else
  111. return 1
  112. end
  113. end
  114. theme = File.open("themes/#{$conf['themename']}/#{$conf['themename']}.html").readlines
  115. template = Template.new(arg[0])
  116. template.parse
  117. html = HTML.create(template.author, template.title, template.body, theme)
  118. static_local = "static-html/"+ file
  119. f = File.open(static_local, "w")
  120. f.puts html
  121. f.close
  122. ftpL = ''
  123. if template.kind == "page"
  124. ftpL = "#{$ftppagedir}/#{file}"
  125. elsif template.kind == "post"
  126. ftpL = "#{$ftppostdir}/#{file}"
  127. end
  128. Page.create( :title => template.title, :date => Date.today.to_s, :author => template.author, :body => template.body, :location => static_local, :ftplocation => ftpL, :kind => template.kind)
  129. self.Log "Generated " + static_local + " from " + arg[0] + "."
  130. self.Log "Written by " + template.author + "."
  131. return 0
  132. end
  133. end
Add Comment
Please, Sign In to add comment