You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
devdocs/lib/docs/core/parser.rb

29 lines
579 B

11 years ago
module Docs
class Parser
attr_reader :title, :html
11 years ago
def initialize(content)
@content = content
@html = document? ? parse_as_document : parse_as_fragment
11 years ago
end
private
DOCUMENT_RGX = /\A(?:\s|(?:<!--.*?-->))*<(?:\!doctype|html)/i
11 years ago
def document?
@content =~ DOCUMENT_RGX
11 years ago
end
def parse_as_document
document = Nokogiri::HTML.parse @content, nil, 'UTF-8'
@title = document.at_css('title').try(:content)
document
11 years ago
end
def parse_as_fragment
Nokogiri::HTML.fragment @content, 'UTF-8'
end
end
end