PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/lump.rb

https://bitbucket.org/apotheon/lump
Ruby | 115 lines | 54 code | 18 blank | 43 comment | 1 complexity | 4f1dd171ed8faf57796056ba98d15912 MD5 | raw file
  1. #/usr/bin/env ruby
  2. require 'yaml'
  3. =begin
  4. Lump is a simple content management system -- *very* simple. Simplicity is
  5. one of its project goals, in fact. The ultimate goal is to have as little
  6. markup embedded code as reasonably possible, and put as much as reasonably
  7. possible into a library (probably one class, maybe two classes tops).
  8. This software is is Copyright Chad Perrin, 2011, and distributed under the
  9. terms of the Open Works License:
  10. # Open Works License
  11. This is version 0.8 of the Open Works License
  12. ## Terms
  13. Permission is hereby granted by the copyright holder(s), author(s), and
  14. contributor(s) of this work, to any person who obtains a copy of this work,
  15. in any form, to reproduce, modify, distribute, publish, sell, use, or
  16. otherwise deal in the licensed material without restriction, provided the
  17. following conditions are met:
  18. 1. Redistributions, modified or unmodified, in whole or in part, must
  19. retain the above license notice, this list of conditions, and the
  20. following disclaimer.
  21. 2. Redistributions, modified or unmodified, in whole or in part, must
  22. retain any applicable notices of attribution and copyright.
  23. No warranty or guarantee is implied by, or should be inferred from, this
  24. license or the act of distribution under the terms of this license. This
  25. license does not grant permission to use the trade names, trademarks,
  26. service marks, product names, or other identifications used by the licensor
  27. except as required for reasonable and customary use in reproducing, and
  28. describing the origin or use of, the work.
  29. =end
  30. class Lump
  31. def initialize(conf=nil)
  32. if conf
  33. @config = YAML.load_file(conf)
  34. else
  35. @config = Hash.new
  36. end
  37. @lump_url = '"http://bitbucket.org/apotheon/lump/"'
  38. end
  39. attr_reader :config
  40. # array.join("\n") approach looks prettier than concatenation to me
  41. def page_head
  42. head_text = [
  43. '<?xml version="1.0" encoding="UTF-8"?>',
  44. '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ' +
  45. 'Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/' +
  46. 'xhtml1-transitional.dtd">',
  47. '<html xmlns="http://www.w3.org/1999/xhtml" ' +
  48. 'dir="ltr" lang="en-US">',
  49. ' <head>'
  50. ]
  51. unless @config.empty?
  52. head_text.push " <title>#{page_title}</title>"
  53. link_list.each {|l| head_text.push(' ' + l) }
  54. end
  55. head_text.push ' </head>'
  56. head_text.join("\n")
  57. end
  58. def page_title
  59. @config['head']['title']
  60. end
  61. # This is primarily for specifying stylesheets in configuration, but was
  62. # generalized "just in case"
  63. def link_list
  64. @config['head']['link'].collect do |k,v|
  65. '<link rel="' + v['rel'].to_s + '" type="' + v['type'].to_s +
  66. '" title="' + v['title'].to_s + '" href="' + v['href'].to_s + '" />'
  67. end
  68. end
  69. # Something needs to be done with this to make it handle other link types,
  70. # such as offsite discussion links (reddit, Hacker News, et cetera).
  71. # Unfortunately, it appears to be a somewhat nontrivial problem at the
  72. # moment, so I've mostly ignored it.
  73. def anchor_list(a_type)
  74. @config['body']['anchor'][a_type].collect do |v|
  75. '<li><a href="' + v['url'].to_s + '">' + v['name'].to_s + '</a></li>'
  76. end
  77. end
  78. def page_foot
  79. foot_text = [ ' <div class="footer">' ]
  80. unless @config.empty?
  81. @config['foot']['copyright'].each do |c|
  82. foot_text.push " <p>#{c}</p>"
  83. end
  84. end
  85. foot_text.push " <p>Powered by <a href=#{@lump_url}>Lump</a></p>"
  86. foot_text.push ' </div>'
  87. foot_text.join "\n"
  88. end
  89. end