forked from metala/jekyll-wikilinks-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwikilinks.rb
74 lines (63 loc) · 1.59 KB
/
wikilinks.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
module Jekyll
module Wikilinks
class Wikilink
def self.parse(text)
inner = text[2..-3]
name, title = inner.split('|', 2)
self.new(name, title)
end
attr_accessor :name, :title
attr_reader :match
def initialize(name, title)
@name = name.strip
@title = title.strip if not title.nil?
end
def title
if @title.nil?
if not @match.nil?
match_title @match
else
@name
end
else
@title
end
end
def match_title(m)
if not m.data.nil? and m.data.include? 'title'
m.data['title']
end
end
def url
"#{Jekyll.sites[0].baseurl}#{@match.url}"
end
def has_match?
not @match.nil?
end
def match_post(posts)
@match = posts.docs.find { |p| p.slug.downcase == @name.downcase or match_title(p) == name }
end
def match_page(pages)
@match = pages.find { |p| p.basename.downcase == @name.downcase or match_title(p) == name }
end
def markdown
@match.nil? ? "[#{title}](#)" : "[#{title}](#{url})"
end
end
end
module Converters
class Markdown < Converter
alias old_convert convert
def convert(content)
pat = /\[\[(.+?)\]\]/
content = content.gsub(pat) do |m|
wl = Wikilinks::Wikilink.parse(m)
wl.match_page(Jekyll.sites[0].pages)
wl.match_post(Jekyll.sites[0].posts) unless wl.has_match?
wl.markdown
end
old_convert(content)
end
end
end
end