Class: GHWikiTools::Page
- Inherits:
-
Object
- Object
- GHWikiTools::Page
- Defined in:
- lib/ghwikitools/page.rb
Overview
Page is a class for wiki pages.
Direct Known Subclasses
Instance Attribute Summary collapse
- #ext ⇒ Object readonly
- #lang ⇒ Object readonly
- #name ⇒ Object readonly
- #path ⇒ Object readonly
Class Method Summary collapse
-
.all ⇒ Array<Page>
Return all pages in the wiki.
-
.by_filename(filename) ⇒ Page
Create a page by the filename.
-
.dir ⇒ Object
Return pages directory.
Instance Method Summary collapse
-
#delete_snippet(name) ⇒ Boolean
Delete the snippet metadata from the page.
-
#find_snippets ⇒ Array<Snippet>
Return snippets in the page.
-
#include_snippet?(name) ⇒ Boolean
Return true if the page includes snippet metadata that have the name.
-
#initialize(path, name, lang, ext) ⇒ Page
constructor
A new instance of Page.
-
#insert_footer ⇒ Boolean
Insert a footer snippet metadata.
-
#insert_header ⇒ Boolean
Insert a header snippet metadata.
-
#render_snippets ⇒ String
Return the result of rendering snippets.
-
#update_snippets ⇒ Boolean
Update sinppets content in the page.
-
#valid? ⇒ Boolean
Return true if the page is valid in the meaning of this tools.
-
#wikiname(lang = nil) ⇒ Object
Return the wikiname of the page with lang.
Constructor Details
#initialize(path, name, lang, ext) ⇒ Page
Returns a new instance of Page.
85 86 87 88 89 90 |
# File 'lib/ghwikitools/page.rb', line 85 def initialize(path, name, lang, ext) @path = path @name = name @lang = lang @ext = ext end |
Instance Attribute Details
#ext ⇒ Object (readonly)
75 76 77 |
# File 'lib/ghwikitools/page.rb', line 75 def ext @ext end |
#lang ⇒ Object (readonly)
71 72 73 |
# File 'lib/ghwikitools/page.rb', line 71 def lang @lang end |
#name ⇒ Object (readonly)
67 68 69 |
# File 'lib/ghwikitools/page.rb', line 67 def name @name end |
#path ⇒ Object (readonly)
63 64 65 |
# File 'lib/ghwikitools/page.rb', line 63 def path @path end |
Class Method Details
.all ⇒ Array<Page>
Return all pages in the wiki.
14 15 16 17 18 |
# File 'lib/ghwikitools/page.rb', line 14 def all Pathname.new(dir).entries.select{|e| (dir + e).file?}.map do |entry| by_filename(entry.to_s) end.select {|page| page.valid?} end |
.by_filename(filename) ⇒ Page
Create a page by the filename.
26 27 28 29 30 |
# File 'lib/ghwikitools/page.rb', line 26 def by_filename(filename) raise ArgumentError.new(filename) unless filename.include?(".") path, basename, lang, ext = parse_filename(filename) new(path, basename, lang, ext) end |
.dir ⇒ Object
Return pages directory.
6 7 8 |
# File 'lib/ghwikitools/page.rb', line 6 def dir GHWikiTools.dir end |
Instance Method Details
#delete_snippet(name) ⇒ Boolean
Delete the snippet metadata from the page. Return true if the page is changed.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/ghwikitools/page.rb', line 142 def delete_snippet(name) if .include?(name) content = @path.read new_content = content.gsub(snippet_regexp(name), "") unless content == new_content @path.open("w+") {|f| f.write(new_content)} return true end end return false end |
#find_snippets ⇒ Array<Snippet>
Return snippets in the page.
174 175 176 177 178 |
# File 'lib/ghwikitools/page.rb', line 174 def find_snippets .map do |name| Snippet.by_filename("_%s.%s" % [name, @ext]) end end |
#include_snippet?(name) ⇒ Boolean
Return true if the page includes snippet metadata that have the name.
186 187 188 |
# File 'lib/ghwikitools/page.rb', line 186 def include_snippet?(name) .include?(name) end |
#insert_footer ⇒ Boolean
Insert a footer snippet metadata.
118 119 120 |
# File 'lib/ghwikitools/page.rb', line 118 def insert_common_snippet("Footer", :bottom) end |
#insert_header ⇒ Boolean
Insert a header snippet metadata.
110 111 112 |
# File 'lib/ghwikitools/page.rb', line 110 def insert_header insert_common_snippet("Header", :top) end |
#render_snippets ⇒ String
Return the result of rendering snippets.
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/ghwikitools/page.rb', line 158 def render_snippets find_snippets.inject(@path.read) do |content, snippet| if snippet.valid? content.gsub(snippet_regexp(snippet.name)) do "%s\n\n%s\n\n%s" % [$1, snippet.render(self), $3] end else content end end end |
#update_snippets ⇒ Boolean
Update sinppets content in the page. Return true if the page is changed.
126 127 128 129 130 131 132 133 134 |
# File 'lib/ghwikitools/page.rb', line 126 def update_snippets if content = render_snippets unless content == @path.read @path.open("w+") {|f| f.write(content)} return true end end return false end |
#valid? ⇒ Boolean
Return true if the page is valid in the meaning of this tools.
96 97 98 99 |
# File 'lib/ghwikitools/page.rb', line 96 def valid? return false unless EXTENSIONS.include?(@ext) and @path.file? and @path.exist? return true end |
#wikiname(lang = nil) ⇒ Object
Return the wikiname of the page with lang
102 103 104 |
# File 'lib/ghwikitools/page.rb', line 102 def wikiname(lang=nil) lang ? "%s.%s" % [@name, lang] : @name end |