Merge pull request #343 from kristi/toc

Table of contents feature
This commit is contained in:
Kristi
2012-05-22 13:14:25 -07:00
37 changed files with 293 additions and 40 deletions
+17
View File
@@ -289,6 +289,22 @@ wiki page, simply preface the link with a single quote (like in LISP):
This is useful for writing about the link syntax in your wiki pages. This is useful for writing about the link syntax in your wiki pages.
## TABLE OF CONTENTS
Gollum has a special tag to insert a table of contents (new in v2.1)
'[[_TOC_]]
This tag is case sensitive, use all upper case. The TOC tag can be inserted
into the `_Header`, `_Footer` or `_Sidebar` files too.
There is also a wiki option `:universal_toc` which will display a
table of contents at the top of all your wiki pages if it is enabled.
The `:universal_toc` is not enabled by default. To set the option,
add the option to the `:wiki_options` hash before starting the
frontend app:
Precious::App.set(:wiki_options, {:universal_toc => true})
## SYNTAX HIGHLIGHTING ## SYNTAX HIGHLIGHTING
@@ -471,6 +487,7 @@ like Rack::Auth, OmniAuth, etc.
gollum_path = File.expand_path(File.dirname(__FILE__)) # CHANGE THIS TO POINT TO YOUR OWN WIKI REPO gollum_path = File.expand_path(File.dirname(__FILE__)) # CHANGE THIS TO POINT TO YOUR OWN WIKI REPO
Precious::App.set(:gollum_path, gollum_path) Precious::App.set(:gollum_path, gollum_path)
Precious::App.set(:default_markup, :markdown) # set your favorite markup language Precious::App.set(:default_markup, :markdown) # set your favorite markup language
Precious::App.set(:wiki_options, {:universal_toc => false})
run Precious::App run Precious::App
## Windows Filename Validation ## Windows Filename Validation
+19 -5
View File
@@ -8,6 +8,17 @@ require 'gollum/frontend/views/editable'
require File.expand_path '../uri_encode_component', __FILE__ require File.expand_path '../uri_encode_component', __FILE__
# Run the frontend, based on Sinatra
#
# There are a number of wiki options that can be set for the frontend
#
# Example
# require 'gollum/frontend/app'
# Precious::App.set(:wiki_options, {
# :universal_toc => false,
# }
#
# See the wiki.rb file for more details on wiki options
module Precious module Precious
class App < Sinatra::Base class App < Sinatra::Base
register Mustache::Sinatra register Mustache::Sinatra
@@ -122,10 +133,11 @@ module Precious
end end
post '/preview' do post '/preview' do
wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options) wiki = Gollum::Wiki.new(settings.gollum_path, settings.wiki_options)
@name = "Preview" @name = "Preview"
@page = wiki.preview_page(@name, params[:content], params[:format]) @page = wiki.preview_page(@name, params[:content], params[:format])
@content = @page.formatted_data @content = @page.formatted_data
@toc_content = wiki.universal_toc ? @page.toc_data : nil
@editable = false @editable = false
mustache :page mustache :page
end end
@@ -220,8 +232,10 @@ module Precious
if page = wiki.page(name) if page = wiki.page(name)
@page = page @page = page
@name = name @name = name
@content = page.formatted_data
@editable = true @editable = true
@content = page.formatted_data
@toc_content = wiki.universal_toc ? @page.toc_data : nil
mustache :page mustache :page
elsif file = wiki.file(name) elsif file = wiki.file(name)
content_type file.mime_type content_type file.mime_type
@@ -75,6 +75,7 @@ a:hover, a:visited {
#wiki-body { #wiki-body {
display: block; display: block;
float: left; float: left;
clear: left;
margin-right: 3%; margin-right: 3%;
margin-bottom: 40px; margin-bottom: 40px;
width: 100%; width: 100%;
@@ -84,6 +85,24 @@ a:hover, a:visited {
width: 68%; width: 68%;
} }
/* @section toc */
#wiki-toc-main {
background-color: #F7F7F7;
border: 1px solid #DDD;
font-size: 13px;
padding: 0px 5px;
float:left;
margin-bottom: 20px;
min-width: 33%;
border-radius: 0.5em;
-moz-border-radius: 0.5em;
-webkit-border-radius: 0.5em;
}
#wiki-toc-main > div {
border: none;
}
/* @section rightbar */ /* @section rightbar */
#wiki-rightbar { #wiki-rightbar {
background-color: #f7f7f7; background-color: #f7f7f7;
@@ -145,6 +164,7 @@ a:hover, a:visited {
#wiki-header #header-content { #wiki-header #header-content {
margin-bottom: 1.5em; margin-bottom: 1.5em;
} }
#wiki-footer #footer-content { #wiki-footer #footer-content {
margin-top: 1.5em; margin-top: 1.5em;
} }
@@ -30,6 +30,11 @@ a.absent {
color: #c00; color: #c00;
} }
.markdown-body a[id].wiki-toc-anchor {
color: inherit;
text-decoration: none;
}
.markdown-body { .markdown-body {
font-size: 14px; font-size: 14px;
line-height: 1.6; line-height: 1.6;
@@ -72,7 +77,7 @@ a.absent {
.markdown-body h4:hover a.anchor, .markdown-body h4:hover a.anchor,
.markdown-body h5:hover a.anchor, .markdown-body h5:hover a.anchor,
.markdown-body h6:hover a.anchor { .markdown-body h6:hover a.anchor {
background: url('/images/para.png') no-repeat 10px center; background: url('/images/pin-20.png') no-repeat left center;
text-decoration: none; text-decoration: none;
} }
.markdown-body h1 tt, .markdown-body h1 tt,
@@ -362,6 +367,43 @@ a.absent {
margin: 0; margin: 0;
padding: 0; padding: 0;
} }
.toc {
background-color: #F7F7F7;
border: 1px solid #ddd;
padding: 5px 10px;
margin: 0;
border-radius: 3px;
}
.toc-title {
color: #888;
font-size: 14px;
line-height: 1.6;
padding: 2px;
border-bottom: 1px solid #ddd;
margin-bottom: 3px;
}
.toc ul {
padding-left: 10px;
margin: 0;
}
.toc>ul {
margin-left: 10px;
font-size: 17px;
}
.toc ul ul {
font-size: 15px;
}
.toc ul ul ul {
font-size: 14px;
}
.toc ul li{
margin: 0;
}
#header-content .toc,
#footer-content .toc,
#sidebar-content .toc {
border: none;
}
.highlight { .highlight {
background: #fff; background: #fff;
} }
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

+13 -8
View File
@@ -20,7 +20,19 @@
</ul> </ul>
</div> </div>
<div id="wiki-content"> <div id="wiki-content">
<div class="wrap {{#has_footer}} has-footer {{/has_footer}} {{#has_sidebar}} has-rightbar{{/has_sidebar}}"> <div class="{{#has_header}}has-header{{/has_header}}{{#has_footer}} has-footer{{/has_footer}}{{#has_sidebar}} has-rightbar{{/has_sidebar}}{{#has_toc}} has-toc{{/has_toc}}">
{{#has_toc}}
<div id="wiki-toc-main">
{{{toc_content}}}
</div>
{{/has_toc}}
{{#has_sidebar}}
<div id="wiki-rightbar" class="gollum-{{sidebar_format}}-content">
<div id="sidebar-content" class="markdown-body">
{{{sidebar_content}}}
</div>
</div>
{{/has_sidebar}}
<div id="wiki-body" class="gollum-{{format}}-content"> <div id="wiki-body" class="gollum-{{format}}-content">
{{#has_header}} {{#has_header}}
<div id="wiki-header" class="gollum-{{header_format}}-content"> <div id="wiki-header" class="gollum-{{header_format}}-content">
@@ -33,13 +45,6 @@
{{{content}}} {{{content}}}
</div> </div>
</div> </div>
{{#has_sidebar}}
<div id="wiki-rightbar" class="gollum-{{sidebar_format}}-content">
<div id="sidebar-content" class="markdown-body">
{{{sidebar_content}}}
</div>
</div>
{{/has_sidebar}}
{{#has_footer}} {{#has_footer}}
<div id="wiki-footer" class="gollum-{{footer_format}}-content"> <div id="wiki-footer" class="gollum-{{footer_format}}-content">
<div id="footer-content" class="markdown-body"> <div id="footer-content" class="markdown-body">
+6
View File
@@ -65,6 +65,12 @@ module Precious
def sidebar_format def sidebar_format
has_sidebar && @sidebar.format.to_s has_sidebar && @sidebar.format.to_s
end end
def has_toc
!@toc_content.nil?
end
def toc_content
@toc_content
end
end end
end end
end end
+66 -7
View File
@@ -6,6 +6,7 @@ require 'base64'
module Gollum module Gollum
class Markup class Markup
attr_accessor :toc
# Initialize a new Markup object. # Initialize a new Markup object.
# #
# page - The Gollum::Page. # page - The Gollum::Page.
@@ -17,12 +18,15 @@ module Gollum
@data = page.text_data @data = page.text_data
@version = page.version.id if page.version @version = page.version.id if page.version
@format = page.format @format = page.format
@sub_page = page.sub_page
@parent_page = page.parent_page
@dir = ::File.dirname(page.path) @dir = ::File.dirname(page.path)
@tagmap = {} @tagmap = {}
@codemap = {} @codemap = {}
@texmap = {} @texmap = {}
@wsdmap = {} @wsdmap = {}
@premap = {} @premap = {}
@toc = nil
end end
# Render the content with Gollum wiki syntax on top of the file's own # Render the content with Gollum wiki syntax on top of the file's own
@@ -54,18 +58,60 @@ module Gollum
data = process_tags(data) data = process_tags(data)
data = process_code(data, encoding) data = process_code(data, encoding)
if sanitize || block_given? doc = Nokogiri::HTML::DocumentFragment.parse(data)
doc = Nokogiri::HTML::DocumentFragment.parse(data) doc = sanitize.clean_node!(doc) if sanitize
doc = sanitize.clean_node!(doc) if sanitize doc,toc = process_headers(doc)
yield doc if block_given? @toc = @sub_page ? ( @parent_page ? @parent_page.toc_data : "[[_TOC_]]" ) : toc
data = doc.to_html yield doc if block_given?
end data = doc.to_html
data = process_toc_tags(data)
data = process_tex(data) data = process_tex(data)
data = process_wsd(data) data = process_wsd(data)
data.gsub!(/<p><\/p>/, '') data.gsub!(/<p><\/p>/, '')
data data
end end
# Inserts header anchors and creates TOC
#
# doc - Nokogiri parsed document
#
# Returns doc Document and toc String
def process_headers(doc)
toc = nil
doc.css('h1,h2,h3,h4,h5,h6').each do |h|
id = CGI::escape(h.content.gsub(' ','-'))
level = h.name.gsub(/[hH]/,'').to_i
# Add anchors
anchor = Nokogiri::XML::Node.new('a', doc)
anchor['class'] = 'anchor'
anchor['id'] = id
anchor['href'] = '#' + id
h.add_child(anchor)
# Build TOC
toc ||= Nokogiri::XML::DocumentFragment.parse('<div class="toc"><div class="toc-title">Table of Contents</div></div>')
tail ||= toc.child
tail_level ||= 0
while tail_level < level
node = Nokogiri::XML::Node.new('ul', doc)
tail = tail.add_child(node)
tail_level += 1
end
while tail_level > level
tail = tail.parent
tail_level -= 1
end
node = Nokogiri::XML::Node.new('li', doc)
node.add_child("<a href='##{id}'>#{h.content}</a>")
tail.add_child(node)
end
toc = toc.to_xhtml if toc != nil
[doc, toc]
end
######################################################################### #########################################################################
# #
# TeX # TeX
@@ -184,7 +230,9 @@ module Gollum
# #
# Returns the String HTML version of the tag. # Returns the String HTML version of the tag.
def process_tag(tag) def process_tag(tag)
if html = process_image_tag(tag) if tag =~ /^_TOC_$/
%{[[#{tag}]]}
elsif html = process_image_tag(tag)
html html
elsif html = process_file_link_tag(tag) elsif html = process_file_link_tag(tag)
html html
@@ -341,6 +389,17 @@ module Gollum
end end
end end
# Process the special table of contents tag [[_TOC_]]
#
# data - The String data (with placeholders).
#
# Returns the marked up String data.
def process_toc_tags(data)
data.gsub!("[[_TOC_]]", @toc.nil? ? '' : @toc)
data
end
# Find the given file in the repo. # Find the given file in the repo.
# #
# name - The String absolute or relative path of the file. # name - The String absolute or relative path of the file.
+35 -2
View File
@@ -20,6 +20,11 @@ module Gollum
# Returns nothing. # Returns nothing.
attr_writer :historical attr_writer :historical
# Parent page if this is a sub page
#
# Returns a Page
attr_accessor :parent_page
# Checks if a filename has a valid extension understood by GitHub::Markup. # Checks if a filename has a valid extension understood by GitHub::Markup.
# #
# filename - String filename, like "Home.md". # filename - String filename, like "Home.md".
@@ -100,6 +105,8 @@ module Gollum
def initialize(wiki) def initialize(wiki)
@wiki = wiki @wiki = wiki
@blob = @header = @footer = @sidebar = nil @blob = @header = @footer = @sidebar = nil
@doc = nil
@parent_page = nil
end end
# Public: The on-disk filename of the page including extension. # Public: The on-disk filename of the page including extension.
@@ -133,6 +140,14 @@ module Gollum
Sanitize.clean(name).strip Sanitize.clean(name).strip
end end
# Public: Determines if this is a sub-page
# Sub-pages have filenames beginning with an underscore
#
# Returns true or false.
def sub_page
filename =~ /^_/
end
# Public: The path of the page within the repo. # Public: The path of the page within the repo.
# #
# Returns the String path. # Returns the String path.
@@ -164,7 +179,21 @@ module Gollum
# #
# Returns the String data. # Returns the String data.
def formatted_data(encoding = nil, &block) def formatted_data(encoding = nil, &block)
@blob && markup_class.render(historical?, encoding, &block) @blob && markup_class.render(historical?, encoding) do |doc|
@doc = doc
yield doc if block_given?
end
end
# Public: The table of contents of the page.
#
# formatted_data - page already marked up in html.
#
# Returns the String data.
def toc_data()
return @parent_page.toc_data if @parent_page and @sub_page
formatted_data if markup_class.toc == nil
markup_class.toc
end end
# Public: The format of the page. # Public: The format of the page.
@@ -400,12 +429,16 @@ module Gollum
map = @wiki.tree_map_for(@wiki.ref) map = @wiki.tree_map_for(@wiki.ref)
while !dirs.empty? while !dirs.empty?
if page = find_page_in_tree(map, name, dirs.join('/')) if page = find_page_in_tree(map, name, dirs.join('/'))
page.parent_page = self
return page return page
end end
dirs.pop dirs.pop
end end
find_page_in_tree(map, name, '') if page = find_page_in_tree(map, name, '')
page.parent_page = self
end
page
end end
def inspect def inspect
+11
View File
@@ -32,6 +32,10 @@ module Gollum
# sanitization altogether. # sanitization altogether.
attr_writer :history_sanitization attr_writer :history_sanitization
# Hash for setting different default wiki options
# These defaults can be overridden by options passed directly to initialize()
attr_accessor :default_options
# Gets the page class used by all instances of this Wiki. # Gets the page class used by all instances of this Wiki.
# Default: Gollum::Page. # Default: Gollum::Page.
def page_class def page_class
@@ -107,6 +111,7 @@ module Gollum
self.default_committer_email = 'anon@anon.com' self.default_committer_email = 'anon@anon.com'
self.default_ws_subs = ['_','-'] self.default_ws_subs = ['_','-']
self.default_options = {}
# The String base path to prefix to internal links. For example, when set # The String base path to prefix to internal links. For example, when set
# to "/wiki", the page "Hobbit" will be linked as "/wiki/Hobbit". Defaults # to "/wiki", the page "Hobbit" will be linked as "/wiki/Hobbit". Defaults
@@ -133,6 +138,7 @@ module Gollum
# path - The String path to the Git repository that holds the Gollum # path - The String path to the Git repository that holds the Gollum
# site. # site.
# options - Optional Hash: # options - Optional Hash:
# :universal_toc - Table of contents on all pages. Default: false
# :base_path - String base path for all Wiki links. # :base_path - String base path for all Wiki links.
# Default: "/" # Default: "/"
# :page_class - The page Class. Default: Gollum::Page # :page_class - The page Class. Default: Gollum::Page
@@ -146,6 +152,7 @@ module Gollum
# #
# Returns a fresh Gollum::Repo. # Returns a fresh Gollum::Repo.
def initialize(path, options = {}) def initialize(path, options = {})
options = self.class.default_options.merge(options)
if path.is_a?(GitAccess) if path.is_a?(GitAccess)
options[:access] = path options[:access] = path
path = path.path path = path.path
@@ -165,6 +172,7 @@ module Gollum
self.class.default_ws_subs self.class.default_ws_subs
@history_sanitization = options[:history_sanitization] || @history_sanitization = options[:history_sanitization] ||
self.class.history_sanitization self.class.history_sanitization
@universal_toc = options.fetch(:universal_toc, false)
end end
# Public: check whether the wiki's git repo exists on the filesystem. # Public: check whether the wiki's git repo exists on the filesystem.
@@ -528,6 +536,9 @@ module Gollum
# Gets the markup class used by all instances of this Wiki. # Gets the markup class used by all instances of this Wiki.
attr_reader :markup_classes attr_reader :markup_classes
# Toggles display of universal table of contents
attr_reader :universal_toc
# Normalize the data. # Normalize the data.
# #
# data - The String data to be normalized. # data - The String data to be normalized.
+1 -1
View File
@@ -1,7 +1,7 @@
[core] [core]
repositoryformatversion = 0 repositoryformatversion = 0
filemode = true filemode = true
bare = false bare = true
logallrefupdates = true logallrefupdates = true
ignorecase = true ignorecase = true
[remote "origin"] [remote "origin"]
+2
View File
@@ -1,3 +1,5 @@
0000000000000000000000000000000000000000 60f12f4254f58801b9ee7db7bca5fa8aeefaa56b rick <technoweenie@gmail.com> 1291341857 -0800 clone: from /Users/rick/p/gollum/test/examples/lotr.git 0000000000000000000000000000000000000000 60f12f4254f58801b9ee7db7bca5fa8aeefaa56b rick <technoweenie@gmail.com> 1291341857 -0800 clone: from /Users/rick/p/gollum/test/examples/lotr.git
60f12f4254f58801b9ee7db7bca5fa8aeefaa56b a8ad3c09dd842a3517085bfadd37718856dee813 rick <technoweenie@gmail.com> 1291341922 -0800 commit: add sidebars 60f12f4254f58801b9ee7db7bca5fa8aeefaa56b a8ad3c09dd842a3517085bfadd37718856dee813 rick <technoweenie@gmail.com> 1291341922 -0800 commit: add sidebars
a8ad3c09dd842a3517085bfadd37718856dee813 1db89ebba7e2c14d93b94ff98cfa3708a4f0d4e3 Arran Cudbard-Bell <a.cudbardb@freeradius.org> 1309107565 +0200 commit: Test out whitespace with Sam a8ad3c09dd842a3517085bfadd37718856dee813 1db89ebba7e2c14d93b94ff98cfa3708a4f0d4e3 Arran Cudbard-Bell <a.cudbardb@freeradius.org> 1309107565 +0200 commit: Test out whitespace with Sam
1db89ebba7e2c14d93b94ff98cfa3708a4f0d4e3 b16b3d9fad9d78e5a669e7f33d94c96da374eccd kristi <kristi.dev@gmail.com> 1336983525 -0700 push
b16b3d9fad9d78e5a669e7f33d94c96da374eccd b0de6e794dfdc7ef3400e894225bfe23308aae5c kristi <kristi.dev@gmail.com> 1336984025 -0700 push
@@ -1,3 +1,5 @@
0000000000000000000000000000000000000000 60f12f4254f58801b9ee7db7bca5fa8aeefaa56b rick <technoweenie@gmail.com> 1291341857 -0800 clone: from /Users/rick/p/gollum/test/examples/lotr.git 0000000000000000000000000000000000000000 60f12f4254f58801b9ee7db7bca5fa8aeefaa56b rick <technoweenie@gmail.com> 1291341857 -0800 clone: from /Users/rick/p/gollum/test/examples/lotr.git
60f12f4254f58801b9ee7db7bca5fa8aeefaa56b a8ad3c09dd842a3517085bfadd37718856dee813 rick <technoweenie@gmail.com> 1291341922 -0800 commit: add sidebars 60f12f4254f58801b9ee7db7bca5fa8aeefaa56b a8ad3c09dd842a3517085bfadd37718856dee813 rick <technoweenie@gmail.com> 1291341922 -0800 commit: add sidebars
a8ad3c09dd842a3517085bfadd37718856dee813 1db89ebba7e2c14d93b94ff98cfa3708a4f0d4e3 Arran Cudbard-Bell <a.cudbardb@freeradius.org> 1309107565 +0200 commit: Test out whitespace with Sam a8ad3c09dd842a3517085bfadd37718856dee813 1db89ebba7e2c14d93b94ff98cfa3708a4f0d4e3 Arran Cudbard-Bell <a.cudbardb@freeradius.org> 1309107565 +0200 commit: Test out whitespace with Sam
1db89ebba7e2c14d93b94ff98cfa3708a4f0d4e3 b16b3d9fad9d78e5a669e7f33d94c96da374eccd kristi <kristi.dev@gmail.com> 1336983525 -0700 push
b16b3d9fad9d78e5a669e7f33d94c96da374eccd b0de6e794dfdc7ef3400e894225bfe23308aae5c kristi <kristi.dev@gmail.com> 1336984025 -0700 push
@@ -0,0 +1,2 @@
x•ŽQ
Â0ýÎ)öJ’m7 ˆx±Ín´Ø‰Ñóð~=楲mKîUU3Ù4²·<ZÇÁf.d¶š¡:õI½šÍ“«>ÌŽf”˜Y¢„IG&Š2v6¤HÂMI ¿Û­T¸×åÕ8þö ú9_7^ÖC*Û "Å iˆ°·ÁZÓi¿×ôoѰÜ”¥«­À¥Téõ\ÖÌçéKº
+1 -1
View File
@@ -1 +1 @@
1db89ebba7e2c14d93b94ff98cfa3708a4f0d4e3 b0de6e794dfdc7ef3400e894225bfe23308aae5c
+1 -1
View File
@@ -50,7 +50,7 @@ context "Wiki" do
end end
test "parents with default master ref" do test "parents with default master ref" do
ref = '1db89ebba7e2c14d93b94ff98cfa3708a4f0d4e3' ref = 'b0de6e794dfdc7ef3400e894225bfe23308aae5c'
committer = Gollum::Committer.new(@wiki) committer = Gollum::Committer.new(@wiki)
assert_equal ref, committer.parents.first.sha assert_equal ref, committer.parents.first.sha
end end
+2 -1
View File
@@ -18,8 +18,9 @@ context "GitAccess" do
assert @access.ref_map.empty? assert @access.ref_map.empty?
assert @access.tree_map.empty? assert @access.tree_map.empty?
@access.tree 'master' @access.tree 'master'
assert_equal({"master"=>"1db89ebba7e2c14d93b94ff98cfa3708a4f0d4e3"}, @access.ref_map) assert_equal({"master"=>"b0de6e794dfdc7ef3400e894225bfe23308aae5c"}, @access.ref_map)
@access.tree '1db89ebba7e2c14d93b94ff98cfa3708a4f0d4e3'
map = @access.tree_map['1db89ebba7e2c14d93b94ff98cfa3708a4f0d4e3'] map = @access.tree_map['1db89ebba7e2c14d93b94ff98cfa3708a4f0d4e3']
assert_equal 'Bilbo-Baggins.md', map[0].path assert_equal 'Bilbo-Baggins.md', map[0].path
assert_equal '', map[0].dir assert_equal '', map[0].dir
+8 -6
View File
@@ -6,6 +6,7 @@ context "Markup" do
@path = testpath("examples/test.git") @path = testpath("examples/test.git")
FileUtils.rm_rf(@path) FileUtils.rm_rf(@path)
Grit::Repo.init_bare(@path) Grit::Repo.init_bare(@path)
Gollum::Wiki.default_options = {:universal_toc => false}
@wiki = Gollum::Wiki.new(@path) @wiki = Gollum::Wiki.new(@path)
end end
@@ -573,14 +574,15 @@ np.array([[2,2],[1,3]],np.float)
test "id with prefix ok" do test "id with prefix ok" do
content = "h2(example#wiki-foo). xxxx" content = "h2(example#wiki-foo). xxxx"
output = %(<h2 class="example" id="wiki-foo">xxxx</h2>) output = %(<h2 class="example" id="wiki-foo">xxxx<a class=\"anchor\" id=\"xxxx\" href=\"#xxxx\"></a></h2>)
compare(content, output, :textile) compare(content, output, :textile)
end end
test "id prefix added" do test "id prefix added" do
content = "h2(#foo). xxxx[1]\n\nfn1.footnote" content = "h2(#foo). xxxx[1]\n\nfn1.footnote"
output = "<h2 id=\"wiki-foo\">xxxx" + output = "<h2 id=\"wiki-foo\">xxxx" +
"<sup class=\"footnote\" id=\"wiki-fnr1\"><a href=\"#wiki-fn1\">1</a></sup></h2>" + "<sup class=\"footnote\" id=\"wiki-fnr1\"><a href=\"#wiki-fn1\">1</a></sup>" +
"<a class=\"anchor\" id=\"xxxx1\" href=\"#xxxx1\"></a></h2>" +
"\n<p class=\"footnote\" id=\"wiki-fn1\"><a href=\"#wiki-fnr1\"><sup>1</sup></a> footnote</p>" "\n<p class=\"footnote\" id=\"wiki-fn1\"><a href=\"#wiki-fnr1\"><sup>1</sup></a> footnote</p>"
compare(content, output, :textile) compare(content, output, :textile)
end end
@@ -618,11 +620,11 @@ np.array([[2,2],[1,3]],np.float)
######################################################################### #########################################################################
test "asciidoc header" do test "asciidoc header" do
compare("= Book Title\n\n== Heading", '<div class="sect1"><h2 id="wiki-_heading">Heading</h2><div class="sectionbody"></div></div>', 'asciidoc') compare("= Book Title\n\n== Heading", '<div class="sect1"><h2 id="wiki-_heading">Heading<a class="anchor" id="Heading" href="#Heading"></a></h2><div class="sectionbody"></div></div>', 'asciidoc')
end end
test "internal links with asciidoc" do test "internal links with asciidoc" do
compare("= Book Title\n\n[[anid]]\n== Heading", '<div class="sect1"><h2 id="wiki-anid">Heading</h2><div class="sectionbody"></div></div>', 'asciidoc') compare("= Book Title\n\n[[anid]]\n== Heading", '<div class="sect1"><h2 id="wiki-anid">Heading<a class="anchor" id="Heading" href="#Heading"></a></h2><div class="sectionbody"></div></div>', 'asciidoc')
end end
######################################################################### #########################################################################
+22 -1
View File
@@ -16,7 +16,7 @@ context "Page" do
page = @wiki.page('Bilbo Baggins') page = @wiki.page('Bilbo Baggins')
assert_equal Gollum::Page, page.class assert_equal Gollum::Page, page.class
assert page.raw_data =~ /^# Bilbo Baggins\n\nBilbo Baggins/ assert page.raw_data =~ /^# Bilbo Baggins\n\nBilbo Baggins/
assert page.formatted_data =~ /<h1>Bilbo Baggins<\/h1>\n\n<p>Bilbo Baggins/ assert page.formatted_data =~ %r{<h1>Bilbo Baggins<a class="anchor" id="Bilbo-Baggins" href="#Bilbo-Baggins"></a>\n</h1>\n\n<p>Bilbo Baggins}
assert_equal 'Bilbo-Baggins.md', page.path assert_equal 'Bilbo-Baggins.md', page.path
assert_equal :markdown, page.format assert_equal :markdown, page.format
assert_equal @wiki.repo.commits.first.id, page.version.id assert_equal @wiki.repo.commits.first.id, page.version.id
@@ -114,6 +114,25 @@ context "Page" do
assert_equal "Eye Of Sauron", page.title assert_equal "Eye Of Sauron", page.title
end end
test "top level header" do
header = @wiki.page('Home').header
assert_equal "Hobbits\n", header.raw_data
assert_equal "_Header.md", header.path
end
test "nested header" do
header = @wiki.page('Eye Of Sauron').header
assert_equal "Sauron\n", header.raw_data
assert_equal "Mordor/_Header.md", header.path
end
test "header itself" do
header = @wiki.page("_Header")
assert_nil header.header
assert_nil header.footer
assert_nil header.sidebar
end
test "top level footer" do test "top level footer" do
footer = @wiki.page('Home').footer footer = @wiki.page('Home').footer
assert_equal 'Lord of the Rings wiki', footer.raw_data assert_equal 'Lord of the Rings wiki', footer.raw_data
@@ -128,6 +147,7 @@ context "Page" do
test "footer itself" do test "footer itself" do
footer = @wiki.page("_Footer") footer = @wiki.page("_Footer")
assert_nil footer.header
assert_nil footer.footer assert_nil footer.footer
assert_nil footer.sidebar assert_nil footer.sidebar
end end
@@ -146,6 +166,7 @@ context "Page" do
test "sidebar itself" do test "sidebar itself" do
sidebar = @wiki.page("_Sidebar") sidebar = @wiki.page("_Sidebar")
assert_nil sidebar.header
assert_nil sidebar.footer assert_nil sidebar.footer
assert_nil sidebar.sidebar assert_nil sidebar.sidebar
end end
+19 -3
View File
@@ -54,12 +54,12 @@ context "Wiki" do
test "list pages" do test "list pages" do
pages = @wiki.pages pages = @wiki.pages
assert_equal \ assert_equal \
['Bilbo-Baggins.md', 'Eye-Of-Sauron.md', 'Home.textile', 'My-Precious.md', 'Samwise Gamgee.mediawiki'], ['Bilbo-Baggins.md', 'Boromir.md', 'Eye-Of-Sauron.md', 'Home.textile', 'My-Precious.md', 'Samwise Gamgee.mediawiki'],
pages.map { |p| p.filename }.sort pages.map { |p| p.filename }.sort
end end
test "counts pages" do test "counts pages" do
assert_equal 5, @wiki.size assert_equal 6, @wiki.size
end end
test "text_data" do test "text_data" do
@@ -92,18 +92,34 @@ end
context "Wiki page previewing" do context "Wiki page previewing" do
setup do setup do
@path = testpath("examples/lotr.git") @path = testpath("examples/lotr.git")
Gollum::Wiki.default_options = {:universal_toc => false}
@wiki = Gollum::Wiki.new(@path) @wiki = Gollum::Wiki.new(@path)
end end
test "preview_page" do test "preview_page" do
page = @wiki.preview_page("Test", "# Bilbo", :markdown) page = @wiki.preview_page("Test", "# Bilbo", :markdown)
assert_equal "# Bilbo", page.raw_data assert_equal "# Bilbo", page.raw_data
assert_equal "<h1>Bilbo</h1>", page.formatted_data assert_equal %Q{<h1>Bilbo<a class="anchor" id="Bilbo" href="#Bilbo"></a>\n</h1>}, page.formatted_data
assert_equal "Test.md", page.filename assert_equal "Test.md", page.filename
assert_equal "Test", page.name assert_equal "Test", page.name
end end
end end
context "Wiki TOC" do
setup do
@path = testpath("examples/lotr.git")
options = {:universal_toc => true}
@wiki = Gollum::Wiki.new(@path, options)
end
test "toc_generation" do
page = @wiki.preview_page("Test", "# Bilbo", :markdown)
assert_equal "# Bilbo", page.raw_data
assert_equal '<h1>Bilbo<a class="anchor" id="Bilbo" href="#Bilbo"></a></h1>', page.formatted_data.gsub(/\n/,"")
assert_equal %{<div class="toc"><div class="toc-title">Table of Contents</div><ul><li><a href="#Bilbo">Bilbo</a></li></ul></div>}, page.toc_data.gsub(/\n */,"")
end
end
context "Wiki page writing" do context "Wiki page writing" do
setup do setup do
@path = testpath("examples/test.git") @path = testpath("examples/test.git")