Class: Ciridiri::Page

Inherits:
Object
  • Object
show all
Extended by:
Finders, Paths
Defined in:
lib/ciridiri/page.rb

Constant Summary collapse

MD_TITLE =

A regular expression for markdown like headers (‘#`, `##`, `###`, `=====`, `—-`)

Regexp.new("(^\#{1,3}\\s*?([^#].*?)#*$)|(^ {0,3}(\\S.*?)\\n(?:=|-)+(?=\\n+|\\Z))", Regexp::MULTILINE)
HTML_TITLE =

HTML headers (‘<h1-3>`)

Regexp.new("^<h[1-3](.*)?>((.*)+)</h[1-3]>")
SOURCE_FILE_EXT =

File extensions

".text".freeze
CACHED_FILE_EXT =
".html".freeze
@@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}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Paths

path_from_uri

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`



61
62
63
# File 'lib/ciridiri/page.rb', line 61

def initialize(uri, contents)
  @path, @uri, @title, @contents = Page.path_from_uri(uri), uri, Page.find_title(contents), contents
end

Instance Attribute Details

#contentsObject

Define attr_reader/accessors



52
53
54
# File 'lib/ciridiri/page.rb', line 52

def contents
  @contents
end

#pathObject (readonly)

Returns the value of attribute path.



53
54
55
# File 'lib/ciridiri/page.rb', line 53

def path
  @path
end

#titleObject

Define attr_reader/accessors



52
53
54
# File 'lib/ciridiri/page.rb', line 52

def title
  @title
end

#uriObject (readonly)

Returns the value of attribute uri.



53
54
55
# File 'lib/ciridiri/page.rb', line 53

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



113
114
115
116
117
118
119
120
121
# File 'lib/ciridiri/page.rb', line 113

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`



82
83
84
# File 'lib/ciridiri/page.rb', line 82

def cache!
  File.open(@path + CACHED_FILE_EXT, 'w') {|f| f.write(@@formatter.call(@contents))}
end

#revisionsObject

Return an array of the page revisions



92
93
94
# File 'lib/ciridiri/page.rb', line 92

def revisions
  @revisions ||= find_revisions
end

#saveObject

Create needed directory hierarchy and backup the file if needed. Write ‘@contents` to the file and return `true` or `false` if any error occured



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ciridiri/page.rb', line 68

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



87
88
89
# File 'lib/ciridiri/page.rb', line 87

def sweep!
  File.delete(@path + CACHED_FILE_EXT)
end

#to_htmlObject

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`



101
102
103
104
105
106
107
108
109
110
# File 'lib/ciridiri/page.rb', line 101

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