From 49545539278aafa63102f4a0942fb36036a2faca Mon Sep 17 00:00:00 2001 From: Roman Zaharenkov Date: Fri, 9 Nov 2012 10:56:02 +0300 Subject: [PATCH 1/2] Page title control Allow to set page title by setting header inside page body. It will use header as title If header is present and page URL otherwise. --- lib/gollum/frontend/views/page.rb | 48 ++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/gollum/frontend/views/page.rb b/lib/gollum/frontend/views/page.rb index b4e562c3..7fe4e921 100644 --- a/lib/gollum/frontend/views/page.rb +++ b/lib/gollum/frontend/views/page.rb @@ -8,7 +8,11 @@ module Precious DEFAULT_AUTHOR = 'you' def title - @page.url_path.gsub("-", " ") + extract_title(@content) || @page.url_path.gsub("-", " ") + end + + def content + without_title(@content) end def author @@ -94,6 +98,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 extract_title(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 without_title(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 From 6545fa691b5e6aae5ce87a95090325c72b8db0b3 Mon Sep 17 00:00:00 2001 From: Roman Zaharenkov Date: Sun, 11 Nov 2012 13:22:10 +0300 Subject: [PATCH 2/2] Adjust page header / title generation. Now page header can be generated by first h1 header from page content. But page title generated by URL is it was before. --- lib/gollum/frontend/templates/page.mustache | 2 +- lib/gollum/frontend/views/page.rb | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) 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 ) {