Skip to content

Commit 992d68e

Browse files
committed
Do [[these]] style links, with class method
1 parent a54d97a commit 992d68e

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

git-wiki.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77

88
module GitWiki
99
class << self
10-
attr_accessor :homepage, :extension, :repository
10+
attr_accessor :homepage, :extension, :repository, :link_pattern
1111
end
1212
@repository = Grit::Repo.new(ARGV[0] || File.expand_path('~/wiki'))
1313
@extension = ARGV[1] || '.markdown'
1414
@homepage = ARGV[2] || 'Home'
15+
@link_pattern = /\[\[(.*?)\]\]/
1516
end
1617

1718
class Page
@@ -25,12 +26,21 @@ def self.find_or_create(name)
2526
new(blob || Grit::Blob.create(GitWiki.repository, :name => path))
2627
end
2728

29+
def self.wikify(content)
30+
content.gsub(GitWiki.link_pattern) {|match| link($1) }
31+
end
32+
33+
def self.link(text)
34+
page = find_or_create(text.gsub(/[^\w\s]/, '').split.join('-').downcase)
35+
"<a class='page #{page.css_class}' href='#{page.url}'>#{text}</a>"
36+
end
37+
2838
def initialize(blob)
2939
@blob = blob
3040
end
3141

3242
def to_html
33-
RDiscount.new(wiki_link(content)).to_html
43+
Page.wikify(RDiscount.new(content).to_html)
3444
end
3545

3646
def to_s
@@ -66,13 +76,6 @@ def add_to_index_and_commit!
6676
def file_name
6777
File.join(GitWiki.repository.working_dir, name + GitWiki.extension)
6878
end
69-
70-
def wiki_link(str)
71-
str.gsub(/([A-Z][a-z]+[A-Z][A-Za-z0-9]+)/) { |page|
72-
%Q{<a class="#{self.class.css_class_for(page)}"} +
73-
%Q{href="/#{page}">#{page}</a>}
74-
}
75-
end
7679
end
7780

7881
set :haml, :format => :html5, :attr_wrapper => '"'

0 commit comments

Comments
 (0)