From ca7489756976e32184237adf0394249f722bfa04 Mon Sep 17 00:00:00 2001 From: kristi Date: Thu, 10 May 2012 00:26:22 -0700 Subject: [PATCH] Add header_hashtag feature option: headers display a link icon when moused over so you can jump to sections Move anchor processing into markup.rb Use dashes for spaces in the anchor hashtag --- .../frontend/public/gollum/css/template.css | 2 +- lib/gollum/frontend/templates/page.mustache | 2 +- lib/gollum/markup.rb | 11 ++++++++++- lib/gollum/page.rb | 1 - lib/gollum/toc.rb | 17 +---------------- lib/gollum/wiki.rb | 5 +++++ 6 files changed, 18 insertions(+), 20 deletions(-) diff --git a/lib/gollum/frontend/public/gollum/css/template.css b/lib/gollum/frontend/public/gollum/css/template.css index 3ee75697..f77de707 100644 --- a/lib/gollum/frontend/public/gollum/css/template.css +++ b/lib/gollum/frontend/public/gollum/css/template.css @@ -73,7 +73,7 @@ a.absent { .markdown-body h4:hover a.anchor, .markdown-body h5:hover a.anchor, .markdown-body h6:hover a.anchor { - background: url('/images/para.png') no-repeat 10px center; + background: url('/images/para.png') no-repeat center center; text-decoration: none; } .markdown-body h1 tt, diff --git a/lib/gollum/frontend/templates/page.mustache b/lib/gollum/frontend/templates/page.mustache index f43969db..cac436e5 100644 --- a/lib/gollum/frontend/templates/page.mustache +++ b/lib/gollum/frontend/templates/page.mustache @@ -18,7 +18,7 @@
-
+
{{#has_toc}}

Table of contents

diff --git a/lib/gollum/markup.rb b/lib/gollum/markup.rb index 06463be7..f838c550 100644 --- a/lib/gollum/markup.rb +++ b/lib/gollum/markup.rb @@ -53,9 +53,18 @@ module Gollum end data = process_tags(data) data = process_code(data, encoding) - if sanitize || block_given? + if sanitize || block_given? || @wiki.header_anchors || @wiki.universal_toc doc = Nokogiri::HTML::DocumentFragment.parse(data) doc = sanitize.clean_node!(doc) if sanitize + + doc.css('h1,h2,h3,h4,h5,h6').each do |h| + link = CGI::escape(h.content.gsub(' ','-')) + anchor = Nokogiri::XML::Node.new('a', doc) + anchor['class'] = 'anchor' + anchor['id'] = link + anchor['href'] = '#' + link + h.child.before(anchor) + end if @wiki.header_anchors || @wiki.universal_toc yield doc if block_given? data = doc.to_html end diff --git a/lib/gollum/page.rb b/lib/gollum/page.rb index 10716dd8..909eb31b 100644 --- a/lib/gollum/page.rb +++ b/lib/gollum/page.rb @@ -180,7 +180,6 @@ module Gollum formatted_data if @doc == nil toc = Toc.new(@doc) if (toc_content = toc.generate) - toc.insert_anchors toc.generate.to_xhtml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XHTML) end end diff --git a/lib/gollum/toc.rb b/lib/gollum/toc.rb index 136218e6..0b748377 100644 --- a/lib/gollum/toc.rb +++ b/lib/gollum/toc.rb @@ -35,28 +35,13 @@ module Gollum flatten_to_lvl(lvl, 0) end - # Escape title string for use in ID attribute - # - # title - Title string - # - # Returns string - def insert_anchors - find_headings.each do |h| - rep_h = Nokogiri::XML::Node.new('a', @doc) - rep_h['class'] = 'wiki-toc-anchor' - rep_h['id'] = anchor_id(h.content) - rep_h.add_child(h.clone) - h.replace(rep_h) - end - end - # Escape title string for use in ID attribute # # title - Title string # # Returns string def anchor_id (title) - CGI::escape(title) + CGI::escape(title.gsub(' ','-')) end # Convert heading into list element diff --git a/lib/gollum/wiki.rb b/lib/gollum/wiki.rb index 9c065c9a..8daf4e57 100644 --- a/lib/gollum/wiki.rb +++ b/lib/gollum/wiki.rb @@ -134,6 +134,7 @@ module Gollum # site. # options - Optional Hash: # :universal_toc - Display table of contents on all pages. Default: false + # :header_hashtags - Show links to headers. Default: true # :base_path - String base path for all Wiki links. # Default: "/" # :page_class - The page Class. Default: Gollum::Page @@ -166,6 +167,7 @@ module Gollum @history_sanitization = options[:history_sanitization] || self.class.history_sanitization @universal_toc = options[:universal_toc] || false + @header_hashtags = options[:header_hashtags] || true end # Public: check whether the wiki's git repo exists on the filesystem. @@ -532,6 +534,9 @@ module Gollum # Toggles display of universal table of contents attr_reader :universal_toc + # Create hashtag link for headers to jump to sections + attr_reader :header_hashtags + # Normalize the data. # # data - The String data to be normalized.