Class: Resource
- Inherits:
-
Object
- Object
- Resource
- Defined in:
- lib/den/resource.rb
Overview
General Den resource object (included by Page and Post)
Instance Method Summary collapse
- #[](index) ⇒ Object
-
#delete ⇒ Object
Deletes itself.
-
#initialize(file) ⇒ Resource
constructor
Initialize the object.
-
#markup(s) ⇒ Object
General markup function.
Constructor Details
#initialize(file) ⇒ Resource
Initialize the object
5 6 7 8 |
# File 'lib/den/resource.rb', line 5 def initialize(file) @file = file process end |
Instance Method Details
#[](index) ⇒ Object
11 12 13 |
# File 'lib/den/resource.rb', line 11 def [](index) @content[index] end |
#delete ⇒ Object
Deletes itself
17 18 19 |
# File 'lib/den/resource.rb', line 17 def delete File.delete(@file) end |
#markup(s) ⇒ Object
General markup function
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 59 |
# File 'lib/den/resource.rb', line 23 def markup(s) s = CGI::escapeHTML(s) title = nil # Extract title s.gsub!(/\A\[(.*\S)\]$/) do |x| title = $1 "" end # Remove duplicate newlines s.gsub!(/\n\n+/, "\n\n") # Remove leading spaces from lines s.gsub!(/^ +/, "") # Pad string to remove edge cases s.gsub!(/(\A\n*)|(\n*\z)/, "\n") # Headers s.gsub!(/^(#+)\s*(.*)/) { len = $1.length "<h#{len+1} class=\"header\">#$2</h#{len+1}>" } # Code (leaving placeholder pre tags) s.gsub!(/^\t(.*)$/, "<pre>\\1</pre>") # Paragraphs s.gsub!(/(\A|\n)\n([^<])/, "\n\n<p>\\2") s.gsub!(/([^>])\n\n/, "\\1</p>\n\n") # Remove placeholder pre tags s.gsub!(/<\/pre>\n<pre>/, "\n") {:title => title, :body => s} end |