forked from al3x/git-wiki
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathextensions.rb
67 lines (54 loc) · 1.9 KB
/
extensions.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
class String
# Pre-formatted code blocks are used for writing about programming or markup
# source code. Rather than forming normal paragraphs, the lines of a code
# block are interpreted literally. Markdown wraps a code block in both <pre>
# and <code> tags.
#
# To produce a code block in Markdown, simply indent every line of the block
# by at least 4 spaces or 1 tab.
MARKDOWN_CODEBLOCK = /^\ {4}|\t/
# Match [[Page]] or even [[a page]] just like in wikipedia and gollum
GIT_WIKI_SIMPLE_LINK = /\[\[([\w\s\+\-\.]+)\]\]/
# Match [[Lone Star state|Texas]] just like gollum and unlike wikipedia
# (wikipedia reverses the order of things)
GIT_WIKI_COMPLEX_LINK = /\[\[([\w\s]+)\|([\w\s\+\-\.]+)\]\]/
# Replace things that are obviously meant to be a url:
# http(s) or ftp or file then a colon and then some number of slashes,
# numbers, chars, question marks, dots (very important)...
# It is far from perfect; it is good enough for now.
GIT_WIKI_OBVIOUS_URI = /(https?|ftps?|file)\:[\/\\\w\d\/\-\+\?\!\&\=\.\_\@\%\&\*\~\#]+/
TAG = /(\#[\w\+\-]+)/
def wiki_linked
lines.map { |line| line.gsub_links }.join
end
def gsub_links
return self if self =~ MARKDOWN_CODEBLOCK # ignore links in code blocks
gsub_simple_links_to_markdown.gsub_complex_links_to_markdown.gsub_tags_to_markdown
end
def gsub_simple_links_to_markdown
gsub(GIT_WIKI_SIMPLE_LINK) do
text = $1
'[%s](/%s)' % [text, text.as_wiki_link]
end
end
def gsub_complex_links_to_markdown
gsub(GIT_WIKI_COMPLEX_LINK) do
text, link = $1, $2
'[%s](/%s)' % [text, link.as_wiki_link]
end
end
def gsub_tags_to_markdown
gsub(TAG) do
text = $1
'[%s](/a/search?q=%s)' % [text, URI.escape(text)]
end
end
def as_wiki_link
gsub(/\+/, ' plus ').gsub(/\*/, ' times ').gsub(/\s/, '_')
end
end
class Time
def to_json
(to_i * 1000).to_s
end
end