Class: Ciridiri::Page
Constant Summary collapse
- @@content_dir =
Where pages should be stored on a file system
'.'
- @@backups =
Should we create backups (‘filename.1278278364.text`, where `1278278364` – current timestamp) or not. Useful when you are not going to place `@@content_dir` under version control
false
- @@caching =
Page fragments (formatted file ‘contents`) caching
true
- @@formatter =
You can use any formatter. For example:
Bluecloth:
require 'bluecloth' Page.formatter = lambda {|text| Bluecloth.new(text).to_html)}
RDiscount:
require 'rdiscount' Page.formatter = lambda {|text| RDiscount.new(text).to_html)}
Rutils with RDiscount:
require 'rutils' require 'rdiscount' Page.formatter = lambda {|text| RuTils::Gilenson::Formatter.new(RDiscount.new(text).to_html).to_html}
HTML escaping:
Page.formatter = {|text| "<pre>#{Rack::Utils.escape_html(text)}</pre>"}
lambda {|text| text}
Constants included from Ciridiri
CACHED_FILE_EXT, HTML_TITLE, MD_TITLE, SOURCE_FILE_EXT
Instance Attribute Summary collapse
-
#contents ⇒ Object
Define attr_reader/accessors.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#title ⇒ Object
Define attr_reader/accessors.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Class Method Summary collapse
-
.content_dir=(dir) ⇒ Object
Tiny ‘attr_writer` for `@@content_dir` which creates the content directory if it doesn’t exist.
Instance Method Summary collapse
-
#cache! ⇒ Object
Save ‘@contents` formatted with `Page.formatter` to the cache file `index.text` -> `index.text.html`.
-
#initialize(uri, contents) ⇒ Page
constructor
Convert ‘uri` to `path`, find the `title` in `contents`.
-
#revisions ⇒ Object
Return an array of the page revisions.
-
#save ⇒ Object
Create needed directory hierarchy and backup the file if needed.
-
#sweep! ⇒ Object
Delete the cache file.
-
#to_html ⇒ Object
Return ‘@contents` HTML representation.
Methods included from Paths
Methods included from Finders
all, find_by_uri, find_by_uri_or_empty
Constructor Details
#initialize(uri, contents) ⇒ Page
Convert ‘uri` to `path`, find the `title` in `contents`
62 63 64 |
# File 'lib/ciridiri/page.rb', line 62 def initialize(uri, contents) @path, @uri, @title, @contents = Page.path_from_uri(uri), uri, Page.find_title(contents), contents end |
Instance Attribute Details
#contents ⇒ Object
Define attr_reader/accessors
53 54 55 |
# File 'lib/ciridiri/page.rb', line 53 def contents @contents end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
54 55 56 |
# File 'lib/ciridiri/page.rb', line 54 def path @path end |
#title ⇒ Object
Define attr_reader/accessors
53 54 55 |
# File 'lib/ciridiri/page.rb', line 53 def title @title end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
54 55 56 |
# File 'lib/ciridiri/page.rb', line 54 def uri @uri end |
Class Method Details
.content_dir=(dir) ⇒ Object
Tiny ‘attr_writer` for `@@content_dir` which creates the content directory if it doesn’t exist
114 115 116 117 118 119 120 121 122 |
# File 'lib/ciridiri/page.rb', line 114 def self.content_dir=(dir) @@content_dir = dir begin FileUtils.mkdir_p(@@content_dir) if !File.exists?(@@content_dir) rescue Errno::EACCES # Can't create content dir, using current working dir @@content_dir = "." end end |
Instance Method Details
#cache! ⇒ Object
Save ‘@contents` formatted with `Page.formatter` to the cache file `index.text` -> `index.text.html`
83 84 85 |
# File 'lib/ciridiri/page.rb', line 83 def cache! File.open(@path + CACHED_FILE_EXT, 'w') {|f| f.write(@@formatter.call(@contents))} end |
#revisions ⇒ Object
Return an array of the page revisions
93 94 95 |
# File 'lib/ciridiri/page.rb', line 93 def revisions @revisions ||= find_revisions end |
#save ⇒ Object
Create needed directory hierarchy and backup the file if needed. Write ‘@contents` to the file and return `true` or `false` if any error occured
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/ciridiri/page.rb', line 69 def save FileUtils.mkdir_p(File.dirname(@path)) unless File.exists?(@path) backup if Page.backups? && File.exists?(@path) begin File.open(@path, "w") {|f| f.write(@contents)} true rescue StandardError false end end |
#sweep! ⇒ Object
Delete the cache file
88 89 90 |
# File 'lib/ciridiri/page.rb', line 88 def sweep! File.delete(@path + CACHED_FILE_EXT) end |
#to_html ⇒ Object
Return ‘@contents` HTML representation. If a page fragments caching enabled (`Page.caching = true`) then regenerate the fragment cache (`index.text.html`) if needed (it’s outdated or doesn’t exist) and return the cached contents. Otherwise (‘Page.caching = false`) return `@contents` formatted with `Page.formatter`
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/ciridiri/page.rb', line 102 def to_html if Page.caching? cached = @path + CACHED_FILE_EXT cache! if !File.exists?(cached) || File.mtime(@path) > File.mtime(cached) File.open(cached).read else @@formatter.call(@contents) end end |