forked from asciidoctor/asciidoctor-doctest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.rb
94 lines (74 loc) · 2.84 KB
/
converter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# frozen_string_literal: false
require 'asciidoctor/doctest/html_normalizer'
require 'corefines'
require 'htmlbeautifier'
require 'nokogiri'
using Corefines::Object::then
module Asciidoctor::DocTest
module HTML
class Converter < AsciidocConverter
def initialize(paragraph_xpath: './p/node()', **opts)
@paragraph_xpath = paragraph_xpath
super opts
end
def convert_examples(input_exmpl, output_exmpl)
opts = input_exmpl.opts.merge(output_exmpl.opts)
# The header & footer are excluded by default; always enable for document examples.
opts[:header_footer] ||= input_exmpl.name.start_with?('document')
# When asserting inline examples, defaults to ignore paragraph "wrapper".
opts[:include] ||= (@paragraph_xpath if input_exmpl.name.start_with? 'inline_')
actual = convert(input_exmpl.content, header_footer: opts[:header_footer])
.then { |s| parse_html s }
.then { |h| find_nodes h, opts[:include] }
.then { |h| remove_nodes h, opts[:exclude] }
.then { |h| normalize h }
expected = normalize(output_exmpl.content)
[actual, expected]
end
protected
def normalize(content)
content = parse_html(content) if content.is_a? String
has_content_type = !!meta_content_type(content)
result = HtmlBeautifier.beautify(content.normalize!)
# XXX: Nokogiri injects meta tag with Content-Type into rendered HTML
# document. This nasty hack removes that tag from the result if not
# present in the original HTML.
if !has_content_type && content.is_a?(Nokogiri::HTML::Document)
result.sub!(/^\s*<meta http-equiv="Content-Type" content="[^"]+"\s*\/?>\n/i, '')
end
result
end
def find_nodes(html, xpaths)
Array(xpaths).reduce(html) do |htm, xpath|
# XPath returns NodeSet, but we need DocumentFragment, so convert it again.
parse_html htm.xpath(xpath).to_html
end
end
def remove_nodes(html, xpaths)
return html unless xpaths
Array(xpaths).each_with_object(html.clone) do |xpath, htm|
htm.xpath(xpath).remove
end
end
def parse_html(str)
if str =~ /^\s*<!DOCTYPE\s/
::Nokogiri::HTML.parse(str)
else
::Nokogiri::HTML.fragment(str)
end
end
private
##
# Searches <tt><meta http-equiv="Content-Type" content="..."></tt>
# element in the given HTML document.
#
# @param html [Nokogiri::HTML::Document, Nokogiri::HTML::DocumentFragment]
# @return [Nokogiri::XML::Element, nil]
def meta_content_type(html)
html.xpath('//meta[@http-equiv and boolean(@content)]').find do |node|
node['http-equiv'] =~ /\AContent-Type\z/i
end
end
end
end
end