Skip to content
Browse files

Merge pull request #576 from roman-zaharenkov/title_control

Page title control
  • Loading branch information...
2 parents bc4fc0e + 6545fa6 commit 44edb8c7da7dcbaba2f0ebf2e559973bb18e6601 @bootstraponline bootstraponline committed Nov 11, 2012
Showing with 51 additions and 1 deletion.
  1. +1 −1 lib/gollum/frontend/templates/page.mustache
  2. +50 −0 lib/gollum/frontend/views/page.rb
View
2 lib/gollum/frontend/templates/page.mustache
@@ -7,7 +7,7 @@ Mousetrap.bind(['e'], function( e ) {
</script>
<div id="wiki-wrapper" class="page">
<div id="head">
- <h1>{{title}}</h1>
+ <h1>{{page_header}}</h1>
<ul class="actions">
<li class="minibutton">
{{>searchbar}}
View
50 lib/gollum/frontend/views/page.rb
@@ -11,6 +11,14 @@ def title
@page.url_path.gsub("-", " ")
end
+ def page_header
+ page_header_from_content(@content) || title
+ end
+
+ def content
+ content_without_page_header(@content)
+ end
+
def author
page_versions = @page.versions
first = page_versions ? page_versions.first : false
@@ -94,6 +102,48 @@ def css # custom css
def metadata
@page.metadata
end
+
+ private
+
+ # Wraps page formatted data to Nokogiri::HTML document.
+ #
+ def build_document(content)
+ Nokogiri::HTML(%{<div id="gollum-root">} + content + %{</div>})
+ end
+
+ # Finds header node inside Nokogiri::HTML document.
+ #
+ def find_header_node(doc)
+ case self.format
+ when :asciidoc
+ doc.css("div#gollum-root > div#header > h1:first-child")
+ when :org
+ doc.css("div#gollum-root > p.title:first-child")
+ when :pod
+ doc.css("div#gollum-root > a.dummyTopAnchor:first-child + h1")
+ when :rest
+ doc.css("div#gollum-root > div > div > h1:first-child")
+ else
+ doc.css("div#gollum-root > h1:first-child")
+ end
+ end
+
+ # Extracts title from page if present.
+ #
+ def page_header_from_content(content)
+ doc = build_document(content)
+ title = find_header_node(doc)
+ Sanitize.clean(title.to_html).strip unless title.empty?
+ end
+
+ # Returns page content without title if it was extracted.
+ #
+ def content_without_page_header(content)
+ doc = build_document(content)
+ title = find_header_node(doc)
+ title.remove unless title.empty?
+ doc.css("div#gollum-root").inner_html
+ end
end
end
end

0 comments on commit 44edb8c

Please sign in to comment.
Something went wrong with that request. Please try again.