7
7
8
8
module GitWiki
9
9
class << self
10
- attr_accessor :homepage , :extension , :repository
10
+ attr_accessor :homepage , :extension , :repository , :link_pattern
11
11
end
12
12
@repository = Grit ::Repo . new ( ARGV [ 0 ] || File . expand_path ( '~/wiki' ) )
13
13
@extension = ARGV [ 1 ] || '.markdown'
14
14
@homepage = ARGV [ 2 ] || 'Home'
15
+ @link_pattern = /\[ \[ (.*?)\] \] /
15
16
end
16
17
17
18
class Page
@@ -25,12 +26,21 @@ def self.find_or_create(name)
25
26
new ( blob || Grit ::Blob . create ( GitWiki . repository , :name => path ) )
26
27
end
27
28
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
+
28
38
def initialize ( blob )
29
39
@blob = blob
30
40
end
31
41
32
42
def to_html
33
- RDiscount . new ( wiki_link ( content ) ) . to_html
43
+ Page . wikify ( RDiscount . new ( content ) . to_html )
34
44
end
35
45
36
46
def to_s
@@ -66,13 +76,6 @@ def add_to_index_and_commit!
66
76
def file_name
67
77
File . join ( GitWiki . repository . working_dir , name + GitWiki . extension )
68
78
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
76
79
end
77
80
78
81
set :haml , :format => :html5 , :attr_wrapper => '"'
0 commit comments