Module: Hoe::Markdown::Util

Defined in:
lib/hoe/markdown/util.rb

Constant Summary collapse

GITHUB_ISSUE_MENTION_REGEX =
%r{
  # not immediately preceded by a word character
  (?<!\w)

  # issue number, like '#1234'
  \#([[:digit:]]+)

  # not already in a markdown hyperlink
  (?!\][\(\[])

  # don't truncate the issue number to meet the previous negative lookahead
  (?![[[:digit:]]])
}x
GITHUB_USER_REGEX =
%r{
  # not obviously part of an email address
  (?<![[:alnum:]])

  # username, like "@flavorjones"
  @([[:alnum:]](?:[[:alnum:]]|-(?=[[:alnum:]])){0,38})

  # not already in a markdown hyperlink
  (?!\][\(\[])

  # don't truncate the username to meet the previous negative lookahead
  (?![[[:alnum:]]-])
}x

Class Method Summary collapse

Class Method Details



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/hoe/markdown/util.rb', line 77

def self.__replace_with_link(match, link)
  skip =
    (match.pre_match.end_with?("\n[") && match.post_match =~ /\A\]:\s+.+\n/) ||
    (match.pre_match =~ /\]\[[^\]]*\z/ && match.post_match =~ /\A[^\]]*\]/)

  if skip
    match[0]
  else
    link % {id: match[1]}
  end
end

.linkify_github_issues(markdown, issues_uri) ⇒ Object



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
# File 'lib/hoe/markdown/util.rb', line 33

def self.linkify_github_issues(markdown, issues_uri)
  if issues_uri.nil? || issues_uri.empty?
    raise "#{__FILE__}:#{__method__}: URI for bugs cannot be empty\n"
  end

  issue_uri_regex = %r{
    # not already in a markdown hyperlink
    (?<!\]\()

    #{issues_uri}/([[:digit:]]+)

    # don't truncate the issue number to meet the previous negative lookahead
    (?![[[:digit:]]])
  }x

  pull_uri = issues_uri.gsub("issues", "pull")
  pull_uri_regex = %r{
    # not already in a markdown hyperlink
    (?<!\]\()

    #{pull_uri}/([[:digit:]]+)

    # don't truncate the issue number to meet the previous negative lookahead
    (?![[[:digit:]]])
  }x

  markdown
    .gsub(GITHUB_ISSUE_MENTION_REGEX) {
      __replace_with_link(Regexp.last_match, "[#%<id>s](#{issues_uri}/%<id>s)")
    }
    .gsub(issue_uri_regex) {
      __replace_with_link(Regexp.last_match, "[#%<id>s](#{issues_uri}/%<id>s)")
    }
    .gsub(pull_uri_regex) {
      __replace_with_link(Regexp.last_match, "[#%<id>s](#{pull_uri}/%<id>s)")
    }
end

.linkify_github_usernames(markdown) ⇒ Object



71
72
73
74
75
# File 'lib/hoe/markdown/util.rb', line 71

def self.linkify_github_usernames(markdown)
  markdown.gsub(GITHUB_USER_REGEX) {
    __replace_with_link(Regexp.last_match, "[@%<id>s](https://github.com/%<id>s)")
  }
end