Class: Elisp::DocStringParser

Inherits:
Object
  • Object
show all
Defined in:
lib/elisp/doc_string_parser.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.



28
29
30
# File 'lib/elisp/doc_string_parser.rb', line 28

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

Instance Method Details

#htmlObject



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

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>)
    elsif @scanner.skip(/Copyright [(]C[)]/)
      result << "Copyright &copy;"
    else
      normal << @scanner.getch
    end
  end
  result << normal
  normal.clear
  result
end

#html_url(url) ⇒ Object



90
91
92
93
94
95
# File 'lib/elisp/doc_string_parser.rb', line 90

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



65
66
67
68
69
70
71
72
73
# File 'lib/elisp/doc_string_parser.rb', line 65

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

#scan_http_uriObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/elisp/doc_string_parser.rb', line 75

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