Skip to content

Commit 73bbef1

Browse files
committed
Refactor linkification
1 parent 0731782 commit 73bbef1

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

git-wiki.rb

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ def file_path(name)
2525
def url(page=nil, params=nil)
2626
'/' + (page && page.name != @root_page ? page.name : '') + (params ? "?#{Rack::Utils.build_query(params)}" : '')
2727
end
28+
def expand_links(html)
29+
html.gsub(@link_pattern) do
30+
link_text = $1
31+
page = Page.find_or_create(link_text.gsub(/[^\w\s]/, '').split.join('-').downcase)
32+
"<a class='page #{'new' unless page.exists?}' href='#{url(page)}'>#{link_text}</a>"
33+
end
34+
end
2835
end
2936
end
3037

@@ -40,16 +47,6 @@ def self.find_or_create(name, rev=nil)
4047
new(blob || Grit::Blob.create(GitWiki.repository, :name => path))
4148
end
4249

43-
def self.wikify(content)
44-
content.gsub(GitWiki.link_pattern) {|match| link($1) }
45-
end
46-
47-
def self.link(text)
48-
page = find_or_create(text.gsub(/[^\w\s]/, '').split.join('-').downcase)
49-
page_class = page.exists? ? 'existing' : 'new'
50-
"<a class='page #{page_class}' href='#{GitWiki.url(page)}'>#{text}</a>"
51-
end
52-
5350
def initialize(blob)
5451
@blob = blob
5552
end
@@ -67,7 +64,7 @@ def content
6764
end
6865

6966
def to_html
70-
Page.wikify(RDiscount.new(content).to_html)
67+
GitWiki.expand_links(RDiscount.new(content).to_html)
7168
end
7269

7370
def log

views/list.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
%ul#list
22
- @pages.each do |p|
33
%li
4-
%a.page.existing{:href => GitWiki.url(p)}= p.name
4+
%a.page{:href => GitWiki.url(p)}= p.name

0 commit comments

Comments
 (0)