Class: Elisp::DocStringParser

Inherits:
Object
  • Object
show all
Defined in:
lib/elisp/comment.rb

Constant Summary collapse

URI_PATTERN =
URI.regexp
URI_PATTERN_WITH_ANGLES =
/[<](?<uri>#{ URI_PATTERN })[>]/
EMAIL =
Regexp.new(URI::MailTo::EMAIL_REGEXP.to_s
.sub(/\A[(][?]-mix:\\A/, "(?-mix:")
.sub(/\\z[)]\z/, ")"))
EMAIL_WITH_ANGLES =
/[<](?<address>#{ EMAIL })[>]/

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ DocStringParser

Returns a new instance of DocStringParser.



27
28
29
# File 'lib/elisp/comment.rb', line 27

def initialize(source)
  @scanner = StringScanner.new(source)
end

Instance Method Details

#htmlObject



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
# File 'lib/elisp/comment.rb', line 31

def html
  result = +""
  normal = +""
  until @scanner.eos?
    if @scanner.skip(/[`](?<code>[A-Za-z!.-]+)[']/)
      result << normal
      normal.clear
      code = CGI.escape_html(@scanner[:code])
      result << "<code>#{code}</code>"
      normal.clear
    elsif @scanner.skip(/\\\\=(?<quote>['`])/)
      normal << @scanner[:quote]
    elsif (uri = scan_http_uri)
      result << normal
      normal.clear
      result << html_url(uri)
    elsif (address = scan_email)
      result << normal
      normal.clear
      uri = CGI.escape(address)
      address = CGI.escape_html(address)
      result << %(<a href="mailto:#{uri}">#{address}</a>)
    else
      normal << @scanner.getch
    end
  end
  result << normal
  normal.clear
  result
end

#html_url(url) ⇒ Object



87
88
89
90
91
92
# File 'lib/elisp/comment.rb', line 87

def html_url(url)
  url = url.to_s
  ref = CGI.escape(url)
  label = CGI.escape_html(url)
  %(<a class="pure-url" href="#{ref}">#{label}</a>)
end

#scan_emailObject



62
63
64
65
66
67
68
69
70
# File 'lib/elisp/comment.rb', line 62

def scan_email
  if (address = @scanner.scan(EMAIL))
    address
  elsif @scanner.skip(EMAIL_WITH_ANGLES)
    @scanner[:address]
  else
    return
  end
end

#scan_http_uriObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/elisp/comment.rb', line 72

def scan_http_uri
  if (uri = @scanner.scan(URI_PATTERN))
  elsif @scanner.skip(URI_PATTERN_WITH_ANGLES)
    uri = @scanner[:uri]
  else
    return
  end
  uri = URI(uri)
  unless uri.is_a?(URI::HTTP)
    @scanner.unscan
    return
  end
  uri
end