Commit 8ce390333a7e08c2956d6334f587e1db7668133e
1 parent
de2770bb
Exists in
master
and in
4 other branches
Add GFM helper
Showing
1 changed file
with
68 additions
and
0 deletions
Show diff stats
app/helpers/application_helper.rb
... | ... | @@ -42,6 +42,74 @@ module ApplicationHelper |
42 | 42 | grouped_options_for_select(options, @ref || @project.default_branch) |
43 | 43 | end |
44 | 44 | |
45 | + def gfm(text, html_options = {}) | |
46 | + return text if text.nil? | |
47 | + | |
48 | + # Extract pre blocks | |
49 | + # from http://github.github.com/github-flavored-markdown/ | |
50 | + extractions = {} | |
51 | + text.gsub!(%r{<pre>.*?</pre>|<code>.*?</code>}m) do |match| | |
52 | + md5 = Digest::MD5.hexdigest(match) | |
53 | + extractions[md5] = match | |
54 | + "{gfm-extraction-#{md5}}" | |
55 | + end | |
56 | + | |
57 | + # match 1 2 3 4 5 6 | |
58 | + text.gsub!(/(\W)?(@([\w\._]+)|[#!$](\d+)|([\h]{6,40}))(\W)?/) do |match| | |
59 | + prefix = $1 | |
60 | + reference = $2 | |
61 | + user_name = $3 | |
62 | + issue_id = $4 | |
63 | + merge_request_id = $4 | |
64 | + snippet_id = $4 | |
65 | + commit_id = $5 | |
66 | + suffix = $6 | |
67 | + | |
68 | + # TODO: add popups with additional information | |
69 | + ref_link = case reference | |
70 | + | |
71 | + # team member: @foo | |
72 | + when /^@/ | |
73 | + user = @project.users.where(:name => user_name).first | |
74 | + member = @project.users_projects.where(:user_id => user).first if user | |
75 | + link_to("@#{user_name}", project_team_member_path(@project, member), html_options.merge(:class => "gfm gfm-team_member #{html_options[:class]}")) if member | |
76 | + | |
77 | + # issue: #123 | |
78 | + when /^#/ | |
79 | + # avoid HTML entities | |
80 | + unless prefix.try(:end_with?, "&") && suffix.try(:start_with?, ";") | |
81 | + issue = @project.issues.where(:id => issue_id).first | |
82 | + link_to("##{issue_id}", project_issue_path(@project, issue), html_options.merge(:title => "Issue: #{issue.title}", :class => "gfm gfm-issue #{html_options[:class]}")) if issue | |
83 | + end | |
84 | + | |
85 | + # merge request: !123 | |
86 | + when /^!/ | |
87 | + merge_request = @project.merge_requests.where(:id => merge_request_id).first | |
88 | + link_to("!#{merge_request_id}", project_merge_request_path(@project, merge_request), html_options.merge(:title => "Merge Request: #{merge_request.title}", :class => "gfm gfm-merge_request #{html_options[:class]}")) if merge_request | |
89 | + | |
90 | + # snippet: $123 | |
91 | + when /^\$/ | |
92 | + snippet = @project.snippets.where(:id => snippet_id).first | |
93 | + link_to("$#{snippet_id}", project_snippet_path(@project, snippet), html_options.merge(:title => "Snippet: #{snippet.title}", :class => "gfm gfm-snippet #{html_options[:class]}")) if snippet | |
94 | + | |
95 | + # commit: 123456... | |
96 | + when /^\h/ | |
97 | + commit = @project.commit(commit_id) | |
98 | + link_to(commit_id, project_commit_path(@project, :id => commit.id), html_options.merge(:title => "Commit: #{commit.author_name} - #{CommitDecorator.new(commit).title}", :class => "gfm gfm-commit #{html_options[:class]}")) if commit | |
99 | + | |
100 | + end # case | |
101 | + | |
102 | + ref_link.nil? ? match : "#{prefix}#{ref_link}#{suffix}" | |
103 | + end # gsub | |
104 | + | |
105 | + # Insert pre block extractions | |
106 | + text.gsub!(/\{gfm-extraction-(\h{32})\}/) do | |
107 | + extractions[$1] | |
108 | + end | |
109 | + | |
110 | + text.html_safe | |
111 | + end | |
112 | + | |
45 | 113 | def markdown(text) |
46 | 114 | @__renderer ||= Redcarpet::Markdown.new(Redcarpet::Render::GitlabHTML.new(self, filter_html: true), { |
47 | 115 | no_intra_emphasis: true, | ... | ... |