Skip to content
This repository has been archived by the owner on Apr 19, 2019. It is now read-only.

Commit

Permalink
Merge pull request gollum#576 from roman-zaharenkov/title_control
Browse files Browse the repository at this point in the history
Page title control
  • Loading branch information
bootstraponline committed Nov 11, 2012
2 parents bc4fc0e + 6545fa6 commit 44edb8c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/gollum/frontend/templates/page.mustache
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Mousetrap.bind(['e'], function( e ) {
</script> </script>
<div id="wiki-wrapper" class="page"> <div id="wiki-wrapper" class="page">
<div id="head"> <div id="head">
<h1>{{title}}</h1> <h1>{{page_header}}</h1>
<ul class="actions"> <ul class="actions">
<li class="minibutton"> <li class="minibutton">
{{>searchbar}} {{>searchbar}}
Expand Down
50 changes: 50 additions & 0 deletions lib/gollum/frontend/views/page.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ def title
@page.url_path.gsub("-", " ") @page.url_path.gsub("-", " ")
end end


def page_header
page_header_from_content(@content) || title
end

def content
content_without_page_header(@content)
end

def author def author
page_versions = @page.versions page_versions = @page.versions
first = page_versions ? page_versions.first : false first = page_versions ? page_versions.first : false
Expand Down Expand Up @@ -94,6 +102,48 @@ def css # custom css
def metadata def metadata
@page.metadata @page.metadata
end 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 end
end end

0 comments on commit 44edb8c

Please sign in to comment.