Class: QuestionCompiler::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/question_compiler/compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log) ⇒ Compiler

Returns a new instance of Compiler.



47
48
49
# File 'lib/question_compiler/compiler.rb', line 47

def initialize(log)
  @log = log
end

Instance Attribute Details

#context_pathObject (readonly)

Returns the value of attribute context_path.



3
4
5
# File 'lib/question_compiler/compiler.rb', line 3

def context_path
  @context_path
end

Instance Method Details

#compile(question_src, context_reader, question_writer) ⇒ Object

Note that ‘QuestionCompiler::Server` doesn’t call this. Instead, it calls ‘#transform_question` directly.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/question_compiler/compiler.rb', line 34

def compile(question_src, context_reader, question_writer)
  context_reader.log = @log
  question_writer.log = @log
  question_writer.open do
    question_doc = Nokogiri.HTML question_src
    question_doc = transform_question(question_doc, context_reader) do |context_path|
      copy_dependencies context_reader, question_writer, 'assets'
    end
  
    question_writer.write 'question.html', question_doc.to_html
  end
end

#copy_dependencies(context_reader, question_writer, directory) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/question_compiler/compiler.rb', line 5

def copy_dependencies(context_reader, question_writer, directory)
  @log.debug "Copying dependencies in #{directory}"
  context_reader.glob(
    File.join(directory, '/**/*')
  ).reject do |dep_path|
    !context_reader.file?(dep_path) or
    File.basename(dep_path).start_with?('.')
  end.each do |dep_path|
    dep_path = dep_path.to_s
    if dep_src = context_reader.read(dep_path)
      question_writer.write dep_path, dep_src
    end
  end
end

#copy_nodes(nodes_to_copy, dst_node, before) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/question_compiler/compiler.rb', line 20

def copy_nodes(nodes_to_copy, dst_node, before)
  @log.debug "Copying nodes [#{nodes_to_copy.map(&:name).join(', ')}] to #{dst_node.name}"
  before = dst_node.at_css(before)
  nodes_to_copy.each do |node|
    if before
      before.add_previous_sibling node
    else
      dst_node << node
    end
  end
end

#transform_question(question_doc, context_reader) ⇒ Object

‘QuestionCompiler::Server` calls this directly, bypassing `#compile`.



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
# File 'lib/question_compiler/compiler.rb', line 52

def transform_question(question_doc, context_reader)
  context_reader.log = @log
  question_head = question_doc.at_css('head')
  @log.debug 'Looking for an embedded context'
  if embed_div = question_doc.css('div[data-embed-context]').first
    @context_path = embed_div['data-embed-context']
    @log.debug "Found embedded context: #{@context_path}"
    
    context_reader.open(@context_path) do
      if context_src = context_reader.read('context.html')
        context_doc = Nokogiri.HTML context_src
      
        if question_head
          copy_nodes context_doc.css('head link'),   question_head, 'link'
          copy_nodes context_doc.css('head style'),  question_head, 'style'
          copy_nodes context_doc.css('head script'), question_head, 'script'
        end
      
        if context_body = context_doc.at_css('body')
          embed_div.inner_html = ''
          embed_div << context_body.inner_html
        end
    
        if block_given?
          yield @context_path
        end
      else
        @log.warn "Could not read context.html in #{@context_path}"
      end
    end
  end
  question_doc
end