Class: GHWikiTools::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/ghwikitools/page.rb

Overview

Page is a class for wiki pages.

Direct Known Subclasses

Snippet

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, name, lang, ext) ⇒ Page

Returns a new instance of Page.

Parameters:

  • path (Pathname)

    page file path

  • name (String)

    name

  • lang (Symbol)

    language name

  • ext (Symbol)

    extension name



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

#extObject (readonly)

Parameters:

  • extension (String)

    of the page



75
76
77
# File 'lib/ghwikitools/page.rb', line 75

def ext
  @ext
end

#langObject (readonly)

Parameters:

  • language (String)

    name of the page



71
72
73
# File 'lib/ghwikitools/page.rb', line 71

def lang
  @lang
end

#nameObject (readonly)

Parameters:

  • name (String)

    of the page



67
68
69
# File 'lib/ghwikitools/page.rb', line 67

def name
  @name
end

#pathObject (readonly)

Parameters:

  • path (Pathname)

    of the page



63
64
65
# File 'lib/ghwikitools/page.rb', line 63

def path
  @path
end

Class Method Details

.allArray<Page>

Return all pages in the wiki.

Returns:

  • (Array<Page>)

    all pages



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.

Parameters:

  • filename (String)

    the filename

Returns:

  • (Page)

    the page

Raises:

  • (ArgumentError)


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

.dirObject

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.

Parameters:

  • name (String)

    snippet name

Returns:

  • (Boolean)

    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_snippetsArray<Snippet>

Return snippets in the page.

Returns:

  • (Array<Snippet>)

    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.

Parameters:

  • snippet (String)

    name

Returns:

  • (Boolean)

    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 a footer snippet metadata.

Returns:

  • (Boolean)

    true if footer metadata was inserted



118
119
120
# File 'lib/ghwikitools/page.rb', line 118

def insert_footer
  insert_common_snippet("Footer", :bottom)
end

#insert_headerBoolean

Insert a header snippet metadata.

Returns:

  • (Boolean)

    true if header metadata was inserted



110
111
112
# File 'lib/ghwikitools/page.rb', line 110

def insert_header
  insert_common_snippet("Header", :top)
end

#render_snippetsString

Return the result of rendering snippets.

Returns:

  • (String)

    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_snippetsBoolean

Update sinppets content in the page. Return true if the page is changed.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    true if the page is valid



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