lib/gollum/page.rb in gollum-2.0.0 vs lib/gollum/page.rb in gollum-2.1.0

- old
+ new

@@ -18,10 +18,15 @@ # Sets a Boolean determing whether this page is a historical version. # # Returns nothing. attr_writer :historical + # Parent page if this is a sub page + # + # Returns a Page + attr_accessor :parent_page + # Checks if a filename has a valid extension understood by GitHub::Markup. # # filename - String filename, like "Home.md". # # Returns the matching String basename of the file without the extension. @@ -97,11 +102,13 @@ # wiki - The Gollum::Wiki in question. # # Returns a newly initialized Gollum::Page. def initialize(wiki) @wiki = wiki - @blob = @footer = @sidebar = nil + @blob = @header = @footer = @sidebar = nil + @doc = nil + @parent_page = nil end # Public: The on-disk filename of the page including extension. # # Returns the String name. @@ -128,18 +135,47 @@ # filename by stripping the extension and replacing any dashes with # spaces. # # Returns the fully sanitized String title. def title - header = Sanitize.clean(name).strip + Sanitize.clean(name).strip end + # Public: Determines if this is a sub-page + # Sub-pages have filenames beginning with an underscore + # + # Returns true or false. + def sub_page + filename =~ /^_/ + end + # Public: The path of the page within the repo. # # Returns the String path. attr_reader :path + # Public: The url path required to reach this page within the repo. + # + # Returns the String url_path + def url_path + path = if self.path.include?('/') + self.path.sub(/\/[^\/]+$/, '/') + else + '' + end + + path << Page.cname(self.name, '-', '-') + path + end + + # Public: The url_path, but CGI escaped. + # + # Returns the String url_path + def escaped_url_path + CGI.escape(self.url_path).gsub('%2F','/') + end + # Public: The raw contents of the page. # # Returns the String data. def raw_data @blob && @blob.data @@ -162,13 +198,27 @@ # # encoding - Encoding Constant or String. # # Returns the String data. def formatted_data(encoding = nil, &block) - @blob && markup_class.render(historical?, encoding, &block) + @blob && markup_class.render(historical?, encoding) do |doc| + @doc = doc + yield doc if block_given? + end end + # Public: The table of contents of the page. + # + # formatted_data - page already marked up in html. + # + # Returns the String data. + def toc_data() + return @parent_page.toc_data if @parent_page and @sub_page + formatted_data if markup_class.toc == nil + markup_class.toc + end + # Public: The format of the page. # # Returns the Symbol format of the page. One of: # [ :markdown | :textile | :rdoc | :org | :rest | :asciidoc | :pod | # :roff ] @@ -207,10 +257,24 @@ else @wiki.repo.log(@wiki.ref, @path, log_pagination_options(options)) end end + # Public: The first 7 characters of the current version. + # + # Returns the first 7 characters of the current version. + def version_short + version.to_s[0,7] + end + + # Public: The header Page. + # + # Returns the header Page or nil if none exists. + def header + @header ||= find_sub_page(:header) + end + # Public: The footer Page. # # Returns the footer Page or nil if none exists. def footer @footer ||= find_sub_page(:footer) @@ -298,13 +362,13 @@ # # name - The human or canonical String page name to find. # version - The String version ID to find. # # Returns a Gollum::Page or nil if the page could not be found. - def find(name, version) + def find(name, version, dir = nil) map = @wiki.tree_map_for(version.to_s) - if page = find_page_in_tree(map, name) + if page = find_page_in_tree(map, name, dir) page.version = version.is_a?(Grit::Commit) ? version : @wiki.commit_for(version) page.historical = page.version.to_s == version.to_s page end @@ -374,12 +438,14 @@ end end false end - # Loads a sub page. Sub page nanes (footers) are prefixed with - # an underscore to distinguish them from other Pages. + # Loads a sub page. Sub page names (footers, headers, sidebars) are prefixed with + # an underscore to distinguish them from other Pages. If there is not one within + # the current directory, starts walking up the directory tree to try and find one + # within parent directories. # # name - String page name. # # Returns the Page or nil if none exists. def find_sub_page(name) @@ -388,18 +454,22 @@ name = "_#{name.to_s.capitalize}" return nil if page_match(name, self.filename) dirs = self.path.split('/') dirs.pop - map = @wiki.tree_map_for(self.version.id) + map = @wiki.tree_map_for(@wiki.ref, true) while !dirs.empty? if page = find_page_in_tree(map, name, dirs.join('/')) + page.parent_page = self return page end dirs.pop end - find_page_in_tree(map, name, '') + if page = find_page_in_tree(map, name, '') + page.parent_page = self + end + page end def inspect %(#<#{self.class.name}:#{object_id} #{name} (#{format}) @wiki=#{@wiki.repo.path.inspect}>) end