parser.rb 541 B

1234567891011121314151617181920212223242526
  1. module Docs
  2. class Parser
  3. attr_reader :title, :html
  4. def initialize(content)
  5. @content = content
  6. @html = document? ? parse_as_document : parse_as_fragment
  7. end
  8. private
  9. def document?
  10. @content =~ /\A\s*<(?:\!doctype|html)/i
  11. end
  12. def parse_as_document
  13. document = Nokogiri::HTML.parse @content, nil, 'UTF-8'
  14. @title = document.at_css('title').try(:content)
  15. document.at_css 'body'
  16. end
  17. def parse_as_fragment
  18. Nokogiri::HTML.fragment @content, 'UTF-8'
  19. end
  20. end
  21. end