Module: GitlabKramdown::Parser::Reference
- Included in:
- Kramdown::Parser::GitlabKramdown
- Defined in:
- lib/gitlab_kramdown/parser/reference.rb
Overview
Special GitLab References
This parser implements any non context-specific reference as described in the GitLab Flavored Markdown reference, except for labels which were extracted into it’s own module.
Constant Summary collapse
- PATH_REGEX =
/[a-zA-Z0-9_.][a-zA-Z0-9_\-.]+/
- NAMESPACE_FORMAT_REGEX =
%r{ (?:#{PATH_REGEX}[a-zA-Z0-9_-]) (?<!\.git|\.atom) |[a-zA-Z0-9_] }x
- FULL_NAMESPACE_FORMAT_REGEX =
%r{(#{NAMESPACE_FORMAT_REGEX}/)*#{NAMESPACE_FORMAT_REGEX}}
- ALWAYS_FULL_NAMESPACE_FORMAT_REGEX =
%r{(#{NAMESPACE_FORMAT_REGEX}/)+#{NAMESPACE_FORMAT_REGEX}}
- NAMESPACE_EXTRACTION_PATTERN =
/(?<namespace>#{ALWAYS_FULL_NAMESPACE_FORMAT_REGEX})\z/
- USER_GROUP_PATTERN =
%r{ #{Regexp.escape('@')} (?<user>#{FULL_NAMESPACE_FORMAT_REGEX}) }x
- PROJECT_COMMIT_PATTERN =
%r{ #{Regexp.escape('@')} (?<commit>[a-f0-9]{7,40}) (?!\.{3}) }x
- PROJECT_COMMIT_DIFF_PATTERN =
%r{ #{Regexp.escape('@')} (?<commit_source>[a-f0-9]{7,40}) \.{3} (?<commit_target>[a-f0-9]{7,40}) }x
- PROJECT_ISSUE_PATTERN =
%r{ #{Regexp.escape('#')} (?<issue>[1-9][0-9]*) }x
- PROJECT_MERGE_REQUEST_PATTERN =
%r{ #{Regexp.escape('!')} (?<merge_request>[1-9][0-9]*) }x
- PROJECT_SNIPPET_PATTERN =
%r{ #{Regexp.escape('$')} (?<snippet>[1-9][0-9]*) }x
Class Method Summary collapse
Instance Method Summary collapse
-
#extract_reference_namespace! ⇒ Object
Extract namespace after a reference has been found.
- #parse_commit ⇒ Object
- #parse_commit_diff ⇒ Object
- #parse_issue ⇒ Object
- #parse_merge_request ⇒ Object
- #parse_snippet ⇒ Object
- #parse_user_group_mention ⇒ Object
Class Method Details
.included(klass) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/gitlab_kramdown/parser/reference.rb', line 60 def self.included(klass) klass.define_parser(:user_group_mention, USER_GROUP_PATTERN, '@') klass.define_parser(:commit_diff, PROJECT_COMMIT_DIFF_PATTERN, '@') klass.define_parser(:commit, PROJECT_COMMIT_PATTERN, '@') klass.define_parser(:issue, PROJECT_ISSUE_PATTERN, '#') klass.define_parser(:merge_request, PROJECT_MERGE_REQUEST_PATTERN, '\!') klass.define_parser(:snippet, PROJECT_SNIPPET_PATTERN, '\$') end |
Instance Method Details
#extract_reference_namespace! ⇒ Object
Extract namespace after a reference has been found
This method provides a workaround for the lack of lookbehind support in Ruby StringScanner.
We look at pre_match content (everything before the reference in the same line) and extract the very last word in the string as long as it matches the NAMESPACE_EXTRACTION_PATTERN.
We then modify the last parsed tree element’s value, removing the matched namespace, so that we can process that as part of the reference code.
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/gitlab_kramdown/parser/reference.rb', line 99 def extract_reference_namespace! data = @src.pre_match.match(NAMESPACE_EXTRACTION_PATTERN) if data namespace = data[:namespace] # Remove namespace from last parsed tree value redacted = @tree.children.last.value[0..-(namespace.size + 1)] @tree.children.last.value = redacted end namespace end |
#parse_commit ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/gitlab_kramdown/parser/reference.rb', line 113 def parse_commit start_line_number = @src.current_line_number @src.pos += @src.matched_size namespace = extract_reference_namespace! reference_text = "#{namespace}#{@src[0]}" # If we can't find a namespace we just add content as regular text and skip unless namespace add_text(@src[0]) return end href = "#{@options[:gitlab_url]}/#{namespace}/commit/#{@src[:commit]}" el = Kramdown::Element.new(:a, nil, { 'href' => href }, location: start_line_number) add_text(reference_text, el) @tree.children << el end |
#parse_commit_diff ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/gitlab_kramdown/parser/reference.rb', line 134 def parse_commit_diff start_line_number = @src.current_line_number @src.pos += @src.matched_size namespace = extract_reference_namespace! reference_text = "#{namespace}#{@src[0]}" # If we can't find a namespace we just add content as regular text and skip unless namespace add_text(@src[0]) return end href = "#{@options[:gitlab_url]}/#{namespace}/compare/" \ "#{@src[:commit_source]}...#{@src[:commit_target]}" el = Kramdown::Element.new(:a, nil, { 'href' => href }, location: start_line_number) add_text(reference_text, el) @tree.children << el end |
#parse_issue ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/gitlab_kramdown/parser/reference.rb', line 156 def parse_issue start_line_number = @src.current_line_number @src.pos += @src.matched_size namespace = extract_reference_namespace! reference_text = "#{namespace}#{@src[0]}" # If we can't find a namespace we just add content as regular text and skip unless namespace add_text(@src[0]) return end href = "#{@options[:gitlab_url]}/#{namespace}/issues/#{@src[:issue]}" el = Kramdown::Element.new(:a, nil, { 'href' => href }, location: start_line_number) add_text(reference_text, el) @tree.children << el end |
#parse_merge_request ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/gitlab_kramdown/parser/reference.rb', line 177 def parse_merge_request start_line_number = @src.current_line_number @src.pos += @src.matched_size namespace = extract_reference_namespace! reference_text = "#{namespace}#{@src[0]}" # If we can't find a namespace we just add content as regular text and skip unless namespace add_text(@src[0]) return end href = "#{@options[:gitlab_url]}/#{namespace}/merge_requests/#{@src[:merge_request]}" el = Kramdown::Element.new(:a, nil, { 'href' => href }, location: start_line_number) add_text(reference_text, el) @tree.children << el end |
#parse_snippet ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/gitlab_kramdown/parser/reference.rb', line 198 def parse_snippet start_line_number = @src.current_line_number @src.pos += @src.matched_size namespace = extract_reference_namespace! reference_text = "#{namespace}#{@src[0]}" # If we can't find a namespace we just add content as regular text and skip unless namespace add_text(@src[0]) return end href = "#{@options[:gitlab_url]}/#{namespace}/snippets/#{@src[:snippet]}" el = Kramdown::Element.new(:a, nil, { 'href' => href }, location: start_line_number) add_text(reference_text, el) @tree.children << el end |
#parse_user_group_mention ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/gitlab_kramdown/parser/reference.rb', line 69 def parse_user_group_mention start_line_number = @src.current_line_number @src.pos += @src.matched_size # should not start with anything that looks like project/namespace if @src.pre_match.match?(/[a-zA-Z0-9_-]\Z/) add_text(@src[0]) return end href = "#{@options[:gitlab_url]}/#{@src[:user]}" el = Kramdown::Element.new(:a, nil, { 'href' => href }, location: start_line_number) add_text(@src[0], el) @tree.children << el end |