/lib/lump.rb
Ruby | 115 lines | 54 code | 18 blank | 43 comment | 1 complexity | 4f1dd171ed8faf57796056ba98d15912 MD5 | raw file
- #/usr/bin/env ruby
- require 'yaml'
- =begin
- Lump is a simple content management system -- *very* simple. Simplicity is
- one of its project goals, in fact. The ultimate goal is to have as little
- markup embedded code as reasonably possible, and put as much as reasonably
- possible into a library (probably one class, maybe two classes tops).
- This software is is Copyright Chad Perrin, 2011, and distributed under the
- terms of the Open Works License:
- # Open Works License
- This is version 0.8 of the Open Works License
- ## Terms
- Permission is hereby granted by the copyright holder(s), author(s), and
- contributor(s) of this work, to any person who obtains a copy of this work,
- in any form, to reproduce, modify, distribute, publish, sell, use, or
- otherwise deal in the licensed material without restriction, provided the
- following conditions are met:
- 1. Redistributions, modified or unmodified, in whole or in part, must
- retain the above license notice, this list of conditions, and the
- following disclaimer.
- 2. Redistributions, modified or unmodified, in whole or in part, must
- retain any applicable notices of attribution and copyright.
- No warranty or guarantee is implied by, or should be inferred from, this
- license or the act of distribution under the terms of this license. This
- license does not grant permission to use the trade names, trademarks,
- service marks, product names, or other identifications used by the licensor
- except as required for reasonable and customary use in reproducing, and
- describing the origin or use of, the work.
- =end
- class Lump
- def initialize(conf=nil)
- if conf
- @config = YAML.load_file(conf)
- else
- @config = Hash.new
- end
- @lump_url = '"http://bitbucket.org/apotheon/lump/"'
- end
- attr_reader :config
- # array.join("\n") approach looks prettier than concatenation to me
- def page_head
- head_text = [
- '<?xml version="1.0" encoding="UTF-8"?>',
- '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ' +
- 'Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/' +
- 'xhtml1-transitional.dtd">',
- '<html xmlns="http://www.w3.org/1999/xhtml" ' +
- 'dir="ltr" lang="en-US">',
- ' <head>'
- ]
- unless @config.empty?
- head_text.push " <title>#{page_title}</title>"
- link_list.each {|l| head_text.push(' ' + l) }
- end
- head_text.push ' </head>'
-
- head_text.join("\n")
- end
- def page_title
- @config['head']['title']
- end
- # This is primarily for specifying stylesheets in configuration, but was
- # generalized "just in case"
- def link_list
- @config['head']['link'].collect do |k,v|
- '<link rel="' + v['rel'].to_s + '" type="' + v['type'].to_s +
- '" title="' + v['title'].to_s + '" href="' + v['href'].to_s + '" />'
- end
- end
- # Something needs to be done with this to make it handle other link types,
- # such as offsite discussion links (reddit, Hacker News, et cetera).
- # Unfortunately, it appears to be a somewhat nontrivial problem at the
- # moment, so I've mostly ignored it.
- def anchor_list(a_type)
- @config['body']['anchor'][a_type].collect do |v|
- '<li><a href="' + v['url'].to_s + '">' + v['name'].to_s + '</a></li>'
- end
- end
- def page_foot
- foot_text = [ ' <div class="footer">' ]
- unless @config.empty?
- @config['foot']['copyright'].each do |c|
- foot_text.push " <p>#{c}</p>"
- end
- end
- foot_text.push " <p>Powered by <a href=#{@lump_url}>Lump</a></p>"
- foot_text.push ' </div>'
- foot_text.join "\n"
- end
- end