diff --git a/lib/gollum/frontend/templates/page.mustache b/lib/gollum/frontend/templates/page.mustache
index 02fae325..083f8a83 100644
--- a/lib/gollum/frontend/templates/page.mustache
+++ b/lib/gollum/frontend/templates/page.mustache
@@ -7,7 +7,7 @@ Mousetrap.bind(['e'], function( e ) {
-
{{title}}
+
{{page_header}}
-
{{>searchbar}}
diff --git a/lib/gollum/frontend/views/page.rb b/lib/gollum/frontend/views/page.rb
index b4e562c3..9d98263f 100644
--- a/lib/gollum/frontend/views/page.rb
+++ b/lib/gollum/frontend/views/page.rb
@@ -11,6 +11,14 @@ module Precious
@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 @@ module Precious
def metadata
@page.metadata
end
+
+ private
+
+ # Wraps page formatted data to Nokogiri::HTML document.
+ #
+ def build_document(content)
+ Nokogiri::HTML(%{
} + content + %{
})
+ 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