LOGIN / SIGN UP

/ lib / septic / wiki.rb

2 Commit: b5c5fddb36480f138502444d14a35bf51571f7d9
Author: Claes Nästén
Date: Fri Feb 26 22:37:58 +0100 2010
Subject: More help information in wiki syntax help and links in activity view.
Show file history
difference betwen
and
wiki.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
require 'mediacloth/mediawikilexer'
require 'mediacloth/mediawikiparser'
require 'mediacloth/mediawikiwalker'
require 'mediacloth/mediawikihtmlgenerator'

module Septic
module Wiki
# Class building Wiki URLs
class LinkHandler

def initialize(project, link_for)
@project = project
@link_for = link_for
end

# Generate url for internal Wiki
def url_for(resource)
'/projects/%d/wikis/%s' % [@project.id, resource]
end

# Generate link for internal pages
def link_for(prefix, text)
'<a href="%s">%s</a>' % [url_for(prefix), text]
end

# Generate link for resources
def link_for_resource(prefix, resource, options=[])
# Wiki resource link handling
text = options.join(' ')

case prefix
when 'Image'
if resource.match('^https?://')
link = '<img src="%s" />' % [resource]
else
link = '<img src="%s" />' % [attachment_for(resource)]
end
when 'File'
link = '<a href="%s">%s</a>' % [attachment_for(resource), text.empty? ? resource : text]
when 'Code'
code_info = resource.split('/', 2)
rev_info = code_info[0].split(':', 2)

if code_info.size == 2 and rev_info.size == 2
url = '/projects/%s/repository/browse?path=%s&revision=%s&branch=%s' % [@project.name, code_info[1], rev_info[1], rev_info[0]]
link = '<a href="%s">%s</a>' % [url, text.empty? ? resource : text]
else
link = '<a href="#">%s</a>' % [_('Invalid Code link "%s"') % [resource]]
end
when 'Commit'
link = '<a href="/projects/%s/commits/%s">%s</a>' % [@project.name, resource, text.empty? ? "r:#{resource}" : text]
when 'Task'
link = '<a href="/projects/%s/tasks/%s">%s</a>' % [@project.name, resource, text.empty? ? "##{resource}" : text]
when 'User'
link = '<a href="/summary/%s/user">%s</a>' % [resource, text.empty? ? resource : text]
else
link = ''
end

link
end

# Get attachment URL for current type of link_for
def attachment_for(resource)
if @link_for.is_a?(WikiPage)
attachment_for_wiki_page(resource)
elsif @link_for.is_a?(Task)
attachment_for_task(resource)
else
'#'
end
end

def attachment_for_wiki_page(resource)
"/projects/#{@project.id}/wikis/#{@link_for.name}/attachment?attachment_id=#{resource}"
end

def attachment_for_task(resource)
"/projects/#{@project.id}/tasks/#{@link_for.id}/attachment?attachment_id=#{resource}"
end
end

class HTMLGenerator < MediaWikiHTMLGenerator
def initialize(options = { })
super()

default_options = { :heading_offset => 2 }
@septic_options = default_options.merge(options)
end

# Overiding parse_section to support offsetting the header styles,
# this is used for Wiki syntax in non-printable Wiki pages and tasks
# where h1 and h2 is already used by the page itself.
def parse_section(ast)
level = ast.level + @septic_options[:heading_offset]
generator = TextGenerator.new
anchor = MediaWikiHTMLGenerator.anchor_for(generator.parse(ast).join(' '))
"<h#{level}><a name='#{anchor}'></a>" + parse_wiki_ast(ast) + "</h#{level}>\n"
end
end

#Parses wiki formatted +input+ and generates its html representation.
def wiki_to_html(input, link_handler, options = { })
parser = MediaWikiParser.new
parser.lexer = MediaWikiLexer.new
ast = parser.parse(input)
walker = HTMLGenerator.new(options)
walker.link_handler = link_handler
walker.parse(ast)
walker.html
end

module_function :wiki_to_html
end
end